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