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