Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/bug6312358.java
38813 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*/2829import java.lang.reflect.InvocationTargetException;30import java.lang.reflect.Method;31import java.util.Locale;3233public class bug6312358 {343536public static void main(String[] args) throws Exception {3738try {39// Locale.getInstance is not directly accessible.40Method getInstanceMethod = Locale.class.getDeclaredMethod(41"getInstance", String.class, String.class, String.class42);43getInstanceMethod.setAccessible(true);4445getInstanceMethod.invoke(null, "null", "GB", "");46try {47getInstanceMethod.invoke(null, null, "GB", "");48throw new RuntimeException("Should NPE with language set to null");49} catch (InvocationTargetException exc) {50Throwable cause = exc.getCause();51if (!(cause instanceof NullPointerException)) {52throw new RuntimeException(cause+" is thrown with language set to null");53}54}5556getInstanceMethod.invoke(null, "en", "null", "");57try {58getInstanceMethod.invoke(null, "en", null, "");59throw new RuntimeException("Should NPE with country set to null");60} catch (InvocationTargetException exc) {61Throwable cause = exc.getCause();62if (!(cause instanceof NullPointerException)) {63throw new RuntimeException(cause+" is thrown with country set to null");64}65}6667getInstanceMethod.invoke(null, "en", "GB", "null");68try {69getInstanceMethod.invoke(null, "en", "GB", null);70throw new RuntimeException("Should NPE with variant set to null");71} catch (InvocationTargetException exc) {72Throwable cause = exc.getCause();73if (!(cause instanceof NullPointerException)) {74throw new RuntimeException(cause+" is thrown with variant set to null");75}76}77} catch (java.lang.NoSuchMethodException exc) {78// method is not found. consider it as a success79}80}81}828384