Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLStreamHandler/TestDefaultBehavior.java
41149 views
1
/*
2
* Copyright (c) 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 8224973
27
* @summary Basic test for the default behavior of openConnection(URL,Proxy)
28
* @run testng TestDefaultBehavior
29
*/
30
31
import java.io.IOException;
32
import java.net.InetSocketAddress;
33
import java.net.Proxy;
34
import java.net.URI;
35
import java.net.URL;
36
import java.net.URLConnection;
37
import java.net.URLStreamHandler;
38
import org.testng.annotations.Test;
39
import static java.net.Proxy.*;
40
import static org.testng.Assert.expectThrows;
41
42
public class TestDefaultBehavior {
43
44
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
45
static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
46
47
static final InetSocketAddress ADDR = InetSocketAddress.createUnresolved("proxy.com", 80);
48
static final URI uri = URI.create("http://example.com:80/");
49
50
@Test
51
public void testDefaultBehavior() {
52
CustomURLStreamHandler handler = new CustomURLStreamHandler();
53
54
expectThrows(IAE, () -> handler.openConnection(null, null));
55
expectThrows(IAE, () -> handler.openConnection(null, NO_PROXY));
56
expectThrows(IAE, () -> handler.openConnection(null, new Proxy(Type.SOCKS, ADDR)));
57
expectThrows(IAE, () -> handler.openConnection(null, new Proxy(Type.HTTP, ADDR)));
58
expectThrows(IAE, () -> handler.openConnection(uri.toURL(), null));
59
60
expectThrows(UOE, () -> handler.openConnection(uri.toURL(), NO_PROXY));
61
expectThrows(UOE, () -> handler.openConnection(uri.toURL(), new Proxy(Type.SOCKS, ADDR)));
62
expectThrows(UOE, () -> handler.openConnection(uri.toURL(), new Proxy(Type.HTTP, ADDR)));
63
}
64
65
// A URLStreamHandler that delegates the overloaded openConnection that
66
// takes a proxy, to the default java.net.URLStreamHandler implementation.
67
static class CustomURLStreamHandler extends URLStreamHandler {
68
69
@Override
70
public URLConnection openConnection(URL url, Proxy proxy) throws IOException {
71
return super.openConnection(url, proxy);
72
}
73
74
@Override
75
protected URLConnection openConnection(URL u) throws IOException {
76
return null;
77
}
78
}
79
}
80
81