< prev index next >

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

Print this page




  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 
 144         // test pass with m.jar:test.jar
 145         int exitValue = exec("-classpath", libs, MAIN_CLASS);
 146         assertTrue(exitValue == 0);
 147 
 148         // m.jar is not on classpath and fails with p.Lib not found
 149         exitValue = exec("-classpath", testJar.toString(), MAIN_CLASS);
 150         assertTrue(exitValue != 0);
 151 
 152         // dry pass passes since main is not executed
 153         exitValue = exec("--dry-run", "-classpath", testJar.toString(), MAIN_CLASS);
 154         assertTrue(exitValue == 0);
 155     }
 156 
 157     /**
 158      * Test automatic modules
 159      */
 160     public void testAutomaticModule() throws Exception {
 161         String libs = LIBS_DIR.resolve(M_MODULE + ".jar").toString() +
 162                         File.pathSeparator +
 163                         LIBS_DIR.resolve(TEST_MODULE + ".jar").toString();
 164         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 165 
 166         // test main method with and without -addmods mm
 167         int exitValue = exec("-modulepath", LIBS_DIR.toString(),
 168                              "-m", mid);
 169         assertTrue(exitValue != 0);
 170 
 171         exitValue = exec("-modulepath", LIBS_DIR.toString(),
 172                          "-addmods", M_MODULE,
 173                          "-m", mid);
 174         assertTrue(exitValue == 0);
 175 
 176         // test dry run with and without -addmods m
 177         // no resolution failure
 178         exitValue = exec("--dry-run", "-modulepath", LIBS_DIR.toString(),
 179                          "-m", mid);
 180         assertTrue(exitValue == 0);
 181 
 182         exitValue = exec("--dry-run", "-modulepath", LIBS_DIR.toString(),
 183                          "-addmods", M_MODULE,
 184                          "-m", mid);
 185         assertTrue(exitValue == 0);
 186     }
 187 
 188     /**
 189      * module m not found
 190      */
 191     public void testMissingModule() throws Exception {
 192         String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
 193         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 194 
 195         // resolution failure
 196         int exitValue = exec("--dry-run", "-modulepath", subdir, "-m", mid);
 197         assertTrue(exitValue != 0);
 198     }
 199 
 200     private static boolean jar(String name, String entries) throws IOException {
 201         Path jar = LIBS_DIR.resolve(name + ".jar");
 202 
 203         // jar --create ...
 204         String classes = MODS_DIR.resolve(name).toString();
 205         String[] args = {
 206             "--create",
 207             "--file=" + jar,
 208             "-C", classes, entries
 209         };
 210         boolean success
 211             = new sun.tools.jar.Main(System.out, System.out, "jar").run(args);
 212         return success;
 213     }
 214 }


  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                                          "--module-source-path", SRC_DIR.toString()));
  73 
  74         assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  75                                          MODS_DIR,
  76                                          "--module-source-path", 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", "--module-path", 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", "--module-path", dir, "-m", mid);
 116         assertTrue(exitValue == 0);
 117 
 118         // expect the test to fail if main class is initialized
 119         exitValue = exec("--module-path", dir, "-m", mid);
 120         assertTrue(exitValue != 0);
 121     }
 122 
 123     /**
 124      * Test non-existence module in --add-modules
 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", "--module-path", dir,
 131                              "--add-modules", "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 
 144         // test pass with m.jar:test.jar
 145         int exitValue = exec("-classpath", libs, MAIN_CLASS);
 146         assertTrue(exitValue == 0);
 147 
 148         // m.jar is not on classpath and fails with p.Lib not found
 149         exitValue = exec("-classpath", testJar.toString(), MAIN_CLASS);
 150         assertTrue(exitValue != 0);
 151 
 152         // dry pass passes since main is not executed
 153         exitValue = exec("--dry-run", "-classpath", testJar.toString(), MAIN_CLASS);
 154         assertTrue(exitValue == 0);
 155     }
 156 
 157     /**
 158      * Test automatic modules
 159      */
 160     public void testAutomaticModule() throws Exception {
 161         String libs = LIBS_DIR.resolve(M_MODULE + ".jar").toString() +
 162                         File.pathSeparator +
 163                         LIBS_DIR.resolve(TEST_MODULE + ".jar").toString();
 164         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 165 
 166         // test main method with and without --add-modules mm
 167         int exitValue = exec("--module-path", LIBS_DIR.toString(),
 168                              "-m", mid);
 169         assertTrue(exitValue != 0);
 170 
 171         exitValue = exec("--module-path", LIBS_DIR.toString(),
 172                          "--add-modules", M_MODULE,
 173                          "-m", mid);
 174         assertTrue(exitValue == 0);
 175 
 176         // test dry run with and without --add-modules m
 177         // no resolution failure
 178         exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(),
 179                          "-m", mid);
 180         assertTrue(exitValue == 0);
 181 
 182         exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(),
 183                          "--add-modules", M_MODULE,
 184                          "-m", mid);
 185         assertTrue(exitValue == 0);
 186     }
 187 
 188     /**
 189      * module m not found
 190      */
 191     public void testMissingModule() throws Exception {
 192         String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
 193         String mid = TEST_MODULE + "/" + MAIN_CLASS;
 194 
 195         // resolution failure
 196         int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid);
 197         assertTrue(exitValue != 0);
 198     }
 199 
 200     private static boolean jar(String name, String entries) throws IOException {
 201         Path jar = LIBS_DIR.resolve(name + ".jar");
 202 
 203         // jar --create ...
 204         String classes = MODS_DIR.resolve(name).toString();
 205         String[] args = {
 206             "--create",
 207             "--file=" + jar,
 208             "-C", classes, entries
 209         };
 210         boolean success
 211             = new sun.tools.jar.Main(System.out, System.out, "jar").run(args);
 212         return success;
 213     }
 214 }
< prev index next >