Path: blob/master/test/langtools/tools/sjavac/OverlappingSrcDst.java
41144 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Make sure sjavac doesn't allow overlapping source and destination26* directories.27* @bug 806132028* @library /tools/lib29* @modules jdk.compiler/com.sun.tools.javac.api30* jdk.compiler/com.sun.tools.javac.main31* jdk.compiler/com.sun.tools.sjavac32* @build Wrapper toolbox.ToolBox33* @run main Wrapper OverlappingSrcDst34*/3536import java.io.File;37import java.nio.file.Paths;3839import toolbox.ToolBox;4041public class OverlappingSrcDst extends SJavacTester {42public static void main(String... args) {43new OverlappingSrcDst().run();44}4546public void run() {47String abs = ToolBox.currDir.toAbsolutePath().toString();4849// Relative vs relative50test("dir", "dir", false);51test("dir", "dir/dst", false);52test("dir/src", "dir", false);53test("src", "dst", true);5455// Absolute vs absolute56test(abs + "/dir", abs + "/dir", false);57test(abs + "/dir", abs + "/dir/dst", false);58test(abs + "/dir/src", abs + "/dir", false);59test(abs + "/src", abs + "/dst", true);6061// Absolute vs relative62test(abs + "/dir", "dir", false);63test(abs + "/dir", "dir/dst", false);64test(abs + "/dir/src", "dir", false);65test(abs + "/src", "dst", true);6667// Relative vs absolute68test("dir", abs + "/dir", false);69test("dir", abs + "/dir/dst", false);70test("dir/src", abs + "/dir", false);71test("src", abs + "/dst", true);72}7374private void test(String srcDir, String dstDir, boolean shouldSucceed) {75boolean succeeded = testCompilation(srcDir, dstDir);76if (shouldSucceed != succeeded) {77throw new AssertionError(78String.format("Test failed for "79+ "srcDir=\"%s\", "80+ "dstDir=\"%s\". "81+ "Compilation was expected to %s but %s.",82srcDir,83dstDir,84shouldSucceed ? "succeed" : "fail",85succeeded ? "succeeded" : "failed"));86}87}8889private boolean testCompilation(String srcDir, String dstDir) {90try {91srcDir = srcDir.replace('/', File.separatorChar);92dstDir = dstDir.replace('/', File.separatorChar);93tb.writeFile(Paths.get(srcDir, "pkg", "A.java"), "package pkg; class A {}");94compile("--state-dir=state", "-src", srcDir, "-d", dstDir);95return true;96} catch (Exception e) {97return false;98}99}100}101102103