langtools/test/tools/javac/annotations/typeAnnotations/referenceinfos/Driver.java
changeset 22443 0922d94d0576
parent 17578 46ac954e4a84
child 23393 1698e8b53501
equal deleted inserted replaced
22442:8fd30fc4e3a3 22443:0922d94d0576
     1 /*
     1 /*
     2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    28 import java.io.PrintStream;
    28 import java.io.PrintStream;
    29 import java.io.PrintWriter;
    29 import java.io.PrintWriter;
    30 import java.lang.annotation.*;
    30 import java.lang.annotation.*;
    31 import java.lang.reflect.*;
    31 import java.lang.reflect.*;
    32 import java.util.ArrayList;
    32 import java.util.ArrayList;
       
    33 import java.util.Arrays;
    33 import java.util.Collections;
    34 import java.util.Collections;
    34 import java.util.HashMap;
    35 import java.util.HashMap;
    35 import java.util.List;
    36 import java.util.List;
    36 import java.util.Map;
    37 import java.util.Map;
    37 
    38 
    48             throw new IllegalArgumentException("Usage: java Driver <test-name>");
    49             throw new IllegalArgumentException("Usage: java Driver <test-name>");
    49         String name = args[0];
    50         String name = args[0];
    50         Class<?> clazz = Class.forName(name);
    51         Class<?> clazz = Class.forName(name);
    51         new Driver().runDriver(clazz.newInstance());
    52         new Driver().runDriver(clazz.newInstance());
    52     }
    53     }
       
    54 
       
    55     String[][] extraParamsCombinations = new String[][] {
       
    56         new String[] { },
       
    57         new String[] { "-g" },
       
    58     };
    53 
    59 
    54     protected void runDriver(Object object) throws Exception {
    60     protected void runDriver(Object object) throws Exception {
    55         int passed = 0, failed = 0;
    61         int passed = 0, failed = 0;
    56         Class<?> clazz = object.getClass();
    62         Class<?> clazz = object.getClass();
    57         out.println("Tests for " + clazz.getName());
    63         out.println("Tests for " + clazz.getName());
    63                 continue;
    69                 continue;
    64             if (method.getReturnType() != String.class)
    70             if (method.getReturnType() != String.class)
    65                 throw new IllegalArgumentException("Test method needs to return a string: " + method);
    71                 throw new IllegalArgumentException("Test method needs to return a string: " + method);
    66             String testClass = testClassOf(method);
    72             String testClass = testClassOf(method);
    67 
    73 
    68             try {
    74             for (String[] extraParams : extraParamsCombinations) {
    69                 String compact = (String)method.invoke(object);
    75                 try {
    70                 String fullFile = wrap(compact);
    76                     String compact = (String)method.invoke(object);
    71                 ClassFile cf = compileAndReturn(fullFile, testClass);
    77                     String fullFile = wrap(compact);
    72                 List<TypeAnnotation> actual = ReferenceInfoUtil.extendedAnnotationsOf(cf);
    78                     ClassFile cf = compileAndReturn(fullFile, testClass, extraParams);
    73                 ReferenceInfoUtil.compare(expected, actual, cf);
    79                     List<TypeAnnotation> actual = ReferenceInfoUtil.extendedAnnotationsOf(cf);
    74                 out.println("PASSED:  " + method.getName());
    80                     ReferenceInfoUtil.compare(expected, actual, cf);
    75                 ++passed;
    81                     out.println("PASSED:  " + method.getName());
    76             } catch (Throwable e) {
    82                     ++passed;
    77                 out.println("FAILED:  " + method.getName());
    83                 } catch (Throwable e) {
    78                 out.println("    " + e.toString());
    84                     out.println("FAILED:  " + method.getName());
    79                 ++failed;
    85                     out.println("    " + e.toString());
       
    86                     ++failed;
       
    87                 }
    80             }
    88             }
    81         }
    89         }
    82 
    90 
    83         out.println();
    91         out.println();
    84         int total = passed + failed;
    92         int total = passed + failed;
   154         } else {
   162         } else {
   155             return "Test";
   163             return "Test";
   156         }
   164         }
   157     }
   165     }
   158 
   166 
   159     private ClassFile compileAndReturn(String fullFile, String testClass) throws Exception {
   167     private ClassFile compileAndReturn(String fullFile, String testClass, String... extraParams) throws Exception {
   160         File source = writeTestFile(fullFile);
   168         File source = writeTestFile(fullFile);
   161         File clazzFile = compileTestFile(source, testClass);
   169         File clazzFile = compileTestFile(source, testClass);
   162         return ClassFile.read(clazzFile);
   170         return ClassFile.read(clazzFile);
   163     }
   171     }
   164 
   172 
   168         out.println(fullFile);
   176         out.println(fullFile);
   169         out.close();
   177         out.close();
   170         return f;
   178         return f;
   171     }
   179     }
   172 
   180 
   173     protected File compileTestFile(File f, String testClass) {
   181     protected File compileTestFile(File f, String testClass, String... extraParams) {
   174         int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
   182         List<String> options = new ArrayList<>();
       
   183         options.addAll(Arrays.asList("-source", "1.8"));
       
   184         options.addAll(Arrays.asList(extraParams));
       
   185         options.add(f.getPath());
       
   186         int rc = com.sun.tools.javac.Main.compile(options.toArray(new String[options.size()]));
   175         if (rc != 0)
   187         if (rc != 0)
   176             throw new Error("compilation failed. rc=" + rc);
   188             throw new Error("compilation failed. rc=" + rc);
   177         String path;
   189         String path;
   178         if (f.getParent() != null) {
   190         if (f.getParent() != null) {
   179             path = f.getParent();
   191             path = f.getParent();