KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core;
21
22 import java.lang.reflect.Field JavaDoc;
23 import java.net.ProxySelector JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.prefs.AbstractPreferences JavaDoc;
26 import java.util.prefs.PreferenceChangeEvent JavaDoc;
27 import java.util.prefs.PreferenceChangeListener JavaDoc;
28 import java.util.prefs.Preferences JavaDoc;
29 import junit.framework.TestResult;
30 import org.netbeans.junit.*;
31 import junit.textui.TestRunner;
32 import org.openide.util.NbPreferences;
33
34 /** Tests HTTP Proxy settings.
35  *
36  * @author Jiri Rechtacek
37  * @see http://www.netbeans.org/issues/show_bug.cgi?id=51641
38  */

39 public class HttpSettingsTest extends NbTestCase {
40     private final Object JavaDoc sync = getEventQueueSync ();
41     private static String JavaDoc SYSTEM_PROXY_HOST = "system.cache.org";
42     private static String JavaDoc SYSTEM_PROXY_PORT = "777";
43     private static String JavaDoc USER_PROXY_HOST = "my.webcache";
44     private static String JavaDoc USER_PROXY_PORT = "8080";
45
46     private Preferences JavaDoc proxyPreferences;
47     private static String JavaDoc SILLY_USER_PROXY_HOST = "http://my.webcache";
48     private static String JavaDoc SILLY_SYSTEM_PROXY_HOST = "http://system.cache.org";
49     private static String JavaDoc SILLY_SYSTEM_PROXY_PORT = "777//";
50     private static String JavaDoc MY_NON_PROXY_HOSTS = "myhost.mydomain.net";
51     
52     private static String JavaDoc NETBEANS_ORG = "*.netbeans.org";
53     private static String JavaDoc OTHER_ORG = "*.other.org";
54     
55     public HttpSettingsTest (String JavaDoc name) {
56         super (name);
57     }
58     
59     public static void main(String JavaDoc[] args) {
60         TestRunner.run (new NbTestSuite (HttpSettingsTest.class));
61     }
62     
63     public void run (final TestResult result) {
64         //just initialize Preferences before code NbTestCase
65
Preferences.userRoot ();
66         super.run (result);
67     }
68     
69     protected int timeOut () {
70         return 20 * 1000;
71     }
72     
73     protected void setUp () throws Exception JavaDoc {
74         super.setUp ();
75         System.setProperty ("http.nonProxyHosts", NETBEANS_ORG + ',' + NETBEANS_ORG);
76         ProxySelector.setDefault (new NbProxySelector ());
77         proxyPreferences = NbPreferences.root ().node ("/org/netbeans/core");
78         proxyPreferences.addPreferenceChangeListener (new PreferenceChangeListener JavaDoc() {
79             public void preferenceChange(PreferenceChangeEvent JavaDoc arg0) {
80                 synchronized (sync) {
81                     sync.notifyAll ();
82                 }
83             }
84         });
85         System.setProperty ("netbeans.system_http_proxy", SYSTEM_PROXY_HOST + ":" + SYSTEM_PROXY_PORT);
86         System.setProperty ("netbeans.system_http_non_proxy_hosts", OTHER_ORG);
87         synchronized (sync) {
88             if (! USER_PROXY_HOST.equals (proxyPreferences.get ("proxyHttpHost", ""))) {
89                 proxyPreferences.put ("proxyHttpHost", USER_PROXY_HOST);
90                 sync.wait (10000);
91             }
92         }
93         synchronized (sync) {
94             if (! USER_PROXY_PORT.equals (proxyPreferences.get ("proxyHttpPort", ""))) {
95                 proxyPreferences.put ("proxyHttpPort", USER_PROXY_PORT);
96                 sync.wait (10000);
97             }
98         }
99     }
100     
101     private void sillySetUp () throws Exception JavaDoc {
102         synchronized (sync) {
103             if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences.getInt ("proxyType", -1))) {
104                 proxyPreferences.putInt ("proxyType", ProxySettings.DIRECT_CONNECTION);
105                 sync.wait ();
106             }
107         }
108         
109         System.setProperty ("netbeans.system_http_proxy", SILLY_SYSTEM_PROXY_HOST + ":" + SILLY_SYSTEM_PROXY_PORT);
110         synchronized (sync) {
111             if (! SILLY_USER_PROXY_HOST.equals (proxyPreferences.get ("proxyHttpHost", ""))) {
112                 proxyPreferences.put ("proxyHttpHost", SILLY_USER_PROXY_HOST);
113                 sync.wait (10000);
114             }
115         }
116         proxyPreferences.put ("proxyHttpPort", USER_PROXY_PORT);
117         synchronized (sync) {
118             if (! USER_PROXY_PORT.equals (proxyPreferences.get ("proxyHttpPort", ""))) {
119                 proxyPreferences.put ("proxyHttpPort", USER_PROXY_PORT);
120                 sync.wait (10000);
121             }
122         }
123     }
124     
125     private void setUpAutoDirect () throws Exception JavaDoc {
126         synchronized (sync) {
127             if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences.getInt ("proxyType", -1))) {
128                 proxyPreferences.putInt ("proxyType", ProxySettings.DIRECT_CONNECTION);
129                 sync.wait ();
130             }
131         }
132         System.setProperty ("netbeans.system_http_proxy", "DIRECT");
133         synchronized (sync) {
134             if (! "".equals (proxyPreferences.get ("proxyHttpHost", ""))) {
135                 proxyPreferences.put ("proxyHttpHost", "");
136                 sync.wait (10000);
137             }
138         }
139         synchronized (sync) {
140             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
141                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
142                 sync.wait ();
143             }
144         }
145     }
146     
147     public void testDirectConnection () throws InterruptedException JavaDoc {
148         System.setProperty ("http.proxyHost", "");
149         System.setProperty ("http.proxyPort", "");
150         synchronized (sync) {
151             if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences.getInt ("proxyType", -1))) {
152                 proxyPreferences.putInt ("proxyType", ProxySettings.DIRECT_CONNECTION);
153                 sync.wait ();
154             }
155         }
156         assertEquals ("Proxy type DIRECT_CONNECTION.", ProxySettings.DIRECT_CONNECTION, ProxySettings.getProxyType ());
157         assertEquals ("No Proxy Host set.", null, System.getProperty ("http.proxyHost"));
158         assertEquals ("No Proxy Port set.", null, System.getProperty ("http.proxyPort"));
159     }
160     
161     public void testAutoDetectProxy () throws InterruptedException JavaDoc {
162         System.setProperty ("http.proxyHost", "");
163         System.setProperty ("http.proxyPort", "");
164         synchronized (sync) {
165             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
166                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
167                 sync.wait ();
168             }
169         }
170         assertEquals("Proxy type AUTO_DETECT_PROXY.",
171                      ProxySettings.AUTO_DETECT_PROXY,
172                      ProxySettings.getProxyType());
173         assertEquals("System Proxy Host: ", SYSTEM_PROXY_HOST,
174                      System.getProperty("http.proxyHost"));
175         assertEquals("System Proxy Port: ", SYSTEM_PROXY_PORT,
176                      System.getProperty("http.proxyPort"));
177     }
178     
179     public void testManualSetProxy () throws InterruptedException JavaDoc {
180         synchronized (sync) {
181             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
182                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
183                 sync.wait ();
184             }
185         }
186         assertEquals ("Proxy type MANUAL_SET_PROXY.", ProxySettings.MANUAL_SET_PROXY, ProxySettings.getProxyType ());
187         assertEquals ("Manual Set Proxy Host from ProxySettings: ", USER_PROXY_HOST, ProxySettings.getHttpHost ());
188         assertEquals ("Manual Set Proxy Port from ProxySettings: ", USER_PROXY_PORT, ProxySettings.getHttpPort ());
189         assertEquals ("Manual Set Proxy Host from System.getProperty(): ", USER_PROXY_HOST, System.getProperty ("http.proxyHost"));
190         assertEquals ("Manual Set Proxy Port from System.getProperty(): ", USER_PROXY_PORT, System.getProperty ("http.proxyPort"));
191     }
192     
193     public void testIfTakeUpNonProxyFromProperty () {
194         assertTrue (NETBEANS_ORG + " in one of Non-Proxy hosts.", ProxySettings.getNonProxyHosts ().indexOf (NETBEANS_ORG) != -1);
195     }
196     
197     public void testNonProxy () throws InterruptedException JavaDoc {
198         assertEquals ("The ProxySettings takes as same value as System properties in initial.", System.getProperty ("http.nonProxyHosts"), ProxySettings.getNonProxyHosts ());
199         
200         // change value in ProxySettings
201
synchronized (sync) {
202             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
203                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
204                 sync.wait ();
205             }
206         }
207         synchronized (sync) {
208             if (! MY_NON_PROXY_HOSTS.equals (proxyPreferences.get ("proxyNonProxyHosts", ""))) {
209                 proxyPreferences.put ("proxyNonProxyHosts", MY_NON_PROXY_HOSTS);
210                 sync.wait ();
211             }
212         }
213         assertEquals ("ProxySettings returns new value.", "myhost.mydomain.net", ProxySettings.getNonProxyHosts ());
214         assertEquals ("System property http.nonProxyHosts was changed as well.", ProxySettings.getNonProxyHosts (), System.getProperty ("http.nonProxyHosts"));
215         
216         // switch proxy type to DIRECT_CONNECTION
217
//System.setProperty ("http.nonProxyHosts", "");
218
synchronized (sync) {
219             if (ProxySettings.DIRECT_CONNECTION != (proxyPreferences.getInt ("proxyType", -1))) {
220                 proxyPreferences.putInt ("proxyType", ProxySettings.DIRECT_CONNECTION);
221                 sync.wait ();
222             }
223         }
224         Thread.sleep (1000); // XXX: tunning test on Windows platform, other platforms are passing
225
assertEquals ("System.getProperty() doesn't return new value if DIRECT_CONNECTION set.", null, System.getProperty ("http.nonProxyHosts"));
226         
227         // switch proxy type back to MANUAL_SET_PROXY
228
synchronized (sync) {
229             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
230                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
231                 sync.wait ();
232             }
233         }
234         assertEquals ("ProxySettings again returns new value.", "myhost.mydomain.net", ProxySettings.getNonProxyHosts ());
235         assertEquals ("System property http.nonProxyHosts was changed as well.", ProxySettings.getNonProxyHosts (), System.getProperty ("http.nonProxyHosts"));
236         
237         // switch proxy type to AUTO_DETECT_PROXY
238
synchronized (sync) {
239             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
240                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
241                 sync.wait ();
242             }
243         }
244         log ("AUTO_DETECT_PROXY: ProxySettings.SystemProxySettings.getNonProxyHosts (): " + ProxySettings.SystemProxySettings.getNonProxyHosts ());
245         log ("AUTO_DETECT_PROXY: System.getProperty (\"http.nonProxyHosts\"): " + System.getProperty ("http.nonProxyHosts"));
246         assertTrue ("ProxySettings contains OTHER_ORG if AUTO_DETECT_PROXY set.", System.getProperty ("http.nonProxyHosts").indexOf (OTHER_ORG) > 0);
247                 
248         // switch proxy type back to MANUAL_SET_PROXY
249
synchronized (sync) {
250             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
251                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
252                 sync.wait ();
253             }
254         }
255         assertEquals ("ProxySettings again returns new value.", "myhost.mydomain.net", ProxySettings.getNonProxyHosts ());
256         assertEquals ("System property http.nonProxyHosts was changed as well.", ProxySettings.getNonProxyHosts (), System.getProperty ("http.nonProxyHosts"));
257     }
258     
259     public void testAvoidDuplicateNonProxySetting () throws InterruptedException JavaDoc {
260         synchronized (sync) {
261             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
262                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
263                 sync.wait ();
264             }
265         }
266         assertTrue (NETBEANS_ORG + " is among Non-proxy hosts detect from OS.", ProxySettings.SystemProxySettings.getNonProxyHosts ().indexOf (NETBEANS_ORG) != -1);
267         assertFalse (NETBEANS_ORG + " is in Non-Proxy hosts only once.", ProxySettings.SystemProxySettings.getNonProxyHosts ().indexOf (NETBEANS_ORG) < ProxySettings.getNonProxyHosts ().lastIndexOf (NETBEANS_ORG));
268         assertEquals ("System property http.nonProxyHosts was changed as well.", ProxySettings.SystemProxySettings.getNonProxyHosts (), System.getProperty ("http.nonProxyHosts"));
269     }
270     
271     public void testReadNonProxySettingFromSystem () throws InterruptedException JavaDoc {
272         synchronized (sync) {
273             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
274                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
275                 sync.wait ();
276             }
277         }
278         assertTrue (OTHER_ORG + " is among Non-proxy hosts detect from OS.", ProxySettings.SystemProxySettings.getNonProxyHosts ().indexOf (OTHER_ORG) != -1);
279         assertEquals ("System property http.nonProxyHosts was changed as well.", ProxySettings.SystemProxySettings.getNonProxyHosts (), System.getProperty ("http.nonProxyHosts"));
280     }
281     
282     public void testSillySetManualProxy () throws Exception JavaDoc {
283         sillySetUp ();
284         synchronized (sync) {
285             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
286                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
287                 sync.wait ();
288             }
289         }
290         assertEquals ("Proxy type MANUAL_SET_PROXY.", ProxySettings.MANUAL_SET_PROXY, ProxySettings.getProxyType ());
291         assertEquals ("Manual Set Proxy Host from ProxySettings doesn't return SILLY_USER_PROXY_HOST anymore.", USER_PROXY_HOST, ProxySettings.getHttpHost ());
292         assertEquals ("Manual Set Proxy Port from ProxySettings doesn't return SILLY_USER_PROXY_PORT anymore.", USER_PROXY_PORT, ProxySettings.getHttpPort ());
293         assertEquals ("Manual Set Proxy Host from System.getProperty(): ", USER_PROXY_HOST, System.getProperty ("http.proxyHost"));
294         assertEquals ("Manual Set Proxy Port from System.getProperty(): ", USER_PROXY_PORT, System.getProperty ("http.proxyPort"));
295     }
296     
297     public void testAutoDetectSillySetProxy () throws Exception JavaDoc {
298         sillySetUp ();
299         synchronized (sync) {
300             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
301                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
302                 sync.wait ();
303             }
304         }
305         assertEquals ("Proxy type AUTO_DETECT_PROXY.", ProxySettings.AUTO_DETECT_PROXY, ProxySettings.getProxyType ());
306         assertEquals ("Auto Detected Proxy Host from ProxySettings doesn't return SILLY_SYSTEM_PROXY_HOST anymore.", SYSTEM_PROXY_HOST, ProxySettings.SystemProxySettings.getHttpHost ());
307         assertEquals ("Auto Detected Proxy Port from ProxySettings doesn't return SILLY_SYSTEM_PROXY_PORT anymore.", SYSTEM_PROXY_PORT, ProxySettings.SystemProxySettings.getHttpPort ());
308         assertEquals ("System Proxy Host: ", SYSTEM_PROXY_HOST, System.getProperty ("http.proxyHost"));
309         assertEquals ("System Proxy Port: ", SYSTEM_PROXY_PORT, System.getProperty ("http.proxyPort"));
310     }
311     
312     public void testSwitchAutoAndManualMode () throws Exception JavaDoc {
313         setUpAutoDirect ();
314         
315         // ensure auto detect mode
316
assertEquals("Proxy type AUTO_DETECT_PROXY.",
317                      ProxySettings.AUTO_DETECT_PROXY,
318                      ProxySettings.getProxyType());
319         assertEquals ("Auto Proxy Host from System.getProperty(): ", null, System.getProperty ("http.proxyHost"));
320         assertEquals ("Auto Proxy Port from System.getProperty(): ", null, System.getProperty ("http.proxyPort"));
321         
322         // switch to manual
323
proxyPreferences.put ("proxyHttpHost", "foo");
324         synchronized (sync) {
325             if (ProxySettings.MANUAL_SET_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
326                 proxyPreferences.putInt ("proxyType", ProxySettings.MANUAL_SET_PROXY);
327                 sync.wait ();
328             }
329         }
330         assertEquals ("Proxy type MANUAL_SET_PROXY.", ProxySettings.MANUAL_SET_PROXY, ProxySettings.getProxyType ());
331         assertEquals ("Auto Proxy Host from System.getProperty(): ", "foo", System.getProperty ("http.proxyHost"));
332         
333         // switch back to auto detect
334
synchronized (sync) {
335             if (ProxySettings.AUTO_DETECT_PROXY != (proxyPreferences.getInt ("proxyType", -1))) {
336                 proxyPreferences.putInt ("proxyType", ProxySettings.AUTO_DETECT_PROXY);
337                 sync.wait ();
338             }
339         }
340         assertEquals("Proxy type AUTO_DETECT_PROXY.",
341                      ProxySettings.AUTO_DETECT_PROXY,
342                      ProxySettings.getProxyType());
343         assertEquals ("Auto Proxy Host from System.getProperty(): ", null, System.getProperty ("http.proxyHost"));
344         assertEquals ("Auto Proxy Port from System.getProperty(): ", null, System.getProperty ("http.proxyPort"));
345         
346     }
347     
348     private Object JavaDoc getEventQueueSync() {
349         try {
350             Field JavaDoc f = AbstractPreferences JavaDoc.class.getDeclaredField("eventQueue");
351             f.setAccessible(true);
352             return f.get(null);
353             
354         } catch (Exception JavaDoc ex) {
355             Logger.getLogger("global").log(java.util.logging.Level.SEVERE,ex.getMessage(), ex);
356         }
357         return null;
358     }
359 }
360
Popular Tags