test/langtools/tools/javac/options/BCPOrSystemNotSpecified.java
changeset 58203 dfd434203aa0
equal deleted inserted replaced
58202:a45cce906207 58203:dfd434203aa0
       
     1 /*
       
     2  * Copyright (c) 2019, 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 8228460
       
    27  * @summary Verify --system is required rather than -bootclasspath for -source 9.
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  * @build toolbox.ToolBox toolbox.JavacTask toolbox.TestRunner
       
    32  * @run main BCPOrSystemNotSpecified
       
    33  */
       
    34 
       
    35 import java.io.IOException;
       
    36 import java.nio.file.Path;
       
    37 import java.nio.file.Paths;
       
    38 import java.util.Arrays;
       
    39 import java.util.List;
       
    40 
       
    41 import java.io.InputStream;
       
    42 import java.nio.file.Files;
       
    43 import java.util.EnumSet;
       
    44 import javax.tools.JavaFileManager;
       
    45 import javax.tools.JavaFileObject;
       
    46 import javax.tools.StandardLocation;
       
    47 import javax.tools.ToolProvider;
       
    48 import toolbox.JavacTask;
       
    49 import toolbox.Task;
       
    50 import toolbox.Task.Expect;
       
    51 import toolbox.TestRunner;
       
    52 import toolbox.ToolBox;
       
    53 
       
    54 public class BCPOrSystemNotSpecified extends TestRunner {
       
    55 
       
    56     private final ToolBox tb = new ToolBox();
       
    57     private final String fileSep = System.getProperty("file.separator");
       
    58 
       
    59     public BCPOrSystemNotSpecified() {
       
    60         super(System.err);
       
    61     }
       
    62 
       
    63     public static void main(String... args) throws Exception {
       
    64         new BCPOrSystemNotSpecified().runTests();
       
    65     }
       
    66 
       
    67     @Test
       
    68     public void testSource8(Path base) throws IOException {
       
    69         Path src = base.resolve("src");
       
    70         tb.writeJavaFiles(src,
       
    71                           "package test; public class Test { } ");
       
    72         Path classes = base.resolve("classes");
       
    73         tb.createDirectories(classes);
       
    74 
       
    75         List<String> log;
       
    76         List<String> expected = Arrays.asList(
       
    77                 "- compiler.warn.source.no.bootclasspath: 8",
       
    78                 "1 warning"
       
    79         );
       
    80 
       
    81         log = new JavacTask(tb)
       
    82                 .options("-XDrawDiagnostics", "-source", "8")
       
    83                 .outdir(classes)
       
    84                 .files(tb.findJavaFiles(src))
       
    85                 .run(Expect.SUCCESS)
       
    86                 .writeAll()
       
    87                 .getOutputLines(Task.OutputKind.DIRECT);
       
    88 
       
    89         if (!expected.equals(log)) {
       
    90             throw new AssertionError("Unexpected output: " + log);
       
    91         }
       
    92 
       
    93         Path bcp = base.resolve("bcp");
       
    94 
       
    95         prepareBCP(bcp);
       
    96 
       
    97         new JavacTask(tb)
       
    98                 .options("-XDrawDiagnostics",
       
    99                          "-source", "8",
       
   100                          "-bootclasspath", bcp.toAbsolutePath().toString(),
       
   101                          "-Werror")
       
   102                 .outdir(classes)
       
   103                 .files(tb.findJavaFiles(src))
       
   104                 .run(Expect.SUCCESS)
       
   105                 .writeAll()
       
   106                 .getOutputLines(Task.OutputKind.DIRECT);
       
   107 
       
   108         if (!expected.equals(log)) {
       
   109             throw new AssertionError("Unexpected output: " + log);
       
   110         }
       
   111     }
       
   112 
       
   113     @Test
       
   114     public void testSource9(Path base) throws IOException {
       
   115         Path src = base.resolve("src");
       
   116         tb.writeJavaFiles(src,
       
   117                           "package test; public class Test { } ");
       
   118         Path classes = base.resolve("classes");
       
   119         tb.createDirectories(classes);
       
   120 
       
   121         List<String> log;
       
   122         List<String> expected = Arrays.asList(
       
   123                 "- compiler.warn.source.no.system.modules.path: 9",
       
   124                 "1 warning"
       
   125         );
       
   126 
       
   127         log = new JavacTask(tb)
       
   128                 .options("-XDrawDiagnostics",
       
   129                          "-source", "9")
       
   130                 .outdir(classes)
       
   131                 .files(tb.findJavaFiles(src))
       
   132                 .run(Expect.SUCCESS)
       
   133                 .writeAll()
       
   134                 .getOutputLines(Task.OutputKind.DIRECT);
       
   135 
       
   136         if (!expected.equals(log)) {
       
   137             throw new AssertionError("Unexpected output: " + log);
       
   138         }
       
   139 
       
   140         Path bcp = base.resolve("bcp");
       
   141 
       
   142         prepareBCP(bcp);
       
   143 
       
   144         log = new JavacTask(tb)
       
   145                 .options("-XDrawDiagnostics",
       
   146                          "-source", "9",
       
   147                          "-bootclasspath", bcp.toAbsolutePath().toString())
       
   148                 .outdir(classes)
       
   149                 .files(tb.findJavaFiles(src))
       
   150                 .run(Expect.SUCCESS)
       
   151                 .writeAll()
       
   152                 .getOutputLines(Task.OutputKind.DIRECT);
       
   153 
       
   154         if (!expected.equals(log)) {
       
   155             throw new AssertionError("Unexpected output: " + log);
       
   156         }
       
   157 
       
   158         new JavacTask(tb)
       
   159                 .options("-XDrawDiagnostics",
       
   160                          "-source", "9",
       
   161                          "--system", "none",
       
   162                          "--module-path", bcp.toAbsolutePath().toString(),
       
   163                          "-Werror")
       
   164                 .outdir(classes)
       
   165                 .files(tb.findJavaFiles(src))
       
   166                 .run(Expect.SUCCESS)
       
   167                 .writeAll()
       
   168                 .getOutputLines(Task.OutputKind.DIRECT);
       
   169 
       
   170         if (!expected.equals(log)) {
       
   171             throw new AssertionError("Unexpected output: " + log);
       
   172         }
       
   173 
       
   174         new JavacTask(tb)
       
   175                 .options("-XDrawDiagnostics",
       
   176                          "-source", "9",
       
   177                          "--system", System.getProperty("java.home"),
       
   178                          "-Werror")
       
   179                 .outdir(classes)
       
   180                 .files(tb.findJavaFiles(src))
       
   181                 .run(Expect.SUCCESS)
       
   182                 .writeAll()
       
   183                 .getOutputLines(Task.OutputKind.DIRECT);
       
   184 
       
   185         if (!expected.equals(log)) {
       
   186             throw new AssertionError("Unexpected output: " + log);
       
   187         }
       
   188     }
       
   189 
       
   190     protected void runTests() throws Exception {
       
   191         runTests(m -> new Object[] { Paths.get(m.getName()).toAbsolutePath() });
       
   192     }
       
   193 
       
   194     private void prepareBCP(Path target) throws IOException {
       
   195         try (JavaFileManager jfm = ToolProvider.getSystemJavaCompiler()
       
   196                                                .getStandardFileManager(null, null, null)) {
       
   197             for (String pack : new String[] {"", "java.lang", "java.lang.annotation"}) {
       
   198                 JavaFileManager.Location javaBase =
       
   199                         jfm.getLocationForModule(StandardLocation.SYSTEM_MODULES,
       
   200                                                  "java.base");
       
   201                 for (JavaFileObject file : jfm.list(javaBase,
       
   202                                                     pack,
       
   203                                                     EnumSet.of(JavaFileObject.Kind.CLASS),
       
   204                                                     false)) {
       
   205                     Path targetDir = target.resolve(pack.replace(".", fileSep));
       
   206                     Files.createDirectories(targetDir);
       
   207                     try (InputStream in = file.openInputStream()) {
       
   208                         String sourcePath = file.getName();
       
   209                         int sepPos = sourcePath.lastIndexOf(fileSep);
       
   210                         String fileName = sourcePath.substring(sepPos + 1);
       
   211                         Files.copy(in, targetDir.resolve(fileName));
       
   212                     }
       
   213                 }
       
   214             }
       
   215         }
       
   216     }
       
   217 }