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 java.util.regex.Pattern.*;
20  import static org.apache.juneau.assertions.AssertionPredicates.*;
21  import static org.apache.juneau.assertions.AssertionPredicates.eq;
22  import static org.apache.juneau.assertions.AssertionPredicates.eqic;
23  import static org.apache.juneau.assertions.AssertionPredicates.ne;
24  import static org.apache.juneau.assertions.Assertions.*;
25  import static org.apache.juneau.commons.utils.Utils.*;
26  import static org.junit.jupiter.api.Assertions.*;
27  
28  import java.util.regex.*;
29  
30  import org.apache.juneau.*;
31  import org.junit.jupiter.api.*;
32  
33  @Deprecated
34  class AssertionPredicates_Test extends TestBase {
35  
36  	//------------------------------------------------------------------------------------------------------------------
37  	// Basic tests
38  	//------------------------------------------------------------------------------------------------------------------
39  
40  	private static StringAssertion A1 = assertString("foo").setSilent(), A2 = assertString(opte()).setSilent();
41  
42  	@Test void a01_any() {
43  		assertDoesNotThrow(()->A1.is(any()));
44  		assertDoesNotThrow(()->A2.is(any()));
45  	}
46  
47  	@Test void a02_notNull() {
48  		A1.is(notNull());
49  		assertThrows(BasicAssertionError.class, ()->A2.is(notNull()), "Value was null.");
50  	}
51  
52  	@Test void a03_isNull() {
53  		assertThrows(BasicAssertionError.class, ()->A1.is(isNull()), "Value was not null.");
54  		A2.is(isNull());
55  	}
56  
57  	@Test void a04_eq() {
58  		A1.is(eq("foo"));
59  		A1.is(eq((Object)"foo"));
60  		assertThrown(()->A1.is(eq("FOO"))).asMessage().asOneLine().is("Value did not match expected.  Expect='FOO'.  Actual='foo'.");
61  		assertThrown(()->A1.is(eq("bar"))).asMessage().asOneLine().is("Value did not match expected.  Expect='bar'.  Actual='foo'.");
62  		assertThrown(()->A1.is(eq((Object)"bar"))).asMessage().asOneLine().is("Value did not match expected.  Expect='bar'.  Actual='foo'.");
63  		A2.is(eq(null));
64  		assertThrown(()->A2.is(eq("foo"))).asMessage().asOneLine().is("Value did not match expected.  Expect='foo'.  Actual='null'.");
65  	}
66  
67  	@Test void a05_eqic() {
68  		A1.is(eqic("foo"));
69  		A1.is(eqic("FOO"));
70  		assertThrown(()->A1.is(eqic("bar"))).asMessage().asOneLine().is("Value did not match expected.  Expect='bar'.  Actual='foo'.");
71  		A2.is(eqic(null));
72  		assertThrown(()->A2.is(eqic("bar"))).asMessage().asOneLine().is("Value did not match expected.  Expect='bar'.  Actual='null'.");
73  	}
74  
75  	@Test void a06_ne() {
76  		A1.is(ne("bar"));
77  		A1.is(ne((Object)"bar"));
78  		assertThrown(()->A1.is(ne("foo"))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='foo'.");
79  		assertThrown(()->A1.is(ne((Object)"foo"))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='foo'.");
80  		A2.is(ne("bar"));
81  		assertThrown(()->A2.is(ne(null))).asMessage().asOneLine().is("Value unexpectedly matched.  Value='null'.");
82  	}
83  
84  	@Test void a07_type() {
85  		A1.is(type(String.class));
86  		A1.is(type(Object.class));
87  		assertThrown(()->A1.is(type(Integer.class))).asMessage().asOneLine().is("Value was not expected type.  Expect='java.lang.Integer'.  Actual='java.lang.String'.");
88  		assertThrown(()->A2.is(type(String.class))).asMessage().asOneLine().is("Value was not expected type.  Expect='java.lang.String'.  Actual='null'.");
89  	}
90  
91  	@Test void a08_exactType() {
92  		A1.is(exactType(String.class));
93  		assertThrown(()->A1.is(exactType(Object.class))).asMessage().asOneLine().is("Value was not expected type.  Expect='java.lang.Object'.  Actual='java.lang.String'.");
94  		assertThrown(()->A2.is(exactType(Object.class))).asMessage().asOneLine().is("Value was not expected type.  Expect='java.lang.Object'.  Actual='null'.");
95  	}
96  
97  	@Test void a03_() {
98  		A1.is(match("fo*"));
99  		assertThrown(()->A1.is(match("ba*"))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='ba*'.  Value='foo'.");
100 		assertThrown(()->A2.is(match("ba*"))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='ba*'.  Value='null'.");
101 	}
102 
103 	@Test void a10_regex() {
104 		A1.is(regex("fo.*"));
105 		assertThrown(()->A1.is(regex("ba.*"))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='ba.*'.  Value='foo'.");
106 		assertThrown(()->A2.is(regex("ba.*"))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='ba.*'.  Value='null'.");
107 	}
108 
109 	@Test void a11_regex_wFlags() {
110 		A1.is(regex("FO.*", CASE_INSENSITIVE));
111 		assertThrown(()->A1.is(regex("BA.*", CASE_INSENSITIVE))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='BA.*'.  Value='foo'.");
112 		assertThrown(()->A2.is(regex("BA.*", CASE_INSENSITIVE))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='BA.*'.  Value='null'.");
113 	}
114 
115 	@Test void a12_regex_wPattern() {
116 		var p1 = Pattern.compile("FO.*", CASE_INSENSITIVE);
117 		var p2 = Pattern.compile("BA.*", CASE_INSENSITIVE);
118 		A1.is(regex(p1));
119 		assertThrown(()->A1.is(regex(p2))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='BA.*'.  Value='foo'.");
120 		assertThrown(()->A2.is(regex(p2))).asMessage().asOneLine().is("Value did not match pattern.  Pattern='BA.*'.  Value='null'.");
121 	}
122 
123 	@Test void a13_and() {
124 		A1.is(and(notNull(),eq("foo"),null,x->x.equals("foo")));
125 		assertThrown(()->A1.is(and(isNull()))).asMessage().asOneLine().is("Predicate test #1 failed.  Value was not null.");
126 		assertThrows(BasicAssertionError.class, ()->A1.is(and(x -> x.equals("bar"))), "Predicate test #1 failed.");
127 	}
128 
129 	@Test void a14_or() {
130 		A1.is(or(null,isNull(),eq("foo"),x->x.equals("bar")));
131 		assertThrows(BasicAssertionError.class, ()->A1.is(or(isNull())), "No predicate tests passed.");
132 		assertThrows(BasicAssertionError.class, ()->A1.is(or(x -> x.equals("bar"))), "No predicate tests passed.");
133 	}
134 
135 	@Test void a15_not() {
136 		A1.is(not(ne("foo")));
137 		assertThrows(BasicAssertionError.class, ()->A2.is(not(ne("foo"))), "Predicate test unexpectedly passed.");
138 		assertThrows(BasicAssertionError.class, ()->A1.is(not(eq("foo"))), "Predicate test unexpectedly passed.");
139 		A2.is(not(x -> x != null && x.equals("bar")));
140 		A1.is(not(null));
141 		A2.is(not(null));
142 	}
143 
144 	@Test void a16_test() {
145 		A1.is(test(eq("foo")));
146 		assertThrown(()->A1.is(test(eq("bar")))).asMessage().asOneLine().is("Value did not pass test.  Value did not match expected.  Expect='bar'.  Actual='foo'.");
147 	}
148 
149 	@Test void a17_contains() {
150 		A1.is(test(contains("o")));
151 		assertThrown(()->A1.is(test(contains("x")))).asMessage().asOneLine().is("Value did not pass test.  Value did not contain expected.  Expect='x'.  Actual='foo'.");
152 	}
153 }