test/tools/javac/policy/test1/Test1b.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2008, 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 6420151
    26  * @summary Compile a group of files and validate the set of class files produced
    27  * @run main Test1b -XDcompilePolicy=byfile A.java B.java D.java
    28  */
    30 /*
    31  * @test 6420151
    32  * @summary Compile a group of files and validate the set of class files produced
    33  * @run main Test1b -XDcompilePolicy=byfile A.java C.java D.java
    34  */
    36 /*
    37  * @test 6420151
    38  * @summary Compile a group of files and validate the set of class files produced
    39  * @run main Test1b -XDcompilePolicy=simple A.java B.java D.java
    40  */
    42 /*
    43  * @test 6420151
    44  * @summary Compile a group of files and validate the set of class files produced
    45  * @run main Test1b -XDcompilePolicy=simple A.java C.java D.java
    46  */
    48 // These test cases should be uncommented when the default compile policy is
    49 // changed to "byfile".  While the default policy is "bytodo", the test cases fail
    50 ///*
    51 // * @test 6420151
    52 // * @summary Compile a group of files and validate the set of class files produced
    53 // * @run main Test1b A.java B.java D.java
    54 // */
    55 //
    56 ///*
    57 // * @test 6420151
    58 // * @summary Compile a group of files and validate the set of class files produced
    59 // * @run main Test1b A.java C.java D.java
    60 // */
    62 // These test cases are retained for debugging; if uncommented, they show that
    63 // to bytodo mode fails the "all or none" class file test
    64 ///*
    65 // * @test 6420151
    66 // * @summary Compile a group of files and validate the set of class files produced
    67 // * @run main Test1b -XDcompilePolicy=bytodo A.java B.java D.java
    68 // */
    69 //
    70 ///*
    71 // * @test 6420151
    72 // * @summary Compile a group of files and validate the set of class files produced
    73 // * @run main Test1b -XDcompilePolicy=bytodo A.java C.java D.java
    74 // */
    76 import java.io.File;
    77 import java.io.PrintWriter;
    78 import java.io.StringWriter;
    79 import java.util.ArrayList;
    80 import java.util.HashMap;
    81 import java.util.List;
    82 import java.util.Map;
    83 import java.util.regex.Matcher;
    84 import java.util.regex.Pattern;
    86 public class Test1b
    87 {
    88     public static void main(String... args) throws Exception {
    89         new Test1b().run(args);
    90     }
    92     void run(String... args) throws Exception {
    93         File testSrcDir = new File(System.getProperty("test.src"));
    94         File tmpClassDir = new File(".");
    95         List<String> l = new ArrayList<String>();
    96         l.add("-d");
    97         l.add(tmpClassDir.getPath());
    98         for (String a: args) {
    99             if (a.endsWith(".java"))
   100                 l.add(new File(testSrcDir, a).getPath());
   101             else
   102                 l.add(a);
   103         }
   105         StringWriter sw = new StringWriter();
   106         int rc = com.sun.tools.javac.Main.compile(l.toArray(new String[l.size()]), new PrintWriter(sw));
   107         System.err.println(sw);
   109         Pattern p = Pattern.compile("([A-Z]+).*");
   110         for (String name: tmpClassDir.list()) {
   111             if (name.endsWith(".class")) {
   112                 Matcher m = p.matcher(name);
   113                 if (m.matches()) {
   114                     found(m.group(1), name);
   115                 }
   116             }
   117         }
   119         // for all classes that might have been compiled, check that
   120         // all the classes in the source file get generated, or none do.
   121         check("A", 3);
   122         check("B", 3);
   123         check("C", 3);
   124         check("D", 3);
   126         if (errors > 0)
   127             throw new Exception(errors + " errors");
   128     }
   130     void check(String prefix, int expect) {
   131         List<String> names = map.get(prefix);
   132         int found = (names == null ? 0 : names.size());
   133         if (found == 0 || found == expect) {
   134             System.err.println("Found " + found + " files for " + prefix + ": OK");
   135             return;
   136         }
   137         error("Found " + found + " files for " + prefix + ": expected 0 or " + expect + " " + names);
   138     }
   140     void found(String prefix, String name) {
   141         List<String> names = map.get(prefix);
   142         if (names == null) {
   143             names = new ArrayList<String>();
   144             map.put(prefix, names);
   145         }
   146         names.add(name);
   147     }
   149     void error(String message) {
   150         System.err.println(message);
   151         errors++;
   152     }
   154     Map<String,List<String>> map = new HashMap<String,List<String>>();
   155     int errors;
   156 }

mercurial