Path: blob/master/test/jdk/java/util/Locale/bug6312358.java
41149 views
/*1* Copyright (c) 2007, 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*/22/*23* @test24* @bug 631235825* @summary Verify that an NPE is thrown by issueing Locale.getInstance() with26* any argument being null.27* @modules java.base/java.util:open28*/2930import java.lang.reflect.InvocationTargetException;31import java.lang.reflect.Method;32import java.util.Locale;3334public class bug6312358 {353637public static void main(String[] args) throws Exception {3839try {40// Locale.getInstance is not directly accessible.41Method getInstanceMethod = Locale.class.getDeclaredMethod(42"getInstance", String.class, String.class, String.class43);44getInstanceMethod.setAccessible(true);4546getInstanceMethod.invoke(null, "null", "GB", "");47try {48getInstanceMethod.invoke(null, null, "GB", "");49throw new RuntimeException("Should NPE with language set to null");50} catch (InvocationTargetException exc) {51Throwable cause = exc.getCause();52if (!(cause instanceof NullPointerException)) {53throw new RuntimeException(cause+" is thrown with language set to null");54}55}5657getInstanceMethod.invoke(null, "en", "null", "");58try {59getInstanceMethod.invoke(null, "en", null, "");60throw new RuntimeException("Should NPE with country set to null");61} catch (InvocationTargetException exc) {62Throwable cause = exc.getCause();63if (!(cause instanceof NullPointerException)) {64throw new RuntimeException(cause+" is thrown with country set to null");65}66}6768getInstanceMethod.invoke(null, "en", "GB", "null");69try {70getInstanceMethod.invoke(null, "en", "GB", null);71throw new RuntimeException("Should NPE with variant set to null");72} catch (InvocationTargetException exc) {73Throwable cause = exc.getCause();74if (!(cause instanceof NullPointerException)) {75throw new RuntimeException(cause+" is thrown with variant set to null");76}77}78} catch (java.lang.NoSuchMethodException exc) {79// method is not found. consider it as a success80}81}82}838485