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

changeset 119
1e83972f53fb
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/policy/test1/Test1b.java	Tue Sep 23 10:44:51 2008 -0700
     1.3 @@ -0,0 +1,156 @@
     1.4 +/*
     1.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test 6420151
    1.29 + * @summary Compile a group of files and validate the set of class files produced
    1.30 + * @run main Test1b -XDcompilePolicy=byfile A.java B.java D.java
    1.31 + */
    1.32 +
    1.33 +/*
    1.34 + * @test 6420151
    1.35 + * @summary Compile a group of files and validate the set of class files produced
    1.36 + * @run main Test1b -XDcompilePolicy=byfile A.java C.java D.java
    1.37 + */
    1.38 +
    1.39 +/*
    1.40 + * @test 6420151
    1.41 + * @summary Compile a group of files and validate the set of class files produced
    1.42 + * @run main Test1b -XDcompilePolicy=simple A.java B.java D.java
    1.43 + */
    1.44 +
    1.45 +/*
    1.46 + * @test 6420151
    1.47 + * @summary Compile a group of files and validate the set of class files produced
    1.48 + * @run main Test1b -XDcompilePolicy=simple A.java C.java D.java
    1.49 + */
    1.50 +
    1.51 +// These test cases should be uncommented when the default compile policy is
    1.52 +// changed to "byfile".  While the default policy is "bytodo", the test cases fail
    1.53 +///*
    1.54 +// * @test 6420151
    1.55 +// * @summary Compile a group of files and validate the set of class files produced
    1.56 +// * @run main Test1b A.java B.java D.java
    1.57 +// */
    1.58 +//
    1.59 +///*
    1.60 +// * @test 6420151
    1.61 +// * @summary Compile a group of files and validate the set of class files produced
    1.62 +// * @run main Test1b A.java C.java D.java
    1.63 +// */
    1.64 +
    1.65 +// These test cases are retained for debugging; if uncommented, they show that
    1.66 +// to bytodo mode fails the "all or none" class file test
    1.67 +///*
    1.68 +// * @test 6420151
    1.69 +// * @summary Compile a group of files and validate the set of class files produced
    1.70 +// * @run main Test1b -XDcompilePolicy=bytodo A.java B.java D.java
    1.71 +// */
    1.72 +//
    1.73 +///*
    1.74 +// * @test 6420151
    1.75 +// * @summary Compile a group of files and validate the set of class files produced
    1.76 +// * @run main Test1b -XDcompilePolicy=bytodo A.java C.java D.java
    1.77 +// */
    1.78 +
    1.79 +import java.io.File;
    1.80 +import java.io.PrintWriter;
    1.81 +import java.io.StringWriter;
    1.82 +import java.util.ArrayList;
    1.83 +import java.util.HashMap;
    1.84 +import java.util.List;
    1.85 +import java.util.Map;
    1.86 +import java.util.regex.Matcher;
    1.87 +import java.util.regex.Pattern;
    1.88 +
    1.89 +public class Test1b
    1.90 +{
    1.91 +    public static void main(String... args) throws Exception {
    1.92 +        new Test1b().run(args);
    1.93 +    }
    1.94 +
    1.95 +    void run(String... args) throws Exception {
    1.96 +        File testSrcDir = new File(System.getProperty("test.src"));
    1.97 +        File tmpClassDir = new File(".");
    1.98 +        List<String> l = new ArrayList<String>();
    1.99 +        l.add("-d");
   1.100 +        l.add(tmpClassDir.getPath());
   1.101 +        for (String a: args) {
   1.102 +            if (a.endsWith(".java"))
   1.103 +                l.add(new File(testSrcDir, a).getPath());
   1.104 +            else
   1.105 +                l.add(a);
   1.106 +        }
   1.107 +
   1.108 +        StringWriter sw = new StringWriter();
   1.109 +        int rc = com.sun.tools.javac.Main.compile(l.toArray(new String[l.size()]), new PrintWriter(sw));
   1.110 +        System.err.println(sw);
   1.111 +
   1.112 +        Pattern p = Pattern.compile("([A-Z]+).*");
   1.113 +        for (String name: tmpClassDir.list()) {
   1.114 +            if (name.endsWith(".class")) {
   1.115 +                Matcher m = p.matcher(name);
   1.116 +                if (m.matches()) {
   1.117 +                    found(m.group(1), name);
   1.118 +                }
   1.119 +            }
   1.120 +        }
   1.121 +
   1.122 +        // for all classes that might have been compiled, check that
   1.123 +        // all the classes in the source file get generated, or none do.
   1.124 +        check("A", 3);
   1.125 +        check("B", 3);
   1.126 +        check("C", 3);
   1.127 +        check("D", 3);
   1.128 +
   1.129 +        if (errors > 0)
   1.130 +            throw new Exception(errors + " errors");
   1.131 +    }
   1.132 +
   1.133 +    void check(String prefix, int expect) {
   1.134 +        List<String> names = map.get(prefix);
   1.135 +        int found = (names == null ? 0 : names.size());
   1.136 +        if (found == 0 || found == expect) {
   1.137 +            System.err.println("Found " + found + " files for " + prefix + ": OK");
   1.138 +            return;
   1.139 +        }
   1.140 +        error("Found " + found + " files for " + prefix + ": expected 0 or " + expect + " " + names);
   1.141 +    }
   1.142 +
   1.143 +    void found(String prefix, String name) {
   1.144 +        List<String> names = map.get(prefix);
   1.145 +        if (names == null) {
   1.146 +            names = new ArrayList<String>();
   1.147 +            map.put(prefix, names);
   1.148 +        }
   1.149 +        names.add(name);
   1.150 +    }
   1.151 +
   1.152 +    void error(String message) {
   1.153 +        System.err.println(message);
   1.154 +        errors++;
   1.155 +    }
   1.156 +
   1.157 +    Map<String,List<String>> map = new HashMap<String,List<String>>();
   1.158 +    int errors;
   1.159 +}

mercurial