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

Wed, 07 Nov 2012 17:01:19 -0800

author
jjg
date
Wed, 07 Nov 2012 17:01:19 -0800
changeset 1401
2986e7052952
child 1492
df694c775e8a
permissions
-rw-r--r--

8002157: Write combo compiler tests for repeating annotations for JDK8
Reviewed-by: darcy, jjg
Contributed-by: sonali.goel@oracle.com

     1 /*
     2  * Copyright (c) 2012, 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.CONTAINERFOR.getVal())
   119                     .append(Helper.ContentVars.CONTAINER.getVal())
   120                     .append(Helper.ContentVars.DEPRECATED.getVal())
   121                     .append(Helper.ContentVars.CONTAINEDBY.getVal())
   122                     .append(Helper.ContentVars.BASE.getVal());
   123             break;
   124         case "DeprecatedonBase":
   125             annoData.append(Helper.ContentVars.CONTAINERFOR.getVal())
   126                     .append(Helper.ContentVars.CONTAINER.getVal())
   127                     .append(Helper.ContentVars.DEPRECATED.getVal())
   128                     .append(Helper.ContentVars.CONTAINEDBY.getVal())
   129                     .append(Helper.ContentVars.BASE.getVal());
   130             break;
   131         case "DeprecatedonContainer":
   132             annoData.append(Helper.ContentVars.DEPRECATED.getVal())
   133                     .append(Helper.ContentVars.CONTAINERFOR.getVal())
   134                     .append(Helper.ContentVars.CONTAINER.getVal())
   135                     .append(Helper.ContentVars.CONTAINEDBY.getVal())
   136                     .append(Helper.ContentVars.BASE.getVal());
   137             break;
   138         }
   140         String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()
   141                           + Helper.ContentVars.IMPORTDEPRECATED.getVal()
   142                           + annoData
   143                           + Helper.ContentVars.REPEATABLEANNO.getVal()
   144                           + "\nclass "+ className + "{}";
   145         return contents;
   146     }
   148     private void error(String msg, String... contents) {
   149         System.out.println("error: " + msg);
   150         errors++;
   151         if (contents.length == 1) {
   152             System.out.println("Contents = " + contents[0]);
   153         }
   154     }
   155 }

mercurial