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 --patch-module $MODULE=patches1/$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,
 102                                           "--patch-module", mn + "=" + src.toString());
 103         assertTrue(compiled, "classes did not compile");
 104 
 105         // javac --patch-module $MODULE=patches2/$MODULE -d patches2/$MODULE patches2/$MODULE/**
 106         src = SRC2_DIR.resolve("java.base");
 107         output = PATCHES2_DIR.resolve(src.getFileName());
 108         Files.createDirectories(output);
 109         mn = src.getFileName().toString();
 110         compiled  = CompilerUtils.compile(src, output,
 111                                           "--patch-module", mn + "=" + src.toString());
 112         assertTrue(compiled, "classes did not compile");
 113 
 114     }
 115 
 116     /**
 117      * Test with --patch-module options patching the same module
 118      */
 119     public void testDuplicateModule() throws Exception {
 120         int exitValue =
 121             executeTestJava("--patch-module", "java.base=" + PATCHES1_DIR.resolve("java.base"),
 122                             "--patch-module", "java.base=" + PATCHES2_DIR.resolve("java.base"),
 123                             "--module-path", MODS_DIR.toString(),
 124                             "-m", "test/jdk.test.Main")
 125                 .outputTo(System.out)
 126                 .errorTo(System.out)
 127                 // error output by VM
 128                 .shouldContain("Cannot specify java.base more than once to --patch-module")
 129                 .getExitValue();
 130 
 131         assertTrue(exitValue != 0);
 132     }
 133 
 134     @DataProvider(name = "emptyItem")
 135     public Object[][] emptyItems() {
 136         String patch1 = PATCHES1_DIR.resolve("java.base").toString();
 137         String patch2 = PATCHES2_DIR.resolve("java.base").toString();
 138         String pathSep = File.pathSeparator;
 139         return new Object[][]{
 140 
 141             { "java.base="+ pathSep + patch1 + pathSep + patch2,            null },
 142             { "java.base="+ patch1 + pathSep + pathSep + patch2,            null },
 143             { "java.base="+ patch1 + pathSep + patch2 + pathSep + pathSep,  null },
 144         };
 145     }
 146 
 147     /**
 148      * Empty item in a non-empty path list
 149      */
 150     @Test(dataProvider = "emptyItem")
 151     public void testEmptyItem(String value, String msg) throws Exception {
 152         // the argument to the test is the list of classes overridden or added
 153         String arg = Stream.of(CLASSES).collect(Collectors.joining(","));
 154 
 155         int exitValue =
 156             executeTestJava("--patch-module", value,
 157                             "--add-exports", "java.base/java.lang2=test",
 158                             "--module-path", MODS_DIR.toString(),
 159                             "-m", "test/jdk.test.Main", arg)
 160                 .outputTo(System.out)
 161                 .errorTo(System.out)
 162                 .getExitValue();
 163 
 164         assertTrue(exitValue == 0);
 165     }
 166 
 167     /**
 168      * Test bad module name that should emit a warning
 169      */
 170     public void testBadName() throws Exception {
 171         // the argument to the test is the list of classes overridden or added
 172         String arg = Stream.of(CLASSES).collect(Collectors.joining(","));
 173 
 174         int exitValue =
 175             executeTestJava("--patch-module", "DoesNotExist=tmp",
 176                             "--patch-module", "java.base=" + PATCHES_PATH,
 177                             "--add-exports", "java.base/java.lang2=test",
 178                             "--module-path", MODS_DIR.toString(),
 179                             "-m", "test/jdk.test.Main", arg)
 180                 .outputTo(System.out)
 181                 .errorTo(System.out)
 182                 .shouldContain("WARNING: Unknown module: DoesNotExist specified to --patch-module")
 183                 .getExitValue();
 184 
 185         assertTrue(exitValue == 0);
 186     }
 187 
 188     @DataProvider(name = "badArguments")
 189     public Object[][] badArguments() {
 190         return new Object[][]{
 191 
 192             // source not found
 193             { "=tmp",            "Unable to parse --patch-module <module>=<value>: =tmp" },
 194 
 195             // target not found: check by VM
 196             { "java.base",       "Missing '=' in --patch-module specification" },
 197             { "foo",             "Missing '=' in --patch-module specification" },
 198 
 199             // target not found
 200             { "java.base=",      "Unable to parse --patch-module <module>=<value>: java.base="  },
 201             { "java.base=" + File.pathSeparator,
 202               "Target must be specified: --patch-module java.base=" + File.pathSeparator }
 203         };
 204     }
 205 
 206     /**
 207      * Test ill-formed argument to --patch-module
 208      */
 209     @Test(dataProvider = "badArguments")
 210     public void testBadArgument(String value, String msg) throws Exception {
 211         int exitValue =
 212             executeTestJava("--patch-module", value,
 213                             "--module-path", MODS_DIR.toString(),
 214                             "-m", "test/jdk.test.Main")
 215                 .outputTo(System.out)
 216                 .errorTo(System.out)
 217                 .shouldContain(msg)
 218                 .getExitValue();
 219 
 220         assertTrue(exitValue != 0);
 221     }
 222 }