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 8168836
  27  * @summary Basic argument validation for --patch-module
  28  * @library /lib/testlibrary
  29  * @modules jdk.compiler
  30  * @build PatchTestWarningError CompilerUtils JarUtils jdk.testlibrary.*
  31  * @run testng PatchTestWarningError
  32  */
  33 
  34 import java.io.File;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.stream.Collectors;
  39 import java.util.stream.Stream;
  40 
  41 import static jdk.testlibrary.ProcessTools.*;
  42 
  43 import org.testng.annotations.BeforeTest;
  44 import org.testng.annotations.DataProvider;
  45 import org.testng.annotations.Test;
  46 import static org.testng.Assert.*;
  47 
  48 
  49 /**
  50  * This test
  51  * See PatchTestWarningError for test description.
  52  */
  53 
  54 @Test
  55 public class PatchTestWarningError {
  56 
  57     // top-level source directory
  58     private static final String TEST_SRC = System.getProperty("test.src");
  59 
  60     // source/destination tree for the test module
  61     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  62     private static final Path MODS_DIR = Paths.get("mods");
  63 
  64     // source/destination tree for patch tree 1
  65     private static final Path SRC1_DIR = Paths.get(TEST_SRC, "src1");
  66     private static final Path PATCHES1_DIR = Paths.get("patches1");
  67 
  68     // source/destination tree for patch tree 2
  69     private static final Path SRC2_DIR = Paths.get(TEST_SRC, "src2");
  70     private static final Path PATCHES2_DIR = Paths.get("patches2");
  71 
  72     // patch path for java.base
  73     private static final String PATCHES_PATH =
  74         PATCHES1_DIR.resolve("java.base") + File.pathSeparator +
  75             PATCHES2_DIR.resolve("java.base");
  76 
  77     // the classes overridden or added with --patch-module
  78     private static final String[] CLASSES = {
  79 
  80         // java.base = boot loader
  81         "java.base/java.text.Annotation",           // override class
  82         "java.base/java.text.AnnotationBuddy",      // add class to package
  83         "java.base/java.lang2.Object",              // new package
  84 
  85     };
  86 
  87 
  88     @BeforeTest
  89     public void setup() throws Exception {
  90 
  91         // javac -d mods/test src/test/**
  92         boolean compiled= CompilerUtils.compile(SRC_DIR.resolve("test"),
  93                                                 MODS_DIR.resolve("test"));
  94         assertTrue(compiled, "classes did not compile");
  95 
  96         // javac -Xmodule:$MODULE -d patches1/$MODULE patches1/$MODULE/**
  97         Path src = SRC1_DIR.resolve("java.base");
  98         Path output = PATCHES1_DIR.resolve(src.getFileName());
  99         Files.createDirectories(output);
 100         String mn = src.getFileName().toString();
 101         compiled  = CompilerUtils.compile(src, output, "-Xmodule:" + mn);
 102         assertTrue(compiled, "classes did not compile");
 103 
 104         // javac -Xmodule:$MODULE -d patches2/$MODULE patches2/$MODULE/**
 105         src = SRC2_DIR.resolve("java.base");
 106         output = PATCHES2_DIR.resolve(src.getFileName());
 107         Files.createDirectories(output);
 108         mn = src.getFileName().toString();
 109         compiled  = CompilerUtils.compile(src, output, "-Xmodule:" + mn);
 110         assertTrue(compiled, "classes did not compile");
 111 
 112     }
 113 
 114     /**
 115      * Test with --patch-module options patching the same module
 116      */
 117     public void testDuplicateModule() throws Exception {
 118         int exitValue =
 119             executeTestJava("--patch-module", "java.base=" + PATCHES1_DIR.resolve("java.base"),
 120                             "--patch-module", "java.base=" + PATCHES2_DIR.resolve("java.base"),
 121                             "--module-path", MODS_DIR.toString(),
 122                             "-m", "test/jdk.test.Main")
 123                 .outputTo(System.out)
 124                 .errorTo(System.out)
 125                 // error output by VM
 126                 .shouldContain("Cannot specify java.base more than once to --patch-module")
 127                 .getExitValue();
 128 
 129         assertTrue(exitValue != 0);
 130     }
 131 
 132     @DataProvider(name = "emptyItem")
 133     public Object[][] emptyItems() {
 134         String patch1 = PATCHES1_DIR.resolve("java.base").toString();
 135         String patch2 = PATCHES2_DIR.resolve("java.base").toString();
 136         String pathSep = File.pathSeparator;
 137         return new Object[][]{
 138 
 139             { "java.base="+ pathSep + patch1 + pathSep + patch2,            null },
 140             { "java.base="+ patch1 + pathSep + pathSep + patch2,            null },
 141             { "java.base="+ patch1 + pathSep + patch2 + pathSep + pathSep,  null },
 142         };
 143     }
 144 
 145     /**
 146      * Empty item in a non-empty path list
 147      */
 148     @Test(dataProvider = "emptyItem")
 149     public void testEmptyItem(String value, String msg) throws Exception {
 150         // the argument to the test is the list of classes overridden or added
 151         String arg = Stream.of(CLASSES).collect(Collectors.joining(","));
 152 
 153         int exitValue =
 154             executeTestJava("--patch-module", value,
 155                             "--add-exports", "java.base/java.lang2=test",
 156                             "--module-path", MODS_DIR.toString(),
 157                             "-m", "test/jdk.test.Main", arg)
 158                 .outputTo(System.out)
 159                 .errorTo(System.out)
 160                 .getExitValue();
 161 
 162         assertTrue(exitValue == 0);
 163     }
 164 
 165     /**
 166      * Test bad module name that should emit a warning
 167      */
 168     public void testBadName() throws Exception {
 169         // the argument to the test is the list of classes overridden or added
 170         String arg = Stream.of(CLASSES).collect(Collectors.joining(","));
 171 
 172         int exitValue =
 173             executeTestJava("--patch-module", "DoesNotExist=tmp",
 174                             "--patch-module", "java.base=" + PATCHES_PATH,
 175                             "--add-exports", "java.base/java.lang2=test",
 176                             "--module-path", MODS_DIR.toString(),
 177                             "-m", "test/jdk.test.Main", arg)
 178                 .outputTo(System.out)
 179                 .errorTo(System.out)
 180                 .shouldContain("WARNING: Unknown module: DoesNotExist specified in --patch-module")
 181                 .getExitValue();
 182 
 183         assertTrue(exitValue == 0);
 184     }
 185 
 186     @DataProvider(name = "badArguments")
 187     public Object[][] badArguments() {
 188         return new Object[][]{
 189 
 190             // source not found
 191             { "=tmp",            "Unable to parse --patch-module <module>=<value>: =tmp" },
 192 
 193             // target not found: check by VM
 194             { "java.base",       "Missing '=' in --patch-module specification" },
 195             { "foo",             "Missing '=' in --patch-module specification" },
 196 
 197             // target not found
 198             { "java.base=",      "Unable to parse --patch-module <module>=<value>: java.base="  },
 199             { "java.base=" + File.pathSeparator,
 200               "Target must be specified: --patch-module java.base=" + File.pathSeparator }
 201         };
 202     }
 203 
 204     /**
 205      * Test ill-formed argument to --patch-module
 206      */
 207     @Test(dataProvider = "badArguments")
 208     public void testBadArgument(String value, String msg) throws Exception {
 209         int exitValue =
 210             executeTestJava("--patch-module", value,
 211                             "--module-path", MODS_DIR.toString(),
 212                             "-m", "test/jdk.test.Main")
 213                 .outputTo(System.out)
 214                 .errorTo(System.out)
 215                 .shouldContain(msg)
 216                 .getExitValue();
 217 
 218         assertTrue(exitValue != 0);
 219     }
 220 }