1 /*
   2  * Copyright (c) 2017, 2018, 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 8178380 8194937
  27  * @modules java.xml
  28  * @library src /test/lib
  29  * @build ValidateModulesTest hello/* jdk.test.lib.util.JarUtils
  30  * @run testng ValidateModulesTest
  31  * @summary Basic test for java --validate-modules
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 
  39 import jdk.test.lib.process.ProcessTools;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.util.JarUtils;
  42 
  43 import org.testng.annotations.Test;
  44 import static org.testng.Assert.*;
  45 
  46 @Test
  47 public class ValidateModulesTest {
  48 
  49     /**
  50      * Basic test --validate-modules when there are no errors.
  51      */
  52     public void testNoErrors() throws Exception {
  53         String modulePath = System.getProperty("test.module.path");
  54 
  55         test("--validate-modules");
  56 
  57         test("--validate-modules", "-version")
  58                 .shouldContain("Runtime Environment");
  59 
  60         test("--validate-modules", "--list-modules")
  61                 .shouldContain("java.base");
  62 
  63         test("--validate-modules", "-d", "java.base")
  64                 .shouldContain("exports java.lang");
  65 
  66         test("-p", modulePath, "-m", "hello/p.Main")
  67                 .shouldContain("Hello world");
  68 
  69         test("-p", modulePath, "--validate-modules", "-m", "hello/p.Main")
  70                 .shouldNotContain("Hello world");
  71 
  72         test("-p", modulePath, "--validate-modules", "--list-modules")
  73                 .shouldContain("hello");
  74 
  75         test("-p", modulePath, "--validate-modules", "-d", "hello")
  76                 .shouldContain("hello")
  77                 .shouldContain("contains p");
  78 
  79         testExpectingError("--validate-modules", "--add-modules", "BAD")
  80                 .shouldContain("Module BAD not found");
  81 
  82         testExpectingError("--validate-modules", "-m", "BAD")
  83                 .shouldContain("Module BAD not found");
  84     }
  85 
  86     /**
  87      * Test an automatic module on the module path with classes in the same
  88      * package as a system module.
  89      */
  90     public void testPackageConflict() throws Exception {
  91         Path tmpdir = Files.createTempDirectory("tmp");
  92 
  93         Path classes = Files.createDirectory(tmpdir.resolve("classes"));
  94         touch(classes, "javax/xml/XMLConstants.class");
  95         touch(classes, "javax/xml/parsers/SAXParser.class");
  96 
  97         Path lib = Files.createDirectory(tmpdir.resolve("lib"));
  98         JarUtils.createJarFile(lib.resolve("xml.jar"), classes);
  99 
 100         testExpectingError("-p", lib.toString(), "--validate-modules")
 101                 .shouldContain("xml automatic")
 102                 .shouldContain("conflicts with module java.xml");
 103     }
 104 
 105     /**
 106      * Test two modules with the same name in a directory.
 107      */
 108     public void testDuplicateModule() throws Exception {
 109         Path tmpdir = Files.createTempDirectory("tmp");
 110 
 111         Path classes = Files.createDirectory(tmpdir.resolve("classes"));
 112         touch(classes, "org/foo/Bar.class");
 113 
 114         Path lib = Files.createDirectory(tmpdir.resolve("lib"));
 115         JarUtils.createJarFile(lib.resolve("foo-1.0.jar"), classes);
 116         JarUtils.createJarFile(lib.resolve("foo-2.0.jar"), classes);
 117 
 118         testExpectingError("-p", lib.toString(), "--validate-modules")
 119                 .shouldContain("contains same module");
 120     }
 121 
 122     /**
 123      * Test two modules with the same name in different directories.
 124      */
 125     public void testShadowed() throws Exception {
 126         Path tmpdir = Files.createTempDirectory("tmp");
 127 
 128         Path classes = Files.createDirectory(tmpdir.resolve("classes"));
 129         touch(classes, "org/foo/Bar.class");
 130 
 131         Path lib1 = Files.createDirectory(tmpdir.resolve("lib1"));
 132         JarUtils.createJarFile(lib1.resolve("foo-1.0.jar"), classes);
 133 
 134         Path lib2 = Files.createDirectory(tmpdir.resolve("lib2"));
 135         JarUtils.createJarFile(lib2.resolve("foo-2.0.jar"), classes);
 136 
 137         test("-p", lib1 + File.pathSeparator + lib2, "--validate-modules")
 138                 .shouldContain("shadowed by");
 139     }
 140 
 141     /**
 142      * Runs the java launcher with the given arguments, expecting a 0 exit code
 143      */
 144     private OutputAnalyzer test(String... args) throws Exception {
 145         OutputAnalyzer analyzer = ProcessTools.executeTestJava(args)
 146                 .outputTo(System.out)
 147                 .errorTo(System.out);
 148         assertTrue(analyzer.getExitValue() == 0);
 149         return analyzer;
 150     }
 151 
 152     /**
 153      * Runs the java launcher with the given arguments, expecting a non-0 exit code
 154      */
 155     private OutputAnalyzer testExpectingError(String... args) throws Exception {
 156         OutputAnalyzer analyzer = ProcessTools.executeTestJava(args)
 157                 .outputTo(System.out)
 158                 .errorTo(System.out);
 159         assertTrue(analyzer.getExitValue() != 0);
 160         return analyzer;
 161     }
 162 
 163     /**
 164      * Creates a file relative the given directory.
 165      */
 166     private void touch(Path dir, String relPath) throws IOException {
 167         Path file = dir.resolve(relPath.replace('/', File.separatorChar));
 168         Files.createDirectories(file.getParent());
 169         Files.createFile(file);
 170     }
 171 }