1 /* 2 * Copyright (c) 2018, 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 8199194 27 * @summary smoke test for enable-preview command line flag 28 * @modules jdk.compiler/com.sun.tools.javac.code 29 */ 30 31 import java.io.*; 32 import java.util.*; 33 import java.util.stream.Collectors; 34 import java.util.stream.Stream; 35 36 import com.sun.tools.javac.code.Source; 37 38 public class PreviewOptionTest { main(String... args)39 public static void main(String... args) throws Exception { 40 PreviewOptionTest t = new PreviewOptionTest(); 41 t.run(); 42 } 43 run()44 public void run() throws Exception { 45 try (FileWriter out = new FileWriter("Test.java")) { 46 out.write("class Test { }"); 47 } 48 49 testWithNoFlags(); 50 51 List<Source> versionsToTest = Stream.of(Source.values()) 52 .filter(s -> s.compareTo(Source.MIN) >= 0) 53 .collect(Collectors.toList()); 54 55 versionsToTest.forEach(this::testWithSourceFlag); 56 versionsToTest.forEach(this::testWithReleaseFlag); 57 58 if (errors > 0) 59 throw new Exception(errors + " errors occurred"); 60 } 61 testWithNoFlags()62 void testWithNoFlags() { 63 testInternal(null, null, true); 64 } 65 testWithSourceFlag(Source source)66 void testWithSourceFlag(Source source) { 67 testInternal(source, null, source != Source.DEFAULT); 68 } 69 testWithReleaseFlag(Source release)70 void testWithReleaseFlag(Source release) { 71 testInternal(null, release, release != Source.DEFAULT); 72 } 73 testInternal(Source source, Source release, boolean shouldFail)74 void testInternal(Source source, Source release, boolean shouldFail) { 75 System.err.println("Test: source:" + source + ", release:" + release + " " + shouldFail + " " + shouldFail); 76 List<String> args = new ArrayList<>(); 77 args.add("--enable-preview"); 78 if (source != null) { 79 args.add("-source"); 80 args.add(source.name); 81 } 82 if (release != null) { 83 args.add("--release"); 84 args.add(release.name); 85 } 86 args.add("Test.java"); 87 88 StringWriter sw = new StringWriter(); 89 PrintWriter pw = new PrintWriter(sw); 90 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); 91 pw.close(); 92 boolean hasErrors = rc != 0; 93 if (hasErrors != shouldFail) { 94 if (hasErrors) { 95 String out = sw.toString(); 96 error("error not expected but found:\n" + out); 97 } else { 98 error("error expected but not found"); 99 } 100 } 101 } 102 error(String msg)103 void error(String msg) { 104 System.err.println("error: " + msg); 105 errors++; 106 } 107 108 int errors; 109 } 110