001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.webservice.jsonws;
020:
021: import java.io.StringReader;
022: import java.io.StringWriter;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map; //import java.util.SortedMap;
027: //import java.util.TreeMap;
028:
029: import junit.framework.TestCase;
030: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
031: import de.schlund.pfixcore.webservice.json.JSONObject;
032: import de.schlund.pfixcore.webservice.json.parser.JSONParser;
033:
034: /**
035: * @author mleidig@schlund.de
036: */
037: public class JSONSerializerTest extends TestCase {
038:
039: BeanDescriptorFactory beanDescFactory;
040: SerializerRegistry serializerRegistry;
041: DeserializerRegistry deserializerRegistry;
042:
043: @Override
044: protected void setUp() throws Exception {
045: beanDescFactory = new BeanDescriptorFactory();
046: serializerRegistry = new SerializerRegistry(beanDescFactory);
047: deserializerRegistry = new DeserializerRegistry(beanDescFactory);
048: }
049:
050: public void testSerialization() throws Exception {
051: JSONSerializer serializer = new JSONSerializer(
052: serializerRegistry);
053: JSONDeserializer deserializer = new JSONDeserializer(
054: deserializerRegistry);
055: StringWriter writer = new StringWriter();
056: TestBean bean = new TestBean();
057: bean.setText("test");
058: bean.setValue(7);
059: List<String> strList = new ArrayList<String>();
060: strList.add("foo");
061: strList.add("bar");
062: bean.setStrList(strList);
063: List<Integer> subIntList1 = new ArrayList<Integer>();
064: subIntList1.add(1);
065: subIntList1.add(2);
066: List<Integer> subIntList2 = new ArrayList<Integer>();
067: subIntList2.add(3);
068: subIntList2.add(4);
069: List<List<Integer>> intList = new ArrayList<List<Integer>>();
070: intList.add(subIntList1);
071: intList.add(subIntList2);
072: bean.setIntList(intList);
073: Map<String, String> strMap = new HashMap<String, String>();
074: strMap.put("key1", "val1");
075: strMap.put("key2", "val2");
076: bean.setStrMap(strMap);
077:
078: Map<String, Map<String, Integer>> mapMap = new HashMap<String, Map<String, Integer>>();
079: Map<String, Integer> subMap1 = new HashMap<String, Integer>();
080: subMap1.put("val1", 1);
081: subMap1.put("val2", 2);
082: mapMap.put("sub1", subMap1);
083: Map<String, Integer> subMap2 = new HashMap<String, Integer>();
084: subMap2.put("val3", 3);
085: subMap2.put("val4", 4);
086: mapMap.put("sub2", subMap2);
087: bean.setMapMap(mapMap);
088:
089: HashMap<String, Integer> intMap = new HashMap<String, Integer>();
090: intMap.put("one", 1);
091: intMap.put("two", 2);
092: intMap.put("three", 3);
093: bean.setIntMap(intMap);
094:
095: serializer.serialize(bean, writer);
096: String json = writer.toString();
097:
098: JSONParser parser = new JSONParser(new StringReader(json));
099: JSONObject jsonObj = (JSONObject) parser.getJSONValue();
100: TestBean refBean = (TestBean) deserializer.deserialize(jsonObj,
101: TestBean.class);
102:
103: writer = new StringWriter();
104: serializer.serialize(refBean, writer);
105: String jsonRef = writer.toString();
106:
107: //System.out.println(json);
108: //System.out.println(jsonRef);
109:
110: assertEquals(json, jsonRef);
111:
112: }
113:
114: /**
115: @SuppressWarnings("unchecked")
116: public void testReadOnlySerialization() throws Exception {
117:
118: JSONSerializer serializer=new JSONSerializer(serializerRegistry);
119: StringWriter writer=new StringWriter();
120: ReadOnlyBean bean=new ReadOnlyBean();
121:
122: SortedMap<String,Number> numMap=new TreeMap<String,Number>();
123: numMap.put("int",new Integer(1));
124: numMap.put("float",new Float(2));
125: numMap.put("double",new Double(3));
126: bean.setNumMap(numMap);
127:
128: List mixedList=new ArrayList();
129: mixedList.add("text");
130: mixedList.add(1);
131: mixedList.add(true);
132: TestBean testBean=new TestBean();
133: testBean.setText("test");
134: testBean.setValue(7);
135: mixedList.add(testBean);
136: bean.setMixedList(mixedList);
137:
138: serializer.serialize(bean,writer);
139: String json=writer.toString();
140: String jsonRef="{\"numMap\":{\"double\":3.0,\"float\":2.0,\"int\":1},\"mixedList\":[\"text\",1,true," +
141: "{\"intMap\":null,\"intList\":null,\"value\":7,\"text\":\"test\",\"mapMap\":null,\"strMap\":null,\"strList\":null}]}";
142:
143: //System.out.println(json);
144:
145: assertEquals(json,jsonRef);
146:
147: }
148: */
149:
150: }
|