aoqi@0: /* aoqi@0: * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 6394683 aoqi@0: * @summary need to resolve different file-type precedence semantics for javac and 269 aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: public class T6394683 { aoqi@0: static final String testSrc = System.getProperty("test.src", "."); aoqi@0: static final File a_java = new File(testSrc, "A.java"); aoqi@0: static final File a_class = new File("A.class"); aoqi@0: static final File b_class = new File("B.class"); aoqi@0: static final File b_java = new File("B.java"); aoqi@0: aoqi@0: static abstract class TestFile extends File { aoqi@0: TestFile(File file) { aoqi@0: super(file.getPath()); aoqi@0: } aoqi@0: aoqi@0: abstract void create() throws IOException; aoqi@0: } aoqi@0: aoqi@0: static class JavaTestFile extends TestFile { aoqi@0: JavaTestFile(File file, String text) { aoqi@0: super(file); aoqi@0: this.text = text; aoqi@0: } aoqi@0: aoqi@0: void create() throws IOException { aoqi@0: BufferedWriter out = new BufferedWriter(new FileWriter(this)); aoqi@0: out.write(text); aoqi@0: out.newLine(); aoqi@0: out.close(); aoqi@0: } aoqi@0: aoqi@0: private String text; aoqi@0: } aoqi@0: aoqi@0: static TestFile good_java = new JavaTestFile(b_java, "class B { }"); aoqi@0: static TestFile bad_java = new JavaTestFile(b_java, "class B"); aoqi@0: aoqi@0: static TestFile good_class = new TestFile(b_class) { aoqi@0: void create() throws IOException { aoqi@0: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); aoqi@0: int rc = javac.run(null, null, null, aoqi@0: "-d", ".", aoqi@0: new File(testSrc, "B.java").getPath()); aoqi@0: if (rc != 0) aoqi@0: throw new AssertionError("compilation failed, rc=" + rc + " creating B.class"); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: static TestFile bad_class = new TestFile(b_class) { aoqi@0: void create() throws IOException { aoqi@0: FileOutputStream out = new FileOutputStream(b_class); aoqi@0: out.close(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: public static void main(String ... args) throws Exception { aoqi@0: boolean ok; aoqi@0: aoqi@0: ok = test("-Xprefer:source", good_java, bad_class); aoqi@0: ok &= test("-Xprefer:source", bad_class, good_java); aoqi@0: ok &= test("-Xprefer:newer", bad_java, good_class); aoqi@0: ok &= test("-Xprefer:newer", bad_class, good_java); aoqi@0: aoqi@0: if (!ok) aoqi@0: throw new AssertionError("test failed"); aoqi@0: } aoqi@0: aoqi@0: static boolean test(String opt, TestFile older, TestFile newer) throws Exception { aoqi@0: aoqi@0: // ensure clean start aoqi@0: a_class.delete(); aoqi@0: b_java.delete(); aoqi@0: b_class.delete(); aoqi@0: aoqi@0: older.create(); aoqi@0: newer.create(); aoqi@0: if (!older.exists() || !newer.exists()) aoqi@0: throw new AssertionError("error creating files"); aoqi@0: aoqi@0: int n = 0; aoqi@0: while (newer.lastModified() <= older.lastModified()) { aoqi@0: if (++n == 5) aoqi@0: throw new Error("Cannot create files"); aoqi@0: Thread.sleep(1000); aoqi@0: newer.create(); aoqi@0: } aoqi@0: aoqi@0: System.err.println("test:" aoqi@0: + "option:" + opt + ", " aoqi@0: + "older:" + older + "[" + older.length() + ":" + older.lastModified() + "], " aoqi@0: + "newer:" + newer + "[" + newer.length() + ":" + newer.lastModified() + "]"); aoqi@0: for (String s: new File(".").list()) aoqi@0: System.err.print(" " + s); aoqi@0: System.err.println(); aoqi@0: aoqi@0: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); aoqi@0: int rc = javac.run(null, null, null, aoqi@0: "-d", ".", aoqi@0: "-classpath", ".", aoqi@0: "-sourcepath", ".", aoqi@0: opt, aoqi@0: a_java.getPath()); aoqi@0: if (rc != 0) { aoqi@0: error("compilation failed, rc=" + rc + ", option: " + opt + ", older:" + older + ", newer" + newer); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: static void error(String msg) { aoqi@0: System.err.println(msg); aoqi@0: } aoqi@0: }