diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2014-09-20 09:17:18 +0200 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2015-04-14 07:44:29 +0200 |
commit | d6f22a2af48f83d63b5381118d2029797458194e (patch) | |
tree | cb6bef9a98335a7af2aee40b0752d14fcee0916e /src/core/JUTests | |
parent | 774194091e9bcee08e48fcdf4127f9afd9d6d644 (diff) | |
download | sssync-d6f22a2af48f83d63b5381118d2029797458194e.tar.gz sssync-d6f22a2af48f83d63b5381118d2029797458194e.tar.bz2 sssync-d6f22a2af48f83d63b5381118d2029797458194e.zip |
Early development stages (before SCM) : WIP_1
Early development stages (before SCM) : WIP_2
Early development stages (before SCM) : WIP_3
Early development stages (before SCM) : WIP_4
Early development stages (before SCM) : WIP_6
Early development stages (before SCM) : WIP_7
Early development stages (before SCM) : WIP_8
Adds documentation folder as an Eclipse project.
Adds README for github.
Decent source tree by tuning Eclise project's location
One forgetten file while movign everything :)
Adding Copyright, licencing (GPL v3), correcting README
Diffstat (limited to 'src/core/JUTests')
-rw-r--r-- | src/core/JUTests/data/MVDataEntryTest.java | 93 | ||||
-rw-r--r-- | src/core/JUTests/data/io/filters/MVDataCombinerTest.java | 148 |
2 files changed, 241 insertions, 0 deletions
diff --git a/src/core/JUTests/data/MVDataEntryTest.java b/src/core/JUTests/data/MVDataEntryTest.java new file mode 100644 index 0000000..19ccb46 --- /dev/null +++ b/src/core/JUTests/data/MVDataEntryTest.java @@ -0,0 +1,93 @@ +package data; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.google.common.collect.HashMultimap; + +public class MVDataEntryTest { + + @Test + public void testMVDataEntryStringIntInt() { + String expected = "{key=line1, attrValPairs={k4=[v4], k1=[v1b, v1a, v1c], k2=[v1c]}}"; + + MVDataEntry e1 = new MVDataEntry("line1", 1, 1); + HashMultimap<String, String> e1v = e1.getAttrValPairs(); + e1v.put("k1", "v1a"); + e1v.put("k1", "v1b"); + e1v.put("k1", "v1b"); // Twice, should disappear silently + e1v.put("k1", "v1c"); + e1v.put("k2", "v1c"); + e1v.put("k4", "v4"); + + assertEquals(expected, e1.toString()); + + } + + @Test + public void testMerge() { + // Test data + MVDataEntry e1 = new MVDataEntry("10"); + HashMultimap<String, String> e1v = e1.getAttrValPairs(); + e1v.put("k1", "v1a"); + e1v.put("k1", "v1b"); + e1v.put("k1", "v1c"); + e1v.put("k2", "v2"); + e1v.put("k4", "v4"); + + MVDataEntry e2 = new MVDataEntry("2"); + HashMultimap<String, String> e2v = e2.getAttrValPairs(); + e2v.put("k2", "v2"); + e2v.put("k1", "v1b"); + e2v.put("k3", "v3"); + + MVDataEntry r1 = new MVDataEntry(e1); + r1.mergeValues(true, e2); + assertNotSame(r1, e1); + String expected1 = "{key=10, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected1, r1.toString()); + + MVDataEntry r2 = new MVDataEntry(e2); + r2.mergeValues(true, e1); + assertNotSame(r2, e2); + String expected2 = "{key=2, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected2, r2.toString()); + + MVDataEntry r3 = new MVDataEntry(e1); + r3.mergeValues(false, e2); + assertNotSame(r3, e1); + String expected3 = "{key=10, attrValPairs={k3=[v3], k4=[v4], k1=[v1b], k2=[v2]}}"; + //System.out.println(expected3); + //System.out.println(r3.toString()); + assertEquals(expected3, r3.toString()); + + MVDataEntry r4 = new MVDataEntry(e2); + r4.mergeValues(false, e1); + assertNotSame(r4, e1); + String expected4 = "{key=2, attrValPairs={k3=[v3], k4=[v4], k1=[v1b, v1a, v1c], k2=[v2]}}"; + assertEquals(expected4, r4.toString()); + + assertTrue(!r2.equals(r3)); + assertEquals(r2,r4); + } + + @Test + public void testSplitAndPut() { + MVDataEntry r1 = new MVDataEntry("10"); + r1.splitAndPut("k1", "v1a;v1b;v1c", ";"); + r1.put("k2", "v2", null); // splitAndPut does not support null regex anymore, use put() + r1.splitAndPut("k4", "v4", "^$"); + + MVDataEntry expected1 = new MVDataEntry("10"); + HashMultimap<String, String> expected1v = expected1.getAttrValPairs(); + expected1v.put("k1", "v1a"); + expected1v.put("k1", "v1b"); + expected1v.put("k1", "v1c"); + expected1v.put("k2", "v2"); + expected1v.put("k4", "v4"); + + assertEquals(r1,expected1); + } + +} diff --git a/src/core/JUTests/data/io/filters/MVDataCombinerTest.java b/src/core/JUTests/data/io/filters/MVDataCombinerTest.java new file mode 100644 index 0000000..5d32dd8 --- /dev/null +++ b/src/core/JUTests/data/io/filters/MVDataCombinerTest.java @@ -0,0 +1,148 @@ +package data.io.filters; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import data.MVDataEntry; +import data.filters.MVDataCombiner; +import data.filters.MVDataCombiner.MVDataCombineMode; +import data.io.MVDataReader; +import data.io.stub.StubDataReader; + +public class MVDataCombinerTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void testOutOfOrderCase() { + // Test Data + MVDataEntry e10 = new MVDataEntry("line2"); + e10.getAttrValPairs().put("merge", "e10"); + MVDataEntry e11 = new MVDataEntry("line1"); + e11.getAttrValPairs().put("merge", "e11"); + + MVDataEntry e21 = new MVDataEntry("line2"); + e21.getAttrValPairs().put("merge", "e21"); + + MVDataEntry[][] fakeEntries = new MVDataEntry[][] { + new MVDataEntry[] { e10, e11 }, + new MVDataEntry[] { e21 }, + }; + + MVDataCombineMode mergeModes[] = new MVDataCombineMode[]{ + MVDataCombineMode.PRIMARY_SOURCE, + MVDataCombineMode.MERGE_APPEND, + }; + + // Expected results + MVDataEntry line1 = new MVDataEntry(e10); + line1.mergeValues(true, e21); + + MVDataEntry expected[] = new MVDataEntry[] { + line1, + null /* Should throw UnsupportedOperationException() before comparing */ + }; + + // Test run + exception.expect(UnsupportedOperationException.class); + doCombineTest(expected, fakeEntries, mergeModes); + } + + + @Test + public void testGeneralCase() { + + // Test Data + MVDataEntry e10 = new MVDataEntry("line3"); + e10.getAttrValPairs().put("from1", "e10"); + e10.getAttrValPairs().put("merge", "e10"); + MVDataEntry e11 = new MVDataEntry("line4"); + e11.getAttrValPairs().put("from1", "e11"); + e11.getAttrValPairs().put("merge", "e11"); + + MVDataEntry e20 = new MVDataEntry("line1"); + e20.getAttrValPairs().put("from2", "e20"); + e20.getAttrValPairs().put("merge", "e20"); + MVDataEntry e21 = new MVDataEntry("line2"); + e21.getAttrValPairs().put("from2", "e21"); + e21.getAttrValPairs().put("merge", "e21"); + MVDataEntry e22 = new MVDataEntry("line3"); + e22.getAttrValPairs().put("from2", "e22"); + e22.getAttrValPairs().put("merge", "e22"); + + MVDataEntry e30 = new MVDataEntry("line2"); + e30.getAttrValPairs().put("from3", "e30"); + e30.getAttrValPairs().put("merge", "e30"); + + + MVDataEntry[][] fakeEntries = new MVDataEntry[][] { + new MVDataEntry[] { e10, e11 }, + new MVDataEntry[] { e20, e21, e22 }, + new MVDataEntry[] { e30 }, + }; + + MVDataCombineMode mergeModes[] = new MVDataCombineMode[]{ + MVDataCombineMode.PRIMARY_SOURCE, + MVDataCombineMode.MERGE_REPLACE, + MVDataCombineMode.MERGE_APPEND, + }; + + // Expected results + MVDataEntry line1 = new MVDataEntry(e20); + + MVDataEntry line2 = new MVDataEntry(e21); + line2.mergeValues(true, e30); + + MVDataEntry line3 = new MVDataEntry(e10); + line3.mergeValues(false, e22); + + MVDataEntry line4 = new MVDataEntry(e11); + + MVDataEntry expected[] = new MVDataEntry[] { + line1,line2,line3,line4 + }; + + // Test run + doCombineTest(expected, fakeEntries, mergeModes); + } + + // TODO : test all Combine modes + + /** + * Helper function to factorise Combiner tests. + * @param expected + * @param fakeEntries + * @param mergeModes + */ + public void doCombineTest(MVDataEntry expected[], MVDataEntry[][] fakeEntries, MVDataCombineMode mergeModes[]) { + // Test init + MVDataReader readers[] = new MVDataReader[fakeEntries.length]; + for (int i = 0; i < fakeEntries.length; i++) { + readers[i] = new StubDataReader("fakeReader"+i,fakeEntries[i]); + } + + MVDataCombiner combiner = new MVDataCombiner("combiner", readers, mergeModes); + + // Test twice to check if asking a new iterator "rewinds" correctly + for (int i=0;i<2;i++) { + //System.out.println("Loop " + (i+1)); + + Iterator<MVDataEntry> combinerIt = combiner.iterator(); + for (int j = 0; j < expected.length; j++) { + assertTrue(combinerIt.hasNext()); + MVDataEntry item = combinerIt.next(); + //System.out.println(expected[i]); + //System.out.println(item); + //System.out.println(); + assertEquals(expected[j], item); + } + assertFalse(combinerIt.hasNext()); + } + } +} |