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 org.MyTestCase;
042: import org.netbeans.installer.downloader.connector.MyProxy;
043: import org.netbeans.installer.downloader.connector.MyProxyType;
044: import org.netbeans.installer.utils.exceptions.ParseException;
045: import org.netbeans.installer.utils.xml.DomUtil;
046: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
047: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050:
051: /**
052: *
053: * @author Danila_Dugurov
054: */
055: public class ProxyTest extends MyTestCase {
056:
057: public void testProxyCreation() {
058: final Proxy real = new Proxy(Proxy.Type.HTTP,
059: new InetSocketAddress("127.0.0.1", 4321));
060: MyProxy proxy = new MyProxy(real);
061: assertEquals(real, proxy.getProxy());
062:
063: proxy = new MyProxy();
064: assertEquals(Proxy.NO_PROXY, proxy.getProxy());
065: }
066:
067: public void testProxySerializing() {
068: final MyProxy proxy = new MyProxy(new Proxy(Proxy.Type.HTTP,
069: new InetSocketAddress("127.0.0.1", 4321)));
070: final MyProxy desirealized = new MyProxy();
071: try {
072: Document doc = DomUtil.parseXmlFile("<proxy-test/>");
073: DomUtil.addChild(doc.getDocumentElement(), proxy);
074:
075: DomVisitor visitor = new RecursiveDomVisitor() {
076: public void visit(Element element) {
077: if ("proxy".equals(element.getNodeName())) {
078: desirealized.readXML(element);
079: } else
080: super .visit(element);
081: }
082: };
083: visitor.visit(doc);
084: } catch (ParseException ex) {
085: }
086: assertEquals(proxy, desirealized);
087: }
088:
089: public void testProxyEquals() {
090: MyProxy proxy1 = new MyProxy();
091: MyProxy proxy2 = new MyProxy();
092: assertTrue(proxy1.equals(proxy2));
093: assertEquals(proxy1.hashCode(), proxy2.hashCode());
094: assertFalse(proxy1.equals(null));
095:
096: proxy1 = new MyProxy();
097: proxy2 = new MyProxy(Proxy.NO_PROXY);
098: assertTrue(proxy1.equals(proxy2));
099:
100: proxy1 = new MyProxy(new Proxy(Proxy.Type.HTTP,
101: new InetSocketAddress("127.0.0.1", 4321)));
102: proxy2 = new MyProxy(new Proxy(Proxy.Type.HTTP,
103: new InetSocketAddress("127.0.0.1", 4321)));
104: assertTrue(proxy1.equals(proxy2));
105: assertEquals(proxy1.hashCode(), proxy2.hashCode());
106:
107: proxy1 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
108: new InetSocketAddress("127.0.0.1", 4321)));
109: proxy2 = new MyProxy(new Proxy(Proxy.Type.HTTP,
110: new InetSocketAddress("127.0.0.1", 4321)));
111: assertFalse(proxy1.equals(proxy2));
112: assertNotSame(proxy1.hashCode(), proxy2.hashCode());
113:
114: proxy1 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
115: new InetSocketAddress("127.0.0.1", 4321)));
116: proxy2 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
117: new InetSocketAddress("127.0.0.1", 4322)));
118: assertFalse(proxy1.equals(proxy2));
119: assertNotSame(proxy1.hashCode(), proxy2.hashCode());
120:
121: //comments: long duration due to host resolving in constructor InetSocketAddress!
122: proxy1 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
123: new InetSocketAddress("125.0.0.1", 4321)));
124: proxy2 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
125: new InetSocketAddress("127.0.0.1", 4321)));
126: assertFalse(proxy1.equals(proxy2));
127: assertNotSame(proxy1.hashCode(), proxy2.hashCode());
128: proxy1 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
129: new InetSocketAddress("www.fake.ru", 4321)));
130: proxy2 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
131: new InetSocketAddress("127.0.0.1", 4321)));
132: assertFalse(proxy1.equals(proxy2));
133: assertNotSame(proxy1.hashCode(), proxy2.hashCode());
134: }
135:
136: public void testIncompatible() {
137: try {
138: MyProxy proxy1 = new MyProxy(new Proxy(Proxy.Type.HTTP,
139: new InetSocketAddress("127.0.0.1", 4321)),
140: MyProxyType.SOCKS);
141: fail();
142: } catch (IllegalArgumentException ex) {
143: }
144: try {
145: MyProxy proxy1 = new MyProxy(new Proxy(Proxy.Type.SOCKS,
146: new InetSocketAddress("127.0.0.1", 4321)),
147: MyProxyType.HTTP);
148: fail();
149: } catch (IllegalArgumentException ex) {
150: }
151: try {
152: MyProxy proxy1 = new MyProxy(new Proxy(Proxy.Type.HTTP,
153: new InetSocketAddress("127.0.0.1", 4321)),
154: MyProxyType.DIRECT);
155: fail();
156: } catch (IllegalArgumentException ex) {
157: }
158: try {
159: MyProxy proxy1 = new MyProxy(new Proxy(Proxy.Type.HTTP,
160: new InetSocketAddress("127.0.0.1", 4321)),
161: MyProxyType.FTP);
162: fail();
163: } catch (IllegalArgumentException ex) {
164: }
165: }
166: }
|