langtools/test/tools/javap/WhitespaceTest.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27858 443efec4bf09
child 30846 2b3f379840f0
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23110
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27858
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23110
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     4
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     8
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    13
 * accompanied this code).
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    14
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    18
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    21
 * questions.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    22
 */
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    23
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    24
/*
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    25
 * @test
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    26
 * @bug 8033581 8033798 8033726
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    27
 * @summary Check whitespace in generated output
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27858
diff changeset
    28
 * @modules jdk.compiler
23110
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    29
 */
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    30
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    31
import java.io.*;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    32
import java.util.*;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    33
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    34
public class WhitespaceTest {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    35
    public static void main(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    36
        new WhitespaceTest().run();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    37
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    38
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    39
    void run() throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    40
        test("-v", "java.lang.String");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    41
        test("-XDtab:1", "-v", "java.lang.String");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    42
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    43
        String testClasses = System.getProperty("test.classes");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    44
        for (int i = 10; i < 40; i++)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    45
            test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    46
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    47
        if (errors > 0)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    48
            throw new Exception(errors + " errors found");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    49
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    50
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    51
    void test(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    52
        // need to avoid "//" appearing as a constant in the constant pool
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    53
        String slash = "/";
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    54
        String doubleSlash = slash + slash;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    55
        System.out.println("test: " + Arrays.asList(args));
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    56
        String out = javap(args);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    57
        for (String line: out.split("[\r\n]+")) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    58
            if (line.endsWith(" "))
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    59
                error("line has trailing whitespace: " + line);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    60
            int comment = line.indexOf(doubleSlash);
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27226
diff changeset
    61
            if (comment > 0 && line.charAt(comment - 1) != ' ') {
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27226
diff changeset
    62
                // make allowance for URLs
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27226
diff changeset
    63
                if (!line.matches(".*\\bfile:/{3}.*"))
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27226
diff changeset
    64
                    error("no space before comment: " + line);
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 27226
diff changeset
    65
            }
23110
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    66
            if (line.matches(" +}"))
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    67
                error("bad indentation: " + line);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    68
        }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    69
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    70
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    71
    String javap(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    72
        StringWriter sw = new StringWriter();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    73
        PrintWriter out = new PrintWriter(sw);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    74
        int rc = com.sun.tools.javap.Main.run(args, out);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    75
        out.close();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    76
        System.out.println(sw.toString());
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    77
        if (rc < 0)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    78
            throw new Exception("javap exited, rc=" + rc);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    79
        return sw.toString();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    80
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    81
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    82
    void error(String msg) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    83
        System.out.println("Error: " + msg);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    84
        errors++;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    85
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    86
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    87
    int errors;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    88
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    89
    // small class to test repeatedly with different tab values
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    90
    static class HelloWorld {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    91
        public static void main(String... args) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    92
            System.out.println("Hello World!");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    93
        }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    94
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    95
}
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    96
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    97