ohrstrom@1504: /* ohrstrom@1504: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. ohrstrom@1504: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1504: * ohrstrom@1504: * This code is free software; you can redistribute it and/or modify it ohrstrom@1504: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1504: * published by the Free Software Foundation. ohrstrom@1504: * ohrstrom@1504: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1504: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1504: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1504: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1504: * accompanied this code). ohrstrom@1504: * ohrstrom@1504: * You should have received a copy of the GNU General Public License version ohrstrom@1504: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1504: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1504: * ohrstrom@1504: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1504: * or visit www.oracle.com if you need additional information or have any ohrstrom@1504: * questions. ohrstrom@1504: */ ohrstrom@1504: ohrstrom@1504: import java.util.*; ohrstrom@1504: import java.io.*; ohrstrom@1504: import java.net.*; ohrstrom@1504: import java.nio.file.*; ohrstrom@1504: import java.nio.file.attribute.*; ohrstrom@1504: import java.nio.charset.*; ohrstrom@1504: ohrstrom@1504: import com.sun.tools.sjavac.Main; ohrstrom@1504: ohrstrom@1504: public ohrstrom@1504: class SJavac { ohrstrom@1504: ohrstrom@1504: public static void main(String... args) throws Exception { ohrstrom@1504: SJavac s = new SJavac(); ohrstrom@1504: s.test(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: FileSystem defaultfs = FileSystems.getDefault(); ohrstrom@1504: ohrstrom@1504: // Where to put generated sources that will ohrstrom@1504: // test aspects of sjavac, ie JTWork/scratch/gensrc ohrstrom@1504: Path gensrc; ohrstrom@1504: // More gensrc dirs are used to test merging of serveral source roots. ohrstrom@1504: Path gensrc2; ohrstrom@1504: Path gensrc3; ohrstrom@1504: ohrstrom@1504: // Where to put compiled classes. ohrstrom@1504: Path bin; ohrstrom@1504: // Where to put c-header files. ohrstrom@1504: Path headers; ohrstrom@1504: ohrstrom@1504: // The sjavac compiler. ohrstrom@1504: Main main = new Main(); ohrstrom@1504: ohrstrom@1504: // Remember the previous bin and headers state here. ohrstrom@1504: Map previous_bin_state; ohrstrom@1504: Map previous_headers_state; ohrstrom@1504: ohrstrom@1504: public void test() throws Exception { ohrstrom@1504: gensrc = defaultfs.getPath("gensrc"); ohrstrom@1504: gensrc2 = defaultfs.getPath("gensrc2"); ohrstrom@1504: gensrc3 = defaultfs.getPath("gensrc3"); ohrstrom@1504: bin = defaultfs.getPath("bin"); ohrstrom@1504: headers = defaultfs.getPath("headers"); ohrstrom@1504: ohrstrom@1504: Files.createDirectory(gensrc); ohrstrom@1504: Files.createDirectory(gensrc2); ohrstrom@1504: Files.createDirectory(gensrc3); ohrstrom@1504: Files.createDirectory(bin); ohrstrom@1504: Files.createDirectory(headers); ohrstrom@1504: ohrstrom@1504: initialCompile(); ohrstrom@1504: incrementalCompileNoChanges(); ohrstrom@1504: incrementalCompileDroppingClasses(); ohrstrom@1504: incrementalCompileWithChange(); ohrstrom@1504: incrementalCompileDropAllNatives(); ohrstrom@1504: incrementalCompileAddNative(); ohrstrom@1504: incrementalCompileChangeNative(); ohrstrom@1504: compileWithOverrideSource(); ohrstrom@1504: compileWithInvisibleSources(); ohrstrom@1504: compileCircularSources(); erikj@1953: compileExcludingDependency(); ohrstrom@1504: ohrstrom@1504: delete(gensrc); ohrstrom@1504: delete(gensrc2); ohrstrom@1504: delete(gensrc3); ohrstrom@1504: delete(bin); erikj@1953: delete(headers); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void initialCompile() throws Exception { ohrstrom@1504: System.out.println("\nInitial compile of gensrc."); ohrstrom@1504: System.out.println("----------------------------"); ohrstrom@1504: populate(gensrc, ohrstrom@1504: "alfa/AINT.java", ohrstrom@1504: "package alfa; public interface AINT { void aint(); }", ohrstrom@1504: ohrstrom@1504: "alfa/A.java", ohrstrom@1504: "package alfa; public class A implements AINT { "+ ohrstrom@1504: "public final static int DEFINITION = 17; public void aint() { } }", ohrstrom@1504: ohrstrom@1504: "alfa/AA.java", ohrstrom@1504: "package alfa;"+ ohrstrom@1504: "// A package private class, not contributing to the public api.\n"+ ohrstrom@1504: "class AA {"+ ohrstrom@1504: " // A properly nested static inner class.\n"+ ohrstrom@1504: " static class AAA { }\n"+ ohrstrom@1504: " // A properly nested inner class.\n"+ ohrstrom@1504: " class AAAA { }\n"+ ohrstrom@1504: " Runnable foo() {\n"+ ohrstrom@1504: " // A proper anonymous class.\n"+ ohrstrom@1504: " return new Runnable() { public void run() { } };\n"+ ohrstrom@1504: " }\n"+ ohrstrom@1504: " AAA aaa;\n"+ ohrstrom@1504: " AAAA aaaa;\n"+ ohrstrom@1504: " AAAAA aaaaa;\n"+ ohrstrom@1504: "}\n"+ ohrstrom@1504: "class AAAAA {\n"+ ohrstrom@1504: " // A bad auxiliary class, but no one is referencing it\n"+ ohrstrom@1504: " // from outside of this source file, therefore it is ok.\n"+ ohrstrom@1504: "}\n", ohrstrom@1504: ohrstrom@1504: "beta/BINT.java", ohrstrom@1504: "package beta;public interface BINT { void foo(); }", ohrstrom@1504: ohrstrom@1504: "beta/B.java", ohrstrom@1504: "package beta; import alfa.A; public class B {"+ ohrstrom@1504: "private int b() { return A.DEFINITION; } native void foo(); }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: previous_bin_state = collectState(bin); ohrstrom@1504: previous_headers_state = collectState(headers); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileNoChanges() throws Exception { ohrstrom@1504: System.out.println("\nTesting that no change in sources implies no change in binaries."); ohrstrom@1504: System.out.println("------------------------------------------------------------------"); ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyEqual(new_bin_state, previous_bin_state); ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyEqual(previous_headers_state, new_headers_state); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileDroppingClasses() throws Exception { ohrstrom@1504: System.out.println("\nTesting that deleting AA.java deletes all"); ohrstrom@1504: System.out.println("generated inner class as well as AA.class"); ohrstrom@1504: System.out.println("-----------------------------------------"); ohrstrom@1504: removeFrom(gensrc, "alfa/AA.java"); ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyThatFilesHaveBeenRemoved(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/alfa/AA$1.class", ohrstrom@1504: "bin/alfa/AA$AAAA.class", ohrstrom@1504: "bin/alfa/AA$AAA.class", ohrstrom@1504: "bin/alfa/AAAAA.class", ohrstrom@1504: "bin/alfa/AA.class"); ohrstrom@1504: ohrstrom@1504: previous_bin_state = new_bin_state; ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyEqual(previous_headers_state, new_headers_state); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileWithChange() throws Exception { ohrstrom@1504: System.out.println("\nNow update the A.java file with a new timestamps and"); ohrstrom@1504: System.out.println("new final static definition. This should trigger a recompile,"); ohrstrom@1504: System.out.println("not only of alfa, but also beta."); ohrstrom@1504: System.out.println("But check that the generated native header was not updated!"); ohrstrom@1504: System.out.println("Since we did not modify the native api of B."); ohrstrom@1504: System.out.println("-------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"alfa/A.java", ohrstrom@1504: "package alfa; public class A implements AINT { "+ ohrstrom@1504: "public final static int DEFINITION = 18; public void aint() { } private void foo() { } }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: ohrstrom@1504: verifyNewerFiles(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/alfa/A.class", ohrstrom@1504: "bin/alfa/AINT.class", ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/beta/BINT.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: previous_bin_state = new_bin_state; ohrstrom@1504: ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyEqual(new_headers_state, previous_headers_state); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileDropAllNatives() throws Exception { ohrstrom@1504: System.out.println("\nNow update the B.java file with one less native method,"); ohrstrom@1504: System.out.println("ie it has no longer any methods!"); ohrstrom@1504: System.out.println("Verify that beta_B.h is removed!"); ohrstrom@1504: System.out.println("---------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"beta/B.java", ohrstrom@1504: "package beta; import alfa.A; public class B {"+ ohrstrom@1504: "private int b() { return A.DEFINITION; } }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyNewerFiles(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/beta/BINT.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: previous_bin_state = new_bin_state; ohrstrom@1504: ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyThatFilesHaveBeenRemoved(previous_headers_state, new_headers_state, ohrstrom@1504: "headers/beta_B.h"); ohrstrom@1504: previous_headers_state = new_headers_state; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileAddNative() throws Exception { ohrstrom@1504: System.out.println("\nNow update the B.java file with a final static annotated with @Native."); ohrstrom@1504: System.out.println("Verify that beta_B.h is added again!"); ohrstrom@1504: System.out.println("------------------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"beta/B.java", ohrstrom@1504: "package beta; import alfa.A; public class B {"+ ohrstrom@1504: "private int b() { return A.DEFINITION; } "+ ohrstrom@1504: "@java.lang.annotation.Native final static int alfa = 42; }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyNewerFiles(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/beta/BINT.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: previous_bin_state = new_bin_state; ohrstrom@1504: ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyThatFilesHaveBeenAdded(previous_headers_state, new_headers_state, ohrstrom@1504: "headers/beta_B.h"); ohrstrom@1504: previous_headers_state = new_headers_state; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void incrementalCompileChangeNative() throws Exception { ohrstrom@1504: System.out.println("\nNow update the B.java file with a new value for the final static"+ ohrstrom@1504: " annotated with @Native."); ohrstrom@1504: System.out.println("Verify that beta_B.h is rewritten again!"); ohrstrom@1504: System.out.println("-------------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"beta/B.java", ohrstrom@1504: "package beta; import alfa.A; public class B {"+ ohrstrom@1504: "private int b() { return A.DEFINITION; } "+ ohrstrom@1504: "@java.lang.annotation.Native final static int alfa = 43; }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false", "--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyNewerFiles(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/beta/BINT.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: previous_bin_state = new_bin_state; ohrstrom@1504: ohrstrom@1504: Map new_headers_state = collectState(headers); ohrstrom@1504: verifyNewerFiles(previous_headers_state, new_headers_state, ohrstrom@1504: "headers/beta_B.h"); ohrstrom@1504: previous_headers_state = new_headers_state; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void compileWithOverrideSource() throws Exception { ohrstrom@1504: System.out.println("\nNow verify that we can override sources to be compiled."); ohrstrom@1504: System.out.println("Compile gensrc and gensrc2. However do not compile broken beta.B in gensrc,"); ohrstrom@1504: System.out.println("only compile ok beta.B in gensrc2."); ohrstrom@1504: System.out.println("---------------------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: delete(gensrc); ohrstrom@1504: delete(gensrc2); ohrstrom@1504: delete(bin); ohrstrom@1504: previous_bin_state = collectState(bin); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"alfa/A.java", ohrstrom@1504: "package alfa; import beta.B; import gamma.C; public class A { B b; C c; }", ohrstrom@1504: "beta/B.java", ohrstrom@1504: "package beta; public class B { broken", ohrstrom@1504: "gamma/C.java", ohrstrom@1504: "package gamma; public class C { }"); ohrstrom@1504: ohrstrom@1504: populate(gensrc2, ohrstrom@1504: "beta/B.java", ohrstrom@1504: "package beta; public class B { }"); ohrstrom@1504: ohrstrom@1504: compile("-x", "beta", "gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/alfa/A.class", ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/gamma/C.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: ohrstrom@1504: System.out.println("----- Compile with exluded beta went well!"); ohrstrom@1504: delete(bin); ohrstrom@1504: compileExpectFailure("gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false"); ohrstrom@1504: ohrstrom@1504: System.out.println("----- Compile without exluded beta failed, as expected! Good!"); ohrstrom@1504: delete(bin); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void compileWithInvisibleSources() throws Exception { ohrstrom@1504: System.out.println("\nNow verify that we can make sources invisible to linking (sourcepath)."); ohrstrom@1504: System.out.println("Compile gensrc and link against gensrc2 and gensrc3, however"); ohrstrom@1504: System.out.println("gensrc2 contains broken code in beta.B, thus we must exclude that package"); ohrstrom@1504: System.out.println("fortunately gensrc3 contains a proper beta.B."); ohrstrom@1504: System.out.println("------------------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: // Start with a fresh gensrcs and bin. ohrstrom@1504: delete(gensrc); ohrstrom@1504: delete(gensrc2); ohrstrom@1504: delete(gensrc3); ohrstrom@1504: delete(bin); ohrstrom@1504: previous_bin_state = collectState(bin); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"alfa/A.java", ohrstrom@1504: "package alfa; import beta.B; import gamma.C; public class A { B b; C c; }"); ohrstrom@1504: populate(gensrc2,"beta/B.java", ohrstrom@1504: "package beta; public class B { broken", ohrstrom@1504: "gamma/C.java", ohrstrom@1504: "package gamma; public class C { }"); ohrstrom@1504: populate(gensrc3, "beta/B.java", ohrstrom@1504: "package beta; public class B { }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-x", "beta", "-sourcepath", "gensrc2", ohrstrom@1504: "-sourcepath", "gensrc3", "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false"); ohrstrom@1504: ohrstrom@1504: System.out.println("The first compile went well!"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/alfa/A.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: ohrstrom@1504: System.out.println("----- Compile with exluded beta went well!"); ohrstrom@1504: delete(bin); ohrstrom@1504: compileExpectFailure("gensrc", "-sourcepath", "gensrc2", "-sourcepath", "gensrc3", ohrstrom@1504: "-d", "bin", "-h", "headers", "-j", "1", ohrstrom@1504: "--server:portfile=testserver,background=false"); ohrstrom@1504: ohrstrom@1504: System.out.println("----- Compile without exluded beta failed, as expected! Good!"); ohrstrom@1504: delete(bin); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void compileCircularSources() throws Exception { ohrstrom@1504: System.out.println("\nNow verify that circular sources split on multiple cores can be compiled."); ohrstrom@1504: System.out.println("---------------------------------------------------------------------------"); ohrstrom@1504: ohrstrom@1504: // Start with a fresh gensrcs and bin. ohrstrom@1504: delete(gensrc); ohrstrom@1504: delete(gensrc2); ohrstrom@1504: delete(gensrc3); ohrstrom@1504: delete(bin); ohrstrom@1504: previous_bin_state = collectState(bin); ohrstrom@1504: ohrstrom@1504: populate(gensrc,"alfa/A.java", ohrstrom@1504: "package alfa; public class A { beta.B b; }", ohrstrom@1504: "beta/B.java", ohrstrom@1504: "package beta; public class B { gamma.C c; }", ohrstrom@1504: "gamma/C.java", ohrstrom@1504: "package gamma; public class C { alfa.A a; }"); ohrstrom@1504: ohrstrom@1504: compile("gensrc", "-d", "bin", "-h", "headers", "-j", "3", ohrstrom@1504: "--server:portfile=testserver,background=false","--log=debug"); ohrstrom@1504: Map new_bin_state = collectState(bin); ohrstrom@1504: verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state, ohrstrom@1504: "bin/alfa/A.class", ohrstrom@1504: "bin/beta/B.class", ohrstrom@1504: "bin/gamma/C.class", ohrstrom@1504: "bin/javac_state"); ohrstrom@1504: delete(bin); ohrstrom@1504: } ohrstrom@1504: erikj@1953: /** erikj@1953: * Tests compiling class A that depends on class B without compiling class B erikj@1953: * @throws Exception If test fails erikj@1953: */ erikj@1953: void compileExcludingDependency() throws Exception { erikj@1953: System.out.println("\nVerify that excluding classes from compilation but not from linking works."); erikj@1953: System.out.println("---------------------------------------------------------------------------"); erikj@1953: erikj@1953: delete(gensrc); erikj@1953: delete(bin); erikj@1953: previous_bin_state = collectState(bin); erikj@1953: erikj@1953: populate(gensrc, erikj@1953: "alfa/A.java", erikj@1953: "package alfa; public class A { beta.B b; }", erikj@1953: "beta/B.java", erikj@1953: "package beta; public class B { }"); erikj@1953: erikj@1953: compile("-x", "beta", "-src", "gensrc", "-x", "alfa", "-sourcepath", "gensrc", erikj@1953: "-d", "bin", "--server:portfile=testserver,background=false"); erikj@1953: erikj@1953: Map new_bin_state = collectState(bin); erikj@1953: verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state, erikj@1953: "bin/alfa/A.class", erikj@1953: "bin/javac_state"); erikj@1953: } erikj@1953: ohrstrom@1504: void removeFrom(Path dir, String... args) throws IOException { ohrstrom@1504: for (String filename : args) { ohrstrom@1504: Path p = dir.resolve(filename); ohrstrom@1504: Files.delete(p); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void populate(Path src, String... args) throws IOException { ohrstrom@1504: if (!Files.exists(src)) { ohrstrom@1504: Files.createDirectory(src); ohrstrom@1504: } ohrstrom@1504: String[] a = args; ohrstrom@1504: for (int i = 0; i() { ohrstrom@1504: @Override ohrstrom@1504: public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException ohrstrom@1504: { ohrstrom@1504: Files.delete(file); ohrstrom@1504: return FileVisitResult.CONTINUE; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: @Override ohrstrom@1504: public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException ohrstrom@1504: { ohrstrom@1504: if (e == null) { ohrstrom@1504: if (!dir.equals(root)) Files.delete(dir); ohrstrom@1504: return FileVisitResult.CONTINUE; ohrstrom@1504: } else { ohrstrom@1504: // directory iteration failed ohrstrom@1504: throw e; ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: }); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void compile(String... args) throws Exception { ohrstrom@1504: int rc = main.go(args, System.out, System.err); ohrstrom@1504: if (rc != 0) throw new Exception("Error during compile!"); ohrstrom@1504: ohrstrom@1504: // Wait a second, to get around the (temporary) problem with ohrstrom@1504: // second resolution in the Java file api. But do not do this ohrstrom@1504: // on windows where the timestamps work. ohrstrom@1504: long in_a_sec = System.currentTimeMillis()+1000; ohrstrom@1504: while (in_a_sec > System.currentTimeMillis()) { ohrstrom@1504: try { ohrstrom@1504: Thread.sleep(1000); ohrstrom@1504: } catch (InterruptedException e) { ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void compileExpectFailure(String... args) throws Exception { ohrstrom@1504: int rc = main.go(args, System.out, System.err); ohrstrom@1504: if (rc == 0) throw new Exception("Expected error during compile! Did not fail!"); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: Map collectState(Path dir) throws IOException ohrstrom@1504: { ohrstrom@1504: final Map files = new HashMap<>(); ohrstrom@1504: Files.walkFileTree(dir, new SimpleFileVisitor() { ohrstrom@1504: @Override ohrstrom@1504: public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) ohrstrom@1504: throws IOException ohrstrom@1504: { ohrstrom@1504: files.put(file.toString(),new Long(Files.getLastModifiedTime(file).toMillis())); ohrstrom@1504: return FileVisitResult.CONTINUE; ohrstrom@1504: } ohrstrom@1504: }); ohrstrom@1504: return files; ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void verifyThatFilesHaveBeenRemoved(Map from, ohrstrom@1504: Map to, ohrstrom@1504: String... args) throws Exception { ohrstrom@1504: ohrstrom@1504: Set froms = from.keySet(); ohrstrom@1504: Set tos = to.keySet(); ohrstrom@1504: ohrstrom@1504: if (froms.equals(tos)) { ohrstrom@1504: throw new Exception("Expected new state to have fewer files than previous state!"); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: for (String t : tos) { ohrstrom@1504: if (!froms.contains(t)) { ohrstrom@1504: throw new Exception("Expected "+t+" to exist in previous state!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: for (String f : args) { ohrstrom@1504: f = f.replace("/", File.separator); ohrstrom@1504: if (!froms.contains(f)) { ohrstrom@1504: throw new Exception("Expected "+f+" to exist in previous state!"); ohrstrom@1504: } ohrstrom@1504: if (tos.contains(f)) { ohrstrom@1504: throw new Exception("Expected "+f+" to have been removed from the new state!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: if (froms.size() - args.length != tos.size()) { ohrstrom@1504: throw new Exception("There are more removed files than the expected list!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void verifyThatFilesHaveBeenAdded(Map from, ohrstrom@1504: Map to, ohrstrom@1504: String... args) throws Exception { ohrstrom@1504: ohrstrom@1504: Set froms = from.keySet(); ohrstrom@1504: Set tos = to.keySet(); ohrstrom@1504: ohrstrom@1504: if (froms.equals(tos)) { ohrstrom@1504: throw new Exception("Expected new state to have more files than previous state!"); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: for (String t : froms) { ohrstrom@1504: if (!tos.contains(t)) { ohrstrom@1504: throw new Exception("Expected "+t+" to exist in new state!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: for (String f : args) { ohrstrom@1504: f = f.replace("/", File.separator); ohrstrom@1504: if (!tos.contains(f)) { ohrstrom@1504: throw new Exception("Expected "+f+" to have been added to new state!"); ohrstrom@1504: } ohrstrom@1504: if (froms.contains(f)) { ohrstrom@1504: throw new Exception("Expected "+f+" to not exist in previous state!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: if (froms.size() + args.length != tos.size()) { ohrstrom@1504: throw new Exception("There are more added files than the expected list!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void verifyNewerFiles(Map from, ohrstrom@1504: Map to, ohrstrom@1504: String... args) throws Exception { ohrstrom@1504: if (!from.keySet().equals(to.keySet())) { ohrstrom@1504: throw new Exception("Expected the set of files to be identical!"); ohrstrom@1504: } ohrstrom@1504: Set files = new HashSet(); ohrstrom@1504: for (String s : args) { ohrstrom@1504: files.add(s.replace("/", File.separator)); ohrstrom@1504: } ohrstrom@1504: for (String fn : from.keySet()) { ohrstrom@1504: long f = from.get(fn); ohrstrom@1504: long t = to.get(fn); ohrstrom@1504: if (files.contains(fn)) { ohrstrom@1504: if (t <= f) { ohrstrom@1504: throw new Exception("Expected "+fn+" to have a more recent timestamp!"); ohrstrom@1504: } ohrstrom@1504: } else { ohrstrom@1504: if (t != f) { ohrstrom@1504: throw new Exception("Expected "+fn+" to have the same timestamp!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: String print(Map m) { ohrstrom@1504: StringBuilder b = new StringBuilder(); ohrstrom@1504: Set keys = m.keySet(); ohrstrom@1504: for (String k : keys) { ohrstrom@1504: b.append(k+" "+m.get(k)+"\n"); ohrstrom@1504: } ohrstrom@1504: return b.toString(); ohrstrom@1504: } ohrstrom@1504: ohrstrom@1504: void verifyEqual(Map from, Map to) throws Exception { ohrstrom@1504: if (!from.equals(to)) { ohrstrom@1504: System.out.println("FROM---"+print(from)); ohrstrom@1504: System.out.println("TO-----"+print(to)); ohrstrom@1504: throw new Exception("The dir should not differ! But it does!"); ohrstrom@1504: } ohrstrom@1504: } ohrstrom@1504: }