View Javadoc

1   /*
2    * $Id: JSONEnumTest.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.util.Map;
24  
25  import junit.framework.TestCase;
26  
27  /***
28   * Test serialization of an Enum in the two supported modes: - Enum as a
29   * name=value pair; - Enum as a bean
30   */
31  public class JSONEnumTest extends TestCase {
32  
33      /***
34       * Asserts that a bean can be serialized to JSON and restored as a map
35       */
36      public void testEnumAsNameValue() throws Exception {
37          Bean bean1 = new Bean();
38  
39          bean1.setStringField("str");
40          bean1.setBooleanField(true);
41          bean1.setCharField('s');
42          bean1.setDoubleField(10.1);
43          bean1.setFloatField(1.5f);
44          bean1.setIntField(10);
45          bean1.setLongField(100);
46          bean1.setEnumField(AnEnum.ValueA);
47          bean1.setEnumBean(AnEnumBean.Two);
48  
49          JSONWriter jsonWriter = new JSONWriter();
50          jsonWriter.setEnumAsBean(false);
51          String json = jsonWriter.write(bean1);
52  
53          Map result = (Map) JSONUtil.deserialize(json);
54          assertEquals("str", result.get("stringField"));
55          assertEquals(true, result.get("booleanField"));
56          assertEquals("s", result.get("charField")); // note: this is a
57                                                              // String
58          assertEquals(10.1, result.get("doubleField"));
59          assertEquals(1.5, result.get("floatField")); // note: this is a
60                                                              // Double
61          assertEquals(10L, result.get("intField")); // note: this is a
62                                                              // Long
63          assertEquals(AnEnum.ValueA, AnEnum.valueOf((String) result.get("enumField"))); // note:
64                                                                                          // this
65                                                                                          // is a
66                                                                                          // String
67          assertEquals(AnEnumBean.Two, AnEnumBean.valueOf((String) result.get("enumBean"))); // note:
68                                                                                              // this
69                                                                                              // is a
70                                                                                              // String
71      }
72  
73      /***
74       * Asserts that a bean can be serialized to JSON and restored as a map <p/>
75       * In this case, the name of the enum is in _name and the two properties of
76       * AnEnumBean are also serialized
77       */
78      public void testEnumAsBean() throws Exception {
79          Bean bean1 = new Bean();
80  
81          bean1.setStringField("str");
82          bean1.setBooleanField(true);
83          bean1.setCharField('s');
84          bean1.setDoubleField(10.1);
85          bean1.setFloatField(1.5f);
86          bean1.setIntField(10);
87          bean1.setLongField(100);
88          bean1.setEnumField(AnEnum.ValueA);
89          bean1.setEnumBean(AnEnumBean.Two);
90  
91          JSONWriter jsonWriter = new JSONWriter();
92          jsonWriter.setEnumAsBean(true);
93          String json = jsonWriter.write(bean1);
94  
95          Map result = (Map) JSONUtil.deserialize(json);
96          assertEquals("str", result.get("stringField"));
97          assertEquals(true, result.get("booleanField"));
98          assertEquals("s", result.get("charField")); // note: this is a
99                                                              // String
100         assertEquals(10.1, result.get("doubleField"));
101         assertEquals(1.5, result.get("floatField")); // note: this is a
102                                                             // Double
103         assertEquals(10L, result.get("intField")); // note: this is a
104                                                             // Long
105         Map enumBean1 = (Map) result.get("enumField");
106         assertNotNull(enumBean1);
107         assertEquals(AnEnum.ValueA, AnEnum.valueOf((String) enumBean1.get("_name"))); // get
108                                                                                         // the
109                                                                                         // special
110                                                                                         // name
111                                                                                         // property
112         Map enumBean2 = (Map) result.get("enumBean");
113         assertEquals(AnEnumBean.Two, AnEnumBean.valueOf((String) enumBean2.get("_name"))); // get
114                                                                                             // the
115                                                                                             // special
116                                                                                             // name
117                                                                                             // property
118         assertEquals(AnEnumBean.Two.getPropA(), (String) enumBean2.get("propA")); // get
119                                                                                     // the
120                                                                                     // propA
121                                                                                     // property
122         assertEquals(AnEnumBean.Two.getPropB(), (String) enumBean2.get("propB")); // get
123                                                                                     // the
124                                                                                     // propA
125                                                                                     // property
126     }
127 }