package com.alibaba.json.bvt.support.jaxrs.mock;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

import javax.ws.rs.core.MediaType;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.jaxrs.FastJsonProvider;

public class FastJsonProviderTest extends TestCase {

	@SuppressWarnings("deprecation")
	public void test_1() throws Exception { 
		
		FastJsonProvider provider1 = new FastJsonProvider("UTF-8");
		Assert.assertEquals("UTF-8", provider1.getCharset().name());
		
		FastJsonProvider provider2 = new FastJsonProvider(new Class[0]);

		provider2.setCharset(Charset.forName("GBK"));
		Assert.assertEquals("GBK", provider2.getCharset().name());
		
		Assert.assertNull(provider2.getDateFormat());
		provider2.setDateFormat("yyyyMMdd");
		
		provider2.setFeatures(SerializerFeature.IgnoreErrorGetter);
		Assert.assertEquals(1, provider2.getFeatures().length);
		Assert.assertEquals(SerializerFeature.IgnoreErrorGetter,
				provider2.getFeatures()[0]);
		
		provider2.setFilters(serializeFilter);
		Assert.assertEquals(1, provider2.getFilters().length);
		Assert.assertEquals(serializeFilter, provider2.getFilters()[0]);
		
		FastJsonProvider provider = new FastJsonProvider();

		Assert.assertNotNull(provider.getFastJsonConfig());
		provider.setFastJsonConfig(new FastJsonConfig());
		
		provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE);
		provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE);
		
		VO vo = (VO) provider.readFrom(null, VO.class, null, MediaType.APPLICATION_JSON_TYPE, null, new ByteArrayInputStream("{\"id\":123}".getBytes(Charset
				.forName("UTF-8"))));
		Assert.assertEquals(123, vo.getId());

		final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		provider.writeTo(vo, VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE, null, byteOut);
		
		byte[] bytes = byteOut.toByteArray();
		Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
		
		provider.getSize(vo, VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE);
		
	}
	
	private SerializeFilter serializeFilter = new ValueFilter() {
		@Override
		public Object process(Object object, String name, Object value) {
			if (value == null) {
				return "";
			}
			if (value instanceof Number) {
				return String.valueOf(value);
			}
			return value;
		}
	};
	
	public static class VO {

		private int id;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

	}
}

@@ -851,23 +851,11 @@
 der(
-new Class%5B0%5D
 );%0A
+
 %0A%09%09p
@@ -1483,24 +1483,47 @@
 sonProvider(
+new Class%5B%5D%7B VO.class %7D
 );%0A%0A%09%09Assert
@@ -1617,32 +1617,58 @@
 Config());%0A%09%09%0A%09%09
+Assert.assertEquals(true, 
 provider.isReada
@@ -1729,20 +1729,47 @@
 ON_TYPE)
+)
 ;%0A%09%09
+Assert.assertEquals(true, 
 provider
@@ -1835,24 +1835,748 @@
 N_JSON_TYPE)
+);%0A%09%09Assert.assertEquals(true, provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_FORM_URLENCODED_TYPE));%0A%09%09Assert.assertEquals(true, provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_FORM_URLENCODED_TYPE));%0A%09%09Assert.assertEquals(false, provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_XML_TYPE));%0A%09%09Assert.assertEquals(false, provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_XML_TYPE));%0A%09%09Assert.assertEquals(false, provider.isReadable(String.class, String.class, null, MediaType.valueOf(%22application/javascript%22)));%0A%09%09Assert.assertEquals(false, provider.isWriteable(String.class, String.class, null, MediaType.valueOf(%22application/x-javascript%22)))
 ;%0A%09%09%0A%09%09VO vo

refactor testcast