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.assertions;
18  
19  import static org.apache.juneau.assertions.AssertionPredicates.ne;
20  import static org.apache.juneau.assertions.Assertions.*;
21  import static org.apache.juneau.commons.lang.Version.*;
22  import static org.apache.juneau.commons.utils.Utils.*;
23  import static org.junit.jupiter.api.Assertions.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.commons.lang.*;
27  import org.apache.juneau.json.*;
28  import org.junit.jupiter.api.*;
29  
30  @Deprecated
31  class VersionAssertion_Test extends TestBase {
32  
33  	//------------------------------------------------------------------------------------------------------------------
34  	// Helpers
35  	//------------------------------------------------------------------------------------------------------------------
36  
37  	private static VersionAssertion test(Version value) {
38  		return assertVersion(value).setSilent();
39  	}
40  
41  	//-----------------------------------------------------------------------------------------------------------------
42  	// Basic tests
43  	//-----------------------------------------------------------------------------------------------------------------
44  
45  	@Test void a01_msg() {
46  		assertThrows(BasicAssertionError.class, ()->test(null).setMsg("Foo {0}", 1).isExists(), "Foo 1");
47  		assertThrows(RuntimeException.class, ()->test(null).setMsg("Foo {0}", 1).setThrowable(RuntimeException.class).isExists(), "Foo 1");
48  	}
49  
50  	@Test void a02_stdout() {
51  		test(null).setStdOut();
52  	}
53  
54  	//-----------------------------------------------------------------------------------------------------------------
55  	// Transform tests
56  	//-----------------------------------------------------------------------------------------------------------------
57  
58  	@Test void ba01a_asString() {
59  		var x = of("1");
60  		var nil = no(Version.class);
61  		test(x).asString().is("1");
62  		test(nil).asString().isNull();
63  	}
64  
65  	@Test void ba01b_asString_wSerializer() {
66  		var x = of("1");
67  		var nil = no(Version.class);
68  		var s = Json5Serializer.DEFAULT;
69  		test(x).asString(s).is("{maintenance:null,major:1,minor:null}");
70  		test(nil).asString(s).is("null");
71  	}
72  
73  	@Test void ba01c_asString_wPredicate() {
74  		var x1 = of("1");
75  		test(x1).asString(x -> "foo").is("foo");
76  	}
77  
78  	@Test void ba02_asJson() {
79  		var x = of("1");
80  		var nil = no(Version.class);
81  		test(x).asJson().is("{maintenance:null,major:1,minor:null}");
82  		test(nil).asJson().is("null");
83  	}
84  
85  	@Test void ba03_asJsonSorted() {
86  		var x = of("1");
87  		var nil = no(Version.class);
88  		test(x).asJsonSorted().is("{maintenance:null,major:1,minor:null}");
89  		test(nil).asJsonSorted().is("null");
90  	}
91  
92  	@Test void ba04_apply() {
93  		var x1 = of("1");
94  		var x2 = of("2");
95  		test(x1).asTransformed(x -> x2).is(x2);
96  	}
97  
98  	@Test void bc01_part() {
99  		var x = of("1.2.3");
100 		var nil = no(Version.class);
101 		test(x).asPart(-1).isNull();
102 		test(x).asPart(0).is(1);
103 		test(x).asPart(1).is(2);
104 		test(x).asPart(2).is(3);
105 		test(x).asPart(3).isNull();
106 		test(nil).asPart(0).isNull();
107 	}
108 
109 	@Test void bc02_major() {
110 		var x = of("1.2.3");
111 		var nil = no(Version.class);
112 		test(x).asMajor().is(1);
113 		test(nil).asMajor().isNull();
114 	}
115 
116 	@Test void bc03_minor() {
117 		var x = of("1.2.3");
118 		var nil = no(Version.class);
119 		test(x).asMinor().is(2);
120 		test(nil).asMinor().isNull();
121 	}
122 
123 	@Test void bc04_maintenance() {
124 		var x = of("1.2.3");
125 		var nil = no(Version.class);
126 		test(x).asMaintenance().is(3);
127 		test(nil).asMaintenance().isNull();
128 	}
129 
130 	//-----------------------------------------------------------------------------------------------------------------
131 	// Test tests
132 	//-----------------------------------------------------------------------------------------------------------------
133 
134 	@Test void ca01_exists() {
135 		var x = of("1");
136 		var nil = no(Version.class);
137 		test(x).isExists().isExists();
138 		assertThrows(BasicAssertionError.class, ()->test(nil).isExists(), "Value was null.");
139 	}
140 
141 	@Test void ca02_isNull() {
142 		var x = of("1");
143 		var nil = no(Version.class);
144 		test(nil).isNull();
145 		assertThrows(BasicAssertionError.class, ()->test(x).isNull(), "Value was not null.");
146 	}
147 
148 	@Test void ca03_isNotNull() {
149 		var x = of("1");
150 		var nil = no(Version.class);
151 		test(x).isNotNull();
152 		assertThrows(BasicAssertionError.class, ()->test(nil).isNotNull(), "Value was null.");
153 	}
154 
155 	@Test void ca04a_is_T() {
156 		var x1 = of("1");
157 		var x1a = of("1");
158 		var x2 = of("2");
159 		var nil = no(Version.class);
160 		test(x1).is(x1);
161 		test(x1).is(x1a);
162 		test(nil).is(nil);
163 		assertThrown(()->test(x1).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='2'.  Actual='1'.");
164 		assertThrown(()->test(x1).is(nil)).asMessage().asOneLine().is("Unexpected value.  Expect='null'.  Actual='1'.");
165 		assertThrown(()->test(nil).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='2'.  Actual='null'.");
166 	}
167 
168 	@Test void ca04b_is_predicate() {
169 		var x1 = of("1");
170 		test(x1).is(x->x.getMajor().orElse(2) == 1);
171 		assertThrown(()->test(x1).is(x->x.getMajor().orElse(2)==2)).asMessage().asOneLine().is("Unexpected value: '1'.");
172 		assertThrown(()->test(x1).is(ne(x1))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='1'.");
173 	}
174 
175 	@Test void ca05_isNot() {
176 		var x1 = of("1");
177 		var x1a = of("1");
178 		var x2 = of("2");
179 		var nil = no(Version.class);
180 		test(x1).isNot(x2);
181 		test(x1).isNot(nil);
182 		test(nil).isNot(x1);
183 		assertThrown(()->test(x1).isNot(x1a)).asMessage().asOneLine().is("Unexpected value.  Did not expect='1'.  Actual='1'.");
184 		assertThrown(()->test(nil).isNot(nil)).asMessage().asOneLine().is("Unexpected value.  Did not expect='null'.  Actual='null'.");
185 	}
186 
187 	@Test void ca06_isAny() {
188 		var x1 = of("1");
189 		var x1a = of("1");
190 		var x2 = of("2");
191 		var nil = no(Version.class);
192 		test(x1).isAny(x1a, x2);
193 		assertThrown(()->test(x1).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[2]'.  Actual='1'.");
194 		assertThrown(()->test(x1).isAny()).asMessage().asOneLine().is("Expected value not found.  Expect='[]'.  Actual='1'.");
195 		assertThrown(()->test(nil).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[2]'.  Actual='null'.");
196 	}
197 
198 	@Test void ca07_isNotAny() {
199 		var x1 = of("1");
200 		var x1a = of("1");
201 		var x2 = of("2");
202 		var nil = no(Version.class);
203 		test(x1).isNotAny(x2);
204 		test(x1).isNotAny();
205 		test(nil).isNotAny(x2);
206 		assertThrown(()->test(x1).isNotAny(x1a)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='1'.  Actual='1'.");
207 		assertThrown(()->test(nil).isNotAny(nil)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='null'.  Actual='null'.");
208 	}
209 
210 	@Test void ca08_isSame() {
211 		var x1 = of("1");
212 		var x1a = of("1");
213 		var nil = no(Version.class);
214 		test(x1).isSame(x1);
215 		test(nil).isSame(nil);
216 		assertThrown(()->test(x1).isSame(x1a)).asMessage().asOneLine().isMatches("Not the same value.  Expect='1(Version@*)'.  Actual='1(Version@*)'.");
217 		assertThrown(()->test(nil).isSame(x1a)).asMessage().asOneLine().isMatches("Not the same value.  Expect='1(Version@*)'.  Actual='null(null)'.");
218 		assertThrown(()->test(x1).isSame(nil)).asMessage().asOneLine().isMatches("Not the same value.  Expect='null(null)'.  Actual='1(Version@*)'.");
219 	}
220 
221 	@Test void ca09_isSameJsonAs() {
222 		var x1 = of("1");
223 		var x1a = of("1");
224 		var x2 = of("2");
225 		var nil = no(Version.class);
226 		test(x1).isSameJsonAs(x1a);
227 		test(nil).isSameJsonAs(nil);
228 		assertThrown(()->test(x1a).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='{maintenance:null,major:1,minor:null}'.");
229 		assertThrown(()->test(nil).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='null'.");
230 		assertThrown(()->test(x1).isSameJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='{maintenance:null,major:1,minor:null}'.");
231 	}
232 
233 	@Test void ca10_isSameSortedJsonAs() {
234 		var x1 = of("1");
235 		var x1a = of("1");
236 		var x2 = of("2");
237 		var nil = no(Version.class);
238 		test(x1).isSameSortedJsonAs(x1a);
239 		test(nil).isSameSortedJsonAs(nil);
240 		assertThrown(()->test(x1a).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='{maintenance:null,major:1,minor:null}'.");
241 		assertThrown(()->test(nil).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='null'.");
242 		assertThrown(()->test(x1).isSameSortedJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='{maintenance:null,major:1,minor:null}'.");
243 	}
244 
245 	@Test void ca11_isSameSerializedAs() {
246 		var x1 = of("1");
247 		var x1a = of("1");
248 		var x2 = of("2");
249 		var nil = no(Version.class);
250 		var s = Json5Serializer.DEFAULT;
251 		test(x1).isSameSerializedAs(x1a, s);
252 		test(nil).isSameSerializedAs(nil, s);
253 		assertThrown(()->test(x1a).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='{maintenance:null,major:1,minor:null}'.");
254 		assertThrown(()->test(nil).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='{maintenance:null,major:2,minor:null}'.  Actual='null'.");
255 		assertThrown(()->test(x1).isSameSerializedAs(nil, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='{maintenance:null,major:1,minor:null}'.");
256 	}
257 
258 	@Test void ca12_isType() {
259 		var x = of("1");
260 		var nil = no(Version.class);
261 		test(x).isType(Version.class);
262 		test(x).isType(Object.class);
263 		assertThrown(()->test(x).isType(String.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.String'.  Actual='org.apache.juneau.commons.lang.Version'.");
264 		assertThrown(()->test(nil).isType(String.class)).asMessage().asOneLine().is("Value was null.");
265 		assertThrown(()->test(x).isType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
266 	}
267 
268 	@Test void ca13_isExactType() {
269 		var x = of("1");
270 		var nil = no(Version.class);
271 		test(x).isExactType(Version.class);
272 		assertThrown(()->test(x).isExactType(Object.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.Object'.  Actual='org.apache.juneau.commons.lang.Version'.");
273 		assertThrown(()->test(x).isExactType(String.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.String'.  Actual='org.apache.juneau.commons.lang.Version'.");
274 		assertThrown(()->test(nil).isExactType(String.class)).asMessage().asOneLine().is("Value was null.");
275 		assertThrown(()->test(x).isExactType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
276 	}
277 
278 	@Test void ca14_isString() {
279 		var x = of("1");
280 		var nil = no(Version.class);
281 		test(x).isString("1");
282 		test(nil).isString(null);
283 		assertThrown(()->test(x).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='1'.");
284 		assertThrown(()->test(x).isString(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='1'.");
285 		assertThrown(()->test(nil).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
286 	}
287 
288 	@Test void ca15_isJson() {
289 		var x = of("1");
290 		var nil = no(Version.class);
291 		test(x).isJson("{maintenance:null,major:1,minor:null}");
292 		test(nil).isJson("null");
293 		assertThrown(()->test(x).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='{maintenance:null,major:1,minor:null}'.");
294 		assertThrown(()->test(x).isJson(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='{maintenance:null,major:1,minor:null}'.");
295 		assertThrown(()->test(nil).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
296 	}
297 
298 	@Test void cb01_isGt() {
299 		var x1 = of("1");
300 		var x2 = of("2");
301 		var nil = no(Version.class);
302 		test(x2).isGt(x1);
303 		assertThrown(()->test(x1).isGt(x1)).asMessage().asOneLine().is("Value was not greater than expected.  Expect='1'.  Actual='1'.");
304 		assertThrown(()->test(x1).isGt(x2)).asMessage().asOneLine().is("Value was not greater than expected.  Expect='2'.  Actual='1'.");
305 		assertThrows(IllegalArgumentException.class, ()->test(x1).isGt(nil), "Argument 'value' cannot be null.");
306 		assertThrows(BasicAssertionError.class, ()->test(nil).isGt(x2), "Value was null.");
307 	}
308 
309 	@Test void cb02_isGte() {
310 		var x1 = of("1");
311 		var x2 = of("2");
312 		var nil = no(Version.class);
313 		test(x2).isGte(x1);
314 		test(x1).isGte(x1);
315 		assertThrown(()->test(x1).isGte(x2)).asMessage().asOneLine().is("Value was not greater than or equals to expected.  Expect='2'.  Actual='1'.");
316 		assertThrows(IllegalArgumentException.class, ()->test(x1).isGte(nil), "Argument 'value' cannot be null.");
317 		assertThrows(BasicAssertionError.class, ()->test(nil).isGte(x2), "Value was null.");
318 	}
319 
320 	@Test void cb03_isLt() {
321 		var x1 = of("1");
322 		var x2 = of("2");
323 		var nil = no(Version.class);
324 		test(x1).isLt(x2);
325 		assertThrown(()->test(x1).isLt(x1)).asMessage().asOneLine().is("Value was not less than expected.  Expect='1'.  Actual='1'.");
326 		assertThrown(()->test(x2).isLt(x1)).asMessage().asOneLine().is("Value was not less than expected.  Expect='1'.  Actual='2'.");
327 		assertThrows(IllegalArgumentException.class, ()->test(x2).isLt(nil), "Argument 'value' cannot be null.");
328 		assertThrows(BasicAssertionError.class, ()->test(nil).isLt(x1), "Value was null.");
329 	}
330 
331 	@Test void cb04_isLte() {
332 		var x1 = of("1");
333 		var x2 = of("2");
334 		var nil = no(Version.class);
335 		test(x1).isLte(x2);
336 		test(x1).isLte(x1);
337 		assertThrown(()->test(x2).isLte(x1)).asMessage().asOneLine().is("Value was not less than or equals to expected.  Expect='1'.  Actual='2'.");
338 		assertThrows(IllegalArgumentException.class, ()->test(x2).isLte(nil), "Argument 'value' cannot be null.");
339 		assertThrows(BasicAssertionError.class, ()->test(nil).isLte(x1), "Value was null.");
340 	}
341 
342 	@Test void cb05_isBetween() {
343 		var x1 = of("1");
344 		var x2 = of("2");
345 		var x3 = of("3");
346 		var x4 = of("4");
347 		var nil = no(Version.class);
348 		test(x1).isBetween(x1, x3);
349 		test(x2).isBetween(x1, x3);
350 		test(x3).isBetween(x1, x3);
351 		assertThrown(()->test(x4).isBetween(x1, x3)).asMessage().asOneLine().is("Value was not less than or equals to expected.  Expect='3'.  Actual='4'.");
352 		assertThrows(BasicAssertionError.class, ()->test(nil).isBetween(x1, x3), "Value was null.");
353 		assertThrows(IllegalArgumentException.class, ()->test(x1).isBetween(nil, x3), "Argument 'lower' cannot be null.");
354 		assertThrown(()->test(x1).isBetween(x1, nil)).asMessage().asOneLine().is("Argument 'upper' cannot be null.");
355 	}
356 }