View Javadoc

1   /*
2    * $Id: JSONUtilTest.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  public class JSONUtilTest extends TestCase {
28  
29      /***
30       * Asserts that a bean can be serialized to JSON and restored as a map
31       */
32      public void testSerializeDeserialize() throws Exception {
33          Bean bean1 = new Bean();
34  
35          bean1.setStringField("str");
36          bean1.setBooleanField(true);
37          bean1.setCharField('s');
38          bean1.setDoubleField(10.1);
39          bean1.setFloatField(1.5f);
40          bean1.setIntField(10);
41          bean1.setLongField(100);
42          bean1.setEnumField(AnEnum.ValueA);
43          bean1.setEnumBean(AnEnumBean.Two);
44  
45          String json = JSONUtil.serialize(bean1);
46  
47          Map result = (Map) JSONUtil.deserialize(json);
48          assertEquals("str", result.get("stringField"));
49          assertEquals(true, result.get("booleanField"));
50          assertEquals("s", result.get("charField")); // note: this is a
51                                                              // String
52          assertEquals(10.1, result.get("doubleField"));
53          assertEquals(1.5, result.get("floatField")); // note: this is a
54                                                              // Double
55          assertEquals(10L, result.get("intField")); // note: this is a
56                                                              // Long
57          assertEquals(AnEnum.ValueA, AnEnum.valueOf((String) result.get("enumField"))); // note:
58                                                                                          // this
59                                                                                          // is a
60                                                                                          // String
61          assertEquals(AnEnumBean.Two, AnEnumBean.valueOf((String) result.get("enumBean"))); // note:
62                                                                                              // this
63                                                                                              // is a
64                                                                                              // String
65      }
66  }