diff --git a/MOPBox/src/de/bodden/mopbox/finitestate/sync/SyncTests.java b/MOPBox/src/de/bodden/mopbox/finitestate/sync/SyncTests.java index e20a67c..3c7cfe4 100644 --- a/MOPBox/src/de/bodden/mopbox/finitestate/sync/SyncTests.java +++ b/MOPBox/src/de/bodden/mopbox/finitestate/sync/SyncTests.java @@ -1,11 +1,11 @@ package de.bodden.mopbox.finitestate.sync; +import junit.framework.Assert; import helpers.FailSafeIterMonitorTemplate; import helpers.FailSafeIterMonitorTemplate.Var; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; +import org.junit.Before; +import org.junit.Test; import com.google.common.collect.Multiset; @@ -15,22 +15,39 @@ public class SyncTests { - protected static String trace = ""; + protected String trace = ""; - static IVariableBinding b_empty = new VariableBinding(); + IVariableBinding b_empty = new VariableBinding(); - static IVariableBinding b_c = new VariableBinding(); + IVariableBinding b_c = new VariableBinding(); - static IVariableBinding b_c2 = new VariableBinding(); + IVariableBinding b_c2 = new VariableBinding(); - static IVariableBinding b_i = new VariableBinding(); + IVariableBinding b_i = new VariableBinding(); - static IVariableBinding b_ci = new VariableBinding(); + IVariableBinding b_ci = new VariableBinding(); - static { - Collection c = new ArrayList(); - Collection c2 = new ArrayList(); - Iterator i = c.iterator(); + FailSafeIterMonitorTemplate fsiNormal; + FailSafeIterMonitorTemplate fsiNumber; + MultisetSyncingTemplate multisetSync; + NumberSyncingTemplate numberSync; + + { + Object c = new Object() { + public String toString() { + return "c"; + } + }; + Object c2 = new Object() { + public String toString() { + return "c2"; + } + }; + Object i = new Object() { + public String toString() { + return "i1"; + } + }; b_c.put(Var.C, c); b_c2.put(Var.C, c2); b_i.put(Var.I, i); @@ -38,10 +55,11 @@ public class SyncTests { b_ci.put(Var.I, i); } - public static void main(String[] args) { - FailSafeIterMonitorTemplate fsi = new FailSafeIterMonitorTemplate(); - FailSafeIterMonitorTemplate fsi2 = new FailSafeIterMonitorTemplate(); - MultisetSyncingTemplate sync = new MultisetSyncingTemplate(fsi,2) { + + @Before + public void setUp() throws Exception { + fsiNormal = new FailSafeIterMonitorTemplate(); + multisetSync = new MultisetSyncingTemplate(new FailSafeIterMonitorTemplate(),2) { protected boolean shouldMonitor(ISymbol symbol,IVariableBinding binding,Multiset> skippedSymbols) { //only skip a single symbol return skippedSymbols.size()>0; @@ -50,35 +68,96 @@ public void matchCompleted(IVariableBinding binding) { trace += binding.toString(); } }; + numberSync = new NumberSyncingTemplate(new FailSafeIterMonitorTemplate(),2) { + protected boolean shouldMonitor(ISymbol symbol,IVariableBinding binding,Multiset> skippedSymbols) { + //only skip a single symbol + return skippedSymbols.size()>0; + } + public void matchCompleted(IVariableBinding binding) { + trace += binding.toString(); + } + }; + trace = ""; + } + + + /* + * Here we test that the number-syncing monitor does not match. + * This is because we skip the update-event below, and therefore, + * because we are only counting the skipped events, do not know whether + * the skipped event is actually a an update or iter event. + * Since the monitor is a must-monitor, it assumes that + * it was not an update, and therefore does not report the violation. + */ + @Test + public void numberSimpleCase() { + numberSync.maybeProcessEvent("update", b_c); //skipped + fsiNormal.processEvent("update", b_c); + + numberSync.maybeProcessEvent("create", b_ci); //monitored + fsiNormal.processEvent("create", b_ci); + + numberSync.maybeProcessEvent("update", b_c); //skipped + fsiNormal.processEvent("update", b_c); + + numberSync.maybeProcessEvent("iter", b_i); //monitored + fsiNormal.processEvent("iter", b_i); + + Assert.assertEquals("",syncTrace()); + Assert.assertEquals("{C=c, I=i1}",normalTrace()); + } + + /* + * This is a simple test case that validates that the multiset-based monitor matches. + */ + @Test + public void multisetSimpleCase() { + multisetSync.maybeProcessEvent("update", b_c); //skipped + fsiNormal.processEvent("update", b_c); + + multisetSync.maybeProcessEvent("create", b_ci); //monitored + fsiNormal.processEvent("create", b_ci); - System.out.println(fsi); - System.out.println("==============="); - System.out.println(sync); + multisetSync.maybeProcessEvent("update", b_c); //skipped + fsiNormal.processEvent("update", b_c); - falsePositive(fsi2, sync); + multisetSync.maybeProcessEvent("iter", b_i); //monitored + fsiNormal.processEvent("iter", b_i); + + Assert.assertEquals("{C=c, I=i1}",syncTrace()); + Assert.assertEquals("{C=c, I=i1}",normalTrace()); } + /* * In this test case, the sync template will raise a false positive. * This is because it currently loses the binding on the third event (which is skipped), and * therefore believes that collection c may have been updated. */ - protected static void falsePositive(FailSafeIterMonitorTemplate fsi2, MultisetSyncingTemplate sync) { - sync.maybeProcessEvent("update", b_c); //skipped - fsi2.processEvent("update", b_c); + @Test + public void multisetFalsePositive() { + multisetSync.maybeProcessEvent("update", b_c); //skipped + fsiNormal.processEvent("update", b_c); - sync.maybeProcessEvent("create", b_ci); //monitored - fsi2.processEvent("create", b_ci); + multisetSync.maybeProcessEvent("create", b_ci); //monitored + fsiNormal.processEvent("create", b_ci); - sync.maybeProcessEvent("update", b_c2); //skipped, binding lost - fsi2.processEvent("update", b_c2); + multisetSync.maybeProcessEvent("update", b_c2); //skipped, binding lost + fsiNormal.processEvent("update", b_c2); - sync.maybeProcessEvent("iter", b_i); //monitored - fsi2.processEvent("iter", b_i); + multisetSync.maybeProcessEvent("iter", b_i); //monitored + fsiNormal.processEvent("iter", b_i); - System.err.println("Sync: "+trace); - System.err.println("Normal: "+fsi2.getTrace()); + Assert.assertEquals("{C=c, I=i1}",syncTrace()); + Assert.assertEquals("",normalTrace()); + } + + private String normalTrace() { + return fsiNormal.getTrace(); } + private String syncTrace() { + return trace; + } }