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