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

Mon, 14 Jan 2013 13:50:01 -0800

author
jjg
date
Mon, 14 Jan 2013 13:50:01 -0800
changeset 1492
df694c775e8a
parent 1401
2986e7052952
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8006119: update javac to follow latest spec for repeatable annotations
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /**
    25  * @test
    26  * @bug      8002157
    27  * @author   sogoel
    28  * @summary  Combo test to check for usage of Deprecated
    29  * @build    Helper
    30  * @compile  DeprecatedAnnoCombo.java
    31  * @run main DeprecatedAnnoCombo
    32  */
    34 import java.util.List;
    35 import javax.tools.Diagnostic;
    36 import javax.tools.DiagnosticCollector;
    37 import javax.tools.JavaFileObject;
    39 /*
    40  * Generate test src for use of @Deprecated on base anno
    41  * or container anno or on both. In all cases, test src should compile and a
    42  * warning should be generated. Repeating annotations used only on class for
    43  * these generated test src.
    44  */
    46 public class DeprecatedAnnoCombo extends Helper {
    47     static int errors = 0;
    49     enum TestCases {
    50         DeprecatedonBoth,
    51         DeprecatedonContainer,
    52         DeprecatedonBase;
    53     }
    55     public static void main(String[] args) throws Exception {
    56         new DeprecatedAnnoCombo().runTest();
    57     }
    59     public void runTest() throws Exception {
    60         boolean ok = false;
    61         int testCtr = 0;
    63         for (TestCases clName : TestCases.values()) {
    64             testCtr++;
    66             // Create test source content
    67             String contents = getContent(clName.toString());
    69             // Compile the generated source file
    70             DiagnosticCollector<JavaFileObject> diagnostics =
    71                     new DiagnosticCollector<JavaFileObject>();
    72             ok = compileCode(clName.toString(), contents, diagnostics);
    74             String errorKey1 = "compiler.note.deprecated.filename";
    75             String errorKey2 = "compiler.note.deprecated.recompile";
    76             List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics();
    78             //Check for deprecated warnings
    79             if (ok) {
    80                 if (diags.size() == 0) {
    81                     error("Did not get any warnings for @Deprecated usage");
    82                 } else {
    83                     for (Diagnostic<?> d : diags) {
    84                         if (d.getKind() == Diagnostic.Kind.NOTE) {
    85                             if (d.getCode().contains(errorKey1)
    86                                 || d.getCode().contains(errorKey2)) {
    87                                 System.out.println("TestCase =" + clName + " passed as expected");
    88                             } else {
    89                                 error("TestCase =" + clName + " did not give correct warnings" +
    90                                     "Expected warning keys: " +
    91                                     "errorKey1 = " + errorKey1 +
    92                                     "errorKey2 = " + errorKey2 +
    93                                     "actualErrorKey = " + d.getCode(), contents);
    94                             }
    95                         } else {
    96                             error("Diagnostic Kind is incorrect, expected = " +
    97                                  Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents);
    98                         }
    99                     }
   100                 }
   101             } else {
   102                 error("TestCase =" + clName + " did not compile as expected", contents);
   103             }
   104         }
   106         System.out.println("Total number of tests run: " + testCtr);
   107         System.out.println("Total number of errors: " + errors);
   108         if (errors > 0)
   109             throw new Exception(errors + " errors found");
   110     }
   112     private String getContent(String className) {
   113         StringBuilder annoData = new StringBuilder();
   115         switch(className) {
   116         case "DeprecatedonBoth":
   117             annoData.append(Helper.ContentVars.DEPRECATED.getVal())
   118                     .append(Helper.ContentVars.CONTAINER.getVal())
   119                     .append(Helper.ContentVars.DEPRECATED.getVal())
   120                     .append(Helper.ContentVars.REPEATABLE.getVal())
   121                     .append(Helper.ContentVars.BASE.getVal());
   122             break;
   123         case "DeprecatedonBase":
   124             annoData.append(Helper.ContentVars.CONTAINER.getVal())
   125                     .append(Helper.ContentVars.DEPRECATED.getVal())
   126                     .append(Helper.ContentVars.REPEATABLE.getVal())
   127                     .append(Helper.ContentVars.BASE.getVal());
   128             break;
   129         case "DeprecatedonContainer":
   130             annoData.append(Helper.ContentVars.DEPRECATED.getVal())
   131                     .append(Helper.ContentVars.CONTAINER.getVal())
   132                     .append(Helper.ContentVars.REPEATABLE.getVal())
   133                     .append(Helper.ContentVars.BASE.getVal());
   134             break;
   135         }
   137         String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()
   138                           + Helper.ContentVars.IMPORTDEPRECATED.getVal()
   139                           + annoData
   140                           + Helper.ContentVars.REPEATABLEANNO.getVal()
   141                           + "\nclass "+ className + "{}";
   142         return contents;
   143     }
   145     private void error(String msg, String... contents) {
   146         System.out.println("error: " + msg);
   147         errors++;
   148         if (contents.length == 1) {
   149             System.out.println("Contents = " + contents[0]);
   150         }
   151     }
   152 }

mercurial