Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/BasicAuthenticatorExceptionCheck.java
41154 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 823015926* @library /test/lib27* @summary Ensure that correct exceptions are being thrown in28* BasicAuthenticator constructor29* @run testng BasicAuthenticatorExceptionCheck30*/313233import java.nio.charset.Charset;34import com.sun.net.httpserver.BasicAuthenticator;35import org.testng.annotations.Test;3637import static org.testng.Assert.expectThrows;38import static java.nio.charset.StandardCharsets.UTF_8;394041public class BasicAuthenticatorExceptionCheck {42static final Class<NullPointerException> NPE = NullPointerException.class;43static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;4445static BasicAuthenticator createBasicAuthenticator(String realm, Charset charset) {46return new BasicAuthenticator(realm, charset) {47public boolean checkCredentials(String username, String pw) {48return true;49}50};51}52static BasicAuthenticator createBasicAuthenticator(String realm) {53return new BasicAuthenticator(realm) {54public boolean checkCredentials(String username, String pw) {55return true;56}57};58}5960@Test61public void testAuthenticationException() {6263Throwable ex = expectThrows(NPE, () ->64createBasicAuthenticator("/test", null));65System.out.println("Valid realm and Null charset provided - " +66"NullPointerException thrown as expected: " + ex);6768ex = expectThrows(NPE, () ->69createBasicAuthenticator(null, UTF_8));70System.out.println("Null realm and valid charset provided - " +71"NullPointerException thrown as expected: " + ex);7273ex = expectThrows(IAE, () ->74createBasicAuthenticator("", UTF_8));75System.out.println("Empty string for realm and valid charset provided - " +76"IllegalArgumentException thrown as expected: " + ex);7778ex = expectThrows(NPE, () ->79createBasicAuthenticator(null));80System.out.println("Null realm provided - " +81"NullPointerException thrown as expected: " + ex);8283ex = expectThrows(IAE, () ->84createBasicAuthenticator(""));85System.out.println("Empty string for realm provided - " +86"IllegalArgumentException thrown as expected: " + ex);87}88}899091