test/tools/javac/annotations/repeatingAnnotations/combo/DeprecatedAnnoCombo.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/DeprecatedAnnoCombo.java	Wed Nov 07 17:01:19 2012 -0800
     1.3 @@ -0,0 +1,155 @@
     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  Combo test to check for usage of Deprecated
    1.32 + * @build    Helper
    1.33 + * @compile  DeprecatedAnnoCombo.java
    1.34 + * @run main DeprecatedAnnoCombo
    1.35 + */
    1.36 +
    1.37 +import java.util.List;
    1.38 +import javax.tools.Diagnostic;
    1.39 +import javax.tools.DiagnosticCollector;
    1.40 +import javax.tools.JavaFileObject;
    1.41 +
    1.42 +/*
    1.43 + * Generate test src for use of @Deprecated on base anno
    1.44 + * or container anno or on both. In all cases, test src should compile and a
    1.45 + * warning should be generated. Repeating annotations used only on class for
    1.46 + * these generated test src.
    1.47 + */
    1.48 +
    1.49 +public class DeprecatedAnnoCombo extends Helper {
    1.50 +    static int errors = 0;
    1.51 +
    1.52 +    enum TestCases {
    1.53 +        DeprecatedonBoth,
    1.54 +        DeprecatedonContainer,
    1.55 +        DeprecatedonBase;
    1.56 +    }
    1.57 +
    1.58 +    public static void main(String[] args) throws Exception {
    1.59 +        new DeprecatedAnnoCombo().runTest();
    1.60 +    }
    1.61 +
    1.62 +    public void runTest() throws Exception {
    1.63 +        boolean ok = false;
    1.64 +        int testCtr = 0;
    1.65 +
    1.66 +        for (TestCases clName : TestCases.values()) {
    1.67 +            testCtr++;
    1.68 +
    1.69 +            // Create test source content
    1.70 +            String contents = getContent(clName.toString());
    1.71 +
    1.72 +            // Compile the generated source file
    1.73 +            DiagnosticCollector<JavaFileObject> diagnostics =
    1.74 +                    new DiagnosticCollector<JavaFileObject>();
    1.75 +            ok = compileCode(clName.toString(), contents, diagnostics);
    1.76 +
    1.77 +            String errorKey1 = "compiler.note.deprecated.filename";
    1.78 +            String errorKey2 = "compiler.note.deprecated.recompile";
    1.79 +            List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics();
    1.80 +
    1.81 +            //Check for deprecated warnings
    1.82 +            if (ok) {
    1.83 +                if (diags.size() == 0) {
    1.84 +                    error("Did not get any warnings for @Deprecated usage");
    1.85 +                } else {
    1.86 +                    for (Diagnostic<?> d : diags) {
    1.87 +                        if (d.getKind() == Diagnostic.Kind.NOTE) {
    1.88 +                            if (d.getCode().contains(errorKey1)
    1.89 +                                || d.getCode().contains(errorKey2)) {
    1.90 +                                System.out.println("TestCase =" + clName + " passed as expected");
    1.91 +                            } else {
    1.92 +                                error("TestCase =" + clName + " did not give correct warnings" +
    1.93 +                                    "Expected warning keys: " +
    1.94 +                                    "errorKey1 = " + errorKey1 +
    1.95 +                                    "errorKey2 = " + errorKey2 +
    1.96 +                                    "actualErrorKey = " + d.getCode(), contents);
    1.97 +                            }
    1.98 +                        } else {
    1.99 +                            error("Diagnostic Kind is incorrect, expected = " +
   1.100 +                                 Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents);
   1.101 +                        }
   1.102 +                    }
   1.103 +                }
   1.104 +            } else {
   1.105 +                error("TestCase =" + clName + " did not compile as expected", contents);
   1.106 +            }
   1.107 +        }
   1.108 +
   1.109 +        System.out.println("Total number of tests run: " + testCtr);
   1.110 +        System.out.println("Total number of errors: " + errors);
   1.111 +        if (errors > 0)
   1.112 +            throw new Exception(errors + " errors found");
   1.113 +    }
   1.114 +
   1.115 +    private String getContent(String className) {
   1.116 +        StringBuilder annoData = new StringBuilder();
   1.117 +
   1.118 +        switch(className) {
   1.119 +        case "DeprecatedonBoth":
   1.120 +            annoData.append(Helper.ContentVars.DEPRECATED.getVal())
   1.121 +                    .append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.122 +                    .append(Helper.ContentVars.CONTAINER.getVal())
   1.123 +                    .append(Helper.ContentVars.DEPRECATED.getVal())
   1.124 +                    .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.125 +                    .append(Helper.ContentVars.BASE.getVal());
   1.126 +            break;
   1.127 +        case "DeprecatedonBase":
   1.128 +            annoData.append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.129 +                    .append(Helper.ContentVars.CONTAINER.getVal())
   1.130 +                    .append(Helper.ContentVars.DEPRECATED.getVal())
   1.131 +                    .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.132 +                    .append(Helper.ContentVars.BASE.getVal());
   1.133 +            break;
   1.134 +        case "DeprecatedonContainer":
   1.135 +            annoData.append(Helper.ContentVars.DEPRECATED.getVal())
   1.136 +                    .append(Helper.ContentVars.CONTAINERFOR.getVal())
   1.137 +                    .append(Helper.ContentVars.CONTAINER.getVal())
   1.138 +                    .append(Helper.ContentVars.CONTAINEDBY.getVal())
   1.139 +                    .append(Helper.ContentVars.BASE.getVal());
   1.140 +            break;
   1.141 +        }
   1.142 +
   1.143 +        String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()
   1.144 +                          + Helper.ContentVars.IMPORTDEPRECATED.getVal()
   1.145 +                          + annoData
   1.146 +                          + Helper.ContentVars.REPEATABLEANNO.getVal()
   1.147 +                          + "\nclass "+ className + "{}";
   1.148 +        return contents;
   1.149 +    }
   1.150 +
   1.151 +    private void error(String msg, String... contents) {
   1.152 +        System.out.println("error: " + msg);
   1.153 +        errors++;
   1.154 +        if (contents.length == 1) {
   1.155 +            System.out.println("Contents = " + contents[0]);
   1.156 +        }
   1.157 +    }
   1.158 +}

mercurial