Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.core; |
46 |
|
47 |
import java.io.IOException; |
48 |
import java.net.InetAddress; |
49 |
import java.net.InetSocketAddress; |
50 |
import java.net.Proxy; |
51 |
import java.net.ProxySelector; |
52 |
import java.net.SocketAddress; |
53 |
import java.net.URI; |
54 |
import java.util.Collections; |
55 |
import java.util.List; |
56 |
import java.util.Locale; |
57 |
import java.util.Locale; |
58 |
import org.netbeans.junit.NbTestCase; |
59 |
import org.openide.util.NetworkSettings; |
60 |
import org.openide.util.lookup.ServiceProvider; |
61 |
|
62 |
/** Check whether we can proxy to localhost. |
63 |
*/ |
64 |
public class CanProxyToLocalhostTest extends NbTestCase { |
65 |
private static String USER_PROXY_HOST = "my.webcache"; |
66 |
private static int USER_PROXY_PORT = 8080; |
67 |
|
68 |
private ProxySelector selector; |
69 |
private static URI TO_LOCALHOST; |
70 |
private static URI TO_NB; |
71 |
private MyPS myPS; |
72 |
|
73 |
public CanProxyToLocalhostTest (String name) { |
74 |
super (name); |
75 |
} |
76 |
|
77 |
@Override |
78 |
protected void setUp () throws Exception { |
79 |
myPS = new MyPS(); |
80 |
selector = NbProxySelector.create(myPS); |
81 |
TO_LOCALHOST = new URI ("http://localhost"); |
82 |
TO_NB = new URI ("http://netbeans.org"); |
83 |
} |
84 |
|
85 |
public void testNoProxyForLocalhost() { |
86 |
Locale.setDefault(Locale.US); |
87 |
assertEquals ("Connect TO_LOCALHOST DIRECT.", "DIRECT", selector.select (TO_LOCALHOST).get(0).toString()); |
88 |
} |
89 |
public void testProxyForLocalhost() { |
90 |
Locale.setDefault(new Locale("te", "ST")); |
91 |
assertEquals ("Connect TO_LOCALHOST provided by MyPS", "HTTP @ my.webcache:8080", selector.select (TO_LOCALHOST).get(0).toString()); |
92 |
assertEquals("One call to my ps", 1, myPS.called); |
93 |
} |
94 |
public void testAlwaysProxyForNonLocalhost() { |
95 |
Locale.setDefault(Locale.US); |
96 |
assertEquals ("Connect TO_LOCALHOST DIRECT.", "HTTP @ my.webcache:8080", selector.select (TO_NB).get(0).toString()); |
97 |
assertEquals("One call to my ps", 1, myPS.called); |
98 |
} |
99 |
|
100 |
private static class MyPS extends ProxySelector { |
101 |
int called; |
102 |
|
103 |
@Override |
104 |
public List<Proxy> select(URI uri) { |
105 |
called++; |
106 |
return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(USER_PROXY_HOST, USER_PROXY_PORT))); |
107 |
} |
108 |
|
109 |
@Override |
110 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
111 |
} |
112 |
} |
113 |
} |