1 /*
   2  * Copyright (c) 2016, 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 8009560
  27  * @summary Test RMIConnector.ObjectInputStreamWithLoader constructor with
  28  *          null Class loader. The test expects a IllegalArgumentException
  29  *          thrown when constructor is invoked with null class loader as
  30  *          an argument.
  31  * @author Amit Sapre
  32  * @modules java.management/javax.management.remote.rmi:open
  33  * @run clean ObjectInputStreamWithLoaderNullCheckTest
  34  * @run build ObjectInputStreamWithLoaderNullCheckTest
  35  * @run main ObjectInputStreamWithLoaderNullCheckTest
  36  */
  37 
  38 import java.lang.reflect.*;
  39 import javax.management.remote.*;
  40 import javax.management.remote.rmi.*;
  41 import java.io.*;
  42 
  43 public class ObjectInputStreamWithLoaderNullCheckTest {
  44 
  45     private static Class<?> innerClass;
  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49        System.out.println(">> == ObjectInputStreamWithLoaderNullCheckTest started...");
  50 
  51        try {
  52            innerClass = Class.forName("javax.management.remote.rmi.RMIConnector$ObjectInputStreamWithLoader");
  53            Constructor<?> ctor = innerClass.getDeclaredConstructor(InputStream.class,ClassLoader.class);
  54            ctor.setAccessible(true);
  55 
  56            ByteArrayOutputStream baos = new ByteArrayOutputStream();
  57            ObjectOutput objOut =  new ObjectOutputStream(baos);
  58            objOut.writeObject(new String("Serialize"));
  59            objOut.close();
  60            baos.close();
  61            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  62 
  63            System.out.println(">> == Testing constructor with null class loader.");
  64            Object obj = ctor.newInstance(bais,null);
  65 
  66            System.out.println(">> == Test case failed. No error occured");
  67            System.exit(1);
  68        } catch (InvocationTargetException ex) {
  69            Throwable cause = ex.getCause();
  70            System.out.println(">> == InvocationTargetException Cause message : " + cause.toString());
  71            if (cause instanceof IllegalArgumentException) {
  72               System.out.println(">> == Test case Passed.");
  73            } else {
  74               System.out.println(">> == Test case Failed.");
  75               ex.printStackTrace();
  76               System.exit(1);
  77            }
  78        } catch (Exception ex) {
  79            System.out.println(">>> == Test case failed with error " + ex.getCause().getMessage());
  80            ex.printStackTrace();
  81            System.exit(1);
  82        }
  83     }
  84 }