1 2 import java.io.File; 3 import java.lang.reflect.Method; 4 import java.net.URL; 5 import java.net.URLClassLoader; 6 import java.util.Arrays; 7 8 /** 9 * @test 10 * @summary Tests for the RMI unmarshalling errors not to cause silent failure. 11 * @author Jaroslav Bachorik 12 * @bug 6937053 8005472 13 * 14 * @run clean TestSerializationMismatch 15 * @run main/othervm TestSerializationMismatch 16 * 17 */ 18 public class TestSerializationMismatch { 19 static final String clientDir = "Client"; 20 static final String serverDir = "Server"; 21 static final String testSrc = System.getProperty("test.src"); 22 static final String testSrcDir = testSrc != null ? testSrc : "."; 23 static final String testSrcClientDir = testSrcDir + File.separator + clientDir + File.separator; 24 static final String testSrcServerDir = testSrcDir + File.separator + serverDir + File.separator; 25 static final String testClasses = System.getProperty("test.classes"); 26 static final String testClassesDir = testClasses != null ? testClasses : "."; 27 static final String testClassesClientDir = testClassesDir + File.separator + clientDir + File.separator; 28 static final String testClassesServerDir = testClassesDir + File.separator + serverDir + File.separator; 29 30 static final boolean debug = true; 31 32 public static void main(String[] args) throws Exception { 33 setup(); 34 35 compileClient(); 36 compileServer(); 37 38 debug("starting server"); 39 String url = startServer(); 40 debug("server started and listening on " + url); 41 debug("starting client"); 42 startClient(url); 43 } 44 45 static void setup() { 46 debug("setting up the output dirs"); 47 cleanupDir(testClassesClientDir); 48 cleanupDir(testClassesServerDir); 49 } 50 51 static void cleanupDir(String path) { 52 debug("cleaning " + path); 53 File dir = new File(path); 54 if (dir.exists()) { 55 for(File src : dir.listFiles()) { 56 boolean rslt = src.delete(); 57 debug((rslt == false ? "not " : "") + "deleted " + src); 58 } 59 } else { 60 dir.mkdirs(); 61 } 62 } 63 64 static void compileClient() { 65 debug("compiling client"); 66 compile("-d" , testClassesClientDir, 67 "-sourcepath", testSrcClientDir, 68 testSrcClientDir + "Client.java", 69 testSrcClientDir + "ConfigKey.java", 70 testSrcClientDir + "TestNotification.java"); 71 } 72 73 static void compileServer() { 74 debug("compiling server"); 75 compile("-d" , testClassesServerDir, 76 "-sourcepath", testSrcServerDir, 77 testSrcServerDir + "Server.java", 78 testSrcServerDir + "ConfigKey.java", 79 testSrcServerDir + "TestNotification.java", 80 testSrcServerDir + "Ste.java", 81 testSrcServerDir + "SteMBean.java"); 82 } 83 84 static String startServer() throws Exception { 85 ClassLoader serverCL = customCL(testClassesServerDir); 86 87 Class serverClz = serverCL.loadClass("Server"); 88 Method startMethod = serverClz.getMethod("start"); 89 return (String)startMethod.invoke(null); 90 } 91 92 static void startClient(String url) throws Exception { 93 ClassLoader clientCL = customCL(testClassesClientDir); 94 95 Thread.currentThread().setContextClassLoader(clientCL); 96 Class clientClz = clientCL.loadClass("Client"); 97 Method runMethod = clientClz.getMethod("run", String.class); 98 runMethod.invoke(null, url); 99 } 100 101 static ClassLoader customCL(String classDir) throws Exception { 102 return new URLClassLoader( 103 new URL[]{ 104 new File(classDir).toURI().toURL() 105 }, 106 TestSerializationMismatch.class.getClassLoader() 107 ); 108 } 109 110 static void debug(Object message) { 111 if (debug) { 112 System.out.println(message); 113 } 114 } 115 116 /* run javac <args> */ 117 static void compile(String... args) { 118 debug("Running: javac " + Arrays.toString(args)); 119 if (com.sun.tools.javac.Main.compile(args) != 0) { 120 throw new RuntimeException("javac failed: args=" + Arrays.toString(args)); 121 } 122 } 123 }