Path: blob/master/test/jdk/tools/launcher/modules/validate/ValidateModulesTest.java
41152 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8178380 819493726* @modules java.xml27* @library src /test/lib28* @build ValidateModulesTest hello/* jdk.test.lib.util.JarUtils29* @run testng ValidateModulesTest30* @summary Basic test for java --validate-modules31*/3233import java.io.File;34import java.io.IOException;35import java.nio.file.Files;36import java.nio.file.Path;3738import jdk.test.lib.process.ProcessTools;39import jdk.test.lib.process.OutputAnalyzer;40import jdk.test.lib.util.JarUtils;4142import org.testng.annotations.Test;43import static org.testng.Assert.*;4445@Test46public class ValidateModulesTest {4748/**49* Basic test --validate-modules when there are no errors.50*/51public void testNoErrors() throws Exception {52String modulePath = System.getProperty("test.module.path");5354test("--validate-modules");5556test("--validate-modules", "-version")57.shouldContain("Runtime Environment");5859test("--validate-modules", "--list-modules")60.shouldContain("java.base");6162test("--validate-modules", "-d", "java.base")63.shouldContain("exports java.lang");6465test("-p", modulePath, "-m", "hello/p.Main")66.shouldContain("Hello world");6768test("-p", modulePath, "--validate-modules", "-m", "hello/p.Main")69.shouldNotContain("Hello world");7071test("-p", modulePath, "--validate-modules", "--list-modules")72.shouldContain("hello");7374test("-p", modulePath, "--validate-modules", "-d", "hello")75.shouldContain("hello")76.shouldContain("contains p");7778testExpectingError("--validate-modules", "--add-modules", "BAD")79.shouldContain("Module BAD not found");8081testExpectingError("--validate-modules", "-m", "BAD")82.shouldContain("Module BAD not found");83}8485/**86* Test an automatic module on the module path with classes in the same87* package as a system module.88*/89public void testPackageConflict() throws Exception {90Path tmpdir = Files.createTempDirectory("tmp");9192Path classes = Files.createDirectory(tmpdir.resolve("classes"));93touch(classes, "javax/xml/XMLConstants.class");94touch(classes, "javax/xml/parsers/SAXParser.class");9596Path lib = Files.createDirectory(tmpdir.resolve("lib"));97JarUtils.createJarFile(lib.resolve("xml.jar"), classes);9899testExpectingError("-p", lib.toString(), "--validate-modules")100.shouldContain("xml automatic")101.shouldContain("conflicts with module java.xml");102}103104/**105* Test two modules with the same name in a directory.106*/107public void testDuplicateModule() throws Exception {108Path tmpdir = Files.createTempDirectory("tmp");109110Path classes = Files.createDirectory(tmpdir.resolve("classes"));111touch(classes, "org/foo/Bar.class");112113Path lib = Files.createDirectory(tmpdir.resolve("lib"));114JarUtils.createJarFile(lib.resolve("foo-1.0.jar"), classes);115JarUtils.createJarFile(lib.resolve("foo-2.0.jar"), classes);116117testExpectingError("-p", lib.toString(), "--validate-modules")118.shouldContain("contains same module");119}120121/**122* Test two modules with the same name in different directories.123*/124public void testShadowed() throws Exception {125Path tmpdir = Files.createTempDirectory("tmp");126127Path classes = Files.createDirectory(tmpdir.resolve("classes"));128touch(classes, "org/foo/Bar.class");129130Path lib1 = Files.createDirectory(tmpdir.resolve("lib1"));131JarUtils.createJarFile(lib1.resolve("foo-1.0.jar"), classes);132133Path lib2 = Files.createDirectory(tmpdir.resolve("lib2"));134JarUtils.createJarFile(lib2.resolve("foo-2.0.jar"), classes);135136test("-p", lib1 + File.pathSeparator + lib2, "--validate-modules")137.shouldContain("shadowed by");138}139140/**141* Runs the java launcher with the given arguments, expecting a 0 exit code142*/143private OutputAnalyzer test(String... args) throws Exception {144OutputAnalyzer analyzer = ProcessTools.executeTestJava(args)145.outputTo(System.out)146.errorTo(System.out);147assertTrue(analyzer.getExitValue() == 0);148return analyzer;149}150151/**152* Runs the java launcher with the given arguments, expecting a non-0 exit code153*/154private OutputAnalyzer testExpectingError(String... args) throws Exception {155OutputAnalyzer analyzer = ProcessTools.executeTestJava(args)156.outputTo(System.out)157.errorTo(System.out);158assertTrue(analyzer.getExitValue() != 0);159return analyzer;160}161162/**163* Creates a file relative the given directory.164*/165private void touch(Path dir, String relPath) throws IOException {166Path file = dir.resolve(relPath.replace('/', File.separatorChar));167Files.createDirectories(file.getParent());168Files.createFile(file);169}170}171172173