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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.connector;
038:
039: import java.net.InetSocketAddress;
040: import java.net.Proxy;
041: import java.net.URI;
042: import java.net.URISyntaxException;
043: import java.util.Arrays;
044: import java.util.HashSet;
045: import java.util.List;
046: import java.util.Set;
047: import org.MyTestCase;
048: import org.netbeans.installer.downloader.connector.MyProxy;
049: import org.netbeans.installer.downloader.connector.MyProxySelector;
050: import org.netbeans.installer.downloader.connector.MyProxyType;
051:
052: /**
053: *
054: * @author Danila_Dugurov
055: */
056: public class ProxySelectorTest extends MyTestCase {
057:
058: URI httpURI;
059: URI ftpURI;
060: URI svnURI;
061:
062: public void setUp() throws Exception {
063: super .setUp();
064: httpURI = new URI("http://www.fumm.off/");
065: ftpURI = new URI("ftp://www.fumm.off/");
066: svnURI = new URI("svn://www.fumm.off/");
067: }
068:
069: public void testSimpleProxySelect() throws URISyntaxException {
070:
071: MyProxySelector selector = new MyProxySelector();
072:
073: final MyProxy proxy1 = new MyProxy(new Proxy(Proxy.Type.HTTP,
074: new InetSocketAddress("localhost", 8080)));
075: final MyProxy proxy2 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
076: new InetSocketAddress("localhost", 8080)));
077: final MyProxy proxy3 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
078: new InetSocketAddress("localhost", 8081)),
079: MyProxyType.FTP);
080: selector.add(proxy2);
081: selector.add(proxy1);
082: selector.add(proxy3);
083: assertEquals(1, selector.select(httpURI).size());
084: assertEquals(1, selector.select(svnURI).size());
085: assertEquals(1, selector.select(ftpURI).size());
086: assertEquals(proxy1.getProxy(), selector.select(httpURI).get(0));
087: assertEquals(proxy3.getProxy(), selector.select(ftpURI).get(0));
088: assertEquals(proxy2.getProxy(), selector.select(svnURI).get(0));
089: }
090:
091: public void testAddAndSelect() {
092: MyProxySelector selector = new MyProxySelector();
093: assertNull(selector.getForType(MyProxyType.HTTP));
094: assertNull(selector.getForType(MyProxyType.SOCKS));
095: assertNull(selector.getForType(MyProxyType.FTP));
096: assertEquals(Proxy.NO_PROXY, selector.select(httpURI).get(0));
097: assertEquals(Proxy.NO_PROXY, selector.select(ftpURI).get(0));
098: assertEquals(Proxy.NO_PROXY, selector.select(svnURI).get(0));
099: final MyProxy http = new MyProxy(new Proxy(Proxy.Type.HTTP,
100: new InetSocketAddress("localhost", 8080)),
101: MyProxyType.HTTP);
102: final MyProxy ftp = new MyProxy(new Proxy(Proxy.Type.SOCKS,
103: new InetSocketAddress("hehe.kill.yourself", 8080)),
104: MyProxyType.FTP);
105: final MyProxy socks = new MyProxy(new Proxy(Proxy.Type.SOCKS,
106: new InetSocketAddress("localhost", 8080)),
107: MyProxyType.SOCKS);
108: selector.add(http);
109: selector.add(ftp);
110: selector.add(socks);
111: assertNotNull(selector.getForType(MyProxyType.HTTP));
112: assertNotNull(selector.getForType(MyProxyType.SOCKS));
113: assertNotNull(selector.getForType(MyProxyType.FTP));
114: assertEquals(http.getProxy(), selector.select(httpURI).get(0));
115: assertEquals(ftp.getProxy(), selector.select(ftpURI).get(0));
116: assertEquals(socks.getProxy(), selector.select(svnURI).get(0));
117: }
118:
119: public void testRemoveReplace() {
120: MyProxySelector selector = new MyProxySelector();
121: final MyProxy http = new MyProxy(new Proxy(Proxy.Type.HTTP,
122: new InetSocketAddress("localhost", 8080)));
123: final MyProxy ftp = new MyProxy(new Proxy(Proxy.Type.SOCKS,
124: new InetSocketAddress("hehe.kill.yourself", 8080)),
125: MyProxyType.FTP);
126: final MyProxy socks = new MyProxy(new Proxy(Proxy.Type.SOCKS,
127: new InetSocketAddress("localhost", 8080)));
128: final MyProxy socks2 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
129: new InetSocketAddress("localhost", 8081)));
130: final MyProxy noProxy = new MyProxy(Proxy.NO_PROXY);
131: selector.add(http);
132: selector.add(socks);
133: selector.add(noProxy);
134: assertEquals(socks.getProxy(), selector.select(svnURI).get(0));
135: selector.add(socks2);
136: assertEquals(socks2.getProxy(), selector.select(svnURI).get(0));
137: selector.remove(MyProxyType.FTP);
138: assertNotNull(selector.getForType(MyProxyType.HTTP));
139: assertNotNull(selector.getForType(MyProxyType.SOCKS));
140: assertNotNull(selector.getForType(MyProxyType.DIRECT));
141: assertNull(selector.getForType(MyProxyType.FTP));
142: selector.remove(MyProxyType.HTTP);
143: selector.remove(MyProxyType.SOCKS);
144: assertNull(selector.getForType(MyProxyType.HTTP));
145: assertNull(selector.getForType(MyProxyType.SOCKS));
146: assertNotNull(selector.getForType(MyProxyType.DIRECT));
147: assertNull(selector.getForType(MyProxyType.FTP));
148: }
149:
150: public void testByPassAddGet() {
151: MyProxySelector selector = new MyProxySelector();
152: assertTrue(selector.getByPass().length == 0);
153: final Set<String> expected = new HashSet<String>();
154: expected.add("sun.com");
155: selector.addByPassHost("sun.com");
156: assertTrue(selector.getByPass().length == 1);
157: assertEquals("sun.com", selector.getByPass()[0]);
158: expected.add("www.my.ru");
159: expected.add("w3c.go.go");
160: expected.add("12.34.65.2");
161: selector.addByPassHost("www.my.ru");
162: selector.addByPassHost("w3c.go.go");
163: selector.addByPassHost("12.34.65.2");
164: final Set<String> list = new HashSet<String>();
165: for (String str : selector.getByPass()) {
166: list.add(str);
167: }
168: assertEquals(expected, list);
169: }
170:
171: public void testClearAndAddNewByPass() {
172: MyProxySelector selector = new MyProxySelector();
173: selector.addByPassHost("sun.com");
174: assertTrue(selector.getByPass().length > 0);
175: selector.clearByPassList();
176: assertTrue(selector.getByPass().length == 0);
177: selector.addByPassHost("mysun.com");
178: assertEquals("mysun.com", selector.getByPass()[0]);
179: }
180:
181: public void testSelectWithByPass() {
182: MyProxySelector selector = new MyProxySelector();
183: final MyProxy http = new MyProxy(new Proxy(Proxy.Type.HTTP,
184: new InetSocketAddress("localhost", 8080)),
185: MyProxyType.HTTP);
186: selector.add(http);
187: assertEquals(http.getProxy(), selector.select(httpURI).get(0));
188: selector.addByPassHost(httpURI.getHost());
189: assertEquals(Proxy.NO_PROXY, selector.select(httpURI).get(0));
190: }
191: }
|