StreamsTest.java revision 3560:bbf4cfc235bd
1/* 2 * Copyright (c) 2016, 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 8162359 27 * @summary extra space in javac -help for -J and @ options 28 * @modules jdk.compiler 29 * @library /tools/lib 30 * @build toolbox.TestRunner toolbox.ToolBox 31 * @run main StreamsTest 32 */ 33 34import java.io.ByteArrayOutputStream; 35import java.io.IOException; 36import java.io.PrintStream; 37import java.nio.file.Path; 38import java.nio.file.Paths; 39import java.util.List; 40 41import static java.util.Arrays.asList; 42 43import toolbox.TestRunner; 44import toolbox.ToolBox; 45 46public class StreamsTest extends TestRunner { 47 public static void main(String... args) throws Exception { 48 new StreamsTest().runTests(m -> new Object[] { Paths.get(m.getName()) }); 49 } 50 51 StreamsTest() { 52 super(System.err); 53 } 54 55 ToolBox tb = new ToolBox(); 56 static final String LINESEP = System.getProperty("line.separator"); 57 58 @Test // errors should be written to stderr 59 public void testError(Path base) throws Exception { 60 Path src = base.resolve("src"); 61 Path classes = base.resolve("classes"); 62 tb.writeJavaFiles(src, 63 "import java.util.*; class C { # }"); 64 test(asList("-d", classes.toString(), src.resolve("C.java").toString()), 65 null, "illegal character: '#'"); 66 } 67 68 @Test // warnings should be written to stderr 69 public void testWarning(Path base) throws Exception { 70 Path src = base.resolve("src"); 71 Path classes = base.resolve("classes"); 72 tb.writeJavaFiles(src, 73 "import java.util.*; class C { List list = new ArrayList(); }"); 74 test(asList("-d", classes.toString(), "-Xlint", src.resolve("C.java").toString()), 75 null, "warning: [rawtypes]"); 76 } 77 78 @Test // notes should be written to stderr 79 public void testNote(Path base) throws Exception { 80 Path src = base.resolve("src"); 81 Path classes = base.resolve("classes"); 82 tb.writeJavaFiles(src, 83 "import java.util.*; class C { List<String> list = (List<String>) new ArrayList(); }"); 84 test(asList("-d", classes.toString(), src.resolve("C.java").toString()), 85 null, "uses unchecked or unsafe operations."); 86 } 87 88 @Test // help output should be written to stdout 89 public void testHelp(Path base) throws Exception { 90 test(asList("-help"), "Usage: javac <options> <source files>", null); 91 } 92 93 @Test // version output should be written to stdout 94 public void testVersion(Path base) throws Exception { 95 test(asList("-version"), "javac", null); 96 } 97 98 @Test // version output should be written to stdout 99 public void testFullVersion(Path base) throws Exception { 100 test(asList("-fullversion"), "javac full version", null); 101 } 102 103 /** 104 * Run javac as though run from the command line (but avoiding the entry point that 105 * calls System.exit()), and that that expected output appears on appropriate output streams. 106 * @param options the command-line options for javac 107 * @param expectOut a string that should be contained in the output generated on stdout, 108 * or null, if no output should be generated to stdout 109 * @param expectErra string that should be contained in the output generated on stderr, 110 * or null, if no output should be generated to stderr 111 * @throws IOException if a problem occurs while setting up the streams 112 */ 113 void test(List<String> options, String expectOut, String expectErr) throws IOException { 114 out.println("test " + options); 115 ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); 116 ByteArrayOutputStream bsErr = new ByteArrayOutputStream(); 117 try (PrintStream psOut = new PrintStream(bsOut); PrintStream psErr = new PrintStream(bsErr)) { 118 int rc; 119 PrintStream saveOut = System.out; 120 PrintStream saveErr = System.err; 121 try { 122 System.setOut(psOut); 123 System.setErr(psErr); 124 rc = com.sun.tools.javac.Main.compile(options.toArray(new String[0])); 125 } finally { 126 System.setErr(saveErr); 127 System.setOut(saveOut); 128 } 129 System.err.println("javac exit code: " + rc); 130 } 131 check("stdout", bsOut.toString(), expectOut); 132 check("stderr", bsErr.toString(), expectErr); 133 } 134 135 /** 136 * Check that output is as expected. 137 * @param name the name of the stream on which the output was found 138 * @param actual the contents written to the stream 139 * @param expect string that should be contained in the output, or null, if the output should be empty 140 */ 141 void check(String name, String actual, String expect) { 142 out.println("Check " + name); 143 out.println("Expected: " + (expect == null ? "(nothing)" : expect)); 144 out.println("Actual:"); 145 out.println(actual.replace("\n", LINESEP)); 146 if (expect == null) { 147 if (!actual.isEmpty()) { 148 error(name + ": unexpected output"); 149 } 150 } else if (!actual.contains(expect)) { 151 error(name + ": expected output not found"); 152 } 153 } 154} 155 156