author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 48083 | b1c1b4ef4be2 |
child 49765 | ee6f7a61f3a5 |
permissions | -rw-r--r-- |
37720 | 1 |
/* |
48083 | 2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
37720 | 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 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
24 |
import org.testng.annotations.Test; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
25 |
|
37720 | 26 |
import java.io.IOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
27 |
import java.util.ArrayList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
28 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
29 |
import java.util.Collections; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
30 |
import java.util.HashMap; |
37720 | 31 |
import java.util.IllegalFormatException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
32 |
import java.util.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
33 |
import java.util.Map; |
37720 | 34 |
import java.util.Set; |
35 |
||
36 |
import static org.testng.Assert.assertEquals; |
|
37 |
import static org.testng.Assert.assertNotNull; |
|
38 |
import static org.testng.Assert.assertNull; |
|
39 |
import static org.testng.Assert.assertTrue; |
|
40 |
||
41 |
/* |
|
42 |
* @test |
|
43 |
* @compile TestKit.java |
|
44 |
* @run testng TestKitTest |
|
45 |
*/ |
|
46 |
public final class TestKitTest { |
|
47 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
48 |
@Test |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
49 |
public void testAssertNotThrows() { |
37720 | 50 |
Integer integer = TestKit.assertNotThrows( |
51 |
() -> TestKit.assertNotThrows(() -> 1) |
|
52 |
); |
|
53 |
assertEquals(integer, Integer.valueOf(1)); |
|
54 |
||
55 |
RuntimeException re = TestKit.assertThrows( |
|
56 |
RuntimeException.class, |
|
57 |
() -> TestKit.assertNotThrows(() -> { throw new IOException(); }) |
|
58 |
); |
|
59 |
assertEquals(re.getMessage(), |
|
60 |
"Expected to run normally, but threw " |
|
61 |
+ "java.io.IOException"); |
|
62 |
||
63 |
TestKit.assertNotThrows( |
|
64 |
() -> TestKit.assertNotThrows(() -> { }) |
|
65 |
); |
|
66 |
||
67 |
re = TestKit.assertThrows( |
|
68 |
RuntimeException.class, |
|
69 |
() -> TestKit.assertNotThrows((TestKit.ThrowingProcedure) () -> { throw new IOException(); }) |
|
70 |
); |
|
71 |
assertEquals(re.getMessage(), |
|
72 |
"Expected to run normally, but threw " |
|
73 |
+ "java.io.IOException"); |
|
74 |
} |
|
75 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
76 |
@Test |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
77 |
public void testAssertThrows() { |
37720 | 78 |
NullPointerException npe = TestKit.assertThrows( |
79 |
NullPointerException.class, |
|
80 |
() -> TestKit.assertThrows(null, null) |
|
81 |
); |
|
82 |
assertNotNull(npe); |
|
83 |
assertTrue(Set.of("clazz", "code").contains(npe.getMessage()), npe.getMessage()); |
|
84 |
||
85 |
npe = TestKit.assertThrows( |
|
86 |
NullPointerException.class, |
|
87 |
() -> TestKit.assertThrows(IOException.class, null) |
|
88 |
); |
|
89 |
assertNotNull(npe); |
|
90 |
assertEquals(npe.getMessage(), "code"); |
|
91 |
||
92 |
npe = TestKit.assertThrows( |
|
93 |
NullPointerException.class, |
|
94 |
() -> TestKit.assertThrows(null, () -> { }) |
|
95 |
); |
|
96 |
assertEquals(npe.getMessage(), "clazz"); |
|
97 |
||
98 |
npe = TestKit.assertThrows( |
|
99 |
NullPointerException.class, |
|
100 |
() -> { throw new NullPointerException(); } |
|
101 |
); |
|
102 |
assertNotNull(npe); |
|
103 |
assertNull(npe.getMessage()); |
|
104 |
assertEquals(npe.getClass(), NullPointerException.class); |
|
105 |
||
106 |
RuntimeException re = TestKit.assertThrows( |
|
107 |
RuntimeException.class, |
|
108 |
() -> TestKit.assertThrows(NullPointerException.class, () -> { }) |
|
109 |
); |
|
110 |
assertEquals(re.getClass(), RuntimeException.class); |
|
111 |
assertEquals(re.getMessage(), |
|
112 |
"Expected to catch an exception of type " |
|
113 |
+ "java.lang.NullPointerException, but caught nothing"); |
|
114 |
||
115 |
re = TestKit.assertThrows( |
|
116 |
RuntimeException.class, |
|
117 |
() -> { throw new NullPointerException(); } |
|
118 |
); |
|
119 |
assertNotNull(re); |
|
120 |
assertNull(re.getMessage()); |
|
121 |
assertEquals(re.getClass(), NullPointerException.class); |
|
122 |
||
123 |
re = TestKit.assertThrows( |
|
124 |
RuntimeException.class, |
|
125 |
() -> TestKit.assertThrows( |
|
126 |
IllegalFormatException.class, |
|
127 |
() -> { throw new IndexOutOfBoundsException(); } |
|
128 |
)); |
|
129 |
assertNotNull(re); |
|
130 |
assertEquals(re.getClass(), RuntimeException.class); |
|
131 |
assertEquals(re.getMessage(), |
|
132 |
"Expected to catch an exception of type java.util.IllegalFormatException" |
|
133 |
+ ", but caught java.lang.IndexOutOfBoundsException"); |
|
134 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
135 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
136 |
@Test |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
137 |
public void testAssertUnmodifiable() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
138 |
TestKit.assertUnmodifiableList( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
139 |
Collections.unmodifiableList( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
140 |
new ArrayList<>(Arrays.asList(1, 2, 3)))); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
141 |
TestKit.assertThrows(RuntimeException.class, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
142 |
() -> TestKit.assertUnmodifiableList(new ArrayList<>())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
143 |
TestKit.assertThrows(RuntimeException.class, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
144 |
() -> TestKit.assertUnmodifiableList(new LinkedList<>())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
145 |
TestKit.assertThrows(RuntimeException.class, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
146 |
() -> TestKit.assertUnmodifiableList( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
147 |
new ArrayList<>(Arrays.asList(1, 2, 3)))); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
148 |
TestKit.assertUnmodifiableMap(Collections.unmodifiableMap(Map.of())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
149 |
TestKit.assertThrows(RuntimeException.class, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
150 |
() -> TestKit.assertUnmodifiableMap(new HashMap<>())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37720
diff
changeset
|
151 |
} |
37720 | 152 |
} |