test/tools/javac/6394683/T6394683.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

duke@1 1 /*
ohair@554 2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
duke@1 22 */
duke@1 23
duke@1 24 /**
duke@1 25 * @test
duke@1 26 * @bug 6394683
duke@1 27 * @summary need to resolve different file-type precedence semantics for javac and 269
duke@1 28 */
duke@1 29
duke@1 30 import java.io.*;
duke@1 31 import javax.tools.*;
duke@1 32
duke@1 33 public class T6394683 {
duke@1 34 static final String testSrc = System.getProperty("test.src", ".");
duke@1 35 static final File a_java = new File(testSrc, "A.java");
duke@1 36 static final File a_class = new File("A.class");
duke@1 37 static final File b_class = new File("B.class");
duke@1 38 static final File b_java = new File("B.java");
duke@1 39
duke@1 40 static abstract class TestFile extends File {
duke@1 41 TestFile(File file) {
duke@1 42 super(file.getPath());
duke@1 43 }
duke@1 44
duke@1 45 abstract void create() throws IOException;
duke@1 46 }
duke@1 47
duke@1 48 static class JavaTestFile extends TestFile {
duke@1 49 JavaTestFile(File file, String text) {
duke@1 50 super(file);
duke@1 51 this.text = text;
duke@1 52 }
duke@1 53
duke@1 54 void create() throws IOException {
duke@1 55 BufferedWriter out = new BufferedWriter(new FileWriter(this));
duke@1 56 out.write(text);
duke@1 57 out.newLine();
duke@1 58 out.close();
duke@1 59 }
duke@1 60
duke@1 61 private String text;
duke@1 62 }
duke@1 63
duke@1 64 static TestFile good_java = new JavaTestFile(b_java, "class B { }");
duke@1 65 static TestFile bad_java = new JavaTestFile(b_java, "class B");
duke@1 66
duke@1 67 static TestFile good_class = new TestFile(b_class) {
duke@1 68 void create() throws IOException {
duke@1 69 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
duke@1 70 int rc = javac.run(null, null, null,
duke@1 71 "-d", ".",
duke@1 72 new File(testSrc, "B.java").getPath());
duke@1 73 if (rc != 0)
duke@1 74 throw new AssertionError("compilation failed, rc=" + rc + " creating B.class");
duke@1 75 }
duke@1 76 };
duke@1 77
duke@1 78 static TestFile bad_class = new TestFile(b_class) {
duke@1 79 void create() throws IOException {
duke@1 80 FileOutputStream out = new FileOutputStream(b_class);
duke@1 81 out.close();
duke@1 82 }
duke@1 83 };
duke@1 84
duke@1 85
duke@1 86 public static void main(String ... args) throws Exception {
duke@1 87 boolean ok;
duke@1 88
duke@1 89 ok = test("-Xprefer:source", good_java, bad_class);
duke@1 90 ok &= test("-Xprefer:source", bad_class, good_java);
duke@1 91 ok &= test("-Xprefer:newer", bad_java, good_class);
duke@1 92 ok &= test("-Xprefer:newer", bad_class, good_java);
duke@1 93
duke@1 94 if (!ok)
duke@1 95 throw new AssertionError("test failed");
duke@1 96 }
duke@1 97
duke@1 98 static boolean test(String opt, TestFile older, TestFile newer) throws Exception {
duke@1 99
duke@1 100 // ensure clean start
duke@1 101 a_class.delete();
duke@1 102 b_java.delete();
duke@1 103 b_class.delete();
duke@1 104
duke@1 105 older.create();
duke@1 106 newer.create();
duke@1 107 if (!older.exists() || !newer.exists())
duke@1 108 throw new AssertionError("error creating files");
duke@1 109
duke@1 110 int n = 0;
duke@1 111 while (newer.lastModified() <= older.lastModified()) {
duke@1 112 if (++n == 5)
duke@1 113 throw new Error("Cannot create files");
duke@1 114 Thread.sleep(1000);
duke@1 115 newer.create();
duke@1 116 }
duke@1 117
duke@1 118 System.err.println("test:"
duke@1 119 + "option:" + opt + ", "
duke@1 120 + "older:" + older + "[" + older.length() + ":" + older.lastModified() + "], "
duke@1 121 + "newer:" + newer + "[" + newer.length() + ":" + newer.lastModified() + "]");
duke@1 122 for (String s: new File(".").list())
duke@1 123 System.err.print(" " + s);
duke@1 124 System.err.println();
duke@1 125
duke@1 126 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
duke@1 127 int rc = javac.run(null, null, null,
duke@1 128 "-d", ".",
duke@1 129 "-classpath", ".",
duke@1 130 "-sourcepath", ".",
duke@1 131 opt,
duke@1 132 a_java.getPath());
duke@1 133 if (rc != 0) {
duke@1 134 error("compilation failed, rc=" + rc + ", option: " + opt + ", older:" + older + ", newer" + newer);
duke@1 135 return false;
duke@1 136 }
duke@1 137
duke@1 138 return true;
duke@1 139 }
duke@1 140
duke@1 141 static void error(String msg) {
duke@1 142 System.err.println(msg);
duke@1 143 }
duke@1 144 }

mercurial