author | skovalev |
Thu, 03 Nov 2016 16:19:29 +0300 | |
changeset 41890 | ab67b8f4f2fe |
parent 38883 | d5de564f8089 |
child 42460 | 7133f144981a |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
24 |
/* |
36131 | 25 |
* @test |
26 |
* @bug 8087112 |
|
38883 | 27 |
* @modules java.httpclient |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
28 |
* java.logging |
38883 | 29 |
* jdk.httpserver |
36131 | 30 |
* @library /lib/testlibrary/ |
31 |
* @build jdk.testlibrary.SimpleSSLContext ProxyServer |
|
37720 | 32 |
* @build TestKit |
36131 | 33 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
34 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
|
35 |
* @run main/othervm APIErrors |
|
36 |
*/ |
|
37 |
//package javaapplication16; |
|
38 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
39 |
import com.sun.net.httpserver.HttpContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
40 |
import com.sun.net.httpserver.HttpHandler; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
41 |
import com.sun.net.httpserver.HttpServer; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
42 |
import com.sun.net.httpserver.HttpsServer; |
36131 | 43 |
import java.io.IOException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
44 |
import java.net.InetSocketAddress; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
45 |
import java.net.PasswordAuthentication; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
46 |
import java.net.ProxySelector; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
47 |
import java.net.URI; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
48 |
import java.net.http.HttpClient; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
49 |
import java.net.http.HttpRequest; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
50 |
import java.net.http.HttpResponse; |
36131 | 51 |
import java.util.LinkedList; |
52 |
import java.util.List; |
|
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
53 |
import java.util.concurrent.ExecutionException; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
54 |
import java.util.concurrent.Executors; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
55 |
import java.util.concurrent.ExecutorService; |
36131 | 56 |
import java.util.function.Supplier; |
57 |
||
58 |
/** |
|
59 |
* Does stupid things with API, to check appropriate errors/exceptions thrown |
|
60 |
*/ |
|
61 |
public class APIErrors { |
|
62 |
||
63 |
static HttpServer s1 = null; |
|
64 |
static ExecutorService executor = null; |
|
65 |
static int port; |
|
66 |
static HttpClient client; |
|
67 |
static String httproot, fileuri, fileroot; |
|
68 |
static List<HttpClient> clients = new LinkedList<>(); |
|
69 |
||
70 |
public static void main(String[] args) throws Exception { |
|
71 |
initServer(); |
|
72 |
fileroot = System.getProperty("test.src") + "/docs"; |
|
73 |
||
74 |
client = HttpClient.create().build(); |
|
75 |
||
76 |
clients.add(HttpClient.getDefault()); |
|
77 |
||
78 |
try { |
|
79 |
test1(); |
|
80 |
test2(); |
|
81 |
test3(); |
|
82 |
} finally { |
|
83 |
s1.stop(0); |
|
84 |
executor.shutdownNow(); |
|
85 |
for (HttpClient client : clients) |
|
86 |
client.executorService().shutdownNow(); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
static void checkNonNull(Supplier<?> r) { |
|
91 |
if (r.get() == null) |
|
92 |
throw new RuntimeException("Unexpected null return:"); |
|
93 |
} |
|
94 |
||
95 |
static void assertTrue(Supplier<Boolean> r) { |
|
96 |
if (r.get() == false) |
|
97 |
throw new RuntimeException("Assertion failure:"); |
|
98 |
} |
|
99 |
||
100 |
// HttpClient.Builder |
|
101 |
static void test1() throws Exception { |
|
102 |
System.out.println("Test 1"); |
|
103 |
HttpClient.Builder cb = HttpClient.create(); |
|
104 |
InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000); |
|
37720 | 105 |
TestKit.assertThrows(IllegalArgumentException.class, () -> cb.priority(-1)); |
106 |
TestKit.assertThrows(IllegalArgumentException.class, () -> cb.priority(500)); |
|
107 |
TestKit.assertNotThrows(() -> cb.priority(1)); |
|
108 |
TestKit.assertNotThrows(() -> cb.priority(255)); |
|
109 |
TestKit.assertNotThrows(() -> { |
|
110 |
clients.add(cb.build()); |
|
111 |
clients.add(cb.build()); |
|
112 |
}); |
|
36131 | 113 |
} |
114 |
||
115 |
static void test2() throws Exception { |
|
116 |
System.out.println("Test 2"); |
|
117 |
HttpClient.Builder cb = HttpClient.create(); |
|
118 |
InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5000); |
|
119 |
cb.proxy(ProxySelector.of(addr)); |
|
120 |
HttpClient c = cb.build(); |
|
121 |
clients.add(c); |
|
122 |
checkNonNull(()-> {return c.executorService();}); |
|
123 |
assertTrue(()-> {return c.followRedirects() == HttpClient.Redirect.NEVER;}); |
|
124 |
assertTrue(()-> {return !c.authenticator().isPresent();}); |
|
125 |
} |
|
126 |
||
127 |
static URI accessibleURI() { |
|
128 |
return URI.create(fileuri); |
|
129 |
} |
|
130 |
||
131 |
static HttpRequest request() { |
|
132 |
return HttpRequest.create(accessibleURI()) |
|
133 |
.GET(); |
|
134 |
} |
|
135 |
||
136 |
static void test3() throws Exception { |
|
137 |
System.out.println("Test 3"); |
|
37720 | 138 |
TestKit.assertThrows(IllegalStateException.class, ()-> { |
36131 | 139 |
try { |
140 |
HttpRequest r1 = request(); |
|
141 |
HttpResponse resp = r1.response(); |
|
142 |
HttpResponse resp1 = r1.response(); |
|
143 |
} catch (IOException |InterruptedException e) { |
|
144 |
throw new RuntimeException(e); |
|
145 |
} |
|
37720 | 146 |
}); |
36131 | 147 |
|
37720 | 148 |
TestKit.assertThrows(IllegalStateException.class, ()-> { |
36131 | 149 |
try { |
150 |
HttpRequest r1 = request(); |
|
151 |
HttpResponse resp = r1.response(); |
|
152 |
HttpResponse resp1 = r1.responseAsync().get(); |
|
153 |
} catch (IOException |InterruptedException | ExecutionException e) { |
|
154 |
throw new RuntimeException(e); |
|
155 |
} |
|
37720 | 156 |
}); |
157 |
TestKit.assertThrows(IllegalStateException.class, ()-> { |
|
36131 | 158 |
try { |
159 |
HttpRequest r1 = request(); |
|
160 |
HttpResponse resp1 = r1.responseAsync().get(); |
|
161 |
HttpResponse resp = r1.response(); |
|
162 |
} catch (IOException |InterruptedException | ExecutionException e) { |
|
163 |
throw new RuntimeException(e); |
|
164 |
} |
|
37720 | 165 |
}); |
36131 | 166 |
} |
167 |
||
168 |
static class Auth extends java.net.Authenticator { |
|
169 |
int count = 0; |
|
170 |
@Override |
|
171 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
172 |
if (count++ == 0) { |
|
173 |
return new PasswordAuthentication("user", "passwd".toCharArray()); |
|
174 |
} else { |
|
175 |
return new PasswordAuthentication("user", "goober".toCharArray()); |
|
176 |
} |
|
177 |
} |
|
178 |
int count() { |
|
179 |
return count; |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
public static void initServer() throws Exception { |
|
184 |
String root = System.getProperty ("test.src")+ "/docs"; |
|
185 |
InetSocketAddress addr = new InetSocketAddress (0); |
|
186 |
s1 = HttpServer.create (addr, 0); |
|
187 |
if (s1 instanceof HttpsServer) { |
|
188 |
throw new RuntimeException ("should not be httpsserver"); |
|
189 |
} |
|
190 |
HttpHandler h = new FileServerHandler(root); |
|
191 |
||
192 |
HttpContext c1 = s1.createContext("/files", h); |
|
193 |
||
194 |
executor = Executors.newCachedThreadPool(); |
|
195 |
s1.setExecutor (executor); |
|
196 |
s1.start(); |
|
197 |
||
198 |
port = s1.getAddress().getPort(); |
|
199 |
System.out.println("HTTP server port = " + port); |
|
200 |
httproot = "http://127.0.0.1:" + port + "/files/"; |
|
201 |
fileuri = httproot + "foo.txt"; |
|
202 |
} |
|
203 |
} |