KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NonProxyHostsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.net.Proxy JavaDoc;
23 import java.net.ProxySelector JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.prefs.PreferenceChangeEvent JavaDoc;
26 import java.util.prefs.PreferenceChangeListener JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28 import org.netbeans.junit.*;
29 import junit.textui.TestRunner;
30 import org.openide.util.NbPreferences;
31
32 /** Tests Detect OS nonProxyHosts settings.
33  *
34  * @author Jiri Rechtacek
35  * @see http://www.netbeans.org/issues/show_bug.cgi?id=77053
36  */

37 public class NonProxyHostsTest extends NbTestCase {
38     private static String JavaDoc SYSTEM_PROXY_HOST = "system.cache.org";
39     private static String JavaDoc SYSTEM_PROXY_PORT = "777";
40     private static String JavaDoc USER_PROXY_HOST = "my.webcache";
41     private static String JavaDoc USER_PROXY_PORT = "8080";
42
43     private Preferences JavaDoc proxyPreferences;
44     private ProxySelector JavaDoc selector;
45     private static URI JavaDoc TO_LOCALHOST;
46     private static URI JavaDoc TO_LOCAL_DOMAIN_1;
47     private static URI JavaDoc TO_LOCAL_DOMAIN_2;
48     private static URI JavaDoc TO_EXTERNAL;
49
50     private boolean isWaiting = false;
51     
52     public NonProxyHostsTest (String JavaDoc name) {
53         super (name);
54     }
55     
56     public static void main(String JavaDoc[] args) {
57         TestRunner.run (new NbTestSuite (NonProxyHostsTest.class));
58     }
59     
60     protected void setUp () throws Exception JavaDoc {
61         super.setUp ();
62         System.setProperty ("netbeans.system_http_proxy", SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
63         System.setProperty ("netbeans.system_http_non_proxy_hosts", "*.other.org");
64         System.setProperty ("http.nonProxyHosts", "*.netbeans.org");
65         ProxySelector.setDefault (new NbProxySelector ());
66         selector = ProxySelector.getDefault ();
67         proxyPreferences = NbPreferences.root ().node ("/org/netbeans/core");;
68         proxyPreferences.addPreferenceChangeListener (new PreferenceChangeListener JavaDoc () {
69             public void preferenceChange (PreferenceChangeEvent JavaDoc arg0) {
70                 isWaiting = false;
71             }
72         });
73         proxyPreferences.put ("proxyHttpHost", USER_PROXY_HOST);
74         proxyPreferences.put ("proxyHttpPort", USER_PROXY_PORT);
75         while (isWaiting);
76         isWaiting = true;
77         TO_LOCALHOST = new URI JavaDoc ("http://localhost");
78         TO_LOCAL_DOMAIN_1 = new URI JavaDoc ("http://core.netbeans.org");
79         TO_LOCAL_DOMAIN_2 = new URI JavaDoc ("http://core.other.org");
80         TO_EXTERNAL = new URI JavaDoc ("http://worldwide.net");
81     }
82     
83     public void testDirectProxySetting () {
84         proxyPreferences.putInt ("proxyType", ProxySettings.DIRECT_CONNECTION);
85         while (isWaiting);
86         assertEquals ("Proxy type DIRECT_CONNECTION.", ProxySettings.DIRECT_CONNECTION, ProxySettings.getProxyType ());
87         assertEquals ("Connect " + TO_LOCALHOST + " DIRECT.", "[DIRECT]", selector.select (TO_LOCALHOST).toString ());
88         assertEquals ("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.", "[DIRECT]", selector.select (TO_LOCAL_DOMAIN_1).toString ());
89         assertEquals ("Connect " + TO_LOCAL_DOMAIN_2 + " DIRECT.", "[DIRECT]", selector.select (TO_LOCAL_DOMAIN_2).toString ());
90         assertEquals ("Connect " + TO_EXTERNAL + " DIRECT.", "[DIRECT]", selector.select (TO_EXTERNAL).toString ());
91     }
92     
93     public void testManualProxySettins () {
94         proxyPreferences.put (ProxySettings.NOT_PROXY_HOSTS, "localhost|" + "*.netbeans.org");
95         proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
96         while (isWaiting);
97         assertEquals ("Proxy type DIRECT_CONNECTION.", ProxySettings.MANUAL_SET_PROXY, ProxySettings.getProxyType ());
98         assertEquals ("Connect TO_LOCALHOST DIRECT.", Proxy.NO_PROXY, selector.select (TO_LOCALHOST).get(0));
99         assertEquals ("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.", Proxy.NO_PROXY, selector.select (TO_LOCAL_DOMAIN_1).get (0));
100         assertEquals ("Connect " + TO_LOCAL_DOMAIN_2 + " via my.webcache:8080 proxy.", "HTTP @ my.webcache:8080", selector.select (TO_LOCAL_DOMAIN_2).get (0).toString ());
101         assertEquals ("Connect TO_EXTERNAL via my.webcache:8080 proxy.", "HTTP @ my.webcache:8080", selector.select (TO_EXTERNAL).get (0).toString ());
102     }
103     
104     public void testSystemProxySettings () {
105         proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
106         while (isWaiting);
107         log ("Value of System.getProperty (\"http.nonProxyHosts\"): " + System.getProperty ("http.nonProxyHosts"));
108         assertTrue ("*.other.org is one of non-proxy hosts", System.getProperty ("http.nonProxyHosts").indexOf ("*.other.org") != -1);
109         assertEquals ("Proxy type DIRECT_CONNECTION.", ProxySettings.AUTO_DETECT_PROXY, ProxySettings.getProxyType ());
110         assertEquals ("Connect TO_LOCALHOST DIRECT.", Proxy.NO_PROXY, selector.select (TO_LOCALHOST).get(0));
111         assertEquals ("Connect " + TO_LOCAL_DOMAIN_1 + " DIRECT.", Proxy.NO_PROXY, selector.select (TO_LOCAL_DOMAIN_1).get (0));
112         assertEquals ("Connect " + TO_LOCAL_DOMAIN_2 + " DIRECT ignoring settings " + System.getProperty ("http.nonProxyHosts"), Proxy.NO_PROXY, selector.select (TO_LOCAL_DOMAIN_2).get (0));
113         assertEquals ("Connect TO_EXTERNAL via system.cache.org:777 proxy.", "HTTP @ system.cache.org:777", selector.select (TO_EXTERNAL).get (0).toString ());
114     }
115
116 }
117
Popular Tags