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.Assertions.*;
20  
21  import org.apache.juneau.*;
22  import org.apache.juneau.utest.utils.*;
23  import org.junit.jupiter.api.*;
24  
25  @SuppressWarnings("serial")
26  @Deprecated
27  class Assertion_Test extends TestBase {
28  
29  	//------------------------------------------------------------------------------------------------------------------
30  	// Helpers
31  	//------------------------------------------------------------------------------------------------------------------
32  
33  	public static class A1 extends RuntimeException {
34  		public A1(String msg, Throwable cause) { super(msg, cause); }
35  	}
36  
37  	public static class A2 extends RuntimeException {
38  		public A2(String msg, Throwable cause) { super(msg, cause); }
39  	}
40  
41  	public static class A3 extends RuntimeException {
42  		public A3(String msg) { throw new A2("fromA3", null); }
43  	}
44  
45  	public static class A extends StringAssertion {
46  		public A(Object text) { super(text); }
47  		public A doError() { throw error(new A1("foo", null), "bar {0}", "baz"); }
48  	}
49  
50  	//-----------------------------------------------------------------------------------------------------------------
51  	// Basic tests
52  	//-----------------------------------------------------------------------------------------------------------------
53  
54  	@Test void a01_basicErrorHandling() {
55  		var a = new A("");
56  		a.setSilent();
57  
58  		assertThrown(a::doError)
59  			.isExactType(BasicAssertionError.class)
60  			.asMessage().is("bar baz")
61  			.asCausedBy().isExactType(A1.class)
62  			.asCausedBy().asMessage().is("foo")
63  			.asCausedBy().asCausedBy().isNull();
64  
65  		a.setThrowable(A2.class);
66  		assertThrown(a::doError)
67  			.isExactType(A2.class)
68  			.asMessage().is("bar baz")
69  			.asCausedBy().isExactType(A1.class)
70  			.asCausedBy().asMessage().is("foo")
71  			.asCausedBy().asCausedBy().isNull();
72  
73  		a.setThrowable(A3.class);
74  		assertThrown(a::doError)
75  			.isExactType(BasicRuntimeException.class)
76  			.asMessage().is("bar baz")
77  			.asCausedBy().isExactType(A1.class)
78  			.asCausedBy().asMessage().is("foo")
79  			.asCausedBy().asCausedBy().isNull();
80  	}
81  
82  	@SuppressWarnings("unused")
83  	@Test void a02_out() {
84  		var out = new CapturingPrintStream();
85  
86  		try {
87  			assertString("x").setOut(out).isNull();
88  		} catch (BasicAssertionError e) { /* Ignore */ }
89  
90  		assertString(out).isContains("Value was not null.");
91  	}
92  }