Path: blob/master/test/jdk/tools/launcher/MultipleJRERemoved.java
41144 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 806743726* @summary Verify Multiple JRE version support has been removed.27* @modules jdk.compiler28* jdk.zipfs29* @build TestHelper30* @run main MultipleJRERemoved31*/3233import java.io.File;34import java.io.FileOutputStream;35import java.io.IOException;36import java.nio.file.Files;37import java.util.*;38import java.util.jar.Attributes;39import java.util.jar.JarOutputStream;40import java.util.jar.Manifest;41import java.util.regex.Pattern;42import java.util.stream.Collectors;43import java.util.stream.Stream;44import java.util.zip.ZipEntry;4546public class MultipleJRERemoved extends TestHelper {4748public static final String VERSION_JAR = "version.jar";49public static final String PRINT_VERSION_CLASS = "PrintVersion";50private final File javaFile = new File(PRINT_VERSION_CLASS + ".java");51private final File clsFile = new File(PRINT_VERSION_CLASS + ".class");5253private MultipleJRERemoved() {54}5556/**57* @param args the command line arguments58* @throws java.io.FileNotFoundException59*/60public static void main(String[] args) throws Exception {61MultipleJRERemoved a = new MultipleJRERemoved();62a.run(args);63}6465/**66* Check all combinations of flags: "-version:", "-jre-restrict-search", "-jre-no-restrict-search". Test expects to see errors.67*/68@Test69public void allFlagCombinations() throws IOException {70final Pattern newLine = Pattern.compile("\n");71createJar(Collections.emptyMap());7273for (Flag flag1 : Flag.values()) {74for (Flag flag2 : Flag.values()) {75for (Flag flag3 : Flag.values()) {76List<Flag> flags = Stream.of(flag1, flag2, flag3)77.filter(f -> !Flag.EMPTY.equals(f))78.collect(Collectors.toList());7980if (flags.size() == 0) continue;8182List<String> flagValues = flags.stream()83.map(Flag::value)84.collect(Collectors.toList());8586List<String> errorMessages = flags.stream()87.map(Flag::errorMessage)88.flatMap(newLine::splitAsStream)89.collect(Collectors.toList());9091List<String> jarCmd = new ArrayList<>();92jarCmd.add(javaCmd);93jarCmd.addAll(flagValues);94jarCmd.add("-jar");95jarCmd.add("version.jar");9697check(jarCmd, errorMessages);9899List<String> cmd = new ArrayList<>();100cmd.add(javaCmd);101cmd.addAll(flagValues);102cmd.add(PRINT_VERSION_CLASS);103104check(cmd, errorMessages);105}106}107}108}109110private void check(List<String> cmd, List<String> errorMessages) {111TestResult tr = doExec(cmd.toArray(new String[cmd.size()]));112tr.checkNegative();113tr.isNotZeroOutput();114errorMessages.forEach(tr::contains);115116if (!tr.testStatus) {117System.out.println(tr);118throw new RuntimeException("test case: failed\n" + cmd);119}120}121122/**123* Verifies that java -help output doesn't contain information about "mJRE" flags.124*/125@Test126public void javaHelp() {127TestResult tr = doExec(javaCmd, "-help");128tr.checkPositive();129tr.isNotZeroOutput();130tr.notContains("-version:<value>");131tr.notContains("-jre-restrict-search");132tr.notContains("-jre-no-restrict-search");133tr.notContains("-no-jre-restrict-search"); //it's not a typo in flag name.134if (!tr.testStatus) {135System.out.println(tr);136throw new RuntimeException("Failed. java -help output contains obsolete flags.\n");137}138}139140/**141* Verifies that java -jar version.jar output ignores "mJRE" manifest directives.142*/143@Test144public void manifestDirectives() throws IOException {145Map<String, String> manifest = new TreeMap<>();146manifest.put("JRE-Version", "1.8");147manifest.put("JRE-Restrict-Search", "1.8");148createJar(manifest);149150TestResult tr = doExec(javaCmd, "-jar", VERSION_JAR);151tr.checkPositive();152tr.contains(System.getProperty("java.version"));153if (!tr.testStatus) {154System.out.println(tr);155throw new RuntimeException("Failed.\n");156}157}158159private void emitFile() throws IOException {160List<String> scr = new ArrayList<>();161scr.add("public class PrintVersion {");162scr.add(" public static void main(String... args) {");163scr.add(" System.out.println(System.getProperty(\"java.version\"));");164scr.add(" }");165scr.add("}");166createFile(javaFile, scr);167compile(javaFile.getName());168}169170private void createJar(Map<String, String> manifestAttributes) throws IOException {171emitFile();172173Manifest manifest = new Manifest();174final Attributes mainAttributes = manifest.getMainAttributes();175mainAttributes.putValue("Manifest-Version", "1.0");176mainAttributes.putValue("Main-Class", PRINT_VERSION_CLASS);177manifestAttributes.forEach(mainAttributes::putValue);178179try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(VERSION_JAR), manifest)) {180jar.putNextEntry(new ZipEntry(PRINT_VERSION_CLASS + ".class"));181jar.write(Files.readAllBytes(clsFile.toPath()));182jar.closeEntry();183} finally {184javaFile.delete();185}186}187188private enum Flag {189EMPTY("", ""),190VERSION("-version:1.9", "Error: Specifying an alternate JDK/JRE version is no longer supported.\n" +191"The use of the flag '-version:' is no longer valid.\n" +192"Please download and execute the appropriate version."),193JRE_RESTRICT_SEARCH("-jre-restrict-search", "Error: Specifying an alternate JDK/JRE is no longer supported.\n" +194"The related flags -jre-restrict-search | -jre-no-restrict-search are also no longer valid."),195JRE_NO_RESTRICT_SEARCH("-jre-no-restrict-search", "Error: Specifying an alternate JDK/JRE is no longer supported.\n" +196"The related flags -jre-restrict-search | -jre-no-restrict-search are also no longer valid.");197private final String flag;198private final String errorMessage;199200Flag(String flag, String errorMessage) {201this.flag = flag;202this.errorMessage = errorMessage;203}204205String value() {206return flag;207}208209String errorMessage() {210return errorMessage;211}212}213}214215216