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  *          jdk.jdeps/com.sun.tools.javap
  36  * @build Wrapper toolbox.ToolBox
  37  * @run main Wrapper OverlappingSrcDst
  38  */
  39 
  40 import java.io.File;
  41 import java.nio.file.Paths;
  42 
  43 public class OverlappingSrcDst extends SJavacTester {
  44     public static void main(String... args) {
  45         new OverlappingSrcDst().run();
  46     }
  47 
  48     public void run() {
  49         String abs = ToolBox.currDir.toAbsolutePath().toString();
  50 
  51         // Relative vs relative
  52         test("dir", "dir", false);
  53         test("dir", "dir/dst", false);
  54         test("dir/src", "dir", false);
  55         test("src", "dst", true);
  56 
  57         // Absolute vs absolute
  58         test(abs + "/dir", abs + "/dir", false);
  59         test(abs + "/dir", abs + "/dir/dst", false);
  60         test(abs + "/dir/src", abs + "/dir", false);
  61         test(abs + "/src", abs + "/dst", true);
  62 
  63         // Absolute vs relative
  64         test(abs + "/dir", "dir", false);
  65         test(abs + "/dir", "dir/dst", false);
  66         test(abs + "/dir/src", "dir", false);
  67         test(abs + "/src", "dst", true);
  68 
  69         // Relative vs absolute
  70         test("dir", abs + "/dir", false);
  71         test("dir", abs + "/dir/dst", false);
  72         test("dir/src", abs + "/dir", false);
  73         test("src", abs + "/dst", true);
  74     }
  75 
  76     private void test(String srcDir, String dstDir, boolean shouldSucceed) {
  77         boolean succeeded = testCompilation(srcDir, dstDir);
  78         if (shouldSucceed != succeeded) {
  79             throw new AssertionError(
  80                     String.format("Test failed for "
  81                                           + "srcDir=\"%s\", "
  82                                           + "dstDir=\"%s\". "
  83                                           + "Compilation was expected to %s but %s.",
  84                                   srcDir,
  85                                   dstDir,
  86                                   shouldSucceed ? "succeed" : "fail",
  87                                   succeeded ? "succeeded" : "failed"));
  88         }
  89     }
  90 
  91     private boolean testCompilation(String srcDir, String dstDir) {
  92         try {
  93             srcDir = srcDir.replace('/', File.separatorChar);
  94             dstDir = dstDir.replace('/', File.separatorChar);
  95             tb.writeFile(Paths.get(srcDir, "pkg", "A.java"), "package pkg; class A {}");
  96             compile("--state-dir=state", "-src", srcDir, "-d", dstDir);
  97             return true;
  98         } catch (Exception e) {
  99             return false;
 100         }
 101     }
 102 }