001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.harmony.luni.tests.java.net;
017:
018: import java.net.InetSocketAddress;
019: import java.net.Proxy;
020: import java.net.SocketAddress;
021:
022: import junit.framework.TestCase;
023:
024: public class ProxyTest extends TestCase {
025:
026: private SocketAddress address = new InetSocketAddress("127.0.0.1",
027: 1234);
028:
029: /**
030: * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
031: */
032: public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal() {
033: // test HTTP type proxy
034: Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
035: assertEquals(Proxy.Type.HTTP, proxy.type());
036: assertEquals(address, proxy.address());
037:
038: // test SOCKS type proxy
039: proxy = new Proxy(Proxy.Type.SOCKS, address);
040: assertEquals(Proxy.Type.SOCKS, proxy.type());
041: assertEquals(address, proxy.address());
042:
043: // test DIRECT type proxy
044: proxy = Proxy.NO_PROXY;
045: assertEquals(Proxy.Type.DIRECT, proxy.type());
046: assertNull(proxy.address());
047: }
048:
049: /**
050: * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
051: */
052: public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_IllegalAddress() {
053: Proxy proxy = null;
054: // test HTTP type proxy
055: try {
056: proxy = new Proxy(Proxy.Type.HTTP, null);
057: fail("should throw IllegalArgumentException");
058: } catch (IllegalArgumentException e) {
059: // expected
060: }
061: // test SOCKS type proxy
062: try {
063: proxy = new Proxy(Proxy.Type.SOCKS, null);
064: fail("should throw IllegalArgumentException");
065: } catch (IllegalArgumentException e) {
066: // expected
067: }
068: // test DIRECT type proxy
069: try {
070: proxy = new Proxy(Proxy.Type.DIRECT, null);
071: fail("should throw IllegalArgumentException");
072: } catch (IllegalArgumentException e) {
073: // expected
074: }
075: // test DIRECT type proxy, any address is illegal
076: try {
077: proxy = new Proxy(Proxy.Type.DIRECT, address);
078: fail("should throw IllegalArgumentException");
079: } catch (IllegalArgumentException e) {
080: // expected
081: }
082:
083: }
084:
085: /**
086: * @tests java.net.Proxy#hashCode()
087: * @see also see test_equalsLjava_lang_Object_Equals
088: */
089: public void test_hashCode() {
090: // This method has been tested in test_equalsLjava_lang_Object_Equals.
091: }
092:
093: /**
094: * @tests java.net.Proxy#type()
095: */
096: public void test_type() {
097: // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
098: }
099:
100: /**
101: * @tests java.net.Proxy#address() This method has been tested in
102: * Constructor test case.
103: */
104: public void test_address() {
105: // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
106: }
107:
108: /**
109: * @tests java.net.Proxy#toString()
110: */
111: public void test_toString() {
112: Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
113: // include type String
114: assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
115: // include address String
116: assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
117:
118: proxy = new Proxy(Proxy.Type.SOCKS, address);
119: // include type String
120: assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
121: // include address String
122: assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
123:
124: proxy = Proxy.NO_PROXY;
125: // include type String
126: assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
127:
128: proxy = new Proxy(null, address);
129: // ensure no NPE is thrown
130: proxy.toString();
131:
132: }
133:
134: /**
135: * @tests java.net.Proxy#equals(Object)
136: */
137: public void test_equalsLjava_lang_Object_Equals() {
138: SocketAddress address1 = new InetSocketAddress("127.0.0.1",
139: 1234);
140: SocketAddress address2 = new InetSocketAddress("127.0.0.1",
141: 1234);
142: // HTTP type
143: Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
144: Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address2);
145: assertTrue(proxy1.equals(proxy2));
146: // assert hashCode
147: assertTrue(proxy1.hashCode() == proxy2.hashCode());
148:
149: // SOCKS type
150: Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1);
151: Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address2);
152: assertTrue(proxy3.equals(proxy4));
153: // assert hashCode
154: assertTrue(proxy3.hashCode() == proxy4.hashCode());
155:
156: // null type
157: Proxy proxy5 = new Proxy(null, address1);
158: Proxy proxy6 = new Proxy(null, address2);
159: assertTrue(proxy5.equals(proxy6));
160: }
161:
162: /**
163: * @tests java.net.Proxy#equals(Object)
164: */
165: public void test_equalsLjava_lang_Object_NotEquals() {
166: SocketAddress address1 = new InetSocketAddress("127.0.0.1",
167: 1234);
168: SocketAddress address2 = new InetSocketAddress("127.0.0.1",
169: 1235);
170: Proxy proxy[] = { new Proxy(Proxy.Type.HTTP, address1),
171: new Proxy(Proxy.Type.HTTP, address2),
172: new Proxy(Proxy.Type.SOCKS, address1),
173: new Proxy(Proxy.Type.SOCKS, address2), Proxy.NO_PROXY,
174: new Proxy(null, address1), new Proxy(null, address2) };
175: // All of them are not equals
176: for (int i = 0; i < proxy.length; i++) {
177: for (int j = i + 1; j < proxy.length; j++) {
178: assertFalse(proxy[i].equals(proxy[j]));
179: }
180: }
181: // Not equals to an Object type instance. Ensure no exception is thrown.
182: assertFalse(proxy[0].equals(new Object()));
183: }
184:
185: /**
186: * @tests java.net.Proxy.Type#valueOf(String)
187: */
188: public void test_Type_valueOfLjava_lang_String_Normal() {
189: assertEquals(Proxy.Type.DIRECT, Proxy.Type.valueOf("DIRECT"));
190: assertEquals(Proxy.Type.HTTP, Proxy.Type.valueOf("HTTP"));
191: assertEquals(Proxy.Type.SOCKS, Proxy.Type.valueOf("SOCKS"));
192: }
193:
194: /**
195: * @tests java.net.Proxy.Type#valueOf(String)
196: */
197: public void test_Type_valueOfLjava_lang_String_IllegalName() {
198: String[] illegalName = { "Direct", "direct", "http", "socks",
199: "illegalName", "" };
200: for (int i = 0; i < illegalName.length; i++) {
201: try {
202: Proxy.Type.valueOf(illegalName[i]);
203: fail("should throw IllegalArgumentException, illegalName:"
204: + illegalName);
205: } catch (IllegalArgumentException e) {
206: // expected
207: }
208: }
209: }
210:
211: /**
212: * @tests java.net.Proxy.Type#valueOf(String)
213: */
214: public void test_Type_valueOfLjava_lang_String_NullPointerException() {
215: // Some old RIs,which throw IllegalArgumentException.
216: // Latest RIs throw NullPointerException.
217: try {
218: Proxy.Type.valueOf(null);
219: fail("should throw an exception.");
220: } catch (NullPointerException e) {
221: // May be caused by some compilers' code
222: } catch (IllegalArgumentException e) {
223: // other compilers will throw this
224: }
225: }
226:
227: /**
228: * @tests java.net.Proxy.Type#values()
229: */
230: public void test_Type_values() {
231: Proxy.Type types[] = Proxy.Type.values();
232: assertEquals(3, types.length);
233: assertEquals(Proxy.Type.DIRECT, types[0]);
234: assertEquals(Proxy.Type.HTTP, types[1]);
235: assertEquals(Proxy.Type.SOCKS, types[2]);
236: }
237:
238: }
|