AuthenticatorTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 17package tests.api.java.net; 18 19import java.net.Authenticator; 20import java.net.InetAddress; 21import java.net.MalformedURLException; 22import java.net.URL; 23import java.net.UnknownHostException; 24import java.net.Authenticator.RequestorType; 25 26import junit.framework.TestCase; 27 28public class AuthenticatorTest extends TestCase { 29 30 /** 31 * @tests java.net.Authenticator.RequestorType#valueOf(String) 32 */ 33 public void test_RequestorType_valueOfLjava_lang_String() throws Exception { 34 assertEquals(RequestorType.PROXY, Authenticator.RequestorType 35 .valueOf("PROXY")); 36 assertEquals(RequestorType.SERVER, Authenticator.RequestorType 37 .valueOf("SERVER")); 38 try { 39 RequestorType rt = Authenticator.RequestorType.valueOf("BADNAME"); 40 fail("Must throw IllegalArgumentException"); 41 } catch (IllegalArgumentException e) { 42 // correct 43 } 44 // Some old RIs throw IllegalArgumentException 45 // Latest RIs throw NullPointerException. 46 try { 47 Authenticator.RequestorType.valueOf(null); 48 fail("Must throw an exception"); 49 } catch (NullPointerException e) { 50 // May be caused by some compilers' code 51 } catch (IllegalArgumentException e) { 52 // other compilers will throw this 53 } 54 } 55 56 /** 57 * @tests java.net.Authenticator.RequestorType#values() 58 */ 59 public void test_RequestorType_values() throws Exception { 60 RequestorType[] rt = RequestorType.values(); 61 assertEquals(RequestorType.PROXY, rt[0]); 62 assertEquals(RequestorType.SERVER, rt[1]); 63 } 64 65 /** 66 * @tests java.net.Authenticator#requestPasswordAuthentication(java.net.InetAddress, int, String, String, String) 67 */ 68 public void test_requestPasswordAuthentication_InetAddress_int_String_String_String() throws Exception { 69 // Regression test for Harmony-2413 70 MockAuthenticator mock = new MockAuthenticator(); 71 InetAddress addr = InetAddress.getLocalHost(); 72 Authenticator.setDefault(mock); 73 Authenticator.requestPasswordAuthentication(addr, -1, "http", "promt", "HTTP"); 74 assertEquals(mock.getRequestorType(), RequestorType.SERVER); 75 } 76 77 /** 78 * @tests java.net.Authenticator#requestPasswordAuthentication(String, java.net.InetAddress, int, String, String, String) 79 */ 80 public void test_requestPasswordAuthentication_String_InetAddress_int_String_String_String() throws Exception { 81 // Regression test for Harmony-2413 82 MockAuthenticator mock = new MockAuthenticator(); 83 InetAddress addr = InetAddress.getLocalHost(); 84 Authenticator.setDefault(mock); 85 Authenticator.requestPasswordAuthentication("test_host", addr, -1, "http", "promt", "HTTP"); 86 assertEquals(mock.getRequestorType(), RequestorType.SERVER); 87 } 88 89 /** 90 * 91 * @tests java.net.Authenticator# 92 * requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType() 93 */ 94 public void test_requestPasswordAuthentication_String_InetAddress_int_String_String_String_URL_Authenticator_RequestorType() 95 throws UnknownHostException, MalformedURLException { 96 MockAuthenticator mock = new MockAuthenticator(); 97 URL url = new URL("http://127.0.0.1"); 98 Authenticator.requestPasswordAuthentication("localhost", InetAddress 99 .getByName("127.0.0.1"), 80, "HTTP", "", "", url, 100 RequestorType.PROXY); 101 assertNull(mock.getRequestingURL()); 102 assertNull(mock.getRequestorType()); 103 } 104 105 /** 106 * 107 * @tests java.net.Authenticator#getRequestingURL() 108 */ 109 public void test_getRequestingURL() throws Exception { 110 MockAuthenticator mock = new MockAuthenticator(); 111 assertNull(mock.getRequestingURL()); 112 } 113 114 /** 115 * 116 * @tests java.net.Authenticator#getRequestorType() 117 */ 118 public void test_getRequestorType() throws Exception { 119 MockAuthenticator mock = new MockAuthenticator(); 120 assertNull(mock.getRequestorType()); 121 } 122 123 /* 124 * Mock Authernticator for test 125 */ 126 class MockAuthenticator extends java.net.Authenticator { 127 public MockAuthenticator() { 128 super(); 129 } 130 131 public URL getRequestingURL() { 132 return super.getRequestingURL(); 133 } 134 135 public Authenticator.RequestorType getRequestorType() { 136 return super.getRequestorType(); 137 } 138 } 139} 140