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.lang;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  class StringValue_Test extends TestBase {
26  
27  	//-----------------------------------------------------------------------------------------------------------------
28  	// Basic tests
29  	//-----------------------------------------------------------------------------------------------------------------
30  
31  	@Test
32  	void a01_create() {
33  		var a = StringValue.create();
34  		assertNull(a.get());
35  		assertFalse(a.isPresent());
36  	}
37  
38  	@Test
39  	void a02_of() {
40  		var a = StringValue.of("Hello");
41  		assertEquals("Hello", a.get());
42  		assertTrue(a.isPresent());
43  	}
44  
45  	@Test
46  	void a03_of_null() {
47  		var a = StringValue.of(null);
48  		assertNull(a.get());
49  		assertFalse(a.isPresent());
50  	}
51  
52  	@Test
53  	void a04_constructor_default() {
54  		var a = new StringValue();
55  		assertNull(a.get());
56  	}
57  
58  	@Test
59  	void a05_constructor_withValue() {
60  		var a = new StringValue("Test");
61  		assertEquals("Test", a.get());
62  	}
63  
64  	//-----------------------------------------------------------------------------------------------------------------
65  	// is() tests
66  	//-----------------------------------------------------------------------------------------------------------------
67  
68  	@Test
69  	void b01_is_match() {
70  		var a = StringValue.of("John");
71  		assertTrue(a.is("John"));
72  	}
73  
74  	@Test
75  	void b02_is_noMatch() {
76  		var a = StringValue.of("John");
77  		assertFalse(a.is("Jane"));
78  	}
79  
80  	@Test
81  	void b03_is_null() {
82  		var a = StringValue.create();
83  		assertTrue(a.is(null));
84  		assertFalse(a.is("something"));
85  	}
86  
87  	@Test
88  	void b04_is_nullArg() {
89  		var a = StringValue.of("John");
90  		assertFalse(a.is(null));
91  	}
92  
93  	//-----------------------------------------------------------------------------------------------------------------
94  	// isAny(...) tests
95  	//-----------------------------------------------------------------------------------------------------------------
96  
97  	@Test
98  	void c01_isAny_match() {
99  		var a = StringValue.of("John");
100 		assertTrue(a.isAny("John", "Jane", "Bob"));
101 	}
102 
103 	@Test
104 	void c02_isAny_noMatch() {
105 		var a = StringValue.of("John");
106 		assertFalse(a.isAny("Alice", "Charlie"));
107 	}
108 
109 	@Test
110 	void c03_isAny_empty() {
111 		var a = StringValue.of("John");
112 		assertFalse(a.isAny());
113 	}
114 
115 	@Test
116 	void c04_isAny_withNull() {
117 		var a = StringValue.create();
118 		assertTrue(a.isAny("John", null, "Jane"));
119 	}
120 
121 	@Test
122 	void c05_isAny_allNull() {
123 		var a = StringValue.create();
124 		assertTrue(a.isAny((String)null));
125 	}
126 
127 	//-----------------------------------------------------------------------------------------------------------------
128 	// setIf() tests
129 	//-----------------------------------------------------------------------------------------------------------------
130 
131 	@Test
132 	void d01_setIf_true() {
133 		var a = StringValue.of("old");
134 		a.setIf(true, "new");
135 		assertEquals("new", a.get());
136 	}
137 
138 	@Test
139 	void d02_setIf_false() {
140 		var a = StringValue.of("old");
141 		a.setIf(false, "new");
142 		assertEquals("old", a.get());
143 	}
144 
145 	@Test
146 	void d03_setIf_chain() {
147 		var a = StringValue.of("start");
148 		a.setIf(false, "skip1").setIf(true, "set").setIf(false, "skip2");
149 		assertEquals("set", a.get());
150 	}
151 
152 	//-----------------------------------------------------------------------------------------------------------------
153 	// update() tests
154 	//-----------------------------------------------------------------------------------------------------------------
155 
156 	@Test
157 	void e01_update_basic() {
158 		var a = StringValue.of("hello");
159 		a.update(String::toUpperCase);
160 		assertEquals("HELLO", a.get());
161 	}
162 
163 	@Test
164 	void e02_update_null() {
165 		var a = StringValue.create();
166 		a.update(String::toUpperCase);
167 		assertNull(a.get());  // Should be no-op
168 	}
169 
170 	@Test
171 	void e03_update_chain() {
172 		var a = StringValue.of("hello");
173 		a.update(String::toUpperCase).update(s -> s + "!");
174 		assertEquals("HELLO!", a.get());
175 	}
176 
177 	@Test
178 	void e04_update_withTrim() {
179 		var a = StringValue.of("  spaces  ");
180 		a.update(String::trim);
181 		assertEquals("spaces", a.get());
182 	}
183 
184 	//-----------------------------------------------------------------------------------------------------------------
185 	// Inherited Value<String> functionality tests
186 	//-----------------------------------------------------------------------------------------------------------------
187 
188 	@Test
189 	void f01_set() {
190 		var a = StringValue.create();
191 		a.set("Value");
192 		assertEquals("Value", a.get());
193 	}
194 
195 	@Test
196 	void f02_setIfEmpty() {
197 		var a = StringValue.of("existing");
198 		a.setIfEmpty("new");
199 		assertEquals("existing", a.get());  // Should not change
200 
201 		a.set(null);
202 		a.setIfEmpty("new");
203 		assertEquals("new", a.get());  // Should change
204 	}
205 
206 	@Test
207 	void f03_getAndSet() {
208 		var a = StringValue.of("old");
209 		var b = a.getAndSet("new");
210 		assertEquals("old", b);
211 		assertEquals("new", a.get());
212 	}
213 
214 	@Test
215 	void f04_getAndUnset() {
216 		var a = StringValue.of("value");
217 		var b = a.getAndUnset();
218 		assertEquals("value", b);
219 		assertNull(a.get());
220 		assertFalse(a.isPresent());
221 	}
222 
223 	@Test
224 	void f05_orElse() {
225 		var a = StringValue.of("value");
226 		assertEquals("value", a.orElse("default"));
227 
228 		a.set(null);
229 		assertEquals("default", a.orElse("default"));
230 	}
231 
232 	@Test
233 	void f06_map() {
234 		var a = StringValue.of("test");
235 		var b = a.map(s -> s.length());
236 		assertEquals(4, b.get());
237 
238 		a.set(null);
239 		var c = a.map(s -> s.length());
240 		assertNull(c.get());
241 	}
242 
243 	//-----------------------------------------------------------------------------------------------------------------
244 	// Use case scenarios
245 	//-----------------------------------------------------------------------------------------------------------------
246 
247 	@Test
248 	void g01_trackLastValue() {
249 		var a = StringValue.create();
250 
251 		var list = l("a", "b", "c", "d", "e");
252 		list.forEach(a::set);
253 
254 		assertEquals("e", a.get());
255 	}
256 
257 	@Test
258 	void g02_conditionalUpdate() {
259 		var a = StringValue.create();
260 
261 		var list = l("apple", "banana", "apricot", "avocado");
262 		list.forEach(x -> a.setIf(x.startsWith("a"), x));
263 
264 		assertEquals("avocado", a.get());  // Last "a" word
265 	}
266 
267 	@Test
268 	void g03_transformationPipeline() {
269 		var a = StringValue.of("  hello world  ");
270 		a.update(String::trim)
271 			.update(String::toUpperCase)
272 			.update(s -> s.replace(" ", "_"));
273 
274 		assertEquals("HELLO_WORLD", a.get());
275 	}
276 }
277