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.commons.collections;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.collections.*;
27  import org.junit.jupiter.api.*;
28  
29  class Args_Test extends TestBase {
30  
31  	//-----------------------------------------------------------------------------------------------------------------
32  	// test - Basic tests
33  	//-----------------------------------------------------------------------------------------------------------------
34  	@Test void basic() {
35  		var a = new Args(a());
36  
37  	// Empty args
38  	assertNull(a.getArg(0));
39  	assertNull(a.getArg(1));
40  	assertNull(a.getArg(-1));
41  	assertNull(a.getArg("foo"));
42  	assertEmpty(a.getArgs("foo"));
43  	assertFalse(a.containsKey("foo"));
44  
45  	a = new Args(a("foo"));
46  	assertEquals("foo", a.getArg(0));
47  	assertNull(a.getArg(1));
48  	assertNull(a.getArg(-1));
49  	assertNull(a.getArg("foo"));
50  	assertEmpty(a.getArgs("foo"));
51  	assertFalse(a.containsKey("foo"));
52  
53  	a = new Args(a("foo", "bar bar"));
54  	assertEquals("foo", a.getArg(0));
55  	assertEquals("bar bar", a.getArg(1));
56  	assertNull(a.getArg(-1));
57  	assertNull(a.getArg("foo"));
58  	assertEmpty(a.getArgs("foo"));
59  	assertFalse(a.containsKey("foo"));
60  
61  	a = new Args(a("foo", "bar bar", "-foo"));
62  	assertEquals("foo", a.getArg(0));
63  	assertEquals("bar bar", a.getArg(1));
64  	assertNull(a.getArg(-1));
65  	assertNull(a.getArg("foo"));
66  	assertEmpty(a.getArgs("foo"));
67  	assertTrue(a.containsKey("foo"));
68  
69  	a = new Args(a("foo", "bar bar", "-foo", "bar bar"));
70  	assertEquals("foo", a.getArg(0));
71  	assertEquals("bar bar", a.getArg(1));
72  	assertNull(a.getArg(-1));
73  	assertEquals("bar bar", a.getArg("foo"));
74  	assertSize(1, a.getArgs("foo"));
75  		assertEquals("bar bar", a.getArgs("foo").get(0));
76  		assertTrue(a.containsKey("foo"));
77  	}
78  
79  	//-----------------------------------------------------------------------------------------------------------------
80  	// test - Fluent setters
81  	//-----------------------------------------------------------------------------------------------------------------
82  	@Test void fluentSetters() {
83  		var a = new Args(a("main1"));
84  
85  		// Test inner() returns same instance for fluent chaining
86  		var innerMap = new HashMap<String,Object>();
87  		innerMap.put("test", "value");
88  		assertSame(a, a.inner(innerMap));
89  
90  		// Test session() returns same instance
91  		BeanSession session = BeanContext.DEFAULT.getSession();
92  		assertSame(a, a.session(session));
93  
94  		// Test append(String, Object) returns same instance
95  		assertSame(a, a.append("key1", "value1"));
96  		assertEquals("value1", a.get("key1"));
97  
98  		// Test append(Map) returns same instance
99  		var appendMap = new HashMap<String,Object>();
100 		appendMap.put("key2", "value2");
101 		assertSame(a, a.append(appendMap));
102 		assertEquals("value2", a.get("key2"));
103 
104 		// Test appendIf() returns same instance
105 		assertSame(a, a.appendIf(true, "key3", "value3"));
106 		assertEquals("value3", a.get("key3"));
107 		assertSame(a, a.appendIf(false, "key4", "value4"));
108 		assertNull(a.get("key4"));
109 
110 		// Test filtered() returns same instance
111 		assertSame(a, a.filtered(x -> x != null));
112 
113 		// Test keepAll() returns same instance
114 		assertSame(a, a.keepAll("key1", "key2"));
115 
116 		// Test setBeanSession() returns same instance
117 		assertSame(a, a.setBeanSession(session));
118 
119 		// Test modifiable() returns same instance
120 		assertSame(a, a.modifiable());
121 
122 		// Test unmodifiable() returns same instance
123 		assertSame(a, a.unmodifiable());
124 	}
125 
126 	@Test void fluentChaining() {
127 		// Test multiple fluent calls can be chained
128 		var a = new Args(a("main1"))
129 			.append("key1", "value1")
130 			.append("key2", "value2")
131 			.appendIf(true, "key3", "value3");
132 
133 		assertEquals("value1", a.get("key1"));
134 		assertEquals("value2", a.get("key2"));
135 		assertEquals("value3", a.get("key3"));
136 	}
137 }