Path: blob/master/test/jdk/javax/management/loading/ArrayClassTest.java
41152 views
/*1* Copyright (c) 2004, 2015, 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 497491326* @summary Test that array classes can be found in signatures always27* and can be deserialized by the deprecated MBeanServer.deserialize method28* @author Eamonn McManus29*30* @run clean ArrayClassTest31* @run build ArrayClassTest32* @run main ArrayClassTest33*/3435import java.io.*;36import java.lang.reflect.*;37import java.net.*;38import java.nio.file.Paths;39import javax.management.*;40import javax.management.loading.*;4142public class ArrayClassTest {43public static void main(String[] args) throws Exception {44MBeanServer mbs = MBeanServerFactory.createMBeanServer();4546String[] cpaths = System.getProperty("test.classes", ".")47.split(File.pathSeparator);48URL[] urls = new URL[cpaths.length];49for (int i=0; i < cpaths.length; i++) {50urls[i] = Paths.get(cpaths[i]).toUri().toURL();51}5253// Create an MLet that can load the same class names but54// will produce different results.55ClassLoader loader = new SpyLoader(urls);56ObjectName loaderName = new ObjectName("test:type=SpyLoader");57mbs.registerMBean(loader, loaderName);5859ObjectName testName = new ObjectName("test:type=Test");60mbs.createMBean(Test.class.getName(), testName, loaderName,61new Object[1], new String[] {X[].class.getName()});62ClassLoader checkLoader = mbs.getClassLoaderFor(testName);63if (checkLoader != loader)64throw new AssertionError("Wrong loader: " + checkLoader);6566mbs.invoke(testName, "ignore", new Object[1],67new String[] {Y[].class.getName()});6869ByteArrayOutputStream bout = new ByteArrayOutputStream();70ObjectOutputStream oout = new ObjectOutputStream(bout);71oout.writeObject(new Z[0]);72oout.close();73byte[] bytes = bout.toByteArray();74ObjectInputStream oin = mbs.deserialize(testName, bytes);75Object zarray = oin.readObject();76String failed = null;77if (zarray instanceof Z[])78failed = "read back a real Z[]";79else if (!zarray.getClass().getName().equals(Z[].class.getName())) {80failed = "returned object of wrong type: " +81zarray.getClass().getName();82} else if (Array.getLength(zarray) != 0)83failed = "returned array of wrong size: " + Array.getLength(zarray);84if (failed != null) {85System.out.println("TEST FAILED: " + failed);86System.exit(1);87}8889System.out.println("Test passed");90}9192public static interface TestMBean {93public void ignore(Y[] ignored);94}9596public static class Test implements TestMBean {97public Test(X[] ignored) {}98public void ignore(Y[] ignored) {}99}100101public static class X {}102public static class Y {}103public static class Z implements Serializable {}104105public static interface SpyLoaderMBean {}106107/* We originally had this extend MLet but for some reason that108stopped the bug from happening. Some side-effect of registering109the MLet in the MBean server caused it not to fail when asked110to load Z[]. */111public static class SpyLoader extends URLClassLoader112implements SpyLoaderMBean, PrivateClassLoader {113public SpyLoader(URL[] urls) {114// important that the parent classloader be null!115// otherwise we can pick up classes from the classpath116super(urls, null);117}118119/*120public Class loadClass(String name) throws ClassNotFoundException {121System.out.println("loadClass: " + name);122return super.loadClass(name);123}124125public Class loadClass(String name, boolean resolve)126throws ClassNotFoundException {127System.out.println("loadClass: " + name + ", " + resolve);128return super.loadClass(name, resolve);129}130*/131132public Class findClass(String name) throws ClassNotFoundException {133System.out.println("findClass: " + name);134if (false)135new Throwable().printStackTrace(System.out);136Class c = super.findClass(name);137System.out.println(" -> " + name + " (" + c.getClassLoader() + ")");138return c;139}140}141}142143144