< prev index next >

test/tools/launcher/modules/dryrun/DryRunTest.java

Print this page




  43 import org.testng.annotations.BeforeTest;
  44 import org.testng.annotations.Test;
  45 import static org.testng.Assert.*;
  46 
  47 
  48 @Test
  49 public class DryRunTest {
  50 
  51     private static final String TEST_SRC = System.getProperty("test.src");
  52 
  53     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  54     private static final Path MODS_DIR = Paths.get("mods");
  55     private static final Path LIBS_DIR = Paths.get("libs");
  56 
  57     // the module name of the test module
  58     private static final String TEST_MODULE = "test";
  59     private static final String M_MODULE = "m";
  60 
  61     // the module main class
  62     private static final String MAIN_CLASS = "jdk.test.Main";

  63 
  64 
  65     @BeforeTest
  66     public void compileTestModule() throws Exception {
  67 
  68         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  69         assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE),
  70                                          MODS_DIR,
  71                                          "-modulesourcepath", SRC_DIR.toString()));
  72 
  73         assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  74                                          MODS_DIR,
  75                                          "-modulesourcepath", SRC_DIR.toString()));
  76 
  77         Files.createDirectories(LIBS_DIR);
  78 
  79         // create JAR files with no module-info.class
  80         assertTrue(jar(M_MODULE, "p/Lib.class"));
  81         assertTrue(jar(TEST_MODULE, "jdk/test/Main.class"));
  82     }
  83 
  84     /**
  85      * Execute "java" with the given arguments, returning the exit code.
  86      */
  87     private int exec(String... args) throws Exception {
  88        return ProcessTools.executeTestJava(args)
  89                 .outputTo(System.out)
  90                 .errorTo(System.out)
  91                 .getExitValue();
  92     }
  93 
  94 
  95     /**
  96      * Launch module main
  97      */
  98     public void testModule() throws Exception {
  99         String dir = MODS_DIR.toString();
 100         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 101 
 102         // java -modulepath mods -module $TESTMODULE/$MAINCLASS
 103         // no resolution failure
 104         int exitValue = exec("--dry-run", "-modulepath", dir, "-m", mid);
 105         assertTrue(exitValue == 0);
 106     }
 107 
 108     /**















 109      * Test non-existence module in -addmods
 110      */
 111     public void testNonExistAddModules() throws Exception {
 112         String dir = MODS_DIR.toString();
 113         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 114 
 115         int exitValue = exec("--dry-run", "-modulepath", dir,
 116                              "-addmods", "non.existence",
 117                              "-m", mid);
 118         assertTrue(exitValue != 0);
 119     }
 120 
 121     /**
 122      * Launch main class from class path
 123      */
 124     public void testClassPath() throws Exception {
 125         Path testJar = LIBS_DIR.resolve(TEST_MODULE + ".jar");
 126         String libs = testJar.toString() + File.pathSeparator +
 127                         LIBS_DIR.resolve(M_MODULE + ".jar").toString();
 128 




  43 import org.testng.annotations.BeforeTest;
  44 import org.testng.annotations.Test;
  45 import static org.testng.Assert.*;
  46 
  47 
  48 @Test
  49 public class DryRunTest {
  50 
  51     private static final String TEST_SRC = System.getProperty("test.src");
  52 
  53     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  54     private static final Path MODS_DIR = Paths.get("mods");
  55     private static final Path LIBS_DIR = Paths.get("libs");
  56 
  57     // the module name of the test module
  58     private static final String TEST_MODULE = "test";
  59     private static final String M_MODULE = "m";
  60 
  61     // the module main class
  62     private static final String MAIN_CLASS = "jdk.test.Main";
  63     private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit";
  64 
  65 
  66     @BeforeTest
  67     public void compileTestModule() throws Exception {
  68 
  69         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  70         assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE),
  71                                          MODS_DIR,
  72                                          "-modulesourcepath", SRC_DIR.toString()));
  73 
  74         assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  75                                          MODS_DIR,
  76                                          "-modulesourcepath", SRC_DIR.toString()));
  77 
  78         Files.createDirectories(LIBS_DIR);
  79 
  80         // create JAR files with no module-info.class
  81         assertTrue(jar(M_MODULE, "p/Lib.class"));
  82         assertTrue(jar(TEST_MODULE, "jdk/test/Main.class"));
  83     }
  84 
  85     /**
  86      * Execute "java" with the given arguments, returning the exit code.
  87      */
  88     private int exec(String... args) throws Exception {
  89        return ProcessTools.executeTestJava(args)
  90                 .outputTo(System.out)
  91                 .errorTo(System.out)
  92                 .getExitValue();
  93     }
  94 
  95 
  96     /**
  97      * Launch module main
  98      */
  99     public void testModule() throws Exception {
 100         String dir = MODS_DIR.toString();
 101         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 102 

 103         // no resolution failure
 104         int exitValue = exec("--dry-run", "-modulepath", dir, "-m", mid);
 105         assertTrue(exitValue == 0);
 106     }
 107 
 108     /**
 109      * Test dryrun that does not invoke <clinit> of the main class
 110      */
 111     public void testMainClinit() throws Exception {
 112         String dir = MODS_DIR.toString();
 113         String mid = TEST_MODULE + "/" + MAIN_CLINIT_CLASS;
 114 
 115         int exitValue = exec("--dry-run", "-modulepath", dir, "-m", mid);
 116         assertTrue(exitValue == 0);
 117 
 118         // expect the test to fail if main class is initialized
 119         exitValue = exec("-modulepath", dir, "-m", mid);
 120         assertTrue(exitValue != 0);
 121     }
 122 
 123     /**
 124      * Test non-existence module in -addmods
 125      */
 126     public void testNonExistAddModules() throws Exception {
 127         String dir = MODS_DIR.toString();
 128         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 129 
 130         int exitValue = exec("--dry-run", "-modulepath", dir,
 131                              "-addmods", "non.existence",
 132                              "-m", mid);
 133         assertTrue(exitValue != 0);
 134     }
 135 
 136     /**
 137      * Launch main class from class path
 138      */
 139     public void testClassPath() throws Exception {
 140         Path testJar = LIBS_DIR.resolve(TEST_MODULE + ".jar");
 141         String libs = testJar.toString() + File.pathSeparator +
 142                         LIBS_DIR.resolve(M_MODULE + ".jar").toString();
 143 


< prev index next >