1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.harmony.tests.java.net; 17 18 import java.net.InetSocketAddress; 19 import java.net.Proxy; 20 import java.net.SocketAddress; 21 22 import junit.framework.TestCase; 23 24 public class ProxyTest extends TestCase { 25 26 private SocketAddress address = new InetSocketAddress("127.0.0.1", 1234); 27 28 /** 29 * java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress) 30 */ 31 public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal() { 32 // test HTTP type proxy 33 Proxy proxy = new Proxy(Proxy.Type.HTTP, address); 34 assertEquals(Proxy.Type.HTTP, proxy.type()); 35 assertEquals(address, proxy.address()); 36 37 // test SOCKS type proxy 38 proxy = new Proxy(Proxy.Type.SOCKS, address); 39 assertEquals(Proxy.Type.SOCKS, proxy.type()); 40 assertEquals(address, proxy.address()); 41 42 // test DIRECT type proxy 43 proxy = Proxy.NO_PROXY; 44 assertEquals(Proxy.Type.DIRECT, proxy.type()); 45 assertNull(proxy.address()); 46 } 47 48 /** 49 * java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress) 50 */ 51 public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_IllegalAddress() { 52 Proxy proxy = null; 53 // test HTTP type proxy 54 try { 55 proxy = new Proxy(Proxy.Type.HTTP, null); 56 fail("should throw IllegalArgumentException"); 57 } catch (IllegalArgumentException e) { 58 // expected 59 } 60 // test SOCKS type proxy 61 try { 62 proxy = new Proxy(Proxy.Type.SOCKS, null); 63 fail("should throw IllegalArgumentException"); 64 } catch (IllegalArgumentException e) { 65 // expected 66 } 67 // test DIRECT type proxy 68 try { 69 proxy = new Proxy(Proxy.Type.DIRECT, null); 70 fail("should throw IllegalArgumentException"); 71 } catch (IllegalArgumentException e) { 72 // expected 73 } 74 // test DIRECT type proxy, any address is illegal 75 try { 76 proxy = new Proxy(Proxy.Type.DIRECT, address); 77 fail("should throw IllegalArgumentException"); 78 } catch (IllegalArgumentException e) { 79 // expected 80 } 81 82 } 83 84 /** 85 * java.net.Proxy#hashCode() 86 * @see also see test_equalsLjava_lang_Object_Equals 87 */ 88 public void test_hashCode() { 89 // This method has been tested in test_equalsLjava_lang_Object_Equals. 90 } 91 92 /** 93 * java.net.Proxy#type() 94 */ 95 public void test_type() { 96 // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal. 97 } 98 99 /** 100 * java.net.Proxy#address() This method has been tested in 101 * Constructor test case. 102 */ 103 public void test_address() { 104 // This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal. 105 } 106 107 /** 108 * java.net.Proxy#toString() 109 */ 110 public void test_toString() { 111 Proxy proxy = new Proxy(Proxy.Type.HTTP, address); 112 // include type String 113 assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1); 114 // include address String 115 assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1); 116 117 proxy = new Proxy(Proxy.Type.SOCKS, address); 118 // include type String 119 assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1); 120 // include address String 121 assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1); 122 123 proxy = Proxy.NO_PROXY; 124 // include type String 125 assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1); 126 127 proxy = new Proxy(null, address); 128 // ensure no NPE is thrown 129 proxy.toString(); 130 131 // Regression test for Java 6 spec change 132 proxy = new Proxy(Proxy.Type.HTTP, address); 133 assertTrue(proxy.toString().contains("@")); 134 proxy = new Proxy(Proxy.Type.SOCKS, address); 135 assertTrue(proxy.toString().contains(address.toString())); 136 } 137 138 /** 139 * java.net.Proxy#equals(Object) 140 */ 141 public void test_equalsLjava_lang_Object_Equals() { 142 SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234); 143 SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1234); 144 // HTTP type 145 Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1); 146 Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address2); 147 assertTrue(proxy1.equals(proxy2)); 148 // assert hashCode 149 assertTrue(proxy1.hashCode() == proxy2.hashCode()); 150 151 // SOCKS type 152 Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1); 153 Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address2); 154 assertTrue(proxy3.equals(proxy4)); 155 // assert hashCode 156 assertTrue(proxy3.hashCode() == proxy4.hashCode()); 157 158 // null type 159 Proxy proxy5 = new Proxy(null, address1); 160 Proxy proxy6 = new Proxy(null, address2); 161 assertTrue(proxy5.equals(proxy6)); 162 } 163 164 /** 165 * java.net.Proxy#equals(Object) 166 */ 167 public void test_equalsLjava_lang_Object_NotEquals() { 168 SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234); 169 SocketAddress address2 = new InetSocketAddress("127.0.0.1", 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 * 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 * 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 * 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 * 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 } 239