Path: blob/master/test/langtools/tools/javac/6394683/T6394683.java
41149 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 639468326* @summary need to resolve different file-type precedence semantics for javac and 26927* @modules java.compiler28* jdk.compiler29*/3031import java.io.*;32import javax.tools.*;3334public class T6394683 {35static final String testSrc = System.getProperty("test.src", ".");36static final File a_java = new File(testSrc, "A.java");37static final File a_class = new File("A.class");38static final File b_class = new File("B.class");39static final File b_java = new File("B.java");4041static abstract class TestFile extends File {42TestFile(File file) {43super(file.getPath());44}4546abstract void create() throws IOException;47}4849static class JavaTestFile extends TestFile {50JavaTestFile(File file, String text) {51super(file);52this.text = text;53}5455void create() throws IOException {56BufferedWriter out = new BufferedWriter(new FileWriter(this));57out.write(text);58out.newLine();59out.close();60}6162private String text;63}6465static TestFile good_java = new JavaTestFile(b_java, "class B { }");66static TestFile bad_java = new JavaTestFile(b_java, "class B");6768static TestFile good_class = new TestFile(b_class) {69void create() throws IOException {70JavaCompiler javac = ToolProvider.getSystemJavaCompiler();71int rc = javac.run(null, null, null,72"-d", ".",73new File(testSrc, "B.java").getPath());74if (rc != 0)75throw new AssertionError("compilation failed, rc=" + rc + " creating B.class");76}77};7879static TestFile bad_class = new TestFile(b_class) {80void create() throws IOException {81FileOutputStream out = new FileOutputStream(b_class);82out.close();83}84};858687public static void main(String ... args) throws Exception {88boolean ok;8990ok = test("-Xprefer:source", good_java, bad_class);91ok &= test("-Xprefer:source", bad_class, good_java);92ok &= test("-Xprefer:newer", bad_java, good_class);93ok &= test("-Xprefer:newer", bad_class, good_java);9495if (!ok)96throw new AssertionError("test failed");97}9899static boolean test(String opt, TestFile older, TestFile newer) throws Exception {100101// ensure clean start102a_class.delete();103b_java.delete();104b_class.delete();105106older.create();107newer.create();108if (!older.exists() || !newer.exists())109throw new AssertionError("error creating files");110111int n = 0;112while (newer.lastModified() <= older.lastModified()) {113if (++n == 5)114throw new Error("Cannot create files");115Thread.sleep(1000);116newer.create();117}118119System.err.println("test:"120+ "option:" + opt + ", "121+ "older:" + older + "[" + older.length() + ":" + older.lastModified() + "], "122+ "newer:" + newer + "[" + newer.length() + ":" + newer.lastModified() + "]");123for (String s: new File(".").list())124System.err.print(" " + s);125System.err.println();126127JavaCompiler javac = ToolProvider.getSystemJavaCompiler();128int rc = javac.run(null, null, null,129"-d", ".",130"-classpath", ".",131"-sourcepath", ".",132opt,133a_java.getPath());134if (rc != 0) {135error("compilation failed, rc=" + rc + ", option: " + opt + ", older:" + older + ", newer" + newer);136return false;137}138139return true;140}141142static void error(String msg) {143System.err.println(msg);144}145}146147148