1 /*
   2  * Copyright (c) 2014, 2015, 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 8067437
  27  * @summary Verify Multiple JRE version support has been removed.
  28  * @modules jdk.compiler
  29  *          jdk.zipfs
  30  * @build TestHelper
  31  * @run main MultipleJRERemoved
  32  */
  33 
  34 import java.io.File;
  35 import java.io.FileOutputStream;
  36 import java.io.IOException;
  37 import java.nio.file.Files;
  38 import java.util.*;
  39 import java.util.jar.Attributes;
  40 import java.util.jar.JarOutputStream;
  41 import java.util.jar.Manifest;
  42 import java.util.regex.Pattern;
  43 import java.util.stream.Collectors;
  44 import java.util.stream.Stream;
  45 import java.util.zip.ZipEntry;
  46 
  47 public class MultipleJRERemoved extends TestHelper {
  48 
  49     public static final String VERSION_JAR = "version.jar";
  50     public static final String PRINT_VERSION_CLASS = "PrintVersion";
  51     private final File javaFile = new File(PRINT_VERSION_CLASS + ".java");
  52     private final File clsFile = new File(PRINT_VERSION_CLASS + ".class");
  53 
  54     private MultipleJRERemoved() {
  55     }
  56 
  57     /**
  58      * @param args the command line arguments
  59      * @throws java.io.FileNotFoundException
  60      */
  61     public static void main(String[] args) throws Exception {
  62         MultipleJRERemoved a = new MultipleJRERemoved();
  63         a.run(args);
  64     }
  65 
  66     /**
  67      * Check all combinations of flags: "-version:", "-jre-restrict-search", "-jre-no-restrict-search". Test expects to see errors.
  68      */
  69     @Test
  70     public void allFlagCombinations() throws IOException {
  71         final Pattern newLine = Pattern.compile("\n");
  72         createJar(Collections.emptyMap());
  73 
  74         for (Flag flag1 : Flag.values()) {
  75             for (Flag flag2 : Flag.values()) {
  76                 for (Flag flag3 : Flag.values()) {
  77                     List<Flag> flags = Stream.of(flag1, flag2, flag3)
  78                             .filter(f -> !Flag.EMPTY.equals(f))
  79                             .collect(Collectors.toList());
  80 
  81                     if (flags.size() == 0) continue;
  82 
  83                     List<String> flagValues = flags.stream()
  84                             .map(Flag::value)
  85                             .collect(Collectors.toList());
  86 
  87                     List<String> errorMessages = flags.stream()
  88                             .map(Flag::errorMessage)
  89                             .flatMap(newLine::splitAsStream)
  90                             .collect(Collectors.toList());
  91 
  92                     List<String> jarCmd = new ArrayList<>();
  93                     jarCmd.add(javaCmd);
  94                     jarCmd.addAll(flagValues);
  95                     jarCmd.add("-jar");
  96                     jarCmd.add("version.jar");
  97 
  98                     check(jarCmd, errorMessages);
  99 
 100                     List<String> cmd = new ArrayList<>();
 101                     cmd.add(javaCmd);
 102                     cmd.addAll(flagValues);
 103                     cmd.add(PRINT_VERSION_CLASS);
 104 
 105                     check(cmd, errorMessages);
 106                 }
 107             }
 108         }
 109     }
 110 
 111     private void check(List<String> cmd, List<String> errorMessages) {
 112         TestResult tr = doExec(cmd.toArray(new String[cmd.size()]));
 113         tr.checkNegative();
 114         tr.isNotZeroOutput();
 115         errorMessages.forEach(tr::contains);
 116 
 117         if (!tr.testStatus) {
 118             System.out.println(tr);
 119             throw new RuntimeException("test case: failed\n" + cmd);
 120         }
 121     }
 122 
 123     /**
 124      * Verifies that java -help output doesn't contain information about "mJRE" flags.
 125      */
 126     @Test
 127     public void javaHelp() {
 128         TestResult tr = doExec(javaCmd, "-help");
 129         tr.checkPositive();
 130         tr.isNotZeroOutput();
 131         tr.notContains("-version:<value>");
 132         tr.notContains("-jre-restrict-search");
 133         tr.notContains("-jre-no-restrict-search");
 134         tr.notContains("-no-jre-restrict-search");  //it's not a typo in flag name.
 135         if (!tr.testStatus) {
 136             System.out.println(tr);
 137             throw new RuntimeException("Failed. java -help output contains obsolete flags.\n");
 138         }
 139     }
 140 
 141     /**
 142      * Verifies that java -jar version.jar output ignores "mJRE" manifest directives.
 143      */
 144     @Test
 145     public void manifestDirectives() throws IOException {
 146         Map<String, String> manifest = new TreeMap<>();
 147         manifest.put("JRE-Version", "1.8");
 148         manifest.put("JRE-Restrict-Search", "1.8");
 149         createJar(manifest);
 150 
 151         TestResult tr = doExec(javaCmd, "-jar", VERSION_JAR);
 152         tr.checkPositive();
 153         tr.contains(System.getProperty("java.version"));
 154         if (!tr.testStatus) {
 155             System.out.println(tr);
 156             throw new RuntimeException("Failed.\n");
 157         }
 158     }
 159 
 160     private void emitFile() throws IOException {
 161         List<String> scr = new ArrayList<>();
 162         scr.add("public class PrintVersion {");
 163         scr.add("    public static void main(String... args) {");
 164         scr.add("       System.out.println(System.getProperty(\"java.version\"));");
 165         scr.add("    }");
 166         scr.add("}");
 167         createFile(javaFile, scr);
 168         compile(javaFile.getName());
 169     }
 170 
 171     private void createJar(Map<String, String> manifestAttributes) throws IOException {
 172         emitFile();
 173 
 174         Manifest manifest = new Manifest();
 175         final Attributes mainAttributes = manifest.getMainAttributes();
 176         mainAttributes.putValue("Manifest-Version", "1.0");
 177         mainAttributes.putValue("Main-Class", PRINT_VERSION_CLASS);
 178         manifestAttributes.forEach(mainAttributes::putValue);
 179 
 180         try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(VERSION_JAR), manifest)) {
 181             jar.putNextEntry(new ZipEntry(PRINT_VERSION_CLASS + ".class"));
 182             jar.write(Files.readAllBytes(clsFile.toPath()));
 183             jar.closeEntry();
 184         } finally {
 185             javaFile.delete();
 186         }
 187     }
 188 
 189     private enum Flag {
 190         EMPTY("", ""),
 191         VERSION("-version:1.9", "Error: Specifying an alternate JDK/JRE version is no longer supported.\n" +
 192                 "The use of the flag '-version:' is no longer valid.\n" +
 193                 "Please download and execute the appropriate version."),
 194         JRE_RESTRICT_SEARCH("-jre-restrict-search", "Error: Specifying an alternate JDK/JRE is no longer supported.\n" +
 195                 "The related flags -jre-restrict-search | -jre-no-restrict-search are also no longer valid."),
 196         JRE_NO_RESTRICT_SEARCH("-jre-no-restrict-search", "Error: Specifying an alternate JDK/JRE is no longer supported.\n" +
 197                 "The related flags -jre-restrict-search | -jre-no-restrict-search are also no longer valid.");
 198         private final String flag;
 199         private final String errorMessage;
 200 
 201         Flag(String flag, String errorMessage) {
 202             this.flag = flag;
 203             this.errorMessage = errorMessage;
 204         }
 205 
 206         String value() {
 207             return flag;
 208         }
 209 
 210         String errorMessage() {
 211             return errorMessage;
 212         }
 213     }
 214 }