Mon, 13 Dec 2010 14:56:50 +0000
7006109: Add test library to simplify the task of writing automated type-system tests
Summary: Types.java needs to be more stress-tested
Reviewed-by: jjg
jjg@12 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. |
jjg@12 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@12 | 4 | * |
jjg@12 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@12 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@12 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@12 | 10 | * |
jjg@12 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@12 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@12 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@12 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@12 | 15 | * accompanied this code). |
jjg@12 | 16 | * |
jjg@12 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@12 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@12 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@12 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
jjg@12 | 24 | */ |
jjg@12 | 25 | |
jjg@12 | 26 | /* |
jjg@12 | 27 | * @test |
jjg@12 | 28 | * @bug 6668794 6668796 |
jjg@12 | 29 | * @summary javac puts localized text in raw diagnostics |
jjg@12 | 30 | * bad diagnostic "bad class file" given for source files |
jjg@12 | 31 | */ |
jjg@12 | 32 | |
jjg@12 | 33 | import java.io.*; |
jjg@12 | 34 | import java.util.*; |
jjg@12 | 35 | import javax.tools.*; |
jjg@12 | 36 | |
jjg@12 | 37 | public class Test { |
jjg@12 | 38 | public static void main(String[] args) throws Exception { |
jjg@12 | 39 | new Test().run(); |
jjg@12 | 40 | } |
jjg@12 | 41 | |
jjg@12 | 42 | void run() throws Exception { |
jjg@12 | 43 | |
jjg@12 | 44 | // compile q.A then move it to p.A |
jjg@12 | 45 | compile("A.java"); |
jjg@12 | 46 | |
jjg@12 | 47 | File p = new File("p"); |
jjg@12 | 48 | p.mkdirs(); |
jjg@12 | 49 | new File("q/A.class").renameTo(new File("p/A.class")); |
jjg@12 | 50 | |
jjg@12 | 51 | // compile B against p.A |
jjg@12 | 52 | String[] out = compile("B.java"); |
jjg@12 | 53 | if (out.length == 0) |
jjg@12 | 54 | throw new Error("no diagnostics generated"); |
jjg@12 | 55 | |
jjg@12 | 56 | String expected = "B.java:6:6: compiler.err.cant.access: p.A, " + |
mcimadamore@221 | 57 | "(compiler.misc.bad.class.file.header: A.class, " + |
mcimadamore@221 | 58 | "(compiler.misc.class.file.wrong.class: q.A))"; |
jjg@12 | 59 | |
jjg@12 | 60 | if (!out[0].equals(expected)) { |
jjg@12 | 61 | System.err.println("expected: " + expected); |
jjg@12 | 62 | System.err.println(" found: " + out[0]); |
jjg@12 | 63 | throw new Error("test failed"); |
jjg@12 | 64 | } |
jjg@12 | 65 | } |
jjg@12 | 66 | |
jjg@12 | 67 | String[] compile(String file) { |
jjg@12 | 68 | String[] options = { |
jjg@12 | 69 | "-XDrawDiagnostics", |
jjg@12 | 70 | "-d", ".", |
jjg@12 | 71 | "-classpath", ".", |
jjg@12 | 72 | new File(testSrc, file).getPath() |
jjg@12 | 73 | }; |
jjg@12 | 74 | |
jjg@12 | 75 | System.err.println("compile: " + Arrays.asList(options)); |
jjg@12 | 76 | StringWriter sw = new StringWriter(); |
jjg@12 | 77 | PrintWriter out = new PrintWriter(sw); |
jjg@12 | 78 | int rc = com.sun.tools.javac.Main.compile(options, out); |
jjg@12 | 79 | out.close(); |
jjg@12 | 80 | |
jjg@12 | 81 | String outText = sw.toString(); |
jjg@12 | 82 | System.err.println(outText); |
jjg@12 | 83 | |
jjg@12 | 84 | return sw.toString().split("[\\r\\n]+"); |
jjg@12 | 85 | } |
jjg@12 | 86 | |
jjg@12 | 87 | File testSrc = new File(System.getProperty("test.src", ".")); |
jjg@12 | 88 | } |