View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.*;
23  
24  import org.apache.juneau.collections.*;
25  import org.junit.jupiter.api.*;
26  
27  @SuppressWarnings("rawtypes")
28  class DataConversion_Test extends TestBase {
29  
30  	@BeforeEach
31  	void beforeTest() {
32  		setLocale(Locale.US);
33  	}
34  
35  	@AfterEach
36  	void afterTest() {
37  		unsetLocale();
38  	}
39  
40  	//====================================================================================================
41  	// testBasic
42  	//====================================================================================================
43  	@Test void a01_basic() throws Exception {
44  		var m = new JsonMap();
45  
46  		// *** Number ***
47  		m.put("x", 123);
48  		assertEquals(123, (int)m.getInt("x"));
49  		assertEquals(123, (long)m.getLong("x"));
50  
51  		// *** Boolean ***
52  		m.put("x", true);
53  		assertEquals(true, (boolean)m.getBoolean("x"));
54  
55  		// *** Null ***
56  		m.put("x", null);
57  		assertNull(m.getString("x"));
58  		assertNull(m.getInt("x"));
59  		assertNull(m.getLong("x"));
60  		assertNull(m.getBoolean("x"));
61  		assertNull(m.getMap("x"));
62  		assertNull(m.getList("x"));
63  
64  		// *** Map ***
65  		m.put("x", new HashMap());
66  		assertEquals("{}", m.getString("x"));
67  
68  		// *** JsonMap ***
69  		m.put("x", JsonMap.ofJson("{foo:123}"));
70  		assertEquals("{foo:123}", m.getString("x"));
71  
72  		// *** Collection ***
73  		var s = new HashSet<Integer>();
74  		s.add(123);
75  		m.put("x", s);
76  		assertEquals("[123]", m.getString("x"));
77  
78  		// *** JsonList ***
79  		m.put("x", JsonList.ofJson("[123]"));
80  		assertEquals("[123]", m.getString("x"));
81  		assertEquals(1, m.getList("x").size());
82  
83  		// *** Array ***
84  		m.put("x", new Integer[]{123});
85  		assertEquals("[123]", m.getString("x"));
86  		assertEquals(1, m.getList("x").size());
87  
88  		// *** Enum ***
89  		m.put("x", TestEnum.ENUM2);
90  		assertEquals("ENUM2", m.getString("x"));
91  		assertFalse(m.getBoolean("x"));
92  		assertThrows(InvalidDataConversionException.class, ()->m.getMap("x"));
93  
94  		// *** Not a bean ***
95  		m.put("x", new NotABean("foo"));
96  		assertEquals("foo", m.getString("x"));
97  		assertThrows(InvalidDataConversionException.class, ()->m.getInt("x"));
98  		assertThrows(InvalidDataConversionException.class, ()->m.getLong("x"));
99  		assertFalse(m.getBoolean("x"));
100 		assertThrows(InvalidDataConversionException.class, ()->m.getMap("x"));
101 	}
102 
103 	public enum TestEnum {
104 		ENUM0, ENUM1, ENUM2
105 	}
106 
107 	public class NotABean {
108 		private String arg;
109 
110 		public NotABean(String arg) {
111 			this.arg = arg;
112 		}
113 
114 		@Override /* Object */
115 		public String toString() {
116 			return arg;
117 		}
118 	}
119 
120 	//====================================================================================================
121 	// Data conversions with swaps.
122 	//====================================================================================================
123 	@Test void a02_objectSwaps() throws Exception {
124 		var s = "2001-12-21T12:34:56Z";
125 		var bc = BeanContext.DEFAULT;
126 		var c = bc.convertToType(s, GregorianCalendar.class);
127 		assertEquals(2001, c.get(Calendar.YEAR));
128 		var c2 = bc.convertToType(s, Calendar.class);
129 		assertEquals(2001, c2.get(Calendar.YEAR));
130 		s = bc.convertToType(c2, String.class);
131 		assertEquals("2001-12-21T12:34:56Z", s);
132 	}
133 }