test/tools/javac/annotations/repeatingAnnotations/combo/DocumentedAnnoCombo.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/DocumentedAnnoCombo.java	Wed Nov 07 17:01:19 2012 -0800
     1.3 @@ -0,0 +1,127 @@
     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  Positive combo test for use of Documented on baseAnno/containerAnno
    1.32 + * @build    Helper
    1.33 + * @compile  DocumentedAnnoCombo.java
    1.34 + * @run main DocumentedAnnoCombo
    1.35 + */
    1.36 +
    1.37 +import javax.tools.DiagnosticCollector;
    1.38 +import javax.tools.JavaFileObject;
    1.39 +
    1.40 +/*
    1.41 + * Generate valid test src for the use of @Documented on container anno
    1.42 + * or on both base anno and container anno. Both test src should compile.
    1.43 + * Repeating annotations used only on class for these generated test src.
    1.44 + */
    1.45 +public class DocumentedAnnoCombo extends Helper {
    1.46 +    static int errors = 0;
    1.47 +
    1.48 +    enum TestCases {
    1.49 +        DocumentedonBothAnno(true),
    1.50 +        DocumentedonContainer(true);
    1.51 +
    1.52 +        TestCases(boolean compile) {
    1.53 +            this.compile = compile;
    1.54 +        }
    1.55 +
    1.56 +        boolean compile;
    1.57 +        boolean shouldCompile() {
    1.58 +            return compile;
    1.59 +        }
    1.60 +    }
    1.61 +
    1.62 +    public static void main(String[] args) throws Exception {
    1.63 +        new DocumentedAnnoCombo().runTest();
    1.64 +    }
    1.65 +
    1.66 +    public void runTest() throws Exception {
    1.67 +        boolean ok = false;
    1.68 +        int testCtr = 0;
    1.69 +
    1.70 +        // Create test source content
    1.71 +        for (TestCases className : TestCases.values()) {
    1.72 +            testCtr++;
    1.73 +            String contents = getContent(className.toString());
    1.74 +
    1.75 +            // Compile the generated source file
    1.76 +            DiagnosticCollector<JavaFileObject> diagnostics =
    1.77 +                    new DiagnosticCollector<JavaFileObject>();
    1.78 +            ok = compileCode(className.toString(), contents, diagnostics);
    1.79 +            if (!ok) {
    1.80 +                error("Class="+ className +" did not compile as expected", contents);
    1.81 +            } else {
    1.82 +                System.out.println("Test passed for className: " + className);
    1.83 +            }
    1.84 +        }
    1.85 +
    1.86 +        System.out.println("Total number of tests run: " + testCtr);
    1.87 +        System.out.println("Total number of errors: " + errors);
    1.88 +
    1.89 +        if (errors > 0)
    1.90 +            throw new Exception(errors + " errors found");
    1.91 +    }
    1.92 +
    1.93 +    private String getContent(String className) {
    1.94 +
    1.95 +        StringBuilder annoData = new StringBuilder();
    1.96 +        switch(className) {
    1.97 +            case "DocumentedonBothAnno":
    1.98 +                annoData.append(Helper.ContentVars.DOCUMENTED.getVal())
    1.99 +                .append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.100 +                .append(Helper.ContentVars.CONTAINER.getVal())
   1.101 +                .append(Helper.ContentVars.DOCUMENTED.getVal())
   1.102 +                .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.103 +                .append(Helper.ContentVars.BASE.getVal());
   1.104 +            break;
   1.105 +            case "DocumentedonContainer":
   1.106 +                annoData.append(Helper.ContentVars.DOCUMENTED.getVal())
   1.107 +                .append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.108 +                .append(Helper.ContentVars.CONTAINER.getVal())
   1.109 +                .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.110 +                .append(Helper.ContentVars.BASE.getVal());
   1.111 +            break;
   1.112 +        }
   1.113 +
   1.114 +        String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()
   1.115 +                          + Helper.ContentVars.IMPORTDOCUMENTED.getVal()
   1.116 +                          + annoData
   1.117 +                          + Helper.ContentVars.REPEATABLEANNO.getVal()
   1.118 +                          + "\nclass "+ className + "{}";
   1.119 +        return contents;
   1.120 +    }
   1.121 +
   1.122 +    private void error(String msg, String... contents) {
   1.123 +        System.out.println("error: " + msg);
   1.124 +        errors++;
   1.125 +        if (contents.length == 1) {
   1.126 +            System.out.println("Contents = " + contents[0]);
   1.127 +        }
   1.128 +    }
   1.129 +}
   1.130 +

mercurial