From 9f60df326b07bee937866fd7f3ca61d5364d9791 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Tue, 2 Sep 2014 12:15:48 +0200 Subject: [PATCH 1/9] New unit tests for compilation unit (work in progress) --- .../common/cleardep/CompilationUnitTest.java | 505 ++++++++++++++++++ .../common/cleardep/TestCompilationUnit.java | 30 ++ 2 files changed, 535 insertions(+) create mode 100644 src/org/sugarj/common/cleardep/CompilationUnitTest.java create mode 100644 src/org/sugarj/common/cleardep/TestCompilationUnit.java diff --git a/src/org/sugarj/common/cleardep/CompilationUnitTest.java b/src/org/sugarj/common/cleardep/CompilationUnitTest.java new file mode 100644 index 0000000..b63aec1 --- /dev/null +++ b/src/org/sugarj/common/cleardep/CompilationUnitTest.java @@ -0,0 +1,505 @@ +package org.sugarj.common.cleardep; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sugarj.common.FileCommands; +import org.sugarj.common.cleardep.mode.ForEditorMode; +import org.sugarj.common.cleardep.mode.Mode; +import org.sugarj.common.path.AbsolutePath; +import org.sugarj.common.path.Path; +import org.sugarj.common.path.RelativePath; + +public class CompilationUnitTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + TestFile sourceFolder; + TestFile compileFolder; + TestFile editedFolder; + + Stamper testStamper = ContentHashStamper.instance; + Mode editorMode = new ForEditorMode(null, true); + TestCompilationUnit a, b, c, d, e; + TestFile f1, f2, f3, f4, f5; + Map sourceArtifacts; + + @Before + public void setUp() throws Exception { + + sourceFolder = new TestFile(tempFolder.newFolder("source")); + compileFolder = new TestFile(tempFolder.newFolder("compile")); + editedFolder = new TestFile(tempFolder.newFolder("edited")); + + // test modules + a = generateRandomModule(); + b = generateRandomModule(); + c = generateRandomModule(); + d = generateRandomModule(); + e = generateRandomModule(); + + // test files + f1 = generateRandomFileIn(editedFolder); + f2 = generateRandomFileIn(editedFolder); + f3 = generateRandomFileIn(editedFolder); + f4 = generateRandomFileIn(editedFolder); + f5 = generateRandomFileIn(editedFolder); + + this.sourceArtifacts = new HashMap(); + } + + @After + public void tearDown() throws Exception { + + } + + + // Tests + + @Test + public void test_getCircularFileDependencies() { + + // TODO: write + } + + @Test + public void test_dependsOn_1() throws Exception { + + a.addModuleDependency(b); // a -> b + + assertTrue(a.dependsOnNoncircularly(b)); + assertTrue(a.dependsOn(b)); + // assertFalse(a.dependsOnTransitively(b)); //TODO: why false? + + b.addModuleDependency(c); // a -> b -> c + + assertTrue(a.dependsOnTransitivelyNoncircularly(c)); + assertTrue(a.dependsOnTransitively(c)); + + } + + @Test + public void test_dependsOn_2() throws Exception { + + a.addCircularModuleDependency(b); // a <-> b + b.addModuleDependency(a); + + assertTrue(a.dependsOn(b)); + assertFalse(a.dependsOnNoncircularly(b)); + + b.addModuleDependency(c); // a <-> b -> c + + // assertTrue(a.dependsOnTransitively(c)); //TODO: why false? + assertFalse(a.dependsOnTransitivelyNoncircularly(c)); + + } + + @Test + public void test_dependsOn_3() throws Exception { + + a.addModuleDependency(b); // a -> b + b.addCircularModuleDependency(c); // a -> b <-> c + c.addModuleDependency(b); + + assertTrue(a.dependsOnTransitively(c)); + assertFalse(a.dependsOnTransitivelyNoncircularly(c)); + + } + + @Test + public void test_isConsistentShallow_1() throws Exception { + + // assertTrue(a.isConsistentShallow( null, testMode)); //TODO: why does this + // fail? + assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + + TestFile f1 = generateRandomFileIn(sourceFolder); + // source artifacts + a.addSourceArtifact(f1.relativeTo(sourceFolder)); + assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + FileCommands.delete(f1); + // assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); // + // TODO: + // why doesn't this fail? + + } + + @Test + public void test_isConsistentShallow_2() throws Exception { + + a.addExternalFileDependency(f1); + assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + + b.addExternalFileDependency(f2); + assertTrue(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + changeContentOf(f2); + assertFalse(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + + } + + @Test + public void test_isConsistentShallow_3() throws Exception { + + a.addGeneratedFile(f1); + assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + + b.addGeneratedFile(f2); + assertTrue(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + changeContentOf(f2); + assertFalse(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + } + + @Test + public void test_isConsistent_1() throws Exception { + + // assertTrue(a.isConsistent( null, testMode)); //TODO: Why fail? + assertTrue(a.isConsistent(a.sourceArtifacts, editorMode)); + + // shallow dependency + a.addExternalFileDependency(f1); + assertTrue(a.isConsistent(a.sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistent(a.sourceArtifacts, editorMode)); + + } + + @Test + public void test_isConsistent_2() throws Exception { + + // a -> b + + sourceArtifacts.putAll(a.sourceArtifacts); + sourceArtifacts.putAll(b.sourceArtifacts); + a.addModuleDependency(b); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + + // a -> b (b inconsistent) + b.addExternalFileDependency(f1); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + } + + @Test + public void test_isConsistent_3() throws Exception { + + // a <-> b + sourceArtifacts.putAll(a.sourceArtifacts); + sourceArtifacts.putAll(b.sourceArtifacts); + a.addCircularModuleDependency(b); + b.addModuleDependency(a); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + + // a <-> b (b inconsistent) + b.addExternalFileDependency(f1); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + + } + + @Test + public void test_isConsistent_4() throws Exception { + + // a -> b -> c + sourceArtifacts.putAll(a.sourceArtifacts); + sourceArtifacts.putAll(b.sourceArtifacts); + sourceArtifacts.putAll(c.sourceArtifacts); + a.addModuleDependency(b); + b.addModuleDependency(c); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + + // a -> b -> c (c inconsistent) + c.addExternalFileDependency(f1); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + + } + + @Test + public void test_isConsistent_5() throws Exception { + + // a -> x (synth) -> b,c + + sourceArtifacts.putAll(a.sourceArtifacts); + sourceArtifacts.putAll(b.sourceArtifacts); // TODO: why is this nessessary??! + sourceArtifacts.putAll(c.sourceArtifacts); // TODO: why is this nessessary??! + Set synModules = new HashSet<>(); + synModules.add(b); + synModules.add(c); + + Set externalDependencies = new HashSet(); + externalDependencies.add(f1); + + TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); + + sourceArtifacts.putAll(x.sourceArtifacts); + + a.addModuleDependency(x); + + + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + FileCommands.delete(f1); + assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + } + + @Test + public void test_isConsistent_6() throws Exception { + + // a -> x (synth) -> b,c (c inconsistent) + + sourceArtifacts.putAll(a.sourceArtifacts); + sourceArtifacts.putAll(b.sourceArtifacts); // TODO: why is this nessessary? + sourceArtifacts.putAll(c.sourceArtifacts); // TODO: why is this nessessary? + Set synModules = new HashSet<>(); + synModules.add(b); + synModules.add(c); + + Set externalDependencies = new HashSet(); + externalDependencies.add(f1); + + TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); + + sourceArtifacts.putAll(x.sourceArtifacts); + + a.addModuleDependency(x); + + // make c inconsistent + c.addGeneratedFile(f2); + assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + + FileCommands.delete(f2); + assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + } + + @Test + public void test_liftEditedToCompiled() throws IOException { + + TestFile e1CompiledDep = randomPathIn(sourceFolder); + TestFile e2CompiledDep = randomPathIn(sourceFolder); + TestFile e3CompiledDep = randomPathIn(sourceFolder); + + TestFile e1EditedDep = randomPathIn(sourceFolder); + TestFile e2EditedDep = randomPathIn(sourceFolder); + TestFile e3EditedDep = randomPathIn(sourceFolder); + + Set e1SourceFiles = new HashSet(); + e1SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); + e1SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); + + Set e2SourceFiles = new HashSet(); + e2SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); + + Set e3SourceFiles = new HashSet(); + e3SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); + + Map e1EditedSourceFiles = new HashMap(); + Map e2EditedSourceFiles = new HashMap(); + Map e3EditedSourceFiles = new HashMap(); + + TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, e1EditedSourceFiles, new ForEditorMode(null, true), null); + + e1.addExternalFileDependency(f1); + e1.addGeneratedFile(f2); + + TestCompilationUnit e2 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e2CompiledDep, compileFolder, e2EditedDep, editedFolder, e2SourceFiles, e2EditedSourceFiles, new ForEditorMode(null, true), null); + + e2.addExternalFileDependency(f1); + e2.addExternalFileDependency(f3); + e2.addGeneratedFile(f4); + + TestCompilationUnit e3 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e3CompiledDep, compileFolder, e3EditedDep, editedFolder, e3SourceFiles, e3EditedSourceFiles, new ForEditorMode(null, true), null); + + e3.addExternalFileDependency(f1); + e3.addGeneratedFile(f2); + e3.addGeneratedFile(f5); + + e1.addModuleDependency(e2); + e2.addModuleDependency(e3); + // e3.addCircularModuleDependency(e1); //TODO: does it work like this? + + e1.liftEditedToCompiled(); + + TestFile cf1 = new TestFile(compileFolder, f1.name()); + TestFile cf2 = new TestFile(compileFolder, f2.name()); + TestFile cf3 = new TestFile(compileFolder, f3.name()); + TestFile cf4 = new TestFile(compileFolder, f4.name()); + TestFile cf5 = new TestFile(compileFolder, f5.name()); + + assertEquals(cf1, f1); + assertEquals(cf2, f2); + assertEquals(cf3, f3); + assertEquals(cf4, f4); + assertEquals(cf5, f5); + + TestCompilationUnit c1 = (TestCompilationUnit) e1.compiledCompilationUnit; + TestCompilationUnit c2 = (TestCompilationUnit) e2.compiledCompilationUnit; + TestCompilationUnit c3 = (TestCompilationUnit) e3.compiledCompilationUnit; + + assertTrue(c1.getModuleDependencies().iterator().next() == c2); + assertTrue(c2.getModuleDependencies().iterator().next() == c3); + + } + + @Test + public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { + + // TODO: write + } + + /* + * Testing needed? + * + * @Test public void computeRanks_Test() {} + * + * @Test public void addExternalFileDependencyTest() { } + * + * @Test public void addExternalFileDependencyLateTest() { } + * + * @Test public void addGeneratedFileTest() { } + * + * @Test public void addCircularModuleDependencyTest() { } + * + * @Test public void addModuleDependencyTest() { } + */ + + + // Helper methods + + /** + * + * @param length + * @return random string containing only lower case letters + */ + public static String randomString(int length) { + + char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + StringBuilder sb = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < length; i++) { + char c = chars[random.nextInt(chars.length)]; + sb.append(c); + } + return sb.toString(); + } + + public TestFile randomPathIn(TestFile folder) { + + return new TestFile(folder, randomString(15)); + } + + public TestFile generateRandomFileIn(TestFile folder) throws IOException { + TestFile file = new TestFile(folder, randomString(15), randomString(300)); + + return file; + } + + public static void changeContentOf(TestFile file) throws IOException { + + if (FileCommands.exists(file)) + file.write(randomString(300)); + else + throw new IllegalStateException("File to be changed doesn't exist"); + } + + public TestCompilationUnit generateRandomModule() throws IOException { + return generateRandomModule(null); + } + + public TestCompilationUnit generateRandomModule(Synthesizer synthesizer) throws IOException { + + Set srcFiles = new HashSet(); + srcFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); + + Map editedSourceFiles = new HashMap(); + + return TestCompilationUnit.create(TestCompilationUnit.class, + testStamper, + randomPathIn(sourceFolder), + compileFolder, + randomPathIn(sourceFolder), + editedFolder, + srcFiles, + editedSourceFiles, + editorMode, + synthesizer); + } + + + // Helper Classes + + public class TestFile extends AbsolutePath { + + public TestFile(AbsolutePath folder, String filename) { + + super(folder.getAbsolutePath() + "/" + filename); + } + public TestFile(AbsolutePath folder, String filename, String content) throws IOException { + + super(folder.getAbsolutePath() + "/" + filename); + write(content); + + } + + public String name() { + + return getFile().getName(); + } + + public RelativePath relativeTo(TestFile folder) { + + return FileCommands.getRelativePath(folder, this); + } + + public TestFile(File f) { + super(f.getAbsolutePath()); + } + + public String read() throws IOException { + return FileCommands.readFileAsString(this); + } + + public void write(String content) throws IOException { + + FileCommands.writeToFile(this, content); + } + + @Override + public boolean equals(Object o) { + + if (o instanceof TestFile) { + try { + if (((TestFile) o).read().equals(this.read())) + return true; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return false; + } + + } + +} diff --git a/src/org/sugarj/common/cleardep/TestCompilationUnit.java b/src/org/sugarj/common/cleardep/TestCompilationUnit.java new file mode 100644 index 0000000..8ef902b --- /dev/null +++ b/src/org/sugarj/common/cleardep/TestCompilationUnit.java @@ -0,0 +1,30 @@ +package org.sugarj.common.cleardep; + +import org.sugarj.common.cleardep.mode.Mode; + +/** + * Test Compilation Unit + * @author User + * + */ +class TestCompilationUnit extends CompilationUnit{ + + + public TestCompilationUnit() { + + } + +/** + * + */ +private static final long serialVersionUID = 863865445137886655L; + +@Override +protected boolean isConsistentExtend(Mode mode) { + + return true; +} + + + +} \ No newline at end of file From cf9c9893565823b71676e9d4c1c11f8bab19e211 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Tue, 2 Sep 2014 12:25:11 +0200 Subject: [PATCH 2/9] Documentation --- .../common/cleardep/CompilationUnit.java | 112 +++++++++++++++++- .../sugarj/common/cleardep/Synthesizer.java | 18 ++- 2 files changed, 127 insertions(+), 3 deletions(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index 4d1963a..590ed9c 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -22,6 +22,9 @@ /** * Dependency management for modules. + *

+ * For each module (i.e: .sugj-File) there are two CompilationUnits. One for automatic compilations (generates only temporary files) + * and one for "real" compilation that may use already generated files (if valid) * * @author Sebastian Erdweg */ @@ -37,17 +40,64 @@ public CompilationUnit() { /* for deserialization only */ } protected Synthesizer syn; + /** + * if this == compiledCompilationUnit -> temporary directory
+ * if this == editedCompilationUnit -> i.e. /bin folder + */ protected Path targetDir; + + /** + * For example the .sugj file
+ * Usually only one file + */ protected Map sourceArtifacts; + protected Set moduleDependencies; - protected Set circularModuleDependencies; + protected Set circularModuleDependencies; + + /** + * For example included .jar files + */ protected Map externalFileDependencies; + + /** + * For example generated .java .class .sdf files + */ protected Map generatedFiles; // ************************** // Methods for initialization // ************************** + /** + * + * @param cl + * @param stamper + * @param compileDep + * path to the persisted compiledCompilationUnit (.dep file) + * @param compileTarget + * i.e.: /bin folder + * @param editedDep + * path to the persisted editedCompilationUnit (.dep file) + * @param editedTarget + * i.e.: some temporary folder + * @param sourceFiles + * i.e.: .sugj file + * @param editedSourceFiles + * TODO: Correct??: is empty (not null), except source has been + * edited (i.e.: .sugj file) + * @param mode + * + * @param syn + * == null, except for generated modules (which are a product of a + * transformation on another module) + * @return
+ *
    + *
  • edited mode -> editedCompilationUnit + *
  • compiled mode -> compiledCompilationUnit + *
+ * @throws IOException + */ @SuppressWarnings("unchecked") final protected static E create(Class cl, Stamper stamper, Path compileDep, Path compileTarget, Path editedDep, Path editedTarget, Set sourceFiles, Map editedSourceFiles, Mode mode, Synthesizer syn) throws IOException { E compileE; @@ -90,6 +140,23 @@ final protected static E create(Class cl, Stamper return e; } + /** + * a) mode == DoCompileMode (true) checks all files in the compileTarget + * folder (i.e. /bin) and all other dependent modules for consistency + * + * @param cl + * @param stamper + * @param compileDep + * path to the persisted compiledCompilationUnit (.dep file) + * @param editedDep + * path to the persisted editedCompilationUnit (.dep file) + * @param editedSourceFiles + * TODO: Correct??: is empty (not null), except source has been + * edited (i.e.: .sugj file) + * @param mode + * @return + * @throws IOException + */ @SuppressWarnings("unchecked") final protected static Pair read(Class cl, Stamper stamper, Path compileDep, Path editedDep, Map editedSourceFiles, Mode mode) throws IOException { E compileE = PersistableEntity.read(cl, stamper, compileDep); @@ -116,6 +183,10 @@ final protected static Pair read(ClasseditedCompilationUnit to a compiledCompilationUnit + * @param compiled + */ protected void copyContentTo(CompilationUnit compiled) { compiled.sourceArtifacts.putAll(sourceArtifacts); @@ -138,6 +209,10 @@ protected void copyContentTo(CompilationUnit compiled) { compiled.addGeneratedFile(FileCommands.tryCopyFile(targetDir, compiled.targetDir, p)); } + /** + * Copies contents of all dependent (consistent) editedCompilationUnits to compiledCompilationUnits + * @throws IOException + */ protected void liftEditedToCompiled() throws IOException { ModuleVisitor liftVisitor = new ModuleVisitor() { @Override public Void visit(CompilationUnit mod, Mode mode) { @@ -220,6 +295,10 @@ public void addModuleDependency(CompilationUnit mod) { // Methods for querying dependencies // ********************************* + /** + * is edited ... + * @return + */ public boolean isParsedCompilationUnit() { return compiledCompilationUnit != null; } @@ -237,6 +316,11 @@ public boolean dependsOnTransitivelyNoncircularly(CompilationUnit other) { return false; } + /** + * Depends on directly + * @param other + * @return + */ public boolean dependsOn(CompilationUnit other) { return moduleDependencies.contains(other) || circularModuleDependencies.contains(other); } @@ -309,6 +393,15 @@ public Synthesizer getSynthesizer() { protected abstract boolean isConsistentExtend(Mode mode); + /** + * TODO: Why does this FAIL if nothing was edited (editedSourceFiles == null) + * ?? + * + * @param editedSourceFiles + * @param mode + * TODO: unused? + * @return + */ protected boolean isConsistentWithSourceArtifacts(Map editedSourceFiles, Mode mode) { if (sourceArtifacts.isEmpty()) return false; @@ -325,6 +418,17 @@ else if (stamp == null && (!FileCommands.exists(e.getKey()) || e.getValue() != s return true; } + /** + * Checks consistency only for this module's ... + *
    + *
  • source files (editedSourceFiles) (i.e.: .sugj file) + *
  • generated files (generatedFiles) (i.e.: .java .class .sdf files) + *
  • external file dependencies (externalFileDependencies) (i.e.: .jar files) + *
+ * @param editedSourceFiles (i.e.: sugj. file) + * @param mode (compiled / edited / ... ) + * @return + */ public boolean isConsistentShallow(Map editedSourceFiles, Mode mode) { if (hasPersistentVersionChanged()) return false; @@ -346,6 +450,12 @@ public boolean isConsistentShallow(Map editedSourceFiles, return true; } + /** + * Checks consistency of this module including its module dependencies
+ * @param editedSourceFiles (i.e.: sugj. file) + * @param mode (compiled / edited / ... ) + * @return + */ public boolean isConsistent(final Map editedSourceFiles, Mode mode) { ModuleVisitor isConsistentVisitor = new ModuleVisitor() { @Override public Boolean visit(CompilationUnit mod, Mode mode) { return mod.isConsistentShallow(editedSourceFiles, mode); } diff --git a/src/org/sugarj/common/cleardep/Synthesizer.java b/src/org/sugarj/common/cleardep/Synthesizer.java index cfe694c..32b6c95 100644 --- a/src/org/sugarj/common/cleardep/Synthesizer.java +++ b/src/org/sugarj/common/cleardep/Synthesizer.java @@ -8,12 +8,25 @@ import org.sugarj.common.path.Path; /** + *
    + *
  • Used to dependency-track modules (called A$B) that are a product of a transformation (B) on another module (A) + *
  • Holds references to the required modules (A and B) + *
  • The module that includes the transformed module has only a dependency on A$B + *
  • The CompilationUnit of A$B now contains this Synthesizer (called CompilationUnit.syn) + *
+ * * @author Sebastian Erdweg */ public class Synthesizer { public Set modules; - public Map files; + public Map files; // external file dependencies + /** + * + * @param modules + * @param files + * external file dependencies + */ public Synthesizer(Set modules, Map files) { this.modules = modules; this.files = files; @@ -28,7 +41,8 @@ public Synthesizer(Stamper stamper, Set modules, Set file public void markSynthesized(CompilationUnit c) { for (CompilationUnit m : modules) - c.addModuleDependency(m); + c.addModuleDependency(m); // TODO: Check: Shouldn't there be a circular + // dependency?? for (Entry e : files.entrySet()) c.addExternalFileDependency(e.getKey(), e.getValue()); } From 7e31f80a66f1e199b6a421951c03038b89f956ee Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Tue, 2 Sep 2014 12:37:26 +0200 Subject: [PATCH 3/9] documentation --- src/org/sugarj/common/cleardep/CompilationUnitTest.java | 5 +++++ src/org/sugarj/common/cleardep/TestCompilationUnit.java | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnitTest.java b/src/org/sugarj/common/cleardep/CompilationUnitTest.java index b63aec1..715d59c 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnitTest.java +++ b/src/org/sugarj/common/cleardep/CompilationUnitTest.java @@ -24,6 +24,11 @@ import org.sugarj.common.path.Path; import org.sugarj.common.path.RelativePath; +/** + * + * @author Simon Ramstedt + * + */ public class CompilationUnitTest { @Rule diff --git a/src/org/sugarj/common/cleardep/TestCompilationUnit.java b/src/org/sugarj/common/cleardep/TestCompilationUnit.java index 8ef902b..b796f94 100644 --- a/src/org/sugarj/common/cleardep/TestCompilationUnit.java +++ b/src/org/sugarj/common/cleardep/TestCompilationUnit.java @@ -4,7 +4,8 @@ /** * Test Compilation Unit - * @author User + * + * @author Simon Ramstedt * */ class TestCompilationUnit extends CompilationUnit{ From 058279a9b30c200d7b0241c3cb4fa0737db7c794 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Tue, 2 Sep 2014 19:32:50 +0200 Subject: [PATCH 4/9] tests moved from /src to /test --- .../common/cleardep/CompilationUnitTest.java | 80 +++++++------------ .../common/cleardep/TestCompilationUnit.java | 0 2 files changed, 29 insertions(+), 51 deletions(-) rename {src => test}/org/sugarj/common/cleardep/CompilationUnitTest.java (79%) rename {src => test}/org/sugarj/common/cleardep/TestCompilationUnit.java (100%) diff --git a/src/org/sugarj/common/cleardep/CompilationUnitTest.java b/test/org/sugarj/common/cleardep/CompilationUnitTest.java similarity index 79% rename from src/org/sugarj/common/cleardep/CompilationUnitTest.java rename to test/org/sugarj/common/cleardep/CompilationUnitTest.java index 715d59c..ccbaa60 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnitTest.java +++ b/test/org/sugarj/common/cleardep/CompilationUnitTest.java @@ -89,7 +89,7 @@ public void test_dependsOn_1() throws Exception { assertTrue(a.dependsOnNoncircularly(b)); assertTrue(a.dependsOn(b)); - // assertFalse(a.dependsOnTransitively(b)); //TODO: why false? + assertTrue(a.dependsOnTransitively(b)); b.addModuleDependency(c); // a -> b -> c @@ -109,7 +109,7 @@ public void test_dependsOn_2() throws Exception { b.addModuleDependency(c); // a <-> b -> c - // assertTrue(a.dependsOnTransitively(c)); //TODO: why false? + assertTrue(a.dependsOnTransitively(c)); assertFalse(a.dependsOnTransitivelyNoncircularly(c)); } @@ -129,18 +129,14 @@ public void test_dependsOn_3() throws Exception { @Test public void test_isConsistentShallow_1() throws Exception { - // assertTrue(a.isConsistentShallow( null, testMode)); //TODO: why does this - // fail? - assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistentShallow(null, editorMode)); TestFile f1 = generateRandomFileIn(sourceFolder); // source artifacts a.addSourceArtifact(f1.relativeTo(sourceFolder)); - assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - // assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); // - // TODO: - // why doesn't this fail? + assertFalse(a.isConsistentShallow(null, editorMode)); } @@ -148,14 +144,14 @@ public void test_isConsistentShallow_1() throws Exception { public void test_isConsistentShallow_2() throws Exception { a.addExternalFileDependency(f1); - assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertFalse(a.isConsistentShallow(null, editorMode)); b.addExternalFileDependency(f2); - assertTrue(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + assertTrue(b.isConsistentShallow(null, editorMode)); changeContentOf(f2); - assertFalse(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + assertFalse(b.isConsistentShallow(null, editorMode)); } @@ -163,27 +159,26 @@ public void test_isConsistentShallow_2() throws Exception { public void test_isConsistentShallow_3() throws Exception { a.addGeneratedFile(f1); - assertTrue(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistentShallow(a.sourceArtifacts, editorMode)); + assertFalse(a.isConsistentShallow(null, editorMode)); b.addGeneratedFile(f2); - assertTrue(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + assertTrue(b.isConsistentShallow(null, editorMode)); changeContentOf(f2); - assertFalse(b.isConsistentShallow(b.sourceArtifacts, editorMode)); + assertFalse(b.isConsistentShallow(null, editorMode)); } @Test public void test_isConsistent_1() throws Exception { - // assertTrue(a.isConsistent( null, testMode)); //TODO: Why fail? - assertTrue(a.isConsistent(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); // shallow dependency a.addExternalFileDependency(f1); - assertTrue(a.isConsistent(a.sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(a.sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @@ -192,33 +187,29 @@ public void test_isConsistent_2() throws Exception { // a -> b - sourceArtifacts.putAll(a.sourceArtifacts); - sourceArtifacts.putAll(b.sourceArtifacts); a.addModuleDependency(b); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); // a -> b (b inconsistent) b.addExternalFileDependency(f1); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @Test public void test_isConsistent_3() throws Exception { // a <-> b - sourceArtifacts.putAll(a.sourceArtifacts); - sourceArtifacts.putAll(b.sourceArtifacts); a.addCircularModuleDependency(b); b.addModuleDependency(a); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); // a <-> b (b inconsistent) b.addExternalFileDependency(f1); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @@ -226,18 +217,15 @@ public void test_isConsistent_3() throws Exception { public void test_isConsistent_4() throws Exception { // a -> b -> c - sourceArtifacts.putAll(a.sourceArtifacts); - sourceArtifacts.putAll(b.sourceArtifacts); - sourceArtifacts.putAll(c.sourceArtifacts); a.addModuleDependency(b); b.addModuleDependency(c); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); // a -> b -> c (c inconsistent) c.addExternalFileDependency(f1); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @@ -246,9 +234,6 @@ public void test_isConsistent_5() throws Exception { // a -> x (synth) -> b,c - sourceArtifacts.putAll(a.sourceArtifacts); - sourceArtifacts.putAll(b.sourceArtifacts); // TODO: why is this nessessary??! - sourceArtifacts.putAll(c.sourceArtifacts); // TODO: why is this nessessary??! Set synModules = new HashSet<>(); synModules.add(b); synModules.add(c); @@ -258,14 +243,13 @@ public void test_isConsistent_5() throws Exception { TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); - sourceArtifacts.putAll(x.sourceArtifacts); a.addModuleDependency(x); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @Test @@ -273,9 +257,6 @@ public void test_isConsistent_6() throws Exception { // a -> x (synth) -> b,c (c inconsistent) - sourceArtifacts.putAll(a.sourceArtifacts); - sourceArtifacts.putAll(b.sourceArtifacts); // TODO: why is this nessessary? - sourceArtifacts.putAll(c.sourceArtifacts); // TODO: why is this nessessary? Set synModules = new HashSet<>(); synModules.add(b); synModules.add(c); @@ -285,16 +266,15 @@ public void test_isConsistent_6() throws Exception { TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); - sourceArtifacts.putAll(x.sourceArtifacts); a.addModuleDependency(x); // make c inconsistent c.addGeneratedFile(f2); - assertTrue(a.isConsistent(sourceArtifacts, editorMode)); + assertTrue(a.isConsistent(null, editorMode)); FileCommands.delete(f2); - assertFalse(a.isConsistent(sourceArtifacts, editorMode)); + assertFalse(a.isConsistent(null, editorMode)); } @Test @@ -341,7 +321,6 @@ public void test_liftEditedToCompiled() throws IOException { e1.addModuleDependency(e2); e2.addModuleDependency(e3); - // e3.addCircularModuleDependency(e1); //TODO: does it work like this? e1.liftEditedToCompiled(); @@ -497,7 +476,6 @@ public boolean equals(Object o) { if (((TestFile) o).read().equals(this.read())) return true; } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } diff --git a/src/org/sugarj/common/cleardep/TestCompilationUnit.java b/test/org/sugarj/common/cleardep/TestCompilationUnit.java similarity index 100% rename from src/org/sugarj/common/cleardep/TestCompilationUnit.java rename to test/org/sugarj/common/cleardep/TestCompilationUnit.java From 306d6bc7590ad5e8456266792cdd04ea2de16988 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Tue, 2 Sep 2014 19:41:30 +0200 Subject: [PATCH 5/9] documentation --- src/org/sugarj/common/cleardep/CompilationUnit.java | 9 ++------- src/org/sugarj/common/cleardep/Synthesizer.java | 8 ++++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index 590ed9c..c0e28b4 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -84,8 +84,7 @@ public CompilationUnit() { /* for deserialization only */ } * @param sourceFiles * i.e.: .sugj file * @param editedSourceFiles - * TODO: Correct??: is empty (not null), except source has been - * edited (i.e.: .sugj file) + * is empty or null, except source has been edited (i.e.: .sugj file) * @param mode * * @param syn @@ -151,8 +150,7 @@ final protected static E create(Class cl, Stamper * @param editedDep * path to the persisted editedCompilationUnit (.dep file) * @param editedSourceFiles - * TODO: Correct??: is empty (not null), except source has been - * edited (i.e.: .sugj file) + * is empty / null, except source has been edited (i.e.: .sugj file) * @param mode * @return * @throws IOException @@ -394,9 +392,6 @@ public Synthesizer getSynthesizer() { protected abstract boolean isConsistentExtend(Mode mode); /** - * TODO: Why does this FAIL if nothing was edited (editedSourceFiles == null) - * ?? - * * @param editedSourceFiles * @param mode * TODO: unused? diff --git a/src/org/sugarj/common/cleardep/Synthesizer.java b/src/org/sugarj/common/cleardep/Synthesizer.java index 32b6c95..d2ed81a 100644 --- a/src/org/sugarj/common/cleardep/Synthesizer.java +++ b/src/org/sugarj/common/cleardep/Synthesizer.java @@ -39,11 +39,11 @@ public Synthesizer(Stamper stamper, Set modules, Set file this.files.put(p, stamper.stampOf(p)); } - public void markSynthesized(CompilationUnit c) { + public void markSynthesized(CompilationUnit synthesizedModule) { for (CompilationUnit m : modules) - c.addModuleDependency(m); // TODO: Check: Shouldn't there be a circular - // dependency?? + synthesizedModule.addModuleDependency(m); + for (Entry e : files.entrySet()) - c.addExternalFileDependency(e.getKey(), e.getValue()); + synthesizedModule.addExternalFileDependency(e.getKey(), e.getValue()); } } From 7d3a68ba6a64df76df263c25edfa5e3d2d775b41 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Thu, 11 Sep 2014 19:04:40 +0200 Subject: [PATCH 6/9] CompilationUnitTest completed and renamed to CompilationUnitMock build.properties updated --- .classpath | 1 + META-INF/MANIFEST.MF | 3 +- build.properties | 3 +- .../common/cleardep/CompilationUnit.java | 43 ++-- .../sugarj/common/cleardep/Synthesizer.java | 4 +- ...UnitTest.java => CompilationUnitMock.java} | 204 +++++++++++++++--- 6 files changed, 202 insertions(+), 56 deletions(-) rename test/org/sugarj/common/cleardep/{CompilationUnitTest.java => CompilationUnitMock.java} (66%) diff --git a/.classpath b/.classpath index 16d067f..640b871 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,7 @@ + diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index b478fe5..a2c0561 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -9,7 +9,8 @@ Require-Bundle: org.eclipse.ui, org.strategoxt.strj, org.spoofax.jsglr, org.eclipse.jdt.core, - org.spoofax.terms + org.spoofax.terms, + org.junit Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Export-Package: org.sugarj.common, diff --git a/build.properties b/build.properties index 34d2e4d..237d94f 100644 --- a/build.properties +++ b/build.properties @@ -1,4 +1,5 @@ -source.. = src/ +source.. = src/,\ + test/ output.. = bin/ bin.includes = META-INF/,\ . diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index c0e28b4..bd1a6f5 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -21,10 +21,11 @@ import org.sugarj.util.Pair; /** - * Dependency management for modules. - *

- * For each module (i.e: .sugj-File) there are two CompilationUnits. One for automatic compilations (generates only temporary files) - * and one for "real" compilation that may use already generated files (if valid) + * Dependency management for modules.
+ *
+ * For each module there are two CompilationUnits. One for automatic + * compilations (generates only temporary files) and one for "real" compilation + * that may use already generated files (if valid) * * @author Sebastian Erdweg */ @@ -47,22 +48,15 @@ public CompilationUnit() { /* for deserialization only */ } protected Path targetDir; /** - * For example the .sugj file
- * Usually only one file + * source files */ protected Map sourceArtifacts; protected Set moduleDependencies; protected Set circularModuleDependencies; - /** - * For example included .jar files - */ protected Map externalFileDependencies; - /** - * For example generated .java .class .sdf files - */ protected Map generatedFiles; // ************************** @@ -82,9 +76,8 @@ public CompilationUnit() { /* for deserialization only */ } * @param editedTarget * i.e.: some temporary folder * @param sourceFiles - * i.e.: .sugj file * @param editedSourceFiles - * is empty or null, except source has been edited (i.e.: .sugj file) + * is empty or null, except source has been edited * @param mode * * @param syn @@ -150,7 +143,7 @@ final protected static E create(Class cl, Stamper * @param editedDep * path to the persisted editedCompilationUnit (.dep file) * @param editedSourceFiles - * is empty / null, except source has been edited (i.e.: .sugj file) + * is empty / null, except source has been edited * @param mode * @return * @throws IOException @@ -188,6 +181,8 @@ final protected static Pair read(Class - *
  • source files (editedSourceFiles) (i.e.: .sugj file) - *
  • generated files (generatedFiles) (i.e.: .java .class .sdf files) - *
  • external file dependencies (externalFileDependencies) (i.e.: .jar files) + *
  • source files + *
  • generated files + *
  • external file dependencies * - * @param editedSourceFiles (i.e.: sugj. file) - * @param mode (compiled / edited / ... ) + * + * @param editedSourceFiles + * @param mode + * (compiled / edited / ... ) * @return */ public boolean isConsistentShallow(Map editedSourceFiles, Mode mode) { @@ -447,8 +444,10 @@ public boolean isConsistentShallow(Map editedSourceFiles, /** * Checks consistency of this module including its module dependencies
    - * @param editedSourceFiles (i.e.: sugj. file) - * @param mode (compiled / edited / ... ) + * + * @param editedSourceFiles + * @param mode + * (compiled / edited / ... ) * @return */ public boolean isConsistent(final Map editedSourceFiles, Mode mode) { diff --git a/src/org/sugarj/common/cleardep/Synthesizer.java b/src/org/sugarj/common/cleardep/Synthesizer.java index d2ed81a..9f592c6 100644 --- a/src/org/sugarj/common/cleardep/Synthesizer.java +++ b/src/org/sugarj/common/cleardep/Synthesizer.java @@ -24,8 +24,10 @@ public class Synthesizer { /** * * @param modules + * required by the module to be synthesized * @param files - * external file dependencies + * external file dependencies required by the module to be + * synthesized */ public Synthesizer(Set modules, Map files) { this.modules = modules; diff --git a/test/org/sugarj/common/cleardep/CompilationUnitTest.java b/test/org/sugarj/common/cleardep/CompilationUnitMock.java similarity index 66% rename from test/org/sugarj/common/cleardep/CompilationUnitTest.java rename to test/org/sugarj/common/cleardep/CompilationUnitMock.java index ccbaa60..753e268 100644 --- a/test/org/sugarj/common/cleardep/CompilationUnitTest.java +++ b/test/org/sugarj/common/cleardep/CompilationUnitMock.java @@ -8,16 +8,20 @@ import java.io.IOException; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sugarj.common.FileCommands; +import org.sugarj.common.cleardep.CompilationUnit.ModuleVisitor; import org.sugarj.common.cleardep.mode.ForEditorMode; import org.sugarj.common.cleardep.mode.Mode; import org.sugarj.common.path.AbsolutePath; @@ -29,7 +33,7 @@ * @author Simon Ramstedt * */ -public class CompilationUnitTest { +public class CompilationUnitMock { @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); @@ -65,7 +69,6 @@ public void setUp() throws Exception { f4 = generateRandomFileIn(editedFolder); f5 = generateRandomFileIn(editedFolder); - this.sourceArtifacts = new HashMap(); } @After @@ -77,9 +80,14 @@ public void tearDown() throws Exception { // Tests @Test - public void test_getCircularFileDependencies() { + public void test_getCircularFileDependencies() throws IOException { - // TODO: write + Assert.assertArrayEquals(new Path[0], a.getCircularFileDependencies().toArray()); + a.addExternalFileDependency(f1); + a.addModuleDependency(b); + b.addExternalFileDependency(f1); + Path[] cfd = { f1 }; + Assert.assertArrayEquals(cfd, a.getCircularFileDependencies().toArray()); } @Test @@ -289,31 +297,23 @@ public void test_liftEditedToCompiled() throws IOException { TestFile e3EditedDep = randomPathIn(sourceFolder); Set e1SourceFiles = new HashSet(); - e1SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); - e1SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); Set e2SourceFiles = new HashSet(); - e2SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); Set e3SourceFiles = new HashSet(); - e3SourceFiles.add(generateRandomFileIn(sourceFolder).relativeTo(sourceFolder)); - - Map e1EditedSourceFiles = new HashMap(); - Map e2EditedSourceFiles = new HashMap(); - Map e3EditedSourceFiles = new HashMap(); - TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, e1EditedSourceFiles, new ForEditorMode(null, true), null); + TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), null); e1.addExternalFileDependency(f1); e1.addGeneratedFile(f2); - TestCompilationUnit e2 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e2CompiledDep, compileFolder, e2EditedDep, editedFolder, e2SourceFiles, e2EditedSourceFiles, new ForEditorMode(null, true), null); + TestCompilationUnit e2 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e2CompiledDep, compileFolder, e2EditedDep, editedFolder, e2SourceFiles, null, new ForEditorMode(null, true), null); e2.addExternalFileDependency(f1); e2.addExternalFileDependency(f3); e2.addGeneratedFile(f4); - TestCompilationUnit e3 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e3CompiledDep, compileFolder, e3EditedDep, editedFolder, e3SourceFiles, e3EditedSourceFiles, new ForEditorMode(null, true), null); + TestCompilationUnit e3 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e3CompiledDep, compileFolder, e3EditedDep, editedFolder, e3SourceFiles, null, new ForEditorMode(null, true), null); e3.addExternalFileDependency(f1); e3.addGeneratedFile(f2); @@ -348,25 +348,167 @@ public void test_liftEditedToCompiled() throws IOException { @Test public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { - // TODO: write + TestFile e1CompiledDep = randomPathIn(sourceFolder); + TestFile e2CompiledDep = randomPathIn(sourceFolder); + TestFile e3CompiledDep = randomPathIn(sourceFolder); + + TestFile e1EditedDep = randomPathIn(sourceFolder); + TestFile e2EditedDep = randomPathIn(sourceFolder); + TestFile e3EditedDep = randomPathIn(sourceFolder); + + Set e1SourceFiles = new HashSet(); + + Set e2SourceFiles = new HashSet(); + + Set e3SourceFiles = new HashSet(); + + TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), null); + + e1.addExternalFileDependency(f1); + e1.addGeneratedFile(f2); + + TestCompilationUnit e2 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e2CompiledDep, compileFolder, e2EditedDep, editedFolder, e2SourceFiles, null, new ForEditorMode(null, true), null); + + e2.addExternalFileDependency(f1); + e2.addExternalFileDependency(f3); + e2.addGeneratedFile(f4); + + // Synthesizer + Set modules = new HashSet(); + Map files = new HashMap(); + + modules.add(e1); + modules.add(e2); + + files.put(f5, testStamper.stampOf(f5)); + + Synthesizer syn = new Synthesizer(modules, files); + + TestCompilationUnit e3 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e3CompiledDep, compileFolder, e3EditedDep, editedFolder, e3SourceFiles, null, new ForEditorMode(null, true), syn); + + e3.liftEditedToCompiled(); + + TestFile cf1 = new TestFile(compileFolder, f1.name()); + TestFile cf2 = new TestFile(compileFolder, f2.name()); + TestFile cf3 = new TestFile(compileFolder, f3.name()); + TestFile cf4 = new TestFile(compileFolder, f4.name()); + TestFile cf5 = new TestFile(compileFolder, f5.name()); + + assertEquals(cf1, f1); + assertEquals(cf2, f2); + assertEquals(cf3, f3); + assertEquals(cf4, f4); + assertEquals(cf5, f5); + + TestCompilationUnit c1 = (TestCompilationUnit) e1.compiledCompilationUnit; + TestCompilationUnit c2 = (TestCompilationUnit) e2.compiledCompilationUnit; + TestCompilationUnit c3 = (TestCompilationUnit) e3.compiledCompilationUnit; + + assertTrue(c3.getModuleDependencies().contains(c1)); + assertTrue(c3.getModuleDependencies().contains(c2)); + + Assert.assertNotNull(c3.getSynthesizer()); + + assertTrue(c3.getSynthesizer().modules.contains(c1)); + assertTrue(c3.getSynthesizer().modules.contains(c2)); } - /* - * Testing needed? - * - * @Test public void computeRanks_Test() {} - * - * @Test public void addExternalFileDependencyTest() { } - * - * @Test public void addExternalFileDependencyLateTest() { } - * - * @Test public void addGeneratedFileTest() { } - * - * @Test public void addCircularModuleDependencyTest() { } - * - * @Test public void addModuleDependencyTest() { } - */ + @Test + public void test_visit() { + + final List visited = new LinkedList(); + + ModuleVisitor v = new ModuleVisitor() { + + @Override + public Void visit(CompilationUnit mod, Mode mode) { + + visited.add(mod); + + return null; + } + + @Override + public Void init() { + return null; + } + + @Override + public Void combine(Void t1, Void t2) { + return null; + } + + @Override + public boolean cancel(Void t) { + return false; + } + }; + + a.addModuleDependency(b); + b.addModuleDependency(c); + b.addModuleDependency(d); + d.addModuleDependency(e); + e.addCircularModuleDependency(a); + + a.visit(v); + + Object[] expected = { a, b, c, d, e }; + Assert.assertArrayEquals(expected, visited.toArray()); + + } + @Test + public void test_readWrite() throws IOException { + + TestFile e1CompiledDep = randomPathIn(sourceFolder); + + TestFile e1EditedDep = randomPathIn(sourceFolder); + + Set e1SourceFiles = new HashSet(); + + // Synthesizer + Set modules = new HashSet(); + Map files = new HashMap(); + + modules.add(b); + + files.put(f5, testStamper.stampOf(f5)); + + Synthesizer syn = new Synthesizer(modules, files); + + TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), syn); + + e1.addModuleDependency(c); + e1.addCircularModuleDependency(d); + e1.addExternalFileDependency(f1); + e1.addGeneratedFile(f2); + + e1.write(); + + e1 = null; + e1 = TestCompilationUnit.read(TestCompilationUnit.class, testStamper, e1EditedDep); + + Assert.assertNotNull(e1.getSynthesizer()); + assertEquals(b.persistentPath.getAbsolutePath(), e1.getSynthesizer().modules.iterator().next().persistentPath.getAbsolutePath()); + + List names = new LinkedList(); + for (CompilationUnit c : e1.getModuleDependencies()) { + names.add(c.getName()); + } + assertTrue(names.contains(b.getName())); + assertTrue(names.contains(c.getName())); + + assertEquals(d.persistentPath.getAbsolutePath(), e1.circularModuleDependencies.iterator().next().persistentPath.getAbsolutePath()); + + List paths = new LinkedList(); + for (Path p : e1.getExternalFileDependencies()) { + paths.add(p.getAbsolutePath()); + } + assertTrue(paths.contains(f5.getAbsolutePath())); + assertTrue(paths.contains(f1.getAbsolutePath())); + + assertEquals(f2.getAbsolutePath(), e1.getGeneratedFiles().iterator().next().getAbsolutePath()); + } // Helper methods From 6e45bcedc08449dac350536a7c5fe7acc02d1c63 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Thu, 11 Sep 2014 19:37:26 +0200 Subject: [PATCH 7/9] 1. addModuleDependency(..) now checks for circularity 2. corresponding unit test added 3. addCirculrModuleDependency(..) depreciated --- .../common/cleardep/CompilationUnit.java | 12 +++- .../common/cleardep/CompilationUnitMock.java | 59 +++++++++---------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index bd1a6f5..0da4fc1 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -275,12 +275,20 @@ public void addGeneratedFile(Path file, int stampOfFile) { generatedFiles.put(file, stampOfFile); } + /** + * Simply use addModuleDependency(...) instead + */ + @Deprecated public void addCircularModuleDependency(CompilationUnit mod) { circularModuleDependencies.add(mod); } - public void addModuleDependency(CompilationUnit mod) { - moduleDependencies.add(mod); + public void addModuleDependency(final CompilationUnit mod) { + + if (mod.dependsOnTransitively(this)) + circularModuleDependencies.add(mod); + else + moduleDependencies.add(mod); } diff --git a/test/org/sugarj/common/cleardep/CompilationUnitMock.java b/test/org/sugarj/common/cleardep/CompilationUnitMock.java index 753e268..c8c62e1 100644 --- a/test/org/sugarj/common/cleardep/CompilationUnitMock.java +++ b/test/org/sugarj/common/cleardep/CompilationUnitMock.java @@ -134,6 +134,19 @@ public void test_dependsOn_3() throws Exception { } + @Test + public void test_addModuleDependency() throws Exception { + + a.addModuleDependency(b); + b.addModuleDependency(c); + c.addModuleDependency(a); + assertTrue(a.circularModuleDependencies.isEmpty()); + assertTrue(b.circularModuleDependencies.isEmpty()); + assertTrue(a.moduleDependencies.contains(b)); + assertTrue(b.moduleDependencies.contains(c)); + assertTrue(c.circularModuleDependencies.contains(a)); + } + @Test public void test_isConsistentShallow_1() throws Exception { @@ -348,45 +361,29 @@ public void test_liftEditedToCompiled() throws IOException { @Test public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { - TestFile e1CompiledDep = randomPathIn(sourceFolder); - TestFile e2CompiledDep = randomPathIn(sourceFolder); - TestFile e3CompiledDep = randomPathIn(sourceFolder); - - TestFile e1EditedDep = randomPathIn(sourceFolder); - TestFile e2EditedDep = randomPathIn(sourceFolder); - TestFile e3EditedDep = randomPathIn(sourceFolder); - Set e1SourceFiles = new HashSet(); - Set e2SourceFiles = new HashSet(); - - Set e3SourceFiles = new HashSet(); + a.addExternalFileDependency(f1); + a.addGeneratedFile(f2); - TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), null); - - e1.addExternalFileDependency(f1); - e1.addGeneratedFile(f2); - - TestCompilationUnit e2 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e2CompiledDep, compileFolder, e2EditedDep, editedFolder, e2SourceFiles, null, new ForEditorMode(null, true), null); - - e2.addExternalFileDependency(f1); - e2.addExternalFileDependency(f3); - e2.addGeneratedFile(f4); + b.addExternalFileDependency(f1); + b.addExternalFileDependency(f3); + b.addGeneratedFile(f4); // Synthesizer Set modules = new HashSet(); Map files = new HashMap(); - - modules.add(e1); - modules.add(e2); - + modules.add(a); + modules.add(b); files.put(f5, testStamper.stampOf(f5)); - Synthesizer syn = new Synthesizer(modules, files); - TestCompilationUnit e3 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e3CompiledDep, compileFolder, e3EditedDep, editedFolder, e3SourceFiles, null, new ForEditorMode(null, true), syn); + TestFile compiledDep = randomPathIn(sourceFolder); + TestFile editedDep = randomPathIn(sourceFolder); + Set sourceFiles = new HashSet(); + TestCompilationUnit compilationUnitWithSynthesizer = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, compiledDep, compileFolder, editedDep, editedFolder, sourceFiles, null, new ForEditorMode(null, true), syn); - e3.liftEditedToCompiled(); + compilationUnitWithSynthesizer.liftEditedToCompiled(); TestFile cf1 = new TestFile(compileFolder, f1.name()); TestFile cf2 = new TestFile(compileFolder, f2.name()); @@ -400,9 +397,9 @@ public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { assertEquals(cf4, f4); assertEquals(cf5, f5); - TestCompilationUnit c1 = (TestCompilationUnit) e1.compiledCompilationUnit; - TestCompilationUnit c2 = (TestCompilationUnit) e2.compiledCompilationUnit; - TestCompilationUnit c3 = (TestCompilationUnit) e3.compiledCompilationUnit; + TestCompilationUnit c1 = (TestCompilationUnit) a.compiledCompilationUnit; + TestCompilationUnit c2 = (TestCompilationUnit) b.compiledCompilationUnit; + TestCompilationUnit c3 = (TestCompilationUnit) compilationUnitWithSynthesizer.compiledCompilationUnit; assertTrue(c3.getModuleDependencies().contains(c1)); assertTrue(c3.getModuleDependencies().contains(c2)); From b10a4225cc07b2afa1a6117b1ab7b53a4d3da840 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Thu, 11 Sep 2014 21:00:05 +0200 Subject: [PATCH 8/9] corrected addModuleDependency and corresponding test --- .../common/cleardep/CompilationUnit.java | 17 +- .../common/cleardep/CompilationUnitMock.java | 227 +++++++++--------- 2 files changed, 131 insertions(+), 113 deletions(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index 0da4fc1..fe11ade 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -283,12 +283,21 @@ public void addCircularModuleDependency(CompilationUnit mod) { circularModuleDependencies.add(mod); } - public void addModuleDependency(final CompilationUnit mod) { + /** + * @return true if mod was added to moduleDependencies
    + * false if mod was added to circularModuleDependencies + */ + public boolean addModuleDependency(final CompilationUnit mod) { - if (mod.dependsOnTransitively(this)) + if (mod.dependsOnTransitively(this) || this.dependsOnTransitively(mod)) { + circularModuleDependencies.add(mod); - else - moduleDependencies.add(mod); + return false; + } + + moduleDependencies.add(mod); + return true; + } diff --git a/test/org/sugarj/common/cleardep/CompilationUnitMock.java b/test/org/sugarj/common/cleardep/CompilationUnitMock.java index c8c62e1..1598981 100644 --- a/test/org/sugarj/common/cleardep/CompilationUnitMock.java +++ b/test/org/sugarj/common/cleardep/CompilationUnitMock.java @@ -44,7 +44,7 @@ public class CompilationUnitMock { Stamper testStamper = ContentHashStamper.instance; Mode editorMode = new ForEditorMode(null, true); - TestCompilationUnit a, b, c, d, e; + TestCompilationUnit m1, m2, m3, m4, m5, m6; TestFile f1, f2, f3, f4, f5; Map sourceArtifacts; @@ -56,11 +56,12 @@ public void setUp() throws Exception { editedFolder = new TestFile(tempFolder.newFolder("edited")); // test modules - a = generateRandomModule(); - b = generateRandomModule(); - c = generateRandomModule(); - d = generateRandomModule(); - e = generateRandomModule(); + m1 = generateRandomModule(); + m2 = generateRandomModule(); + m3 = generateRandomModule(); + m4 = generateRandomModule(); + m5 = generateRandomModule(); + m6 = generateRandomModule(); // test files f1 = generateRandomFileIn(editedFolder); @@ -82,124 +83,132 @@ public void tearDown() throws Exception { @Test public void test_getCircularFileDependencies() throws IOException { - Assert.assertArrayEquals(new Path[0], a.getCircularFileDependencies().toArray()); - a.addExternalFileDependency(f1); - a.addModuleDependency(b); - b.addExternalFileDependency(f1); + Assert.assertArrayEquals(new Path[0], m1.getCircularFileDependencies().toArray()); + m1.addExternalFileDependency(f1); + m1.addModuleDependency(m2); + m2.addExternalFileDependency(f1); Path[] cfd = { f1 }; - Assert.assertArrayEquals(cfd, a.getCircularFileDependencies().toArray()); + Assert.assertArrayEquals(cfd, m1.getCircularFileDependencies().toArray()); } @Test public void test_dependsOn_1() throws Exception { - a.addModuleDependency(b); // a -> b + m1.addModuleDependency(m2); // a -> b - assertTrue(a.dependsOnNoncircularly(b)); - assertTrue(a.dependsOn(b)); - assertTrue(a.dependsOnTransitively(b)); + assertTrue(m1.dependsOnNoncircularly(m2)); + assertTrue(m1.dependsOn(m2)); + assertTrue(m1.dependsOnTransitively(m2)); - b.addModuleDependency(c); // a -> b -> c + m2.addModuleDependency(m3); // a -> b -> c - assertTrue(a.dependsOnTransitivelyNoncircularly(c)); - assertTrue(a.dependsOnTransitively(c)); + assertTrue(m1.dependsOnTransitivelyNoncircularly(m3)); + assertTrue(m1.dependsOnTransitively(m3)); } @Test public void test_dependsOn_2() throws Exception { - a.addCircularModuleDependency(b); // a <-> b - b.addModuleDependency(a); + m1.addCircularModuleDependency(m2); // a <-> b + m2.addModuleDependency(m1); - assertTrue(a.dependsOn(b)); - assertFalse(a.dependsOnNoncircularly(b)); + assertTrue(m1.dependsOn(m2)); + assertFalse(m1.dependsOnNoncircularly(m2)); - b.addModuleDependency(c); // a <-> b -> c + m2.addModuleDependency(m3); // a <-> b -> c - assertTrue(a.dependsOnTransitively(c)); - assertFalse(a.dependsOnTransitivelyNoncircularly(c)); + assertTrue(m1.dependsOnTransitively(m3)); + assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); } @Test public void test_dependsOn_3() throws Exception { - a.addModuleDependency(b); // a -> b - b.addCircularModuleDependency(c); // a -> b <-> c - c.addModuleDependency(b); + m1.addModuleDependency(m2); // a -> b + m2.addCircularModuleDependency(m3); // a -> b <-> c + m3.addModuleDependency(m2); - assertTrue(a.dependsOnTransitively(c)); - assertFalse(a.dependsOnTransitivelyNoncircularly(c)); + assertTrue(m1.dependsOnTransitively(m3)); + assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); } @Test public void test_addModuleDependency() throws Exception { - a.addModuleDependency(b); - b.addModuleDependency(c); - c.addModuleDependency(a); - assertTrue(a.circularModuleDependencies.isEmpty()); - assertTrue(b.circularModuleDependencies.isEmpty()); - assertTrue(a.moduleDependencies.contains(b)); - assertTrue(b.moduleDependencies.contains(c)); - assertTrue(c.circularModuleDependencies.contains(a)); + m1.addModuleDependency(m2); + m2.addModuleDependency(m3); + m3.addModuleDependency(m1); + assertTrue(m1.circularModuleDependencies.isEmpty()); + assertTrue(m2.circularModuleDependencies.isEmpty()); + assertTrue(m1.moduleDependencies.contains(m2)); + assertTrue(m2.moduleDependencies.contains(m3)); + assertTrue(m3.circularModuleDependencies.contains(m1)); + + // TODO: should this case be covered or rejected by addModuleDependency(..) + // ? + m4.addModuleDependency(m5); + m5.addModuleDependency(m6); + assertFalse(m4.circularModuleDependencies.contains(m6)); + m4.addModuleDependency(m6); + assertTrue(m4.circularModuleDependencies.contains(m6)); } @Test public void test_isConsistentShallow_1() throws Exception { - assertTrue(a.isConsistentShallow(null, editorMode)); + assertTrue(m1.isConsistentShallow(null, editorMode)); TestFile f1 = generateRandomFileIn(sourceFolder); // source artifacts - a.addSourceArtifact(f1.relativeTo(sourceFolder)); - assertTrue(a.isConsistentShallow(null, editorMode)); + m1.addSourceArtifact(f1.relativeTo(sourceFolder)); + assertTrue(m1.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistentShallow(null, editorMode)); + assertFalse(m1.isConsistentShallow(null, editorMode)); } @Test public void test_isConsistentShallow_2() throws Exception { - a.addExternalFileDependency(f1); - assertTrue(a.isConsistentShallow(null, editorMode)); + m1.addExternalFileDependency(f1); + assertTrue(m1.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistentShallow(null, editorMode)); + assertFalse(m1.isConsistentShallow(null, editorMode)); - b.addExternalFileDependency(f2); - assertTrue(b.isConsistentShallow(null, editorMode)); + m2.addExternalFileDependency(f2); + assertTrue(m2.isConsistentShallow(null, editorMode)); changeContentOf(f2); - assertFalse(b.isConsistentShallow(null, editorMode)); + assertFalse(m2.isConsistentShallow(null, editorMode)); } @Test public void test_isConsistentShallow_3() throws Exception { - a.addGeneratedFile(f1); - assertTrue(a.isConsistentShallow(null, editorMode)); + m1.addGeneratedFile(f1); + assertTrue(m1.isConsistentShallow(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistentShallow(null, editorMode)); + assertFalse(m1.isConsistentShallow(null, editorMode)); - b.addGeneratedFile(f2); - assertTrue(b.isConsistentShallow(null, editorMode)); + m2.addGeneratedFile(f2); + assertTrue(m2.isConsistentShallow(null, editorMode)); changeContentOf(f2); - assertFalse(b.isConsistentShallow(null, editorMode)); + assertFalse(m2.isConsistentShallow(null, editorMode)); } @Test public void test_isConsistent_1() throws Exception { - assertTrue(a.isConsistent(null, editorMode)); + assertTrue(m1.isConsistent(null, editorMode)); // shallow dependency - a.addExternalFileDependency(f1); - assertTrue(a.isConsistent(null, editorMode)); + m1.addExternalFileDependency(f1); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @@ -208,29 +217,29 @@ public void test_isConsistent_2() throws Exception { // a -> b - a.addModuleDependency(b); - assertTrue(a.isConsistent(null, editorMode)); + m1.addModuleDependency(m2); + assertTrue(m1.isConsistent(null, editorMode)); // a -> b (b inconsistent) - b.addExternalFileDependency(f1); - assertTrue(a.isConsistent(null, editorMode)); + m2.addExternalFileDependency(f1); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @Test public void test_isConsistent_3() throws Exception { // a <-> b - a.addCircularModuleDependency(b); - b.addModuleDependency(a); - assertTrue(a.isConsistent(null, editorMode)); + m1.addCircularModuleDependency(m2); + m2.addModuleDependency(m1); + assertTrue(m1.isConsistent(null, editorMode)); // a <-> b (b inconsistent) - b.addExternalFileDependency(f1); - assertTrue(a.isConsistent(null, editorMode)); + m2.addExternalFileDependency(f1); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @@ -238,15 +247,15 @@ public void test_isConsistent_3() throws Exception { public void test_isConsistent_4() throws Exception { // a -> b -> c - a.addModuleDependency(b); - b.addModuleDependency(c); - assertTrue(a.isConsistent(null, editorMode)); + m1.addModuleDependency(m2); + m2.addModuleDependency(m3); + assertTrue(m1.isConsistent(null, editorMode)); // a -> b -> c (c inconsistent) - c.addExternalFileDependency(f1); - assertTrue(a.isConsistent(null, editorMode)); + m3.addExternalFileDependency(f1); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @@ -256,8 +265,8 @@ public void test_isConsistent_5() throws Exception { // a -> x (synth) -> b,c Set synModules = new HashSet<>(); - synModules.add(b); - synModules.add(c); + synModules.add(m2); + synModules.add(m3); Set externalDependencies = new HashSet(); externalDependencies.add(f1); @@ -265,12 +274,12 @@ public void test_isConsistent_5() throws Exception { TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); - a.addModuleDependency(x); + m1.addModuleDependency(x); - assertTrue(a.isConsistent(null, editorMode)); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @Test @@ -279,8 +288,8 @@ public void test_isConsistent_6() throws Exception { // a -> x (synth) -> b,c (c inconsistent) Set synModules = new HashSet<>(); - synModules.add(b); - synModules.add(c); + synModules.add(m2); + synModules.add(m3); Set externalDependencies = new HashSet(); externalDependencies.add(f1); @@ -288,14 +297,14 @@ public void test_isConsistent_6() throws Exception { TestCompilationUnit x = generateRandomModule(new Synthesizer(testStamper, synModules, externalDependencies)); - a.addModuleDependency(x); + m1.addModuleDependency(x); // make c inconsistent - c.addGeneratedFile(f2); - assertTrue(a.isConsistent(null, editorMode)); + m3.addGeneratedFile(f2); + assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f2); - assertFalse(a.isConsistent(null, editorMode)); + assertFalse(m1.isConsistent(null, editorMode)); } @Test @@ -363,18 +372,18 @@ public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { - a.addExternalFileDependency(f1); - a.addGeneratedFile(f2); + m1.addExternalFileDependency(f1); + m1.addGeneratedFile(f2); - b.addExternalFileDependency(f1); - b.addExternalFileDependency(f3); - b.addGeneratedFile(f4); + m2.addExternalFileDependency(f1); + m2.addExternalFileDependency(f3); + m2.addGeneratedFile(f4); // Synthesizer Set modules = new HashSet(); Map files = new HashMap(); - modules.add(a); - modules.add(b); + modules.add(m1); + modules.add(m2); files.put(f5, testStamper.stampOf(f5)); Synthesizer syn = new Synthesizer(modules, files); @@ -397,8 +406,8 @@ public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { assertEquals(cf4, f4); assertEquals(cf5, f5); - TestCompilationUnit c1 = (TestCompilationUnit) a.compiledCompilationUnit; - TestCompilationUnit c2 = (TestCompilationUnit) b.compiledCompilationUnit; + TestCompilationUnit c1 = (TestCompilationUnit) m1.compiledCompilationUnit; + TestCompilationUnit c2 = (TestCompilationUnit) m2.compiledCompilationUnit; TestCompilationUnit c3 = (TestCompilationUnit) compilationUnitWithSynthesizer.compiledCompilationUnit; assertTrue(c3.getModuleDependencies().contains(c1)); @@ -441,15 +450,15 @@ public boolean cancel(Void t) { } }; - a.addModuleDependency(b); - b.addModuleDependency(c); - b.addModuleDependency(d); - d.addModuleDependency(e); - e.addCircularModuleDependency(a); + m1.addModuleDependency(m2); + m2.addModuleDependency(m3); + m2.addModuleDependency(m4); + m4.addModuleDependency(m5); + m5.addCircularModuleDependency(m1); - a.visit(v); + m1.visit(v); - Object[] expected = { a, b, c, d, e }; + Object[] expected = { m1, m2, m3, m4, m5 }; Assert.assertArrayEquals(expected, visited.toArray()); } @@ -467,7 +476,7 @@ public void test_readWrite() throws IOException { Set modules = new HashSet(); Map files = new HashMap(); - modules.add(b); + modules.add(m2); files.put(f5, testStamper.stampOf(f5)); @@ -475,8 +484,8 @@ public void test_readWrite() throws IOException { TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), syn); - e1.addModuleDependency(c); - e1.addCircularModuleDependency(d); + e1.addModuleDependency(m3); + e1.addCircularModuleDependency(m4); e1.addExternalFileDependency(f1); e1.addGeneratedFile(f2); @@ -486,16 +495,16 @@ public void test_readWrite() throws IOException { e1 = TestCompilationUnit.read(TestCompilationUnit.class, testStamper, e1EditedDep); Assert.assertNotNull(e1.getSynthesizer()); - assertEquals(b.persistentPath.getAbsolutePath(), e1.getSynthesizer().modules.iterator().next().persistentPath.getAbsolutePath()); + assertEquals(m2.persistentPath.getAbsolutePath(), e1.getSynthesizer().modules.iterator().next().persistentPath.getAbsolutePath()); List names = new LinkedList(); for (CompilationUnit c : e1.getModuleDependencies()) { names.add(c.getName()); } - assertTrue(names.contains(b.getName())); - assertTrue(names.contains(c.getName())); + assertTrue(names.contains(m2.getName())); + assertTrue(names.contains(m3.getName())); - assertEquals(d.persistentPath.getAbsolutePath(), e1.circularModuleDependencies.iterator().next().persistentPath.getAbsolutePath()); + assertEquals(m4.persistentPath.getAbsolutePath(), e1.circularModuleDependencies.iterator().next().persistentPath.getAbsolutePath()); List paths = new LinkedList(); for (Path p : e1.getExternalFileDependencies()) { From 51e3967b6920f3ba1f37dc7724a52d5a5f8fcfc2 Mon Sep 17 00:00:00 2001 From: SimonRamstedt Date: Thu, 2 Oct 2014 17:24:34 +0200 Subject: [PATCH 9/9] CompilationUnitMock: -corrected visitor-tests -minor other changes --- .../common/cleardep/CompilationUnit.java | 22 ++- .../sugarj/common/cleardep/Synthesizer.java | 2 + .../common/cleardep/CompilationUnitMock.java | 143 +++++++++++------- 3 files changed, 103 insertions(+), 64 deletions(-) diff --git a/src/org/sugarj/common/cleardep/CompilationUnit.java b/src/org/sugarj/common/cleardep/CompilationUnit.java index fe11ade..8a4e4be 100644 --- a/src/org/sugarj/common/cleardep/CompilationUnit.java +++ b/src/org/sugarj/common/cleardep/CompilationUnit.java @@ -181,7 +181,7 @@ final protected static Pair read(Class, Map> computeR /** - * Visits the module graph starting from this module, satisfying the following properties: - * - every module transitively imported from `this` module is visited exactly once - * - if a module M1 is visited before a module M2, - * then (i) M1 is not transitively imported from M2 or - * (ii) M1 and M2 transitively have a circular dependency and - * M1 transitively imports M2 using `moduleDependencies` only. + * Visits the module graph starting from this module, satisfying the following + * properties: - every module transitively imported from `this` module is + * visited exactly once - if a module M1 is visited before a module M2, then + * (i) M1 is not transitively imported from M2 or (ii) M1 and M2 transitively + * have a circular dependency and M1 transitively imports M2 using + * `moduleDependencies` only. + * + * Shorter: Visits every module that is transitively imported from 'this' + * module exactly once Visits Module M1 before M2 if M1 is not transitively + * (-noncircularly) imported from M2 */ public T visit(ModuleVisitor visitor) { return visit(visitor, null); } public T visit(ModuleVisitor visitor, Mode thisMode) { diff --git a/src/org/sugarj/common/cleardep/Synthesizer.java b/src/org/sugarj/common/cleardep/Synthesizer.java index 9f592c6..1af97e2 100644 --- a/src/org/sugarj/common/cleardep/Synthesizer.java +++ b/src/org/sugarj/common/cleardep/Synthesizer.java @@ -44,6 +44,8 @@ public Synthesizer(Stamper stamper, Set modules, Set file public void markSynthesized(CompilationUnit synthesizedModule) { for (CompilationUnit m : modules) synthesizedModule.addModuleDependency(m); + // TODO: maybe the bug was here, when addModuleDiependency didn't recognize + // cycles for (Entry e : files.entrySet()) synthesizedModule.addExternalFileDependency(e.getKey(), e.getValue()); diff --git a/test/org/sugarj/common/cleardep/CompilationUnitMock.java b/test/org/sugarj/common/cleardep/CompilationUnitMock.java index 1598981..70c8450 100644 --- a/test/org/sugarj/common/cleardep/CompilationUnitMock.java +++ b/test/org/sugarj/common/cleardep/CompilationUnitMock.java @@ -94,13 +94,13 @@ public void test_getCircularFileDependencies() throws IOException { @Test public void test_dependsOn_1() throws Exception { - m1.addModuleDependency(m2); // a -> b + m1.addModuleDependency(m2); // m1 -> m2 assertTrue(m1.dependsOnNoncircularly(m2)); assertTrue(m1.dependsOn(m2)); assertTrue(m1.dependsOnTransitively(m2)); - m2.addModuleDependency(m3); // a -> b -> c + m2.addModuleDependency(m3); // m1 -> m2 -> m3 assertTrue(m1.dependsOnTransitivelyNoncircularly(m3)); assertTrue(m1.dependsOnTransitively(m3)); @@ -110,28 +110,39 @@ public void test_dependsOn_1() throws Exception { @Test public void test_dependsOn_2() throws Exception { - m1.addCircularModuleDependency(m2); // a <-> b + // m1 --> m2 + // m1 <~~ m2 + m1.addModuleDependency(m2); m2.addModuleDependency(m1); assertTrue(m1.dependsOn(m2)); - assertFalse(m1.dependsOnNoncircularly(m2)); + assertTrue(m1.dependsOnNoncircularly(m2)); + assertFalse(m2.dependsOnNoncircularly(m1)); - m2.addModuleDependency(m3); // a <-> b -> c + // m1 --> m2 --> m3 + // m1 <~~ m2 + m2.addModuleDependency(m3); assertTrue(m1.dependsOnTransitively(m3)); - assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); + // assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); + + // m4 --> m1 --> m2 + // m1 <~~ m2 + m4.addModuleDependency(m1); } @Test public void test_dependsOn_3() throws Exception { - m1.addModuleDependency(m2); // a -> b - m2.addCircularModuleDependency(m3); // a -> b <-> c + // m1 --> m2 --> m3 + // m2 <~~ m3 + m1.addModuleDependency(m2); + m2.addModuleDependency(m3); m3.addModuleDependency(m2); assertTrue(m1.dependsOnTransitively(m3)); - assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); + // assertFalse(m1.dependsOnTransitivelyNoncircularly(m3)); } @@ -215,12 +226,12 @@ public void test_isConsistent_1() throws Exception { @Test public void test_isConsistent_2() throws Exception { - // a -> b + // m1 -> m2 m1.addModuleDependency(m2); assertTrue(m1.isConsistent(null, editorMode)); - // a -> b (b inconsistent) + // m1 -> m2 (m2 inconsistent) m2.addExternalFileDependency(f1); assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); @@ -230,12 +241,12 @@ public void test_isConsistent_2() throws Exception { @Test public void test_isConsistent_3() throws Exception { - // a <-> b - m1.addCircularModuleDependency(m2); + // m1 <-> m2 + m1.addModuleDependency(m2); m2.addModuleDependency(m1); assertTrue(m1.isConsistent(null, editorMode)); - // a <-> b (b inconsistent) + // m1 <-> m2 (m2 inconsistent) m2.addExternalFileDependency(f1); assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); @@ -246,12 +257,12 @@ public void test_isConsistent_3() throws Exception { @Test public void test_isConsistent_4() throws Exception { - // a -> b -> c + // m1 -> m2 -> m3 m1.addModuleDependency(m2); m2.addModuleDependency(m3); assertTrue(m1.isConsistent(null, editorMode)); - // a -> b -> c (c inconsistent) + // m1 -> m2 -> m3 (m3 inconsistent) m3.addExternalFileDependency(f1); assertTrue(m1.isConsistent(null, editorMode)); FileCommands.delete(f1); @@ -262,7 +273,7 @@ public void test_isConsistent_4() throws Exception { @Test public void test_isConsistent_5() throws Exception { - // a -> x (synth) -> b,c + // m1 -> x (synth) -> m2,m3 Set synModules = new HashSet<>(); synModules.add(m2); @@ -285,7 +296,7 @@ public void test_isConsistent_5() throws Exception { @Test public void test_isConsistent_6() throws Exception { - // a -> x (synth) -> b,c (c inconsistent) + // m1 -> x (synth) -> m2,m3 (m3 inconsistent) Set synModules = new HashSet<>(); synModules.add(m2); @@ -299,7 +310,7 @@ public void test_isConsistent_6() throws Exception { m1.addModuleDependency(x); - // make c inconsistent + // make m3 inconsistent m3.addGeneratedFile(f2); assertTrue(m1.isConsistent(null, editorMode)); @@ -413,53 +424,38 @@ public void test_liftEditedToCompiled_WithSynthesizer() throws IOException { assertTrue(c3.getModuleDependencies().contains(c1)); assertTrue(c3.getModuleDependencies().contains(c2)); - Assert.assertNotNull(c3.getSynthesizer()); - - assertTrue(c3.getSynthesizer().modules.contains(c1)); - assertTrue(c3.getSynthesizer().modules.contains(c2)); + // TODO: Should the synthesizer also be copied? + // Assert.assertNotNull(c3.getSynthesizer()); + // assertTrue(c3.getSynthesizer().modules.contains(c1)); + // assertTrue(c3.getSynthesizer().modules.contains(c2)); } - @Test - public void test_visit() { - final List visited = new LinkedList(); - - ModuleVisitor v = new ModuleVisitor() { - - @Override - public Void visit(CompilationUnit mod, Mode mode) { - - visited.add(mod); + @Test + public void test_visit_1() { - return null; - } + // m1 ~~> m2 --> m3 + // m1 <-- m2 + m2.addModuleDependency(m1); + m1.addModuleDependency(m2); + m2.addModuleDependency(m3); - @Override - public Void init() { - return null; - } + Object[] expected = { m3, m1, m2 }; + Assert.assertArrayEquals(expected, visitSequence(m1).toArray()); - @Override - public Void combine(Void t1, Void t2) { - return null; - } + } - @Override - public boolean cancel(Void t) { - return false; - } - }; + @Test + public void test_visit_2() { + // m1 --> m2 --> m3 + // m1 <~~ m2 m1.addModuleDependency(m2); + m2.addModuleDependency(m1); m2.addModuleDependency(m3); - m2.addModuleDependency(m4); - m4.addModuleDependency(m5); - m5.addCircularModuleDependency(m1); - m1.visit(v); - - Object[] expected = { m1, m2, m3, m4, m5 }; - Assert.assertArrayEquals(expected, visited.toArray()); + Object[] expected = { m3, m2, m1 }; + Assert.assertArrayEquals(expected, visitSequence(m1).toArray()); } @@ -485,7 +481,7 @@ public void test_readWrite() throws IOException { TestCompilationUnit e1 = TestCompilationUnit.create(TestCompilationUnit.class, testStamper, e1CompiledDep, compileFolder, e1EditedDep, editedFolder, e1SourceFiles, null, new ForEditorMode(null, true), syn); e1.addModuleDependency(m3); - e1.addCircularModuleDependency(m4); + e1.circularModuleDependencies.add(m4); e1.addExternalFileDependency(f1); e1.addGeneratedFile(f2); @@ -518,6 +514,41 @@ public void test_readWrite() throws IOException { // Helper methods + List visitSequence(CompilationUnit start) { + + final List visited = new LinkedList(); + + ModuleVisitor v = new ModuleVisitor() { + + @Override + public Void visit(CompilationUnit mod, Mode mode) { + + visited.add(mod); + + return null; + } + + @Override + public Void init() { + return null; + } + + @Override + public Void combine(Void t1, Void t2) { + return null; + } + + @Override + public boolean cancel(Void t) { + return false; + } + }; + + start.visit(v); + + return visited; + } + /** * * @param length