< prev index next >
1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import java.io.IOException;
25 import java.util.IllegalFormatException;
26 import java.util.Set;
27
28 import static org.testng.Assert.assertEquals;
29 import static org.testng.Assert.assertNotNull;
30 import static org.testng.Assert.assertNull;
31 import static org.testng.Assert.assertTrue;
32
33 /*
34 * @test
35 * @compile TestKit.java
36 * @run testng TestKitTest
37 */
38 public final class TestKitTest {
39
40 public static void main(String[] args) {
41 testAssertNotThrows();
42 testAssertThrows();
43 }
44
45 private static void testAssertNotThrows() {
46 Integer integer = TestKit.assertNotThrows(
47 () -> TestKit.assertNotThrows(() -> 1)
48 );
49 assertEquals(integer, Integer.valueOf(1));
50
51 RuntimeException re = TestKit.assertThrows(
52 RuntimeException.class,
53 () -> TestKit.assertNotThrows(() -> { throw new IOException(); })
54 );
55 assertEquals(re.getMessage(),
56 "Expected to run normally, but threw "
57 + "java.io.IOException");
58
59 TestKit.assertNotThrows(
60 () -> TestKit.assertNotThrows(() -> { })
61 );
62
63 re = TestKit.assertThrows(
64 RuntimeException.class,
65 () -> TestKit.assertNotThrows((TestKit.ThrowingProcedure) () -> { throw new IOException(); })
66 );
67 assertEquals(re.getMessage(),
68 "Expected to run normally, but threw "
69 + "java.io.IOException");
70 }
71
72 private static void testAssertThrows() {
73 NullPointerException npe = TestKit.assertThrows(
74 NullPointerException.class,
75 () -> TestKit.assertThrows(null, null)
76 );
77 assertNotNull(npe);
78 assertTrue(Set.of("clazz", "code").contains(npe.getMessage()), npe.getMessage());
79
80 npe = TestKit.assertThrows(
81 NullPointerException.class,
82 () -> TestKit.assertThrows(IOException.class, null)
83 );
84 assertNotNull(npe);
85 assertEquals(npe.getMessage(), "code");
86
87 npe = TestKit.assertThrows(
88 NullPointerException.class,
89 () -> TestKit.assertThrows(null, () -> { })
90 );
91 assertEquals(npe.getMessage(), "clazz");
92
93 npe = TestKit.assertThrows(
94 NullPointerException.class,
95 () -> { throw new NullPointerException(); }
96 );
97 assertNotNull(npe);
98 assertNull(npe.getMessage());
99 assertEquals(npe.getClass(), NullPointerException.class);
100
101 RuntimeException re = TestKit.assertThrows(
102 RuntimeException.class,
103 () -> TestKit.assertThrows(NullPointerException.class, () -> { })
104 );
105 assertEquals(re.getClass(), RuntimeException.class);
106 assertEquals(re.getMessage(),
107 "Expected to catch an exception of type "
108 + "java.lang.NullPointerException, but caught nothing");
109
110 re = TestKit.assertThrows(
111 RuntimeException.class,
112 () -> { throw new NullPointerException(); }
113 );
114 assertNotNull(re);
115 assertNull(re.getMessage());
116 assertEquals(re.getClass(), NullPointerException.class);
117
118 re = TestKit.assertThrows(
119 RuntimeException.class,
120 () -> TestKit.assertThrows(
121 IllegalFormatException.class,
122 () -> { throw new IndexOutOfBoundsException(); }
123 ));
124 assertNotNull(re);
125 assertEquals(re.getClass(), RuntimeException.class);
126 assertEquals(re.getMessage(),
127 "Expected to catch an exception of type java.util.IllegalFormatException"
128 + ", but caught java.lang.IndexOutOfBoundsException");
129 }
130 }
< prev index next >