001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.net.Proxy;
045: import java.net.ProxySelector;
046: import java.net.URI;
047: import java.util.prefs.PreferenceChangeEvent;
048: import java.util.prefs.PreferenceChangeListener;
049: import java.util.prefs.Preferences;
050: import org.netbeans.junit.*;
051: import junit.textui.TestRunner;
052: import org.openide.util.NbPreferences;
053:
054: /** Tests Detect OS nonProxyHosts settings.
055: *
056: * @author Jiri Rechtacek
057: * @see http://www.netbeans.org/issues/show_bug.cgi?id=77053
058: */
059: public class NonProxyHostsTest extends NbTestCase {
060: private static String SYSTEM_PROXY_HOST = "system.cache.org";
061: private static String SYSTEM_PROXY_PORT = "777";
062: private static String USER_PROXY_HOST = "my.webcache";
063: private static String USER_PROXY_PORT = "8080";
064:
065: private Preferences proxyPreferences;
066: private ProxySelector selector;
067: private static URI TO_LOCALHOST;
068: private static URI TO_LOCAL_DOMAIN_1;
069: private static URI TO_LOCAL_DOMAIN_2;
070: private static URI TO_EXTERNAL;
071: private static URI SOCKS_TO_LOCALHOST;
072: private static URI SOCKS_TO_LOCAL_DOMAIN_1;
073: private static URI SOCKS_TO_LOCAL_DOMAIN_2;
074: private static URI SOCKS_TO_EXTERNAL;
075:
076: private boolean isWaiting = false;
077:
078: public NonProxyHostsTest(String name) {
079: super (name);
080: }
081:
082: public static void main(String[] args) {
083: TestRunner.run(new NbTestSuite(NonProxyHostsTest.class));
084: }
085:
086: protected void setUp() throws Exception {
087: super .setUp();
088: System.setProperty("netbeans.system_http_proxy",
089: SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
090: System.setProperty("netbeans.system_socks_proxy",
091: SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
092: System.setProperty("netbeans.system_http_non_proxy_hosts",
093: "*.other.org");
094: System.setProperty("http.nonProxyHosts", "*.netbeans.org");
095: ProxySelector.setDefault(new NbProxySelector());
096: selector = ProxySelector.getDefault();
097: proxyPreferences = NbPreferences.root().node(
098: "/org/netbeans/core");
099: ;
100: proxyPreferences
101: .addPreferenceChangeListener(new PreferenceChangeListener() {
102: public void preferenceChange(
103: PreferenceChangeEvent arg0) {
104: isWaiting = false;
105: }
106: });
107: proxyPreferences.put("proxyHttpHost", USER_PROXY_HOST);
108: proxyPreferences.put("proxyHttpPort", USER_PROXY_PORT);
109: proxyPreferences.put("proxySocksHost", USER_PROXY_HOST);
110: proxyPreferences.put("proxySocksPort", USER_PROXY_PORT);
111: while (isWaiting)
112: ;
113: isWaiting = true;
114: TO_LOCALHOST = new URI("http://localhost");
115: TO_LOCAL_DOMAIN_1 = new URI("http://core.netbeans.org");
116: TO_LOCAL_DOMAIN_2 = new URI("http://core.other.org");
117: TO_EXTERNAL = new URI("http://worldwide.net");
118:
119: SOCKS_TO_LOCALHOST = new URI("socket://localhost:8041");
120: SOCKS_TO_LOCAL_DOMAIN_1 = new URI("socket://core.netbeans.org");
121: SOCKS_TO_LOCAL_DOMAIN_2 = new URI("socket://core.other.org");
122: SOCKS_TO_EXTERNAL = new URI("socket://worldwide.net");
123: }
124:
125: public void testDirectProxySetting() {
126: proxyPreferences.putInt("proxyType",
127: ProxySettings.DIRECT_CONNECTION);
128: while (isWaiting)
129: ;
130: assertEquals("Proxy type DIRECT_CONNECTION.",
131: ProxySettings.DIRECT_CONNECTION, ProxySettings
132: .getProxyType());
133: assertEquals("Connect " + TO_LOCALHOST + " DIRECT.",
134: "[DIRECT]", selector.select(TO_LOCALHOST).toString());
135: assertEquals("Connect " + SOCKS_TO_LOCALHOST + " DIRECT.",
136: Proxy.NO_PROXY, selector.select(SOCKS_TO_LOCALHOST)
137: .get(0));
138: assertEquals("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.",
139: "[DIRECT]", selector.select(TO_LOCAL_DOMAIN_1)
140: .toString());
141: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_1 + " DIRECT.",
142: "[DIRECT]", selector.select(SOCKS_TO_LOCAL_DOMAIN_1)
143: .toString());
144: assertEquals("Connect " + TO_LOCAL_DOMAIN_2 + " DIRECT.",
145: "[DIRECT]", selector.select(TO_LOCAL_DOMAIN_2)
146: .toString());
147: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_2 + " DIRECT.",
148: "[DIRECT]", selector.select(SOCKS_TO_LOCAL_DOMAIN_2)
149: .toString());
150: assertEquals("Connect " + TO_EXTERNAL + " DIRECT.", "[DIRECT]",
151: selector.select(TO_EXTERNAL).toString());
152: assertEquals("Connect " + SOCKS_TO_EXTERNAL + " DIRECT.",
153: "[DIRECT]", selector.select(SOCKS_TO_EXTERNAL)
154: .toString());
155: }
156:
157: public void testManualProxySettins() {
158: proxyPreferences.put(ProxySettings.NOT_PROXY_HOSTS,
159: "localhost|" + "*.netbeans.org");
160: proxyPreferences.putInt("proxyType",
161: ProxySettings.MANUAL_SET_PROXY);
162: while (isWaiting)
163: ;
164: assertEquals("Proxy type DIRECT_CONNECTION.",
165: ProxySettings.MANUAL_SET_PROXY, ProxySettings
166: .getProxyType());
167: assertEquals("Connect TO_LOCALHOST DIRECT.", Proxy.NO_PROXY,
168: selector.select(TO_LOCALHOST).get(0));
169: assertEquals("Connect " + SOCKS_TO_LOCALHOST + " DIRECT.",
170: Proxy.NO_PROXY, selector.select(SOCKS_TO_LOCALHOST)
171: .get(0));
172: assertEquals("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.",
173: Proxy.NO_PROXY, selector.select(TO_LOCAL_DOMAIN_1).get(
174: 0));
175: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_1 + " DIRECT.",
176: Proxy.NO_PROXY, selector
177: .select(SOCKS_TO_LOCAL_DOMAIN_1).get(0));
178: assertEquals("Connect " + TO_LOCAL_DOMAIN_2
179: + " via my.webcache:8080 proxy.",
180: "HTTP @ my.webcache:8080", selector.select(
181: TO_LOCAL_DOMAIN_2).get(0).toString());
182: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_2
183: + " via my.webcache:8080 proxy.",
184: "SOCKS @ my.webcache:8080", selector.select(
185: SOCKS_TO_LOCAL_DOMAIN_2).get(0).toString());
186: assertEquals("Connect TO_EXTERNAL via my.webcache:8080 proxy.",
187: "HTTP @ my.webcache:8080", selector.select(TO_EXTERNAL)
188: .get(0).toString());
189: assertEquals(
190: "Connect SOCKS_TO_EXTERNAL via my.webcache:8080 proxy.",
191: "SOCKS @ my.webcache:8080", selector.select(
192: SOCKS_TO_EXTERNAL).get(0).toString());
193: }
194:
195: public void testSystemProxySettings() {
196: proxyPreferences.putInt("proxyType",
197: ProxySettings.AUTO_DETECT_PROXY);
198: while (isWaiting)
199: ;
200: log("Value of System.getProperty (\"http.nonProxyHosts\"): "
201: + System.getProperty("http.nonProxyHosts"));
202: assertTrue("*.other.org is one of non-proxy hosts", System
203: .getProperty("http.nonProxyHosts").indexOf(
204: "*.other.org") != -1);
205: assertEquals("Proxy type DIRECT_CONNECTION.",
206: ProxySettings.AUTO_DETECT_PROXY, ProxySettings
207: .getProxyType());
208: assertEquals("Connect TO_LOCALHOST DIRECT.", Proxy.NO_PROXY,
209: selector.select(TO_LOCALHOST).get(0));
210: assertEquals("Connect " + SOCKS_TO_LOCALHOST + " DIRECT.",
211: Proxy.NO_PROXY, selector.select(SOCKS_TO_LOCALHOST)
212: .get(0));
213: assertEquals("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.",
214: Proxy.NO_PROXY, selector.select(TO_LOCAL_DOMAIN_1).get(
215: 0));
216: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_1 + " DIRECT.",
217: Proxy.NO_PROXY, selector
218: .select(SOCKS_TO_LOCAL_DOMAIN_1).get(0));
219: assertEquals("Connect " + TO_LOCAL_DOMAIN_2
220: + " DIRECT ignoring settings "
221: + System.getProperty("http.nonProxyHosts"),
222: Proxy.NO_PROXY, selector.select(TO_LOCAL_DOMAIN_2).get(
223: 0));
224: assertEquals("Connect " + SOCKS_TO_LOCAL_DOMAIN_2
225: + " DIRECT ignoring settings "
226: + System.getProperty("http.nonProxyHosts"),
227: Proxy.NO_PROXY, selector
228: .select(SOCKS_TO_LOCAL_DOMAIN_2).get(0));
229: assertEquals(
230: "Connect TO_EXTERNAL via system.cache.org:777 proxy.",
231: "HTTP @ system.cache.org:777", selector.select(
232: TO_EXTERNAL).get(0).toString());
233: assertEquals(
234: "Connect SOCKS_TO_EXTERNAL via system.cache.org:777 proxy.",
235: "SOCKS @ system.cache.org:777", selector.select(
236: SOCKS_TO_EXTERNAL).get(0).toString());
237: }
238:
239: }
|