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  class JsonList_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// testBasic
31  	//====================================================================================================
32  	@Test void a01_basic() {
33  
34  		assertEquals(
35  			"['A','B','C']",
36  			new JsonList((Object[])new String[]{"A","B","C"}).toString()
37  		);
38  
39  		assertEquals(
40  			"['A','B','C']",
41  			new JsonList("A","B","C").toString()
42  		);
43  
44  		assertEquals(
45  			"['A','B','C']",
46  			new JsonList(Arrays.asList(new String[]{"A","B","C"})).toString()
47  		);
48  	}
49  
50  	//====================================================================================================
51  	// testIterateAs
52  	//====================================================================================================
53  	@Test void a02_iterateAs() throws Exception {
54  
55  		// Iterate over a list of JsonMaps.
56  		var l = new JsonList("[{foo:'bar'},{baz:123}]");
57  		var i1 = l.elements(JsonMap.class).iterator();
58  		assertEquals("bar", i1.next().getString("foo"));
59  		assertEquals(123, (int)i1.next().getInt("baz"));
60  
61  		// Iterate over a list of ints.
62  		l = new JsonList("[1,2,3]");
63  		var i2 = l.elements(Integer.class).iterator();
64  		assertEquals(1, (int)i2.next());
65  		assertEquals(2, (int)i2.next());
66  		assertEquals(3, (int)i2.next());
67  
68  		// Iterate over a list of beans.
69  		// Automatically converts to beans.
70  		l = new JsonList("[{name:'John Smith',age:45}]");
71  		var i3 = l.elements(Person.class).iterator();
72  		assertEquals("John Smith", i3.next().name);
73  	}
74  
75  	public static class Person {
76  		public String name;
77  		public int age;
78  	}
79  
80  	//====================================================================================================
81  	// testAtMethods
82  	//====================================================================================================
83  	@Test void a03_atMethods() throws Exception {
84  		var l = new JsonList("[{foo:'bar'},{baz:123}]");
85  		var r = l.getAt("0/foo", String.class);
86  
87  		assertEquals("bar", r);
88  
89  		l.putAt("0/foo", "bing");
90  		r = l.getAt("0/foo", String.class);
91  		assertEquals("bing", r);
92  
93  		l.postAt("", JsonMap.ofJson("{a:'b'}"));
94  		r = l.getAt("2/a", String.class);
95  		assertEquals("b", r);
96  
97  		l.deleteAt("2");
98  		assertEquals("[{foo:'bing'},{baz:123}]", l.toString());
99  	}
100 
101 	//====================================================================================================
102 	// JsonList(Reader)
103 	//====================================================================================================
104 	@Test void a04_fromReader() throws Exception {
105 		assertList(new JsonList(reader("[1,2,3]")), "1", "2", "3");
106 	}
107 
108 	//====================================================================================================
109 	// testGetMap
110 	//====================================================================================================
111 	@Test void a05_getMap() throws Exception {
112 		var l = new JsonList("[{1:'true',2:'false'}]");
113 		var m2 = l.getMap(0, Integer.class, Boolean.class);
114 		assertJson("{'1':true,'2':false}", m2);
115 		assertEquals(Integer.class, m2.keySet().iterator().next().getClass());
116 		assertEquals(Boolean.class, m2.values().iterator().next().getClass());
117 
118 		m2 = l.get(0, Map.class, Integer.class, Boolean.class);
119 		assertJson("{'1':true,'2':false}", m2);
120 		assertEquals(Integer.class, m2.keySet().iterator().next().getClass());
121 		assertEquals(Boolean.class, m2.values().iterator().next().getClass());
122 	}
123 
124 	//====================================================================================================
125 	// testGetList
126 	//====================================================================================================
127 	@Test void a06_getList() throws Exception {
128 		var l = new JsonList("[['123','456']]");
129 		var l2 = l.getList(0, Integer.class);
130 		assertList(l2, "123", "456");
131 		assertEquals(Integer.class, l2.iterator().next().getClass());
132 
133 		l2 = l.get(0, List.class, Integer.class);
134 		assertList(l2, "123", "456");
135 		assertEquals(Integer.class, l2.iterator().next().getClass());
136 	}
137 }