test/tools/javac/annotations/repeatingAnnotations/combo/BasicSyntaxCombo.java

changeset 1401
2986e7052952
child 1492
df694c775e8a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/BasicSyntaxCombo.java	Wed Nov 07 17:01:19 2012 -0800
     1.3 @@ -0,0 +1,209 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug      8002157
    1.30 + * @author   sogoel
    1.31 + * @summary  Basic Syntax test for repeating annotations on all elements
    1.32 + * @build    Helper
    1.33 + * @compile  BasicSyntaxCombo.java
    1.34 + * @run main BasicSyntaxCombo
    1.35 + */
    1.36 +
    1.37 +
    1.38 +import java.util.Arrays;
    1.39 +import javax.tools.DiagnosticCollector;
    1.40 +import javax.tools.JavaFileObject;
    1.41 +import javax.tools.Diagnostic;
    1.42 +
    1.43 +/*
    1.44 + * Generate test src for element kinds with repeating annotations.
    1.45 + * The test uses Helper.java to get the template to create test src and
    1.46 + * compile the test src.
    1.47 + * The test passes if valid test src compile as expected and
    1.48 + * and invalid test src fail as expected.
    1.49 + */
    1.50 +
    1.51 +public class BasicSyntaxCombo extends Helper{
    1.52 +    static int errors = 0;
    1.53 +    static boolean exitMode = false;
    1.54 +    static String TESTPKG = "testpkg";
    1.55 +    static String srcContent = "";
    1.56 +    static String pkgInfoContent = "";
    1.57 +
    1.58 +    static {
    1.59 +        // If EXIT_ON_FAIL is set, the combo test will exit at the first error
    1.60 +        String exitOnFail = System.getenv("EXIT_ON_FAIL");
    1.61 +        if (exitOnFail == null || exitOnFail == ""  ) {
    1.62 +            exitMode = false;
    1.63 +        }
    1.64 +        else {
    1.65 +            if (exitOnFail.equalsIgnoreCase("YES") ||
    1.66 +                    exitOnFail.equalsIgnoreCase("Y") ||
    1.67 +                    exitOnFail.equalsIgnoreCase("TRUE") ||
    1.68 +                    exitOnFail.equalsIgnoreCase("T")) {
    1.69 +                exitMode = true;
    1.70 +            }
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    enum TestElem {
    1.75 +        ANNOTATION_TYPE(true),
    1.76 +        PACKAGE(true),
    1.77 +        CONSTRUCTOR(true),
    1.78 +        FIELD(true),
    1.79 +        LOCAL_VARIABLE(true),
    1.80 +        METHOD(true),
    1.81 +        TYPE(true),
    1.82 +        PARAMETER(true),
    1.83 +        INNER_CLASS(true),
    1.84 +        STATIC_INI(false),
    1.85 +        INSTANCE_INI(false);
    1.86 +
    1.87 +        TestElem(boolean compile) {
    1.88 +            this.compile = compile;
    1.89 +        }
    1.90 +
    1.91 +        boolean compile;
    1.92 +        boolean shouldCompile() {
    1.93 +            return compile;
    1.94 +        }
    1.95 +    }
    1.96 +
    1.97 +    public static void main(String[] args) throws Exception {
    1.98 +        new BasicSyntaxCombo().runTest();
    1.99 +    }
   1.100 +
   1.101 +    public void runTest() throws Exception {
   1.102 +        boolean result = false;
   1.103 +        Iterable<? extends JavaFileObject> files = null;
   1.104 +        int testCtr = 0;
   1.105 +        for (TestElem type : TestElem.values()) {
   1.106 +            testCtr++;
   1.107 +            String className = "BasicCombo_"+type;
   1.108 +            files = getFileList(className, type);
   1.109 +
   1.110 +            boolean shouldCompile = type.shouldCompile();
   1.111 +            result = getCompileResult(className, shouldCompile,files);
   1.112 +
   1.113 +            if (shouldCompile && !result) {
   1.114 +                error(className + " did not compile as expected", srcContent);
   1.115 +                if(!pkgInfoContent.isEmpty()) {
   1.116 +                    System.out.println("package-info.java contents: " + pkgInfoContent);
   1.117 +                }
   1.118 +            }
   1.119 +
   1.120 +            if (!shouldCompile && !result) {
   1.121 +                error(className + " compiled unexpectedly", srcContent);
   1.122 +                if(!pkgInfoContent.isEmpty()) {
   1.123 +                    System.out.println("package-info.java contents: " + pkgInfoContent);
   1.124 +                }
   1.125 +            }
   1.126 +        }
   1.127 +
   1.128 +        System.out.println("Total number of tests run: " + testCtr);
   1.129 +        System.out.println("Total number of errors: " + errors);
   1.130 +
   1.131 +        if (errors > 0)
   1.132 +            throw new Exception(errors + " errors found");
   1.133 +    }
   1.134 +
   1.135 +    private boolean getCompileResult(String className, boolean shouldCompile,
   1.136 +            Iterable<? extends JavaFileObject> files) throws Exception {
   1.137 +
   1.138 +        DiagnosticCollector<JavaFileObject> diagnostics =
   1.139 +                new DiagnosticCollector<JavaFileObject>();
   1.140 +        boolean ok =  Helper.compileCode(diagnostics,files);
   1.141 +        if (!shouldCompile && !ok) {
   1.142 +            checkErrorKeys(className, diagnostics);
   1.143 +        }
   1.144 +        return (shouldCompile == ok);
   1.145 +    }
   1.146 +
   1.147 +    private void checkErrorKeys (
   1.148 +            String className, DiagnosticCollector<JavaFileObject> diagnostics) throws Exception {
   1.149 +        String expectedErrKey = "compiler.err.illegal.start.of.type";
   1.150 +        for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
   1.151 +            if ((d.getKind() == Diagnostic.Kind.ERROR) &&
   1.152 +                d.getCode().contains(expectedErrKey)) {
   1.153 +                break; // Found the expected error
   1.154 +            } else {
   1.155 +                error("Incorrect error key, expected = "
   1.156 +                      + expectedErrKey + ", Actual = " + d.getCode()
   1.157 +                      + " for className = " + className, srcContent);
   1.158 +            }
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    private Iterable<? extends JavaFileObject> getFileList(String className,
   1.163 +            TestElem type ) {
   1.164 +
   1.165 +        String template = Helper.template;
   1.166 +        String replaceStr = "/*"+type+"*/";
   1.167 +        StringBuilder annoData = new StringBuilder();
   1.168 +        annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal())
   1.169 +                .append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.170 +                .append(Helper.ContentVars.CONTAINER.getVal())
   1.171 +                .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.172 +                .append(Helper.ContentVars.BASE.getVal());
   1.173 +
   1.174 +        JavaFileObject pkgInfoFile = null;
   1.175 +
   1.176 +        if (type.equals("PACKAGE")) {
   1.177 +            srcContent = template.replace(replaceStr, "package testpkg;")
   1.178 +                        .replace("#ClassName", className);
   1.179 +
   1.180 +            String pkgInfoName = TESTPKG+"."+"package-info";
   1.181 +            pkgInfoContent = Helper.ContentVars.REPEATABLEANNO.getVal()
   1.182 +                             + "package " + TESTPKG + ";"
   1.183 +                             + annoData;
   1.184 +            pkgInfoFile = getFile(pkgInfoName, pkgInfoContent);
   1.185 +        } else {
   1.186 +            template = template.replace(replaceStr, Helper.ContentVars.REPEATABLEANNO.getVal())
   1.187 +                       .replace("#ClassName", className);
   1.188 +            srcContent = annoData + template;
   1.189 +        }
   1.190 +
   1.191 +        JavaFileObject srcFile = getFile(className, srcContent);
   1.192 +
   1.193 +        Iterable<? extends JavaFileObject> files = null;
   1.194 +        if (pkgInfoFile != null) {
   1.195 +            files = Arrays.asList(pkgInfoFile,srcFile);
   1.196 +        }
   1.197 +        else {
   1.198 +            files = Arrays.asList(srcFile);
   1.199 +        }
   1.200 +        return files;
   1.201 +    }
   1.202 +
   1.203 +    private void error(String msg, String... contents) throws Exception {
   1.204 +        System.out.println("error: " + msg);
   1.205 +        errors++;
   1.206 +        if (contents.length == 1) {
   1.207 +            System.out.println("Contents = " + contents[0]);
   1.208 +        }
   1.209 +        // Test exits as soon as it gets a failure
   1.210 +        if (exitMode) throw new Exception();
   1.211 +    }
   1.212 +}

mercurial