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.*;
20  import static org.apache.juneau.assertions.Assertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.common.utils.*;
25  import org.apache.juneau.json.*;
26  import org.junit.jupiter.api.*;
27  
28  @Deprecated
29  class ByteArrayAssertion_Test extends TestBase {
30  
31  	//------------------------------------------------------------------------------------------------------------------
32  	// Helpers
33  	//------------------------------------------------------------------------------------------------------------------
34  
35  	private ByteArrayAssertion test(byte[] value) {
36  		return assertBytes(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  		byte[] x = {'a'}, nil = null;
58  		test(x).asString().is("a");
59  		test(nil).asString().isNull();
60  	}
61  
62  	@Test void ba01b_asString_wSerializer() {
63  		byte[] x = {1}, nil = null;
64  		var s = Json5Serializer.DEFAULT;
65  		test(x).asString(s).is("[1]");
66  		test(nil).asString(s).is("null");
67  	}
68  
69  	@Test void ba01c_asString_wPredicate() {
70  		byte[] x1 = {1};
71  		test(x1).asString(x -> "foo").is("foo");
72  	}
73  
74  	@Test void ba02_asJson() {
75  		byte[] x = {1}, nil = null;
76  		test(x).asJson().is("[1]");
77  		test(nil).asJson().is("null");
78  	}
79  
80  	@Test void ba03_asJsonSorted() {
81  		byte[] x1 = {2,1}, nil = null;
82  		test(x1).asJsonSorted().is("[1,2]");
83  		test(nil).asJsonSorted().is("null");
84  	}
85  
86  	@Test void ba04_apply() {
87  		byte[] x1 = {1}, x2 = {2};
88  		test(x1).asTransformed(x -> x2).is(x2);
89  	}
90  
91  	@Test void bb01_item() {
92  		byte[] x = {1}, nil = null;
93  		test(x).asItem(0).is((byte)1);
94  		test(x).asItem(1).isNull();
95  		test(x).asItem(-1).isNull();
96  		test(nil).asItem(0).isNull();
97  	}
98  
99  	@Test void bb02_length() {
100 		byte[] x = {1}, nil = null;
101 		test(x).asLength().is(1);
102 		test(nil).asLength().isNull();
103 	}
104 
105 	@Test void bc01_asString_wCharset() {
106 		byte[] x = {'a','b'}, nil = null;
107 		test(x).asString(IOUtils.UTF8).is("ab");
108 		test(nil).asString(IOUtils.UTF8).isNull();
109 		assertThrown(()->test(x).asString(IOUtils.UTF8).is("xx")).asMessage().asOneLine().is("String differed at position 0.  Expect='xx'.  Actual='ab'.");
110 	}
111 
112 	@Test void bc02_asBase64() {
113 		byte[] x = {'a','b'}, nil = null;
114 		test(x).asBase64().is("YWI=");
115 		test(nil).asBase64().isNull();
116 		assertThrown(()->test(x).asBase64().is("xx")).asMessage().asOneLine().is("String differed at position 0.  Expect='xx'.  Actual='YWI='.");
117 	}
118 
119 	@Test void bc03_asHex() {
120 		byte[] x = {'a','b'}, nil = null;
121 		test(x).asHex().is("6162");
122 		test(nil).asHex().isNull();
123 		assertThrown(()->test(x).asHex().is("xx")).asMessage().asOneLine().is("String differed at position 0.  Expect='xx'.  Actual='6162'.");
124 	}
125 
126 	@Test void bc04_asSpacedHex() {
127 		byte[] x = {'a','b'}, nil = null;
128 		test(x).asSpacedHex().is("61 62");
129 		test(nil).asSpacedHex().isNull();
130 		assertThrown(()->test(x).asSpacedHex().is("xx")).asMessage().asOneLine().is("String differed at position 0.  Expect='xx'.  Actual='61 62'.");
131 	}
132 
133 	//-----------------------------------------------------------------------------------------------------------------
134 	// Test tests
135 	//-----------------------------------------------------------------------------------------------------------------
136 
137 	@Test void ca01_exists() {
138 		byte[] x = {}, nil = null;
139 		test(x).isExists().isExists();
140 		assertThrows(BasicAssertionError.class, ()->test(nil).isExists(), "Value was null.");
141 	}
142 
143 	@Test void ca02_isNull() {
144 		byte[] x = {}, nil = null;
145 		test(nil).isNull();
146 		assertThrows(BasicAssertionError.class, ()->test(x).isNull(), "Value was not null.");
147 	}
148 
149 	@Test void ca03_isNotNull() {
150 		byte[] x = {}, nil = null;
151 		test(x).isNotNull();
152 		assertThrows(BasicAssertionError.class, ()->test(nil).isNotNull(), "Value was null.");
153 	}
154 
155 	@Test void ca04a_is_T() {
156 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {3,4}, nil = null;
157 		test(x1).is(x1);
158 		test(x1).is(x1a);
159 		test(nil).is(nil);
160 		assertThrown(()->test(x1).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='[3, 4]'.  Actual='[1, 2]'.");
161 		assertThrown(()->test(x1).is(nil)).asMessage().asOneLine().is("Unexpected value.  Expect='null'.  Actual='[1, 2]'.");
162 		assertThrown(()->test(nil).is(x2)).asMessage().asOneLine().is("Unexpected value.  Expect='[3, 4]'.  Actual='null'.");
163 	}
164 
165 	@Test void ca04b_is_predicate() {
166 		byte[] x1 = {1,2};
167 		test(x1).is(x->x.length==2);
168 		assertThrown(()->test(x1).is(x->x.length==3)).asMessage().asOneLine().is("Unexpected value: '[1, 2]'.");
169 		assertThrown(()->test(x1).is(ne(x1))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='[1, 2]'.");
170 	}
171 
172 	@Test void ca05_isNot() {
173 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {2,3}, nil = null;
174 		test(x1).isNot(x2);
175 		test(x1).isNot(nil);
176 		test(nil).isNot(x1);
177 		assertThrown(()->test(x1).isNot(x1a)).asMessage().asOneLine().is("Unexpected value.  Did not expect='[1, 2]'.  Actual='[1, 2]'.");
178 		assertThrown(()->test(nil).isNot(nil)).asMessage().asOneLine().is("Unexpected value.  Did not expect='null'.  Actual='null'.");
179 	}
180 
181 	@Test void ca06_isAny() {
182 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {2,3}, nil = null;
183 		test(x1).isAny(x1a, x2);
184 		assertThrown(()->test(x1).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[[2, 3]]'.  Actual='[1, 2]'.");
185 		assertThrown(()->test(x1).isAny()).asMessage().asOneLine().is("Expected value not found.  Expect='[]'.  Actual='[1, 2]'.");
186 		assertThrown(()->test(nil).isAny(x2)).asMessage().asOneLine().is("Expected value not found.  Expect='[[2, 3]]'.  Actual='null'.");
187 	}
188 
189 	@Test void ca07_isNotAny() {
190 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {2,3}, nil = null;
191 		test(x1).isNotAny(x2);
192 		test(x1).isNotAny();
193 		test(nil).isNotAny(x2);
194 		assertThrown(()->test(x1).isNotAny(x1a)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='[1, 2]'.  Actual='[1, 2]'.");
195 		assertThrown(()->test(nil).isNotAny(nil)).asMessage().asOneLine().is("Unexpected value found.  Unexpected='null'.  Actual='null'.");
196 	}
197 
198 	@Test void ca08_isSame() {
199 		byte[] x1 = {1,2}, x1a = {1,2}, nil = null;
200 		test(x1).isSame(x1);
201 		test(nil).isSame(nil);
202 		assertThrown(()->test(x1).isSame(x1a)).asMessage().asOneLine().isMatches("Not the same value.  Expect='[1, 2](byte[]@*)'.  Actual='[1, 2](byte[]@*)'.");
203 		assertThrown(()->test(nil).isSame(x1a)).asMessage().asOneLine().isMatches("Not the same value.  Expect='[1, 2](byte[]@*)'.  Actual='null(null)'.");
204 		assertThrown(()->test(x1).isSame(nil)).asMessage().asOneLine().isMatches("Not the same value.  Expect='null(null)'.  Actual='[1, 2](byte[]@*)'.");
205 	}
206 
207 	@Test void ca09_isSameJsonAs() {
208 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {2,3}, nil = null;
209 		test(x1).isSameJsonAs(x1a);
210 		test(nil).isSameJsonAs(nil);
211 		assertThrown(()->test(x1a).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[2,3]'.  Actual='[1,2]'.");
212 		assertThrown(()->test(nil).isSameJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[2,3]'.  Actual='null'.");
213 		assertThrown(()->test(x1).isSameJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='[1,2]'.");
214 	}
215 
216 	@Test void ca10_isSameSortedJsonAs() {
217 		byte[] x1 = {1,2}, x1a = {2,1}, x2 = {1,3}, nil = null;
218 		test(x1).isSameSortedJsonAs(x1a);
219 		test(nil).isSameSortedJsonAs(nil);
220 		assertThrown(()->test(x1a).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[1,3]'.  Actual='[1,2]'.");
221 		assertThrown(()->test(nil).isSameSortedJsonAs(x2)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[1,3]'.  Actual='null'.");
222 		assertThrown(()->test(x1).isSameSortedJsonAs(nil)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='[1,2]'.");
223 	}
224 
225 	@Test void ca11_isSameSerializedAs() {
226 		byte[] x1 = {1,2}, x1a = {1,2}, x2 = {1,3}, nil = null;
227 		var s = Json5Serializer.DEFAULT;
228 		test(x1).isSameSerializedAs(x1a, s);
229 		test(nil).isSameSerializedAs(nil, s);
230 		assertThrown(()->test(x1a).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[1,3]'.  Actual='[1,2]'.");
231 		assertThrown(()->test(nil).isSameSerializedAs(x2, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='[1,3]'.  Actual='null'.");
232 		assertThrown(()->test(x1).isSameSerializedAs(nil, s)).asMessage().asOneLine().is("Unexpected comparison.  Expect='null'.  Actual='[1,2]'.");
233 	}
234 
235 	@Test void ca12_isType() {
236 		byte[] x = {1,2}, nil = null;
237 		test(x).isType(byte[].class);
238 		test(x).isType(Object.class);
239 		assertThrown(()->test(x).isType(String.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.String'.  Actual='[B'.");
240 		assertThrown(()->test(nil).isType(String.class)).asMessage().asOneLine().is("Value was null.");
241 		assertThrown(()->test(x).isType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
242 	}
243 
244 	@Test void ca13_isExactType() {
245 		byte[] x = {1,2}, nil = null;
246 		test(x).isExactType(byte[].class);
247 		assertThrown(()->test(x).isExactType(Object.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.Object'.  Actual='[B'.");
248 		assertThrown(()->test(x).isExactType(String.class)).asMessage().asOneLine().is("Unexpected type.  Expect='java.lang.String'.  Actual='[B'.");
249 		assertThrown(()->test(nil).isExactType(String.class)).asMessage().asOneLine().is("Value was null.");
250 		assertThrown(()->test(x).isExactType(null)).asMessage().asOneLine().is("Argument 'parent' cannot be null.");
251 	}
252 
253 	@Test void ca14_isString() {
254 		byte[] x = {'a','b'}, nil = null;
255 		test(x).isString("ab");
256 		test(nil).isString(null);
257 		assertThrown(()->test(x).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='ab'.");
258 		assertThrown(()->test(x).isString(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='ab'.");
259 		assertThrown(()->test(nil).isString("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
260 	}
261 
262 	@Test void ca15_isJson() {
263 		byte[] x = {1,2}, nil = null;
264 		test(x).isJson("[1,2]");
265 		test(nil).isJson("null");
266 		assertThrown(()->test(x).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='[1,2]'.");
267 		assertThrown(()->test(x).isJson(null)).asMessage().asOneLine().is("String differed at position 0.  Expect='null'.  Actual='[1,2]'.");
268 		assertThrown(()->test(nil).isJson("bad")).asMessage().asOneLine().is("String differed at position 0.  Expect='bad'.  Actual='null'.");
269 	}
270 
271 	@Test void cb01_isEmpty() {
272 		byte[] x1 = {}, x2 = {1,2}, nil = null;
273 		test(x1).isEmpty();
274 		assertThrows(BasicAssertionError.class, ()->test(x2).isEmpty(), "Array was not empty.");
275 		assertThrows(BasicAssertionError.class, ()->test(nil).isEmpty(), "Value was null.");
276 	}
277 
278 	@Test void cb02_isNotEmpty() {
279 		byte[] x1={}, x2={1,2}, nil = null;
280 		test(x2).isNotEmpty();
281 		assertThrows(BasicAssertionError.class, ()->test(x1).isNotEmpty(), "Array was empty.");
282 		assertThrows(BasicAssertionError.class, ()->test(nil).isNotEmpty(), "Value was null.");
283 	}
284 
285 	@Test void cb03_contains() {
286 		byte[] x1 = {1,2}, nil = null;
287 		test(x1).isContains((byte)1);
288 		assertThrown(()->test(x1).isContains((byte)3)).asMessage().asOneLine().is("Array did not contain expected value.  Expect='3'.  Actual='[1, 2]'.");
289 		assertThrown(()->test(x1).isContains(null)).asMessage().asOneLine().is("Array did not contain expected value.  Expect='null'.  Actual='[1, 2]'.");
290 		assertThrows(BasicAssertionError.class, ()->test(nil).isContains((byte)3), "Value was null.");
291 	}
292 
293 	@Test void cb04_doesNotContain() {
294 		byte[] x1 = {1,2}, nil = null;
295 		test(x1).isNotContains((byte)3);
296 		test(x1).isNotContains(null);
297 		assertThrown(()->test(x1).isNotContains((byte)1)).asMessage().asOneLine().is("Array contained unexpected value.  Unexpected='1'.  Actual='[1, 2]'.");
298 		assertThrows(BasicAssertionError.class, ()->test(nil).isNotContains((byte)3), "Value was null.");
299 	}
300 
301 	@Test void cb05_isSize() {
302 		byte[] x1 = {}, x2={1,2}, nil = null;
303 		test(x1).isSize(0);
304 		test(x2).isSize(2);
305 		assertThrown(()->test(x1).isSize(2)).asMessage().asOneLine().is("Array did not have the expected size.  Expect=2.  Actual=0.");
306 		assertThrown(()->test(x2).isSize(0)).asMessage().asOneLine().is("Array did not have the expected size.  Expect=0.  Actual=2.");
307 		assertThrows(BasicAssertionError.class, ()->test(nil).isSize(0), "Value was null.");
308 	}
309 }