WhitespaceTest.java revision 2734:b96d74fa60aa
1/* 2 * Copyright (c) 2014, 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 8033581 8033798 8033726 27 * @summary Check whitespace in generated output 28 */ 29 30import java.io.*; 31import java.util.*; 32 33public class WhitespaceTest { 34 public static void main(String... args) throws Exception { 35 new WhitespaceTest().run(); 36 } 37 38 void run() throws Exception { 39 test("-v", "java.lang.String"); 40 test("-XDtab:1", "-v", "java.lang.String"); 41 42 String testClasses = System.getProperty("test.classes"); 43 for (int i = 10; i < 40; i++) 44 test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld"); 45 46 if (errors > 0) 47 throw new Exception(errors + " errors found"); 48 } 49 50 void test(String... args) throws Exception { 51 // need to avoid "//" appearing as a constant in the constant pool 52 String slash = "/"; 53 String doubleSlash = slash + slash; 54 System.out.println("test: " + Arrays.asList(args)); 55 String out = javap(args); 56 for (String line: out.split("[\r\n]+")) { 57 if (line.endsWith(" ")) 58 error("line has trailing whitespace: " + line); 59 int comment = line.indexOf(doubleSlash); 60 if (comment > 0 && line.charAt(comment - 1) != ' ' && !line.contains("file:///")) 61 error("no space before comment: " + line); 62 if (line.matches(" +}")) 63 error("bad indentation: " + line); 64 } 65 } 66 67 String javap(String... args) throws Exception { 68 StringWriter sw = new StringWriter(); 69 PrintWriter out = new PrintWriter(sw); 70 int rc = com.sun.tools.javap.Main.run(args, out); 71 out.close(); 72 System.out.println(sw.toString()); 73 if (rc < 0) 74 throw new Exception("javap exited, rc=" + rc); 75 return sw.toString(); 76 } 77 78 void error(String msg) { 79 System.out.println("Error: " + msg); 80 errors++; 81 } 82 83 int errors; 84 85 // small class to test repeatedly with different tab values 86 static class HelloWorld { 87 public static void main(String... args) { 88 System.out.println("Hello World!"); 89 } 90 } 91} 92 93 94