1 19 20 package org.netbeans.core; 21 22 import java.net.Proxy ; 23 import java.net.ProxySelector ; 24 import java.net.URI ; 25 import java.util.prefs.PreferenceChangeEvent ; 26 import java.util.prefs.PreferenceChangeListener ; 27 import java.util.prefs.Preferences ; 28 import org.netbeans.junit.*; 29 import junit.textui.TestRunner; 30 import org.openide.util.NbPreferences; 31 32 37 public class NonProxyHostsTest extends NbTestCase { 38 private static String SYSTEM_PROXY_HOST = "system.cache.org"; 39 private static String SYSTEM_PROXY_PORT = "777"; 40 private static String USER_PROXY_HOST = "my.webcache"; 41 private static String USER_PROXY_PORT = "8080"; 42 43 private Preferences proxyPreferences; 44 private ProxySelector selector; 45 private static URI TO_LOCALHOST; 46 private static URI TO_LOCAL_DOMAIN_1; 47 private static URI TO_LOCAL_DOMAIN_2; 48 private static URI TO_EXTERNAL; 49 50 private boolean isWaiting = false; 51 52 public NonProxyHostsTest (String name) { 53 super (name); 54 } 55 56 public static void main(String [] args) { 57 TestRunner.run (new NbTestSuite (NonProxyHostsTest.class)); 58 } 59 60 protected void setUp () throws Exception { 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 () { 69 public void preferenceChange (PreferenceChangeEvent 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 ("http://localhost"); 78 TO_LOCAL_DOMAIN_1 = new URI ("http://core.netbeans.org"); 79 TO_LOCAL_DOMAIN_2 = new URI ("http://core.other.org"); 80 TO_EXTERNAL = new URI ("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 |