langtools/test/tools/javac/6902720/Test.java
author jjg
Wed, 29 Oct 2014 17:25:23 -0700
changeset 27319 030080f03e4f
parent 5520 86e4b9a9da40
child 30730 d3ce7619db2c
permissions -rw-r--r--
8062348: langtools tests should close file manager (group 1) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     4
 *
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     8
 *
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    13
 * accompanied this code).
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    14
 *
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4413
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4413
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4413
diff changeset
    21
 * questions.
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    22
 */
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    23
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    24
import java.io.*;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    25
import java.net.*;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    26
import javax.tools.*;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    27
import java.util.*;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    28
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    29
import com.sun.source.tree.CompilationUnitTree;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    30
import com.sun.source.util.JavacTask;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    31
import com.sun.tools.javac.api.JavacTool;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    32
import com.sun.tools.javac.tree.JCTree;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.tree.Pretty;
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    34
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    35
/**
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    36
 * @test
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    37
 * @bug 6902720
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    38
 * @summary javac pretty printer does not handle enums correctly
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    39
 */
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    40
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    41
public class Test {
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    42
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    44
        Test t = new Test();
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    45
        t.run("E1.java", "E2.java");
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    46
    }
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    47
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    48
    void run(String... args) throws Exception {
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    49
        File testSrcDir = new File(System.getProperty("test.src"));
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    50
        for (String arg: args) {
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    51
            test(new File(testSrcDir, arg));
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    52
        }
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    53
    }
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    54
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    55
    void test(File test) throws Exception {
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    56
        JavacTool tool1 = JavacTool.create();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    57
        try (StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    58
            Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    59
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    60
            // parse test file into a tree, and write it out to a stringbuffer using Pretty
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    61
            JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    62
            StringWriter sw = new StringWriter();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    63
            PrintWriter pw = new PrintWriter(sw);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    64
            Iterable<? extends CompilationUnitTree> trees = t1.parse();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    65
            for (CompilationUnitTree tree: trees) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    66
                new Pretty(pw, true).printExpr((JCTree) tree);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    67
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    68
            pw.close();
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    69
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    70
            final String out = sw.toString();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    71
            System.err.println("generated code:\n" + out + "\n");
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    72
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    73
            // verify the generated code is valid Java by compiling it
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    74
            JavacTool tool2 = JavacTool.create();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    75
            JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    76
                @Override
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    77
                public CharSequence getCharContent(boolean ignoreEncodingErrors) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    78
                    return out;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    79
                }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    80
            };
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    81
            JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    82
            boolean ok = t2.call();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    83
            if (!ok)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    84
                throw new Exception("compilation of generated code failed");
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    85
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    86
            File expectedClass = new File(test.getName().replace(".java", ".class"));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    87
            if (!expectedClass.exists())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    88
                throw new Exception(expectedClass + " not found");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 5520
diff changeset
    89
        }
4413
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    90
    }
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    91
}
2ae470f0b492 6902720: javac pretty printer does not handle enums correctly
jjg
parents:
diff changeset
    92