duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4974913 |
| 27 | * @summary Test that array classes can be found in signatures always |
| 28 | * and can be deserialized by the deprecated MBeanServer.deserialize method |
| 29 | * @author Eamonn McManus |
| 30 | * @run clean ArrayClassTest |
| 31 | * @run build ArrayClassTest |
| 32 | * @run main ArrayClassTest |
| 33 | */ |
| 34 | |
| 35 | import java.io.*; |
| 36 | import java.lang.reflect.*; |
| 37 | import java.net.*; |
| 38 | import javax.management.*; |
| 39 | import javax.management.loading.*; |
| 40 | |
| 41 | public class ArrayClassTest { |
| 42 | public static void main(String[] args) throws Exception { |
| 43 | MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
| 44 | |
| 45 | /* If this test isn't loaded by a URLClassLoader we will get |
| 46 | a ClassCastException here, which is good because it means |
| 47 | this test isn't valid. */ |
| 48 | URLClassLoader testLoader = |
| 49 | (URLClassLoader) ArrayClassTest.class.getClassLoader(); |
| 50 | |
| 51 | // Create an MLet that can load the same class names but |
| 52 | // will produce different results. |
| 53 | ClassLoader loader = new SpyLoader(testLoader.getURLs()); |
| 54 | ObjectName loaderName = new ObjectName("test:type=SpyLoader"); |
| 55 | mbs.registerMBean(loader, loaderName); |
| 56 | |
| 57 | ObjectName testName = new ObjectName("test:type=Test"); |
| 58 | mbs.createMBean(Test.class.getName(), testName, loaderName, |
| 59 | new Object[1], new String[] {X[].class.getName()}); |
| 60 | ClassLoader checkLoader = mbs.getClassLoaderFor(testName); |
| 61 | if (checkLoader != loader) |
| 62 | throw new AssertionError("Wrong loader: " + checkLoader); |
| 63 | |
| 64 | mbs.invoke(testName, "ignore", new Object[1], |
| 65 | new String[] {Y[].class.getName()}); |
| 66 | |
| 67 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 68 | ObjectOutputStream oout = new ObjectOutputStream(bout); |
| 69 | oout.writeObject(new Z[0]); |
| 70 | oout.close(); |
| 71 | byte[] bytes = bout.toByteArray(); |
| 72 | ObjectInputStream oin = mbs.deserialize(testName, bytes); |
| 73 | Object zarray = oin.readObject(); |
| 74 | String failed = null; |
| 75 | if (zarray instanceof Z[]) |
| 76 | failed = "read back a real Z[]"; |
| 77 | else if (!zarray.getClass().getName().equals(Z[].class.getName())) { |
| 78 | failed = "returned object of wrong type: " + |
| 79 | zarray.getClass().getName(); |
| 80 | } else if (Array.getLength(zarray) != 0) |
| 81 | failed = "returned array of wrong size: " + Array.getLength(zarray); |
| 82 | if (failed != null) { |
| 83 | System.out.println("TEST FAILED: " + failed); |
| 84 | System.exit(1); |
| 85 | } |
| 86 | |
| 87 | System.out.println("Test passed"); |
| 88 | } |
| 89 | |
| 90 | public static interface TestMBean { |
| 91 | public void ignore(Y[] ignored); |
| 92 | } |
| 93 | |
| 94 | public static class Test implements TestMBean { |
| 95 | public Test(X[] ignored) {} |
| 96 | public void ignore(Y[] ignored) {} |
| 97 | } |
| 98 | |
| 99 | public static class X {} |
| 100 | public static class Y {} |
| 101 | public static class Z implements Serializable {} |
| 102 | |
| 103 | public static interface SpyLoaderMBean {} |
| 104 | |
| 105 | /* We originally had this extend MLet but for some reason that |
| 106 | stopped the bug from happening. Some side-effect of registering |
| 107 | the MLet in the MBean server caused it not to fail when asked |
| 108 | to load Z[]. */ |
| 109 | public static class SpyLoader extends URLClassLoader |
| 110 | implements SpyLoaderMBean, PrivateClassLoader { |
| 111 | public SpyLoader(URL[] urls) { |
| 112 | // important that the parent classloader be null! |
| 113 | // otherwise we can pick up classes from the classpath |
| 114 | super(urls, null); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | public Class loadClass(String name) throws ClassNotFoundException { |
| 119 | System.out.println("loadClass: " + name); |
| 120 | return super.loadClass(name); |
| 121 | } |
| 122 | |
| 123 | public Class loadClass(String name, boolean resolve) |
| 124 | throws ClassNotFoundException { |
| 125 | System.out.println("loadClass: " + name + ", " + resolve); |
| 126 | return super.loadClass(name, resolve); |
| 127 | } |
| 128 | */ |
| 129 | |
| 130 | public Class findClass(String name) throws ClassNotFoundException { |
| 131 | System.out.println("findClass: " + name); |
| 132 | if (false) |
| 133 | new Throwable().printStackTrace(System.out); |
| 134 | Class c = super.findClass(name); |
| 135 | System.out.println(" -> " + name + " (" + c.getClassLoader() + ")"); |
| 136 | return c; |
| 137 | } |
| 138 | } |
| 139 | } |