Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6394683/T6394683.java
38813 views
/*1* Copyright (c) 2006, 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*/2829import java.io.*;30import javax.tools.*;3132public class T6394683 {33static final String testSrc = System.getProperty("test.src", ".");34static final File a_java = new File(testSrc, "A.java");35static final File a_class = new File("A.class");36static final File b_class = new File("B.class");37static final File b_java = new File("B.java");3839static abstract class TestFile extends File {40TestFile(File file) {41super(file.getPath());42}4344abstract void create() throws IOException;45}4647static class JavaTestFile extends TestFile {48JavaTestFile(File file, String text) {49super(file);50this.text = text;51}5253void create() throws IOException {54BufferedWriter out = new BufferedWriter(new FileWriter(this));55out.write(text);56out.newLine();57out.close();58}5960private String text;61}6263static TestFile good_java = new JavaTestFile(b_java, "class B { }");64static TestFile bad_java = new JavaTestFile(b_java, "class B");6566static TestFile good_class = new TestFile(b_class) {67void create() throws IOException {68JavaCompiler javac = ToolProvider.getSystemJavaCompiler();69int rc = javac.run(null, null, null,70"-d", ".",71new File(testSrc, "B.java").getPath());72if (rc != 0)73throw new AssertionError("compilation failed, rc=" + rc + " creating B.class");74}75};7677static TestFile bad_class = new TestFile(b_class) {78void create() throws IOException {79FileOutputStream out = new FileOutputStream(b_class);80out.close();81}82};838485public static void main(String ... args) throws Exception {86boolean ok;8788ok = test("-Xprefer:source", good_java, bad_class);89ok &= test("-Xprefer:source", bad_class, good_java);90ok &= test("-Xprefer:newer", bad_java, good_class);91ok &= test("-Xprefer:newer", bad_class, good_java);9293if (!ok)94throw new AssertionError("test failed");95}9697static boolean test(String opt, TestFile older, TestFile newer) throws Exception {9899// ensure clean start100a_class.delete();101b_java.delete();102b_class.delete();103104older.create();105newer.create();106if (!older.exists() || !newer.exists())107throw new AssertionError("error creating files");108109int n = 0;110while (newer.lastModified() <= older.lastModified()) {111if (++n == 5)112throw new Error("Cannot create files");113Thread.sleep(1000);114newer.create();115}116117System.err.println("test:"118+ "option:" + opt + ", "119+ "older:" + older + "[" + older.length() + ":" + older.lastModified() + "], "120+ "newer:" + newer + "[" + newer.length() + ":" + newer.lastModified() + "]");121for (String s: new File(".").list())122System.err.print(" " + s);123System.err.println();124125JavaCompiler javac = ToolProvider.getSystemJavaCompiler();126int rc = javac.run(null, null, null,127"-d", ".",128"-classpath", ".",129"-sourcepath", ".",130opt,131a_java.getPath());132if (rc != 0) {133error("compilation failed, rc=" + rc + ", option: " + opt + ", older:" + older + ", newer" + newer);134return false;135}136137return true;138}139140static void error(String msg) {141System.err.println(msg);142}143}144145146