WhitespaceTest.java revision 2740:c956c25f9334
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) != ' ') { 61 // make allowance for URLs 62 if (!line.matches(".*\\bfile:/{3}.*")) 63 error("no space before comment: " + line); 64 } 65 if (line.matches(" +}")) 66 error("bad indentation: " + line); 67 } 68 } 69 70 String javap(String... args) throws Exception { 71 StringWriter sw = new StringWriter(); 72 PrintWriter out = new PrintWriter(sw); 73 int rc = com.sun.tools.javap.Main.run(args, out); 74 out.close(); 75 System.out.println(sw.toString()); 76 if (rc < 0) 77 throw new Exception("javap exited, rc=" + rc); 78 return sw.toString(); 79 } 80 81 void error(String msg) { 82 System.out.println("Error: " + msg); 83 errors++; 84 } 85 86 int errors; 87 88 // small class to test repeatedly with different tab values 89 static class HelloWorld { 90 public static void main(String... args) { 91 System.out.println("Hello World!"); 92 } 93 } 94} 95 96 97