Path: blob/master/test/jdk/com/sun/jmx/remote/NotificationMarshalVersions/TestSerializationMismatch.java
41155 views
/*1* Copyright (c) 2013, 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*/2223import java.io.File;24import java.lang.reflect.Method;25import java.net.URL;26import java.net.URLClassLoader;27import java.util.Arrays;2829/**30* @test31* @summary Tests for the RMI unmarshalling errors not to cause silent failure.32* @author Jaroslav Bachorik33* @bug 6937053 800547234*35* @modules java.management36* jdk.compiler37* @run clean TestSerializationMismatch38* @run main/othervm TestSerializationMismatch39*40*/41public class TestSerializationMismatch {42static final String clientDir = "Client";43static final String serverDir = "Server";44static final String testSrc = System.getProperty("test.src");45static final String testSrcDir = testSrc != null ? testSrc : ".";46static final String testSrcClientDir = testSrcDir + File.separator + clientDir + File.separator;47static final String testSrcServerDir = testSrcDir + File.separator + serverDir + File.separator;48static final String testClasses = System.getProperty("test.classes");49static final String testClassesDir = testClasses != null ? testClasses : ".";50static final String testClassesClientDir = testClassesDir + File.separator + clientDir + File.separator;51static final String testClassesServerDir = testClassesDir + File.separator + serverDir + File.separator;5253static final boolean debug = true;5455public static void main(String[] args) throws Exception {56setup();5758compileClient();59compileServer();6061debug("starting server");62String url = startServer();63debug("server started and listening on " + url);64debug("starting client");65startClient(url);66}6768static void setup() {69debug("setting up the output dirs");70cleanupDir(testClassesClientDir);71cleanupDir(testClassesServerDir);72}7374static void cleanupDir(String path) {75debug("cleaning " + path);76File dir = new File(path);77if (dir.exists()) {78for(File src : dir.listFiles()) {79boolean rslt = src.delete();80debug((rslt == false ? "not " : "") + "deleted " + src);81}82} else {83dir.mkdirs();84}85}8687static void compileClient() {88debug("compiling client");89compile("-d" , testClassesClientDir,90"-sourcepath", testSrcClientDir,91testSrcClientDir + "Client.java",92testSrcClientDir + "ConfigKey.java",93testSrcClientDir + "TestNotification.java");94}9596static void compileServer() {97debug("compiling server");98compile("-d" , testClassesServerDir,99"-sourcepath", testSrcServerDir,100testSrcServerDir + "Server.java",101testSrcServerDir + "ConfigKey.java",102testSrcServerDir + "TestNotification.java",103testSrcServerDir + "Ste.java",104testSrcServerDir + "SteMBean.java");105}106107static String startServer() throws Exception {108ClassLoader serverCL = customCL(testClassesServerDir);109110Class serverClz = serverCL.loadClass("Server");111Method startMethod = serverClz.getMethod("start");112return (String)startMethod.invoke(null);113}114115static void startClient(String url) throws Exception {116ClassLoader clientCL = customCL(testClassesClientDir);117118Thread.currentThread().setContextClassLoader(clientCL);119Class clientClz = clientCL.loadClass("Client");120Method runMethod = clientClz.getMethod("run", String.class);121runMethod.invoke(null, url);122}123124static ClassLoader customCL(String classDir) throws Exception {125return new URLClassLoader(126new URL[]{127new File(classDir).toURI().toURL()128},129TestSerializationMismatch.class.getClassLoader()130);131}132133static void debug(Object message) {134if (debug) {135System.out.println(message);136}137}138139/* run javac <args> */140static void compile(String... args) {141debug("Running: javac " + Arrays.toString(args));142if (com.sun.tools.javac.Main.compile(args) != 0) {143throw new RuntimeException("javac failed: args=" + Arrays.toString(args));144}145}146}147148149