Path: blob/master/test/jdk/javax/naming/InitialContext/AppletIsNotUsed.java
41149 views
/*1* Copyright (c) 2014, 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*/2223import javax.naming.Context;24import javax.naming.InitialContext;25import javax.naming.NamingException;26import javax.naming.NoInitialContextException;27import java.util.Hashtable;2829/*30* @test31* @bug 805142232* @summary Make sure java.applet.Applet is not used as a source of33* configuration parameters for an InitialContext34*/35public class AppletIsNotUsed {3637@SuppressWarnings("deprecation")38public static void main(String[] args) throws NamingException {3940testWith(Context.APPLET);41testWith("java.naming.applet");4243}4445private static void testWith(String appletProperty) throws NamingException {46Hashtable<Object, Object> env = new Hashtable<>();47// Deliberately put java.lang.Object rather than java.applet.Applet48// if an applet was used we would see a ClassCastException down there49env.put(appletProperty, new Object());50// It's ok to instantiate InitialContext with no parameters51// and be unaware of it right until you try to use it52Context ctx = new InitialContext(env);53boolean threw = true;54try {55ctx.lookup("whatever");56threw = false;57} catch (NoInitialContextException e) {58String m = e.getMessage();59if (m == null || m.contains("applet"))60throw new RuntimeException("The exception message is incorrect", e);61} catch (Throwable t) {62throw new RuntimeException(63"The test was supposed to catch NoInitialContextException" +64" here, but caught: " + t.getClass().getName(), t);65} finally {66ctx.close();67}6869if (!threw)70throw new RuntimeException("The test was supposed to catch NoInitialContextException here");71}72}737475