Path: blob/master/test/jdk/java/net/URLStreamHandler/TestDefaultBehavior.java
41149 views
/*1* Copyright (c) 2019, 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 822497326* @summary Basic test for the default behavior of openConnection(URL,Proxy)27* @run testng TestDefaultBehavior28*/2930import java.io.IOException;31import java.net.InetSocketAddress;32import java.net.Proxy;33import java.net.URI;34import java.net.URL;35import java.net.URLConnection;36import java.net.URLStreamHandler;37import org.testng.annotations.Test;38import static java.net.Proxy.*;39import static org.testng.Assert.expectThrows;4041public class TestDefaultBehavior {4243static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;44static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;4546static final InetSocketAddress ADDR = InetSocketAddress.createUnresolved("proxy.com", 80);47static final URI uri = URI.create("http://example.com:80/");4849@Test50public void testDefaultBehavior() {51CustomURLStreamHandler handler = new CustomURLStreamHandler();5253expectThrows(IAE, () -> handler.openConnection(null, null));54expectThrows(IAE, () -> handler.openConnection(null, NO_PROXY));55expectThrows(IAE, () -> handler.openConnection(null, new Proxy(Type.SOCKS, ADDR)));56expectThrows(IAE, () -> handler.openConnection(null, new Proxy(Type.HTTP, ADDR)));57expectThrows(IAE, () -> handler.openConnection(uri.toURL(), null));5859expectThrows(UOE, () -> handler.openConnection(uri.toURL(), NO_PROXY));60expectThrows(UOE, () -> handler.openConnection(uri.toURL(), new Proxy(Type.SOCKS, ADDR)));61expectThrows(UOE, () -> handler.openConnection(uri.toURL(), new Proxy(Type.HTTP, ADDR)));62}6364// A URLStreamHandler that delegates the overloaded openConnection that65// takes a proxy, to the default java.net.URLStreamHandler implementation.66static class CustomURLStreamHandler extends URLStreamHandler {6768@Override69public URLConnection openConnection(URL url, Proxy proxy) throws IOException {70return super.openConnection(url, proxy);71}7273@Override74protected URLConnection openConnection(URL u) throws IOException {75return null;76}77}78}798081