Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/HttpURLConnection/UnmodifiableMaps.java
41149 views
1
/*
2
* Copyright (c) 2012, 2019, 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
/**
25
* @test
26
* @bug 7128648
27
* @library /test/lib
28
* @modules jdk.httpserver
29
* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map
30
*/
31
32
import java.io.IOException;
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.net.URI;
36
import java.net.HttpURLConnection;
37
import java.util.Collection;
38
import java.util.ArrayList;
39
import java.util.List;
40
import java.util.Map;
41
import com.sun.net.httpserver.HttpExchange;
42
import com.sun.net.httpserver.HttpHandler;
43
import com.sun.net.httpserver.HttpServer;
44
import com.sun.net.httpserver.Headers;
45
import static java.net.Proxy.NO_PROXY;
46
import jdk.test.lib.net.URIBuilder;
47
48
public class UnmodifiableMaps {
49
50
void test(String[] args) throws Exception {
51
HttpServer server = startHttpServer();
52
try {
53
InetSocketAddress address = server.getAddress();
54
URI uri = URIBuilder.newBuilder()
55
.scheme("http")
56
.host(address.getAddress())
57
.port(address.getPort())
58
.path("/foo")
59
.build();
60
doClient(uri);
61
} finally {
62
server.stop(0);
63
}
64
}
65
66
void doClient(URI uri) throws Exception {
67
HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection(NO_PROXY);
68
69
// Test1: getRequestProperties is unmodifiable
70
System.out.println("Check getRequestProperties");
71
checkUnmodifiable(uc.getRequestProperties());
72
uc.addRequestProperty("X", "V");
73
uc.addRequestProperty("X1", "V1");
74
checkUnmodifiable(uc.getRequestProperties());
75
76
int resp = uc.getResponseCode();
77
check(resp == 200,
78
"Unexpected response code. Expected 200, got " + resp);
79
80
// Test2: getHeaderFields is unmodifiable
81
System.out.println("Check getHeaderFields");
82
checkUnmodifiable(uc.getHeaderFields());
83
// If the implementation does caching, check again.
84
checkUnmodifiable(uc.getHeaderFields());
85
}
86
87
// HTTP Server
88
HttpServer startHttpServer() throws IOException {
89
InetAddress loopback = InetAddress.getLoopbackAddress();
90
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
91
httpServer.createContext("/foo", new SimpleHandler());
92
httpServer.start();
93
return httpServer;
94
}
95
96
class SimpleHandler implements HttpHandler {
97
@Override
98
public void handle(HttpExchange t) throws IOException {
99
Headers respHeaders = t.getResponseHeaders();
100
// ensure some response headers, over the usual ones
101
respHeaders.add("RespHdr1", "Value1");
102
respHeaders.add("RespHdr2", "Value2");
103
respHeaders.add("RespHdr3", "Value3");
104
t.sendResponseHeaders(200, -1);
105
t.close();
106
}
107
}
108
109
void checkUnmodifiable(Map<String,List<String>> map) {
110
checkUnmodifiableMap(map);
111
112
// Now check the individual values
113
Collection<List<String>> values = map.values();
114
for (List<String> value : values) {
115
checkUnmodifiableList(value);
116
}
117
}
118
119
void checkUnmodifiableMap(final Map<String,List<String>> map) {
120
expectThrow( new Runnable() {
121
public void run() { map.clear(); }});
122
expectThrow( new Runnable() {
123
public void run() { map.put("X", new ArrayList<String>()); }});
124
expectThrow( new Runnable() {
125
public void run() { map.remove("X"); }});
126
}
127
128
void checkUnmodifiableList(final List<String> list) {
129
expectThrow( new Runnable() {
130
public void run() { list.clear(); }});
131
expectThrow( new Runnable() {
132
public void run() { list.add("X"); }});
133
expectThrow( new Runnable() {
134
public void run() { list.remove("X"); }});
135
}
136
137
void expectThrow(Runnable r) {
138
try { r.run(); fail("Excepted UOE to be thrown."); Thread.dumpStack(); }
139
catch (UnsupportedOperationException e) { pass(); }
140
}
141
142
volatile int passed = 0, failed = 0;
143
void pass() {passed++;}
144
void fail() {failed++;}
145
void fail(String msg) {System.err.println(msg); fail();}
146
void unexpected(Throwable t) {failed++; t.printStackTrace();}
147
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
148
public static void main(String[] args) throws Throwable {
149
Class<?> k = new Object(){}.getClass().getEnclosingClass();
150
try {k.getMethod("instanceMain",String[].class)
151
.invoke( k.newInstance(), (Object) args);}
152
catch (Throwable e) {throw e.getCause();}}
153
public void instanceMain(String[] args) throws Throwable {
154
try {test(args);} catch (Throwable t) {unexpected(t);}
155
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
156
if (failed > 0) throw new AssertionError("Some tests failed");}
157
}
158
159