test/langtools/tools/sjavac/OverlappingSrcDst.java
changeset 47216 71c04702a3d5
parent 40308 274367a99f98
child 52377 907fdbbdf584
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @summary Make sure sjavac doesn't allow overlapping source and destination
       
    29  *          directories.
       
    30  * @bug 8061320
       
    31  * @library /tools/lib
       
    32  * @modules jdk.compiler/com.sun.tools.javac.api
       
    33  *          jdk.compiler/com.sun.tools.javac.main
       
    34  *          jdk.compiler/com.sun.tools.sjavac
       
    35  * @build Wrapper toolbox.ToolBox
       
    36  * @run main Wrapper OverlappingSrcDst
       
    37  */
       
    38 
       
    39 import java.io.File;
       
    40 import java.nio.file.Paths;
       
    41 
       
    42 import toolbox.ToolBox;
       
    43 
       
    44 public class OverlappingSrcDst extends SJavacTester {
       
    45     public static void main(String... args) {
       
    46         new OverlappingSrcDst().run();
       
    47     }
       
    48 
       
    49     public void run() {
       
    50         String abs = ToolBox.currDir.toAbsolutePath().toString();
       
    51 
       
    52         // Relative vs relative
       
    53         test("dir", "dir", false);
       
    54         test("dir", "dir/dst", false);
       
    55         test("dir/src", "dir", false);
       
    56         test("src", "dst", true);
       
    57 
       
    58         // Absolute vs absolute
       
    59         test(abs + "/dir", abs + "/dir", false);
       
    60         test(abs + "/dir", abs + "/dir/dst", false);
       
    61         test(abs + "/dir/src", abs + "/dir", false);
       
    62         test(abs + "/src", abs + "/dst", true);
       
    63 
       
    64         // Absolute vs relative
       
    65         test(abs + "/dir", "dir", false);
       
    66         test(abs + "/dir", "dir/dst", false);
       
    67         test(abs + "/dir/src", "dir", false);
       
    68         test(abs + "/src", "dst", true);
       
    69 
       
    70         // Relative vs absolute
       
    71         test("dir", abs + "/dir", false);
       
    72         test("dir", abs + "/dir/dst", false);
       
    73         test("dir/src", abs + "/dir", false);
       
    74         test("src", abs + "/dst", true);
       
    75     }
       
    76 
       
    77     private void test(String srcDir, String dstDir, boolean shouldSucceed) {
       
    78         boolean succeeded = testCompilation(srcDir, dstDir);
       
    79         if (shouldSucceed != succeeded) {
       
    80             throw new AssertionError(
       
    81                     String.format("Test failed for "
       
    82                                           + "srcDir=\"%s\", "
       
    83                                           + "dstDir=\"%s\". "
       
    84                                           + "Compilation was expected to %s but %s.",
       
    85                                   srcDir,
       
    86                                   dstDir,
       
    87                                   shouldSucceed ? "succeed" : "fail",
       
    88                                   succeeded ? "succeeded" : "failed"));
       
    89         }
       
    90     }
       
    91 
       
    92     private boolean testCompilation(String srcDir, String dstDir) {
       
    93         try {
       
    94             srcDir = srcDir.replace('/', File.separatorChar);
       
    95             dstDir = dstDir.replace('/', File.separatorChar);
       
    96             tb.writeFile(Paths.get(srcDir, "pkg", "A.java"), "package pkg; class A {}");
       
    97             compile("--state-dir=state", "-src", srcDir, "-d", dstDir);
       
    98             return true;
       
    99         } catch (Exception e) {
       
   100             return false;
       
   101         }
       
   102     }
       
   103 }