From 12b3267d5cfa781782e7c039f921afda41af8422 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 22 Oct 2021 11:49:15 +0100 Subject: [PATCH] Added arbitrary data merge tests. --- .../qortal/arbitrary/ArbitraryDataMerge.java | 1 - .../arbitrary/ArbitraryDataDigestTests.java | 2 +- .../arbitrary/ArbitraryDataMergeTests.java | 143 ++++++++++++++++++ .../resources/arbitrary/demo1/.qortal/cache | 2 +- .../resources/arbitrary/demo1/dir1/lorem4.txt | 11 +- .../arbitrary/demo2/dir1/dir2/lorem5.txt | 2 +- .../resources/arbitrary/demo2/dir1/lorem4.txt | 12 +- src/test/resources/arbitrary/demo2/lorem1.txt | 2 +- src/test/resources/arbitrary/demo2/lorem2.txt | 3 +- 9 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/qortal/test/arbitrary/ArbitraryDataMergeTests.java diff --git a/src/main/java/org/qortal/arbitrary/ArbitraryDataMerge.java b/src/main/java/org/qortal/arbitrary/ArbitraryDataMerge.java index 05745ffb..98ac124f 100644 --- a/src/main/java/org/qortal/arbitrary/ArbitraryDataMerge.java +++ b/src/main/java/org/qortal/arbitrary/ArbitraryDataMerge.java @@ -7,7 +7,6 @@ import com.github.difflib.patch.PatchFailedException; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.eclipse.jetty.util.IO; import org.qortal.arbitrary.metadata.ArbitraryDataMetadataPatch; import org.qortal.settings.Settings; import org.qortal.utils.FilesystemUtils; diff --git a/src/test/java/org/qortal/test/arbitrary/ArbitraryDataDigestTests.java b/src/test/java/org/qortal/test/arbitrary/ArbitraryDataDigestTests.java index b2772a28..a2f07b49 100644 --- a/src/test/java/org/qortal/test/arbitrary/ArbitraryDataDigestTests.java +++ b/src/test/java/org/qortal/test/arbitrary/ArbitraryDataDigestTests.java @@ -24,7 +24,7 @@ public class ArbitraryDataDigestTests extends Common { @Test public void testDirectoryDigest() throws IOException { Path dataPath = Paths.get("src/test/resources/arbitrary/demo1"); - String expectedHash58 = "59dw8CgVybcHAUL5GYgYUUfFffVVhiMKZLCnULPKT6oC"; + String expectedHash58 = "4cibh7BWNUiaPXL88EgsoKxwg4SG7GVmRRQbBYUrkbcy"; // Ensure directory exists assertTrue(dataPath.toFile().exists()); diff --git a/src/test/java/org/qortal/test/arbitrary/ArbitraryDataMergeTests.java b/src/test/java/org/qortal/test/arbitrary/ArbitraryDataMergeTests.java new file mode 100644 index 00000000..e873112e --- /dev/null +++ b/src/test/java/org/qortal/test/arbitrary/ArbitraryDataMergeTests.java @@ -0,0 +1,143 @@ +package org.qortal.test.arbitrary; + +import org.junit.Before; +import org.junit.Test; +import org.qortal.arbitrary.ArbitraryDataCreatePatch; +import org.qortal.arbitrary.ArbitraryDataDigest; +import org.qortal.arbitrary.ArbitraryDataMerge; +import org.qortal.crypto.Crypto; +import org.qortal.repository.DataException; +import org.qortal.test.common.Common; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class ArbitraryDataMergeTests extends Common { + + @Before + public void beforeTest() throws DataException { + Common.useDefaultSettings(); + } + + @Test + public void testCreateAndMergePatch() throws IOException, DataException { + Path path1 = Paths.get("src/test/resources/arbitrary/demo1"); + Path path2 = Paths.get("src/test/resources/arbitrary/demo2"); + + // Create a patch using the differences in path2 compared with path1 + ArbitraryDataCreatePatch patch = new ArbitraryDataCreatePatch(path1, path2, new byte[16]); + patch.create(); + Path patchPath = patch.getFinalPath(); + assertTrue(Files.exists(patchPath)); + + // Check that lorem1, 2, 4, and 5 exist + assertTrue(Files.exists(Paths.get(patchPath.toString(), "lorem1.txt"))); + assertTrue(Files.exists(Paths.get(patchPath.toString(), "lorem2.txt"))); + assertTrue(Files.exists(Paths.get(patchPath.toString(), "dir1", "lorem4.txt"))); + assertTrue(Files.exists(Paths.get(patchPath.toString(), "dir1", "dir2", "lorem5.txt"))); + + // Ensure that lorem3 doesn't exist, as this file is identical in the original paths + assertFalse(Files.exists(Paths.get(patchPath.toString(), "lorem3.txt"))); + + // Ensure that the patch files differ from the first path (except for lorem3, which is missing) + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path1.toString(), "lorem1.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "lorem1.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path1.toString(), "lorem2.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "lorem2.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path1.toString(), "dir1", "lorem4.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "dir1", "lorem4.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path1.toString(), "dir1", "dir2", "lorem5.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "dir1", "dir2", "lorem5.txt").toFile()) + )); + + // Ensure that the patch files differ from the second path (except for lorem3, which is missing) + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path2.toString(), "lorem1.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "lorem1.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path2.toString(), "lorem2.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "lorem2.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path2.toString(), "dir1", "lorem4.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "dir1", "lorem4.txt").toFile()) + )); + assertFalse(Arrays.equals( + Crypto.digest(Paths.get(path2.toString(), "dir1", "dir2", "lorem5.txt").toFile()), + Crypto.digest(Paths.get(patchPath.toString(), "dir1", "dir2", "lorem5.txt").toFile()) + )); + + // Now merge the patch with the original path + ArbitraryDataMerge merge = new ArbitraryDataMerge(path1, patchPath, "unified-diff"); + merge.compute(); + Path finalPath = merge.getMergePath(); + + // Ensure that all files exist in the final path (including lorem3) + assertTrue(Files.exists(Paths.get(finalPath.toString(), "lorem1.txt"))); + assertTrue(Files.exists(Paths.get(finalPath.toString(), "lorem2.txt"))); + assertTrue(Files.exists(Paths.get(finalPath.toString(), "lorem3.txt"))); + assertTrue(Files.exists(Paths.get(finalPath.toString(), "dir1", "lorem4.txt"))); + assertTrue(Files.exists(Paths.get(finalPath.toString(), "dir1", "dir2", "lorem5.txt"))); + + // Ensure that the files match those in path2 exactly + assertArrayEquals( + Crypto.digest(Paths.get(finalPath.toString(), "lorem1.txt").toFile()), + Crypto.digest(Paths.get(path2.toString(), "lorem1.txt").toFile()) + ); + assertArrayEquals( + Crypto.digest(Paths.get(finalPath.toString(), "lorem2.txt").toFile()), + Crypto.digest(Paths.get(path2.toString(), "lorem2.txt").toFile()) + ); + assertArrayEquals( + Crypto.digest(Paths.get(finalPath.toString(), "lorem3.txt").toFile()), + Crypto.digest(Paths.get(path2.toString(), "lorem3.txt").toFile()) + ); + assertArrayEquals( + Crypto.digest(Paths.get(finalPath.toString(), "dir1", "lorem4.txt").toFile()), + Crypto.digest(Paths.get(path2.toString(), "dir1", "lorem4.txt").toFile()) + ); + assertArrayEquals( + Crypto.digest(Paths.get(finalPath.toString(), "dir1", "dir2", "lorem5.txt").toFile()), + Crypto.digest(Paths.get(path2.toString(), "dir1", "dir2", "lorem5.txt").toFile()) + ); + + // Also check that the directory digests match + ArbitraryDataDigest path2Digest = new ArbitraryDataDigest(path2); + path2Digest.compute(); + ArbitraryDataDigest finalPathDigest = new ArbitraryDataDigest(path2); + finalPathDigest.compute(); + assertEquals(path2Digest.getHash58(), finalPathDigest.getHash58()); + } + + @Test + public void testIdenticalPaths() throws IOException, DataException { + Path path = Paths.get("src/test/resources/arbitrary/demo1"); + + // Create a patch from two identical paths + ArbitraryDataCreatePatch patch = new ArbitraryDataCreatePatch(path, path, new byte[16]); + + // Ensure that an exception is thrown due to matching states + try { + patch.create(); + fail("Creating patch should fail due to matching states"); + + } catch (IllegalStateException expectedException) { + assertEquals("Current state matches previous state. Nothing to do.", expectedException.getMessage()); + } + + } + +} diff --git a/src/test/resources/arbitrary/demo1/.qortal/cache b/src/test/resources/arbitrary/demo1/.qortal/cache index d10d5a03..90f0f8dc 100644 --- a/src/test/resources/arbitrary/demo1/.qortal/cache +++ b/src/test/resources/arbitrary/demo1/.qortal/cache @@ -1 +1 @@ -2ea5a99a-3b85-4f1f-a259-436787f90bd1 \ No newline at end of file +db2d9ab2-a97e-43bf-a259-ebbc1a1b0c59 \ No newline at end of file diff --git a/src/test/resources/arbitrary/demo1/dir1/lorem4.txt b/src/test/resources/arbitrary/demo1/dir1/lorem4.txt index 80d1dda7..6ac4bdd8 100644 --- a/src/test/resources/arbitrary/demo1/dir1/lorem4.txt +++ b/src/test/resources/arbitrary/demo1/dir1/lorem4.txt @@ -1 +1,10 @@ -Sed non lacus ante. +Pellentesque mollis risus laoreet neque lobortis, ut euismod nisl gravida. +Nullam sit amet scelerisque sapien, id aliquet elit. Suspendisse eu +accumsan eros. Nullam non nunc ut risus facilisis posuere sed sed ipsum. +Pellentesque habitant morbi tristique senectus et netus et malesuada fames +ac turpis egestas. Nullam magna felis, vehicula a accumsan luctus, vulputate +vitae justo. Integer mollis lacus eu nisi iaculis, ac ultrices sem aliquam. +Sed ac lacus eget nibh posuere sodales. Phasellus sodales, augue ac +tincidunt scelerisque, mi erat varius mauris, sed blandit ex nisl ut +justo. Etiam ac nisl venenatis, malesuada odio vitae, blandit velit. +Phasellus congue leo a porttitor hendrerit. diff --git a/src/test/resources/arbitrary/demo2/dir1/dir2/lorem5.txt b/src/test/resources/arbitrary/demo2/dir1/dir2/lorem5.txt index ef07da1f..73b058e4 100644 --- a/src/test/resources/arbitrary/demo2/dir1/dir2/lorem5.txt +++ b/src/test/resources/arbitrary/demo2/dir1/dir2/lorem5.txt @@ -1 +1 @@ -Pellentesque laoreet laoreet dui ut volutpat. +Pellentesque laoreet laoreet dui ut volutpat. Sentence added. diff --git a/src/test/resources/arbitrary/demo2/dir1/lorem4.txt b/src/test/resources/arbitrary/demo2/dir1/lorem4.txt index 80d1dda7..30c17943 100644 --- a/src/test/resources/arbitrary/demo2/dir1/lorem4.txt +++ b/src/test/resources/arbitrary/demo2/dir1/lorem4.txt @@ -1 +1,11 @@ -Sed non lacus ante. +Pellentesque mollis risus laoreet neque lobortis, ut euismod nisl gravida. +Nullam sit amet scelerisque sapien, id aliquet elit. Suspendisse eu +accumsan eros. Nullam non nunc ut risus facilisis posuere sed sed ipsum. +Pellentesque habitant morbi tristique senectus et netus et malesuada fames +ac turpis egestas. Nullam magna felis; vehicula a accumsan luctus, vulputate +vitae justo. Integer mollis lacus eu nisi iaculis, ac ultrices sem aliquam. +Sed ac lacus eget nibh posuere sodales. Phasellus sodales, augue ac +tincidunt scelerisque, mi erat Varius mauris, sed blandit ex nisl ut +justo. Etiam ac nisl venenatis, malesuada odio vitae, blandit velit. +Phasellus congue leo a porttitor hendrerit. +Line added. diff --git a/src/test/resources/arbitrary/demo2/lorem1.txt b/src/test/resources/arbitrary/demo2/lorem1.txt index 4f006a88..febb7414 100644 --- a/src/test/resources/arbitrary/demo2/lorem1.txt +++ b/src/test/resources/arbitrary/demo2/lorem1.txt @@ -1 +1 @@ -Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Lorem ipsum dolor sit amet, adipiscing elit. diff --git a/src/test/resources/arbitrary/demo2/lorem2.txt b/src/test/resources/arbitrary/demo2/lorem2.txt index 8a9c4367..de4628f2 100644 --- a/src/test/resources/arbitrary/demo2/lorem2.txt +++ b/src/test/resources/arbitrary/demo2/lorem2.txt @@ -1 +1,2 @@ -Quisque viverra neque quis eros dapibus +Quisque viverra neque +quis eros dapibus