WhitespaceTest.java revision 2942:08092deced3f
11590Srgrimes/*
265910Sache * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4160750Syar *
5160750Syar * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
739261Sgibbs * published by the Free Software Foundation.
8253360Sglebius *
9303684Smr * This code is distributed in the hope that it will be useful, but WITHOUT
10108684Sphk * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11160750Syar * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12160750Syar * version 2 for more details (a copy is included in the LICENSE file that
13160750Syar * accompanied this code).
14160750Syar *
15160750Syar * You should have received a copy of the GNU General Public License version
16160750Syar * 2 along with this work; if not, write to the Free Software Foundation,
17201386Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18201386Sed *
19262643Sbrooks * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20262643Sbrooks * or visit www.oracle.com if you need additional information or have any
211590Srgrimes * questions.
22262643Sbrooks */
23262643Sbrooks
24262643Sbrooks/*
25262643Sbrooks * @test
26262643Sbrooks * @bug 8033581 8033798 8033726
27262643Sbrooks * @summary Check whitespace in generated output
28262643Sbrooks * @modules jdk.jdeps
29262643Sbrooks */
30262643Sbrooks
311590Srgrimesimport java.io.*;
32import java.util.*;
33
34public class WhitespaceTest {
35    public static void main(String... args) throws Exception {
36        new WhitespaceTest().run();
37    }
38
39    void run() throws Exception {
40        test("-v", "java.lang.String");
41        test("-XDtab:1", "-v", "java.lang.String");
42
43        String testClasses = System.getProperty("test.classes");
44        for (int i = 10; i < 40; i++)
45            test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld");
46
47        if (errors > 0)
48            throw new Exception(errors + " errors found");
49    }
50
51    void test(String... args) throws Exception {
52        // need to avoid "//" appearing as a constant in the constant pool
53        String slash = "/";
54        String doubleSlash = slash + slash;
55        System.out.println("test: " + Arrays.asList(args));
56        String out = javap(args);
57        for (String line: out.split("[\r\n]+")) {
58            if (line.endsWith(" "))
59                error("line has trailing whitespace: " + line);
60            int comment = line.indexOf(doubleSlash);
61            if (comment > 0 && line.charAt(comment - 1) != ' ') {
62                // make allowance for URLs
63                if (!line.matches(".*\\bfile:/{3}.*"))
64                    error("no space before comment: " + line);
65            }
66            if (line.matches(" +}"))
67                error("bad indentation: " + line);
68        }
69    }
70
71    String javap(String... args) throws Exception {
72        StringWriter sw = new StringWriter();
73        PrintWriter out = new PrintWriter(sw);
74        int rc = com.sun.tools.javap.Main.run(args, out);
75        out.close();
76        System.out.println(sw.toString());
77        if (rc < 0)
78            throw new Exception("javap exited, rc=" + rc);
79        return sw.toString();
80    }
81
82    void error(String msg) {
83        System.out.println("Error: " + msg);
84        errors++;
85    }
86
87    int errors;
88
89    // small class to test repeatedly with different tab values
90    static class HelloWorld {
91        public static void main(String... args) {
92            System.out.println("Hello World!");
93        }
94    }
95}
96
97
98