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