1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8159596
  27  * @library /lib/testlibrary
  28  * @modules jdk.compiler
  29  *          jdk.jartool/sun.tools.jar
  30  * @build DryRunTest CompilerUtils jdk.testlibrary.ProcessTools
  31  * @run testng DryRunTest
  32  * @summary Test java --dry-run
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 
  41 import jdk.testlibrary.ProcessTools;
  42 
  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 
 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 }