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