Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/ObjectInputStreamWithLoaderNullCheckTest.java
41159 views
/*1* Copyright (c) 2016, 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 800956026* @summary Test RMIConnector.ObjectInputStreamWithLoader constructor with27* null Class loader. The test expects a IllegalArgumentException28* thrown when constructor is invoked with null class loader as29* an argument.30* @author Amit Sapre31* @modules java.management.rmi/javax.management.remote.rmi:open32* @run clean ObjectInputStreamWithLoaderNullCheckTest33* @run build ObjectInputStreamWithLoaderNullCheckTest34* @run main ObjectInputStreamWithLoaderNullCheckTest35*/3637import java.lang.reflect.*;38import javax.management.remote.*;39import javax.management.remote.rmi.*;40import java.io.*;4142public class ObjectInputStreamWithLoaderNullCheckTest {4344private static Class<?> innerClass;4546public static void main(String[] args) throws Exception {4748System.out.println(">> == ObjectInputStreamWithLoaderNullCheckTest started...");4950try {51innerClass = Class.forName("javax.management.remote.rmi.RMIConnector$ObjectInputStreamWithLoader");52Constructor<?> ctor = innerClass.getDeclaredConstructor(InputStream.class,ClassLoader.class);53ctor.setAccessible(true);5455ByteArrayOutputStream baos = new ByteArrayOutputStream();56ObjectOutput objOut = new ObjectOutputStream(baos);57objOut.writeObject(new String("Serialize"));58objOut.close();59baos.close();60ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());6162System.out.println(">> == Testing constructor with null class loader.");63Object obj = ctor.newInstance(bais,null);6465System.out.println(">> == Test case failed. No error occured");66System.exit(1);67} catch (InvocationTargetException ex) {68Throwable cause = ex.getCause();69System.out.println(">> == InvocationTargetException Cause message : " + cause.toString());70if (cause instanceof IllegalArgumentException) {71System.out.println(">> == Test case Passed.");72} else {73System.out.println(">> == Test case Failed.");74ex.printStackTrace();75System.exit(1);76}77} catch (Exception ex) {78System.out.println(">>> == Test case failed with error " + ex.getCause().getMessage());79ex.printStackTrace();80System.exit(1);81}82}83}848586