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