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