View Javadoc

1   /*
2    * $Id: JSONPopulatorTest.java 799110 2009-07-29 22:44:26Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.json;
22  
23  import java.beans.IntrospectionException;
24  import java.io.StringReader;
25  import java.lang.reflect.InvocationTargetException;
26  import java.math.BigDecimal;
27  import java.math.BigInteger;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import junit.framework.TestCase;
32  
33  public class JSONPopulatorTest extends TestCase {
34  
35      public void testNulls() throws IntrospectionException, InvocationTargetException, NoSuchMethodException,
36              JSONException, InstantiationException, IllegalAccessException {
37          JSONPopulator populator = new JSONPopulator();
38          OtherBean bean = new OtherBean();
39          Map jsonMap = new HashMap();
40  
41          jsonMap.put("intField", null);
42          jsonMap.put("booleanField", null);
43          jsonMap.put("charField", null);
44          jsonMap.put("longField", null);
45          jsonMap.put("floatField", null);
46          jsonMap.put("doubleField", null);
47          jsonMap.put("byteField", null);
48  
49          populator.populateObject(bean, jsonMap);
50          assertNull(bean.getIntField());
51          assertNull(bean.isBooleanField());
52          assertNull(bean.getCharField());
53          assertNull(bean.getLongField());
54          assertNull(bean.getDoubleField());
55          assertNull(bean.getByteField());
56      }
57  
58      public void testPrimitiveBean() throws Exception {
59          StringReader stringReader = new StringReader(TestUtils.readContent(JSONInterceptorTest.class
60                  .getResource("json-7.txt")));
61          Object json = JSONUtil.deserialize(stringReader);
62          assertNotNull(json);
63          assertTrue(json instanceof Map);
64          Map jsonMap = (Map) json;
65          JSONPopulator populator = new JSONPopulator();
66          Bean bean = new Bean();
67          populator.populateObject(bean, jsonMap);
68          assertTrue(bean.isBooleanField());
69          assertEquals("test\u000E\u000f", bean.getStringField());
70          assertEquals(10, bean.getIntField());
71          assertEquals('s', bean.getCharField());
72          assertEquals(10.1d, bean.getDoubleField(), 0d);
73          assertEquals(3, bean.getByteField());
74          assertEquals(new BigDecimal(111111.5d), bean.getBigDecimal());
75          assertEquals(new BigInteger("111111"), bean.getBigInteger());
76      }
77  
78      public void testObjectBean() throws Exception {
79          String text = TestUtils.readContent(JSONInterceptorTest.class.getResource("json-7.txt"));
80          Object json = JSONUtil.deserialize(text);
81          assertNotNull(json);
82          assertTrue(json instanceof Map);
83          Map jsonMap = (Map) json;
84          JSONPopulator populator = new JSONPopulator();
85          WrapperClassBean bean = new WrapperClassBean();
86          populator.populateObject(bean, jsonMap);
87          assertEquals(Boolean.TRUE, bean.getBooleanField());
88          assertEquals(true, bean.isPrimitiveBooleanField1());
89          assertEquals(false, bean.isPrimitiveBooleanField2());
90          assertEquals(false, bean.isPrimitiveBooleanField3());
91          assertEquals("test\u000E\u000f", bean.getStringField());
92          assertEquals(new Integer(10), bean.getIntField());
93          assertEquals(0, bean.getNullIntField());
94          assertEquals(new Character('s'), bean.getCharField());
95          assertEquals(10.1d, bean.getDoubleField());
96          assertEquals(new Byte((byte) 3), bean.getByteField());
97  
98          assertEquals(2, bean.getListField().size());
99          assertEquals("1", bean.getListField().get(0).getValue());
100         assertEquals("2", bean.getListField().get(1).getValue());
101 
102         assertEquals(1, bean.getListMapField().size());
103         assertEquals(2, bean.getListMapField().get(0).size());
104         assertEquals(new Long(2073501), bean.getListMapField().get(0).get("id1"));
105         assertEquals(new Long(3), bean.getListMapField().get(0).get("id2"));
106 
107         assertEquals(2, bean.getMapListField().size());
108         assertEquals(3, bean.getMapListField().get("id1").size());
109         assertEquals(new Long(2), bean.getMapListField().get("id1").get(1));
110         assertEquals(4, bean.getMapListField().get("id2").size());
111         assertEquals(new Long(3), bean.getMapListField().get("id2").get(1));
112 
113         assertEquals(1, bean.getArrayMapField().length);
114         assertEquals(2, bean.getArrayMapField()[0].size());
115         assertEquals(new Long(2073501), bean.getArrayMapField()[0].get("id1"));
116         assertEquals(new Long(3), bean.getArrayMapField()[0].get("id2"));
117     }
118 
119     public void testObjectBeanWithStrings() throws Exception {
120         StringReader stringReader = new StringReader(TestUtils.readContent(JSONInterceptorTest.class
121                 .getResource("json-8.txt")));
122         Object json = JSONUtil.deserialize(stringReader);
123         assertNotNull(json);
124         assertTrue(json instanceof Map);
125         Map jsonMap = (Map) json;
126         JSONPopulator populator = new JSONPopulator();
127         WrapperClassBean bean = new WrapperClassBean();
128         populator.populateObject(bean, jsonMap);
129         assertEquals(Boolean.TRUE, bean.getBooleanField());
130         assertEquals("test", bean.getStringField());
131         assertEquals(new Integer(10), bean.getIntField());
132         assertEquals(new Character('s'), bean.getCharField());
133         assertEquals(10.1d, bean.getDoubleField());
134         assertEquals(new Byte((byte) 3), bean.getByteField());
135 
136         assertEquals(null, bean.getListField());
137         assertEquals(null, bean.getListMapField());
138         assertEquals(null, bean.getMapListField());
139         assertEquals(null, bean.getArrayMapField());
140     }
141 
142     public void testInfiniteLoop() throws JSONException {
143         try {
144             JSONReader reader = new JSONReader();
145             reader.read("[1,\"a]");
146             fail("Should have thrown an exception");
147         } catch (JSONException e) {
148             // I can't get JUnit to ignore the exception
149             // @Test(expected = JSONException.class)
150         }
151     }
152 
153     public void testParseBadInput() throws JSONException {
154         try {
155             JSONReader reader = new JSONReader();
156             reader.read("[1,\"a\"1]");
157             fail("Should have thrown an exception");
158         } catch (JSONException e) {
159             // I can't get JUnit to ignore the exception
160             // @Test(expected = JSONException.class)
161         }
162     }
163 }