1 /*
  2  * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 6312358
 27  * @summary Verify that an NPE is thrown by invoking Locale.getInstance() with
 28  * any argument being null.
 29  * @modules java.base/java.util:open
 30  * @run junit GetInstanceCheck
 31  */
 32 
 33 import java.lang.reflect.InvocationTargetException;
 34 import java.lang.reflect.Method;
 35 import java.util.Locale;
 36 import java.util.stream.Stream;
 37 
 38 import org.junit.jupiter.api.BeforeAll;
 39 import org.junit.jupiter.params.ParameterizedTest;
 40 import org.junit.jupiter.params.provider.Arguments;
 41 import org.junit.jupiter.params.provider.MethodSource;
 42 
 43 import static org.junit.jupiter.api.Assertions.fail;
 44 
 45 public class GetInstanceCheck {
 46 
 47     static Method getInstanceMethod;
 48     static final String NAME = "getInstance";
 49 
 50     /**
 51      * Initialize the non-public Locale.getInstance() method.
 52      */
 53     @BeforeAll
 54     static void initializeMethod() {
 55         try {
 56             // Locale.getInstance is not directly accessible.
 57             getInstanceMethod = Locale.class.getDeclaredMethod(
 58                     NAME, String.class, String.class, String.class
 59             );
 60             getInstanceMethod.setAccessible(true);
 61         } catch (java.lang.NoSuchMethodException exc) {
 62             // The test should fail if we can not test the desired method
 63             fail(String.format("Tried to get the method '%s' which was not found," +
 64                     " further testing is not possible, failing test", NAME));
 65         }
 66     }
 67 
 68     /**
 69      * Exists as sanity check that Locale.getInstance() will not throw
 70      * an NPE if no arguments are null.
 71      */
 72     @ParameterizedTest
 73     @MethodSource("passingArguments")
 74     public void noNPETest(String language, String country, String variant)
 75             throws IllegalAccessException {
 76         try {
 77             getInstanceMethod.invoke(null, language, country, variant);
 78         } catch (InvocationTargetException exc) {
 79             // Determine underlying exception
 80             Throwable cause = exc.getCause();
 81             if (exc.getCause() instanceof NullPointerException) {
 82                 fail(String.format("%s should not be thrown when no args are null", cause));
 83             } else {
 84                 fail(String.format("%s unexpectedly thrown, when no exception should be thrown", cause));
 85             }
 86         }
 87     }
 88 
 89     /**
 90      * Make sure the Locale.getInstance() method throws an NPE
 91      * if any given argument is null.
 92      */
 93     @ParameterizedTest
 94     @MethodSource("failingArguments")
 95     public void throwNPETest(String language, String country, String variant)
 96             throws IllegalAccessException {
 97         try {
 98             getInstanceMethod.invoke(null, language, country, variant);
 99             fail("Should NPE with any argument set to null");
100         } catch (InvocationTargetException exc) {
101             // Determine underlying exception
102             Throwable cause = exc.getCause();
103             if (cause instanceof NullPointerException) {
104                 System.out.println("NPE successfully thrown");
105             } else {
106                 fail(cause + " is thrown, when NPE should have been thrown");
107             }
108         }
109     }
110 
111     private static Stream<Arguments> passingArguments() {
112         return Stream.of(
113                 Arguments.of("null", "GB", ""),
114                 Arguments.of("en", "null", ""),
115                 Arguments.of("en", "GB", "null")
116         );
117     }
118 
119     private static Stream<Arguments> failingArguments() {
120         return Stream.of(
121                 Arguments.of(null, "GB", ""),
122                 Arguments.of("en", null, ""),
123                 Arguments.of("en", "GB", null)
124         );
125     }
126 }