Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/HttpURLConnection/UnmodifiableMaps.java
38821 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 712864826* @modules jdk.httpserver27* @summary HttpURLConnection.getHeaderFields should return an unmodifiable Map28*/2930import java.io.IOException;31import java.net.InetSocketAddress;32import java.net.URI;33import java.net.HttpURLConnection;34import java.util.Collection;35import java.util.ArrayList;36import java.util.List;37import java.util.Map;38import com.sun.net.httpserver.HttpExchange;39import com.sun.net.httpserver.HttpHandler;40import com.sun.net.httpserver.HttpServer;41import com.sun.net.httpserver.Headers;42import static java.net.Proxy.NO_PROXY;4344public class UnmodifiableMaps {4546void test(String[] args) throws Exception {47HttpServer server = startHttpServer();48try {49InetSocketAddress address = server.getAddress();50URI uri = new URI("http://localhost:" + address.getPort() + "/foo");51doClient(uri);52} finally {53server.stop(0);54}55}5657void doClient(URI uri) throws Exception {58HttpURLConnection uc = (HttpURLConnection) uri.toURL().openConnection(NO_PROXY);5960// Test1: getRequestProperties is unmodifiable61System.out.println("Check getRequestProperties");62checkUnmodifiable(uc.getRequestProperties());63uc.addRequestProperty("X", "V");64uc.addRequestProperty("X1", "V1");65checkUnmodifiable(uc.getRequestProperties());6667int resp = uc.getResponseCode();68check(resp == 200,69"Unexpected response code. Expected 200, got " + resp);7071// Test2: getHeaderFields is unmodifiable72System.out.println("Check getHeaderFields");73checkUnmodifiable(uc.getHeaderFields());74// If the implementation does caching, check again.75checkUnmodifiable(uc.getHeaderFields());76}7778// HTTP Server79HttpServer startHttpServer() throws IOException {80HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);81httpServer.createContext("/foo", new SimpleHandler());82httpServer.start();83return httpServer;84}8586class SimpleHandler implements HttpHandler {87@Override88public void handle(HttpExchange t) throws IOException {89Headers respHeaders = t.getResponseHeaders();90// ensure some response headers, over the usual ones91respHeaders.add("RespHdr1", "Value1");92respHeaders.add("RespHdr2", "Value2");93respHeaders.add("RespHdr3", "Value3");94t.sendResponseHeaders(200, -1);95t.close();96}97}9899void checkUnmodifiable(Map<String,List<String>> map) {100checkUnmodifiableMap(map);101102// Now check the individual values103Collection<List<String>> values = map.values();104for (List<String> value : values) {105checkUnmodifiableList(value);106}107}108109void checkUnmodifiableMap(final Map<String,List<String>> map) {110expectThrow( new Runnable() {111public void run() { map.clear(); }});112expectThrow( new Runnable() {113public void run() { map.put("X", new ArrayList<String>()); }});114expectThrow( new Runnable() {115public void run() { map.remove("X"); }});116}117118void checkUnmodifiableList(final List<String> list) {119expectThrow( new Runnable() {120public void run() { list.clear(); }});121expectThrow( new Runnable() {122public void run() { list.add("X"); }});123expectThrow( new Runnable() {124public void run() { list.remove("X"); }});125}126127void expectThrow(Runnable r) {128try { r.run(); fail("Excepted UOE to be thrown."); Thread.dumpStack(); }129catch (UnsupportedOperationException e) { pass(); }130}131132volatile int passed = 0, failed = 0;133void pass() {passed++;}134void fail() {failed++;}135void fail(String msg) {System.err.println(msg); fail();}136void unexpected(Throwable t) {failed++; t.printStackTrace();}137void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}138public static void main(String[] args) throws Throwable {139Class<?> k = new Object(){}.getClass().getEnclosingClass();140try {k.getMethod("instanceMain",String[].class)141.invoke( k.newInstance(), (Object) args);}142catch (Throwable e) {throw e.getCause();}}143public void instanceMain(String[] args) throws Throwable {144try {test(args);} catch (Throwable t) {unexpected(t);}145System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);146if (failed > 0) throw new AssertionError("Some tests failed");}147}148149150151