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