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