44 private final static String WARNING_HEADER =
45 "// This is a machine generated file, do not edit\n" +
46 "// Created by RunMTest v0.5\n";
47
48 private final static String[] SP_HEADER = new String[] {
49 "import java.lang.ModuleInfo.*;",
50 "import java.module.annotation.*;",
51 "",
52 };
53
54 private static final Map<String,String> defaultProperties;
55
56 static {
57 Properties p = System.getProperties();
58 @SuppressWarnings("unchecked")
59 Map<String,String> m = (Map<String,String>)(Map)p;
60 defaultProperties = new HashMap<String,String>(m);
61 defaultProperties.put("header", WARNING_HEADER);
62 }
63
64 private final File file;
65 private final File outputDirectory;
66 private final List<ModuleDescription> modules;
67 private final List<TestDescription> tests;
68
69 private RunMTest(File file, String baseDirectory) throws IOException {
70 this.file = file;
71 String mString = SEP + "mtest" + SEP;
72 String cPath = file.getCanonicalPath();
73 int k = cPath.lastIndexOf(mString);
74 String subdir;
75 if (k == -1) {
76 subdir = file.getName();
77 } else {
78 subdir = cPath.substring(k + mString.length());
79 }
80 System.out.println(">>> Test " + subdir);
81 outputDirectory = new File(baseDirectory, subdir);
82 if (outputDirectory.exists()) {
83 recursiveDelete(outputDirectory);
84 }
85 modules = new ArrayList<ModuleDescription>();
86 tests = new ArrayList<TestDescription>();
87 parse();
88 }
89
90 private String getName() {
91 return file.getName();
92 }
93
94 private static String template;
95
96 static String getTemplate() throws IOException {
97 if (template != null) {
98 return template;
99 }
100 File f = new File(System.getProperty("test.src", "."), "cl-template.java");
101 InputStream in = new FileInputStream(f);
102 ByteArrayOutputStream out = new ByteArrayOutputStream();
103 byte[] data = new byte[8192];
104 while (true) {
105 int n = in.read(data);
106 if (n < 0) {
107 break;
108 }
109 out.write(data, 0, n);
110 }
293 File moduledir = md.getModuleDir();
294 File pkgdir = new File(moduledir, pkg.replace('.', SEP));
295 pkgdir.mkdirs();
296 File srcfile = new File(pkgdir, name + ".java");
297 String template = getTemplate();
298 String srcstring = substituteProperties(template, properties);
299 OutputStream out = new FileOutputStream(srcfile);
300 out.write(srcstring.getBytes("UTF8"));
301 out.close();
302 return srcfile;
303 }
304
305 }
306
307 private void runTests() throws Exception {
308 for (TestDescription t : tests) {
309 t.runTest(this);
310 }
311 }
312
313 private static class TestDescription {
314
315 private final String name;
316 private String result;
317
318 private TestDescription(String name) {
319 this.name = name;
320 }
321
322 private void runTest(RunMTest mTest) throws Exception {
323 System.out.println("> Running test " + name + "...");
324 Repository parent = sun.module.repository.RepositoryConfig.getSystemRepository();
325 Repository repository = Modules.newLocalRepository(mTest.getName(), mTest.outputDirectory, null, parent);
326 ModuleDefinition md = repository.find(name);
327 try {
328 Module m = md.getModuleInstance();
329 String mainClassName = md.getAnnotation(MainClass.class).value();
330 ClassLoader cl = m.getClassLoader();
331 Class<?> clazz = cl.loadClass(mainClassName);
332 Method method = clazz.getMethod("main", String[].class);
333 method.invoke(null, (Object)new String[0]);
334 } catch (Exception e) {
335 if ((e instanceof InvocationTargetException) && (e.getCause() instanceof Exception)) {
336 e = (Exception)e.getCause();
337 }
338 if (result.equals("return")) {
339 throw new Exception("test failed", e);
340 }
341 if (result.startsWith("exception ")) {
342 String excname = result.substring("exception ".length());
343 String[] fqe = e.getClass().getName().split("\\.");
344 if (fqe[fqe.length - 1].equals(excname)) {
345 System.out.println("> Test completed with expected exception: " + e);
346 return;
347 }
348 throw new Exception("Expected exception " + excname, e);
349 }
350 throw new Exception("Unknown test result: " + result);
351 }
352 if (!result.equals("return")) {
353 throw new Exception("Test unexpectedly returned normally");
354 }
355 System.out.println("> Test completed.");
356 }
357
358 }
359
360 private TestDescription parseTest(String header, BufferedReader reader) throws IOException {
361 String[] s = header.split(" ");
362 String name = s[s.length - 1];
363 TestDescription test = new TestDescription(name);
364 while (true) {
365 String line = getLine(reader);
366 if (line == null) {
367 throw new EOFException();
368 }
369 if (line.equals(">>> end test")) {
370 break;
371 }
372 test.result = line;
373 }
374 return test;
375 }
376
377 private ClassDescription parseClass(String header, BufferedReader reader) throws IOException {
378 ClassDescription cd = new ClassDescription(header);
379 String prop = "run";
380 while (true) {
381 String line = getLine(reader);
382 if (line == null) {
383 throw new EOFException();
|
44 private final static String WARNING_HEADER =
45 "// This is a machine generated file, do not edit\n" +
46 "// Created by RunMTest v0.5\n";
47
48 private final static String[] SP_HEADER = new String[] {
49 "import java.lang.ModuleInfo.*;",
50 "import java.module.annotation.*;",
51 "",
52 };
53
54 private static final Map<String,String> defaultProperties;
55
56 static {
57 Properties p = System.getProperties();
58 @SuppressWarnings("unchecked")
59 Map<String,String> m = (Map<String,String>)(Map)p;
60 defaultProperties = new HashMap<String,String>(m);
61 defaultProperties.put("header", WARNING_HEADER);
62 }
63
64 protected final File file;
65 protected final File outputDirectory;
66 private final List<ModuleDescription> modules;
67 private final List<TestDescription> tests;
68
69 private RunMTest(File file, String baseDirectory) throws IOException {
70 this.file = file;
71 String mString = SEP + "mtest" + SEP;
72 String cPath = file.getCanonicalPath();
73 int k = cPath.lastIndexOf(mString);
74 String subdir;
75 if (k == -1) {
76 subdir = file.getName();
77 } else {
78 subdir = cPath.substring(k + mString.length());
79 }
80 System.out.println(">>>Test " + subdir);
81 outputDirectory = new File(baseDirectory, subdir);
82 if (outputDirectory.exists()) {
83 recursiveDelete(outputDirectory);
84 }
85 modules = new ArrayList<ModuleDescription>();
86 tests = new ArrayList<TestDescription>();
87 parse();
88 }
89
90 protected String getName() {
91 return file.getName();
92 }
93
94 private static String template;
95
96 static String getTemplate() throws IOException {
97 if (template != null) {
98 return template;
99 }
100 File f = new File(System.getProperty("test.src", "."), "cl-template.java");
101 InputStream in = new FileInputStream(f);
102 ByteArrayOutputStream out = new ByteArrayOutputStream();
103 byte[] data = new byte[8192];
104 while (true) {
105 int n = in.read(data);
106 if (n < 0) {
107 break;
108 }
109 out.write(data, 0, n);
110 }
293 File moduledir = md.getModuleDir();
294 File pkgdir = new File(moduledir, pkg.replace('.', SEP));
295 pkgdir.mkdirs();
296 File srcfile = new File(pkgdir, name + ".java");
297 String template = getTemplate();
298 String srcstring = substituteProperties(template, properties);
299 OutputStream out = new FileOutputStream(srcfile);
300 out.write(srcstring.getBytes("UTF8"));
301 out.close();
302 return srcfile;
303 }
304
305 }
306
307 private void runTests() throws Exception {
308 for (TestDescription t : tests) {
309 t.runTest(this);
310 }
311 }
312
313
314 // Abstracting the creation of TestDescription instances to a factory
315 // allows other tests to override TestDescription.runTest.
316 //
317 public static class TestDescriptionFactory {
318 private static TestDescriptionFactory instance;
319
320 static TestDescription create(String name) {
321 return getInstance().doCreate(name);
322 }
323
324 private static TestDescriptionFactory getInstance() {
325 if (instance == null) {
326 String factName = System.getProperty("TestDescriptionFactory.classname");
327 if (factName != null) {
328 try {
329 Class<?> clazz = Class.forName(factName);
330 instance = (TestDescriptionFactory) clazz.newInstance();
331 } catch (Throwable t) {
332 throw new RuntimeException(t);
333 }
334 } else {
335 instance = new TestDescriptionFactory();
336 }
337 }
338 return instance;
339 }
340
341 protected TestDescription doCreate(String name) {
342 return new TestDescription(name);
343 }
344 }
345
346 public static class TestDescription {
347
348 protected final String name;
349 protected String result;
350
351 TestDescription(String name) {
352 this.name = name;
353 }
354
355 protected void runTest(RunMTest mTest) throws Exception {
356 System.out.println("> Running test " + name + "...");
357 Repository parent = sun.module.repository.RepositoryConfig.getSystemRepository();
358 Repository repository = Modules.newLocalRepository(mTest.getName(), mTest.outputDirectory, null, parent);
359 ModuleDefinition md = repository.find(name);
360 try {
361 Module m = md.getModuleInstance();
362 String mainClassName = md.getAnnotation(MainClass.class).value();
363 ClassLoader cl = m.getClassLoader();
364 Class<?> clazz = cl.loadClass(mainClassName);
365 Method method = clazz.getMethod("main", String[].class);
366 method.invoke(null, (Object)new String[0]);
367 } catch (Exception e) {
368 if ((e instanceof InvocationTargetException) && (e.getCause() instanceof Exception)) {
369 e = (Exception)e.getCause();
370 }
371 if (result.equals("return")) {
372 throw new Exception("test failed", e);
373 }
374 if (result.startsWith("exception ")) {
375 String excname = result.substring("exception ".length());
376 String[] fqe = e.getClass().getName().split("\\.");
377 if (fqe[fqe.length - 1].equals(excname)) {
378 System.out.println("> Test completed with expected exception: " + e);
379 return;
380 }
381 throw new Exception("Expected exception " + excname, e);
382 }
383 throw new Exception("Unknown test result: " + result);
384 }
385 if (!result.equals("return")) {
386 throw new Exception("Test unexpectedly returned normally");
387 }
388 System.out.println("> Test completed.");
389 }
390
391 }
392
393 private TestDescription parseTest(String header, BufferedReader reader) throws IOException {
394 String[] s = header.split(" ");
395 String name = s[s.length - 1];
396 TestDescription test = TestDescriptionFactory.create(name);
397 while (true) {
398 String line = getLine(reader);
399 if (line == null) {
400 throw new EOFException();
401 }
402 if (line.equals(">>> end test")) {
403 break;
404 }
405 test.result = line;
406 }
407 return test;
408 }
409
410 private ClassDescription parseClass(String header, BufferedReader reader) throws IOException {
411 ClassDescription cd = new ClassDescription(header);
412 String prop = "run";
413 while (true) {
414 String line = getLine(reader);
415 if (line == null) {
416 throw new EOFException();
|