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