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

     1 /*
     2  * Copyright (c) 2006, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /**
    25  * @test
    26  * @bug 6394683
    27  * @summary need to resolve different file-type precedence semantics for javac and 269
    28  */
    30 import java.io.*;
    31 import javax.tools.*;
    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");
    40     static abstract class TestFile extends File {
    41         TestFile(File file) {
    42             super(file.getPath());
    43         }
    45         abstract void create() throws IOException;
    46     }
    48     static class JavaTestFile extends TestFile {
    49         JavaTestFile(File file, String text) {
    50             super(file);
    51             this.text = text;
    52         }
    54         void create() throws IOException {
    55             BufferedWriter out = new BufferedWriter(new FileWriter(this));
    56             out.write(text);
    57             out.newLine();
    58             out.close();
    59         }
    61         private String text;
    62     }
    64     static TestFile good_java = new JavaTestFile(b_java, "class B { }");
    65     static TestFile bad_java  = new JavaTestFile(b_java, "class B");
    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         };
    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         };
    86     public static void main(String ... args) throws Exception {
    87         boolean ok;
    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);
    94         if (!ok)
    95             throw new AssertionError("test failed");
    96     }
    98     static boolean test(String opt, TestFile older, TestFile newer) throws Exception {
   100         // ensure clean start
   101         a_class.delete();
   102         b_java.delete();
   103         b_class.delete();
   105         older.create();
   106         newer.create();
   107         if (!older.exists() || !newer.exists())
   108             throw new AssertionError("error creating files");
   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         }
   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();
   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         }
   138         return true;
   139     }
   141     static void error(String msg) {
   142         System.err.println(msg);
   143     }
   144 }

mercurial