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.svl;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.collections.*;
24  import org.junit.jupiter.api.*;
25  
26  class ResolvingJsonMapTest extends TestBase {
27  
28  	//====================================================================================================
29  	// test - Basic tests
30  	//====================================================================================================
31  	@Test void a01_basic() {
32  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
33  		var m = new ResolvingJsonMap(vr.createSession());
34  
35  		m.put("foo", "$X{a}");
36  		assertEquals("1", m.get("foo"));
37  
38  		m.put("foo", new String[]{"$X{a}"});
39  		assertList(m.get("foo"), "1");
40  
41  		m.put("foo", list("$X{a}"));
42  		assertList(m.get("foo"), "1");
43  
44  		m.put("foo", map("k1","$X{a}"));
45  		assertMap(m, "foo{k1}", "{1}");
46  	}
47  
48  	public static class XVar extends MapVar {
49  		public XVar() {
50  			super("X", JsonMap.of("a", 1, "b", 2, "c", 3));
51  		}
52  	}
53  
54  	//====================================================================================================
55  	// testNulls
56  	//====================================================================================================
57  	@Test void a02_nulls() {
58  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
59  		var m = new ResolvingJsonMap(vr.createSession());
60  
61  		m.put("foo", null);
62  		assertNull(m.get("foo"));
63  
64  		m.put("foo", new String[]{null});
65  		assertList(m.get("foo"), (Object)null);
66  
67  		m.put("foo", list((String)null));
68  		assertList(m.get("foo"), (Object)null);
69  
70  		m.put("foo", map("k1",null));
71  		assertMap(m, "foo{k1}", "{<null>}");
72  	}
73  
74  	//====================================================================================================
75  	// testNonStrings
76  	//====================================================================================================
77  	@Test void a03_nonStrings() {
78  		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
79  		var m = new ResolvingJsonMap(vr.createSession());
80  
81  		m.put("foo", FooEnum.ONE);
82  		assertString("ONE", m.get("foo"));
83  		m.put("foo", new Object[]{FooEnum.ONE});
84  		assertList(m.get("foo"), "ONE");
85  
86  		m.put("foo", list(FooEnum.ONE));
87  		assertList(m.get("foo"), "ONE");
88  
89  		m.put("foo", map(FooEnum.ONE,FooEnum.ONE));
90  		assertBean(m, "foo", "{ONE=ONE}");
91  	}
92  
93  	public enum FooEnum {
94  		ONE
95  	}
96  
97  	//====================================================================================================
98  	// testInner - Test inner maps
99  	//====================================================================================================
100 	@Test void a04_inner() {
101 		var vr = VarResolver.create().defaultVars().vars(XVar.class).build();
102 		var m = new ResolvingJsonMap(vr.createSession());
103 		var m2 = new JsonMap();
104 		var m3 = new JsonMap();
105 		m.inner(m2);
106 		m2.inner(m3);
107 
108 		m3.put("foo", "$X{a}");
109 		assertEquals("1", m.get("foo"));
110 
111 		m3.put("foo", new String[]{"$X{a}"});
112 		assertList(m.get("foo"), "1");
113 
114 		m3.put("foo", list("$X{a}"));
115 		assertList(m.get("foo"), "1");
116 
117 		m3.put("foo", map("k1","$X{a}"));
118 		assertMap(m, "foo{k1}", "{1}");
119 	}
120 }