test/tools/javac/annotations/repeatingAnnotations/combo/RetentionAnnoCombo.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 for all possible combinations for Retention Values
    29  * @build    Helper
    30  * @compile  RetentionAnnoCombo.java
    31  * @run main RetentionAnnoCombo
    32  */
    34 import java.util.HashMap;
    35 import java.util.Map;
    37 import javax.tools.DiagnosticCollector;
    38 import javax.tools.JavaFileObject;
    39 import javax.tools.Diagnostic;
    41 /*
    42  * Generate all combinations for the use of @Retention on base anno or container
    43  * anno or both. The test passes if valid test src compile as expected and
    44  * and invalid test src fail as expected.
    45  * Repeating annotations used only on class for these generated test src.
    46  */
    48 public class RetentionAnnoCombo extends Helper {
    49     static int errors = 0;
    50     static boolean exitMode = false;
    51     static {
    52         String exitOnFail = System.getenv("EXIT_ON_FAIL");
    53         if (exitOnFail == null || exitOnFail == ""  ) {
    54             exitMode = false;
    55         }
    56         else {
    57             if (exitOnFail.equalsIgnoreCase("YES") ||
    58                     exitOnFail.equalsIgnoreCase("Y") ||
    59                     exitOnFail.equalsIgnoreCase("TRUE") ||
    60                     exitOnFail.equalsIgnoreCase("T")) {
    61                 exitMode = true;
    62             }
    63         }
    64     }
    66     public static void main(String args[]) throws Exception {
    67         new RetentionAnnoCombo().runTest();
    68     }
    70     public void runTest() throws Exception {
    72         /* 4x4 matrix for Retention values SOURCE, DEFAULT, CLASS, RUNTIME
    73          * i -> Retention value on ContainerAnno
    74          * j -> Retention value on BaseAnno
    75          * 1 -> retention value combo should compile
    76          */
    77         int[][] retention = { {1, 0, 0, 0},
    78                               {1, 1, 1, 0},
    79                               {1, 1, 1, 0},
    80                               {1, 1, 1, 1} };
    82         Map<Integer, String> retMap = setRetentionValMatrix();
    83         String contents = "";
    84         boolean result = false;
    85         int testCtr = 0;
    86         for (int i = 0; i < 4 ; i ++) {
    87             for (int j = 0; j < 4; j++ ) {
    88                 testCtr++;
    89                 String className = "RetentionTest_"+i+"_"+j;
    90                 contents = getContent(className, retMap, i, j);
    91                 if (retention[i][j] == 1) {
    92                     // Code generated should compile
    93                     result = getCompileResult(contents,className, true);
    94                     if (!result) {
    95                         error("FAIL: " +  className + " did not compile as expected!", contents);
    96                     }
    97                 } else {
    98                     result = getCompileResult(contents,className, false);
    99                     if (!result) {
   100                         error("FAIL: " +  className + " compiled unexpectedly!", contents);
   101                     }
   102                 }
   103                 if (result) {
   104                     System.out.println("Test passed for className = " + className);
   105                 }
   106             }
   107         }
   109         System.out.println("Total number of tests run: " + testCtr);
   110         System.out.println("Total number of errors: " + errors);
   112         if (errors > 0)
   113             throw new Exception(errors + " errors found");
   114     }
   116     private boolean getCompileResult(String contents, String className,
   117             boolean shouldCompile) throws Exception{
   119         DiagnosticCollector<JavaFileObject> diagnostics =
   120                 new DiagnosticCollector<JavaFileObject>();
   121         boolean ok = compileCode(className, contents, diagnostics);
   123         String expectedErrKey = "compiler.err.invalid.containedby" +
   124                                         ".annotation.retention";
   125         if (!shouldCompile && !ok) {
   126             for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
   127                 if (!((d.getKind() == Diagnostic.Kind.ERROR) &&
   128                     d.getCode().contains(expectedErrKey))) {
   129                     error("FAIL: Incorrect error given, expected = "
   130                             + expectedErrKey + ", Actual = " + d.getCode()
   131                             + " for className = " + className, contents);
   132                 }
   133             }
   134         }
   136         return (shouldCompile  == ok);
   137     }
   139     private Map<Integer,String> setRetentionValMatrix() {
   140         HashMap<Integer,String> hm = new HashMap<>();
   141         hm.put(0,"SOURCE");
   142         hm.put(1,"DEFAULT");
   143         hm.put(2,"CLASS");
   144         hm.put(3,"RUNTIME");
   145         return hm;
   146     }
   148     private String getContent(String className, Map<Integer, String> retMap,
   149             int i, int j) {
   151         String retContainerVal = retMap.get(i).toString();
   152         String retBaseVal = retMap.get(j).toString();
   153         String replacedRetBaseVal = "", replacedRetCAVal = "";
   154         String retention = Helper.ContentVars.RETENTION.getVal();
   156         // @Retention is available as default for both base and container anno
   157         if (retContainerVal.equalsIgnoreCase("DEFAULT")
   158                 && retBaseVal.equalsIgnoreCase("DEFAULT")) {
   159             replacedRetBaseVal = "";
   160             replacedRetCAVal = "";
   161         // @Retention is available as default for container anno
   162         } else if (retContainerVal.equalsIgnoreCase("DEFAULT")) {
   163             replacedRetBaseVal = retention.replace("#VAL", retBaseVal);
   164             replacedRetCAVal = "";
   165         // @Retention is available as default for base anno
   166         } else if (retBaseVal.equalsIgnoreCase("DEFAULT")) {
   167             replacedRetBaseVal = "";
   168             replacedRetCAVal = retention.replace("#VAL", retContainerVal);
   169         // @Retention is not available as default for both base and container anno
   170         } else {
   171             replacedRetBaseVal = retention.replace("#VAL", retBaseVal);
   172             replacedRetCAVal = retention.replace("#VAL", retContainerVal);
   173         }
   175         StringBuilder annoData = new StringBuilder();
   176         annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal())
   177                 .append(Helper.ContentVars.IMPORTRETENTION.getVal())
   178                 .append(Helper.ContentVars.CONTAINERFOR.getVal())
   179                 .append(replacedRetCAVal)
   180                 .append(Helper.ContentVars.CONTAINER.getVal())
   181                 .append(Helper.ContentVars.CONTAINEDBY.getVal())
   182                 .append(replacedRetBaseVal)
   183                 .append(Helper.ContentVars.BASE.getVal());
   185         String contents = annoData
   186                           + Helper.ContentVars.REPEATABLEANNO.getVal()
   187                           + "\nclass "+ className + "{}";
   188         return contents;
   189     }
   191     private void error(String msg,String... contents) throws Exception {
   192         System.out.println("error: " + msg);
   193         errors++;
   194         if (contents.length == 1) {
   195             System.out.println("Contents = " + contents[0]);
   196         }
   197         // Test exits as soon as it gets a failure
   198         if (exitMode) throw new Exception();
   199     }
   200 }

mercurial