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.common.utils.Utils.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.json.*;
26  import org.junit.jupiter.api.*;
27  
28  @Deprecated
29  class BooleanAssertion_Test extends TestBase {
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Helpers
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	private BooleanAssertion test(Boolean value) {
36  		return assertBoolean(value).setSilent();
37  	}
38  
39  	//-----------------------------------------------------------------------------------------------------------------
40  	// Basic tests
41  	//-----------------------------------------------------------------------------------------------------------------
42  
43  	@Test void a01_msg() {
44  		assertThrows(BasicAssertionError.class, ()->test(null).setMsg("A {0}", 1).isExists(), "A 1");
45  		assertThrows(RuntimeException.class, ()->test(null).setMsg("A {0}", 1).setThrowable(RuntimeException.class).isExists(), "A 1");
46  	}
47  
48  	@Test void a02_stdout() {
49  		test(null).setStdOut();
50  	}
51  
52  	//-----------------------------------------------------------------------------------------------------------------
53  	// Transform tests
54  	//-----------------------------------------------------------------------------------------------------------------
55  
56  	@Test void ba01a_asString() {
57  		var x = true;
58  		var nil = n(Boolean.class);
59  		test(x).asString().is("true");
60  		test(nil).asString().isNull();
61  	}
62  
63  	@Test void ba01b_asString_wSerializer() {
64  		var x = true;
65  		var nil = n(Boolean.class);
66  		var s = Json5Serializer.DEFAULT;
67  		test(x).asString(s).is("true");
68  		test(nil).asString(s).is("null");
69  	}
70  
71  	@Test void ba01c_asString_wPredicate() {
72  		var x1 = true;
73  		test(x1).asString(x -> "foo").is("foo");
74  	}
75  
76  	@Test void ba02_asJson() {
77  		var x = true;
78  		var nil = n(Boolean.class);
79  		test(x).asJson().is("true");
80  		test(nil).asJson().is("null");
81  	}
82  
83  	@Test void ba03_asJsonSorted() {
84  		var x1 = true;
85  		var nil = n(Boolean.class);
86  		test(x1).asJsonSorted().is("true");
87  		test(nil).asJsonSorted().is("null");
88  	}
89  
90  	@Test void ba04_apply() {
91  		var x1 = true;
92  		test(x1).asTransformed(x -> false).is(false);
93  	}
94  
95  	//-----------------------------------------------------------------------------------------------------------------
96  	// Test tests
97  	//-----------------------------------------------------------------------------------------------------------------
98  
99  	@Test void ca01_exists() {
100 		var x = true;
101 		var nil = n(Boolean.class);
102 		test(x).isExists().isExists();
103 		assertThrows(BasicAssertionError.class, ()->test(nil).isExists(), "Value was null.");
104 	}
105 
106 	@Test void ca02_isNull() {
107 		var x = true;
108 		var nil = n(Boolean.class);
109 		test(nil).isNull();
110 		assertThrows(BasicAssertionError.class, ()->test(x).isNull(), "Value was not null.");
111 	}
112 
113 	@Test void ca03_isNotNull() {
114 		var x = true;
115 		var nil = n(Boolean.class);
116 		test(x).isNotNull();
117 		assertThrows(BasicAssertionError.class, ()->test(nil).isNotNull(), "Value was null.");
118 	}
119 
120 	@Test void ca04a_is_T() {
121 		var x1 = true;
122 		var x1a = true;
123 		var x2 = false;
124 		var nil = n(Boolean.class);
125 		test(x1).is(x1);
126 		test(x1).is(x1a);
127 		test(nil).is(nil);
128 		assertThrown(()->test(x1).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='false'.  Actual='true'.");
129 		assertThrown(()->test(x1).is(nil)).asMessage().asOneLine().is("Unexpected value.  Expect='null'.  Actual='true'.");
130 		assertThrown(()->test(nil).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='false'.  Actual='null'.");
131 	}
132 
133 	@Test void ca04b_is_predicate() {
134 		var x1 = true;
135 		test(x1).is(x->x);
136 		assertThrown(()->test(x1).is(x->!x)).asMessage().asOneLine().is("Unexpected value: 'true'.");
137 		assertThrown(()->test(x1).is(ne(x1))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='true'.");
138 	}
139 
140 	@Test void ca05_isNot() {
141 		var x1 = true;
142 		var x1a = true;
143 		var x2 = false;
144 		var nil = n(Boolean.class);
145 		test(x1).isNot(x2);
146 		test(x1).isNot(nil);
147 		test(nil).isNot(x1);
148 		assertThrown(()->test(x1).isNot(x1a)).asMessage().asOneLine().is("Unexpected value.  Did not expect='true'.  Actual='true'.");
149 		assertThrown(()->test(nil).isNot(nil)).asMessage().asOneLine().is("Unexpected value.  Did not expect='null'.  Actual='null'.");
150 	}
151 
152 	@Test void ca06_isAny() {
153 		var x1 = true;
154 		var x1a = true;
155 		var x2 = false;
156 		var nil = n(Boolean.class);
157 		test(x1).isAny(x1a, x2);
158 		assertThrown(()->test(x1).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[false]'.  Actual='true'.");
159 		assertThrown(()->test(x1).isAny()).asMessage().asOneLine().is("Expected value not found.  Expect='[]'.  Actual='true'.");
160 		assertThrown(()->test(nil).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[false]'.  Actual='null'.");
161 	}
162 
163 	@Test void ca07_isNotAny() {
164 		var x1 = true;
165 		var x1a = true;
166 		var x2 = false;
167 		var nil = n(Boolean.class);
168 		test(x1).isNotAny(x2);
169 		test(x1).isNotAny();
170 		test(nil).isNotAny(x2);
171 		assertThrown(()->test(x1).isNotAny(x1a)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='true'.  Actual='true'.");
172 		assertThrown(()->test(nil).isNotAny(nil)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='null'.  Actual='null'.");
173 	}
174 
175 	@Test void ca08_isSame() {
176 		// Note that even the following returns the same object sometimes.
177 		var x1 = Boolean.valueOf(new String("true"));
178 		var x1a = Boolean.valueOf(new String("true"));
179 		var nil = n(Boolean.class);
180 		test(x1).isSame(x1);
181 		test(nil).isSame(nil);
182 		assertThrown(()->test(nil).isSame(x1a)).asMessage().asOneLine().isMatches("Not the same value.  Expect='true(Boolean@*)'.  Actual='null(null)'.");
183 		assertThrown(()->test(x1).isSame(nil)).asMessage().asOneLine().isMatches("Not the same value.  Expect='null(null)'.  Actual='true(Boolean@*)'.");
184 	}
185 
186 	@Test void ca09_isSameJsonAs() {
187 		var x1 = true;
188 		var x1a = true;
189 		var x2 = false;
190 		var nil = n(Boolean.class);
191 		test(x1).isSameJsonAs(x1a);
192 		test(nil).isSameJsonAs(nil);
193 		assertThrown(()->test(x1a).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='true'.");
194 		assertThrown(()->test(nil).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='null'.");
195 		assertThrown(()->test(x1).isSameJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='true'.");
196 	}
197 
198 	@Test void ca10_isSameSortedJsonAs() {
199 		var x1 = true;
200 		var x1a = true;
201 		var x2 = false;
202 		var nil = n(Boolean.class);
203 		test(x1).isSameSortedJsonAs(x1a);
204 		test(nil).isSameSortedJsonAs(nil);
205 		assertThrown(()->test(x1a).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='true'.");
206 		assertThrown(()->test(nil).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='null'.");
207 		assertThrown(()->test(x1).isSameSortedJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='true'.");
208 	}
209 
210 	@Test void ca11_isSameSerializedAs() {
211 		var x1 = true;
212 		var x1a = true;
213 		var x2 = false;
214 		var nil = n(Boolean.class);
215 		var s = Json5Serializer.DEFAULT;
216 		test(x1).isSameSerializedAs(x1a, s);
217 		test(nil).isSameSerializedAs(nil, s);
218 		assertThrown(()->test(x1a).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='true'.");
219 		assertThrown(()->test(nil).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='false'.  Actual='null'.");
220 		assertThrown(()->test(x1).isSameSerializedAs(nil, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='true'.");
221 	}
222 
223 	@Test void ca12_isType() {
224 		var x = true;
225 		var nil = n(Boolean.class);
226 		test(x).isType(Boolean.class);
227 		test(x).isType(Object.class);
228 		assertThrown(()->test(x).isType(String.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.String'.  Actual='java.lang.Boolean'.");
229 		assertThrown(()->test(nil).isType(String.class)).asMessage().asOneLine().is("Value was null.");
230 		assertThrown(()->test(x).isType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
231 	}
232 
233 	@Test void ca13_isExactType() {
234 		var x = true;
235 		var nil = n(Boolean.class);
236 		test(x).isExactType(Boolean.class);
237 		assertThrown(()->test(x).isExactType(Object.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.Object'.  Actual='java.lang.Boolean'.");
238 		assertThrown(()->test(nil).isExactType(String.class)).asMessage().asOneLine().is("Value was null.");
239 		assertThrown(()->test(x).isExactType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
240 	}
241 
242 	@Test void ca14_isString() {
243 		var x = true;
244 		var nil = n(Boolean.class);
245 		test(x).isString("true");
246 		test(nil).isString(null);
247 		assertThrown(()->test(x).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='true'.");
248 		assertThrown(()->test(x).isString(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='true'.");
249 		assertThrown(()->test(nil).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
250 	}
251 
252 	@Test void ca15_isJson() {
253 		var x = true;
254 		var nil = n(Boolean.class);
255 		test(x).isJson("true");
256 		test(nil).isJson("null");
257 		assertThrown(()->test(x).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='true'.");
258 		assertThrown(()->test(x).isJson(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='true'.");
259 		assertThrown(()->test(nil).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
260 	}
261 
262 	@Test void cc01_isTrue() {
263 		var nil = n(Boolean.class);
264 		test(true).isTrue();
265 		assertThrows(BasicAssertionError.class, ()->test(false).isTrue(), "Value was false.");
266 		assertThrows(BasicAssertionError.class, ()->test(nil).isTrue(), "Value was null.");
267 	}
268 
269 	@Test void cc02_isFalse() {
270 		var nil = n(Boolean.class);
271 		test(false).isFalse();
272 		assertThrows(BasicAssertionError.class, ()->test(true).isFalse(), "Value was true.");
273 		assertThrows(BasicAssertionError.class, ()->test(nil).isFalse(), "Value was null.");
274 	}
275 }