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