Added arbitrary data merge tests.

This commit is contained in:
CalDescent 2021-10-22 11:49:15 +01:00
parent d6d564c027
commit 12b3267d5c
9 changed files with 170 additions and 8 deletions

View File

@ -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;

View File

@ -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());

View File

@ -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());
}
}
}

View File

@ -1 +1 @@
2ea5a99a-3b85-4f1f-a259-436787f90bd1
db2d9ab2-a97e-43bf-a259-ebbc1a1b0c59

View File

@ -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.

View File

@ -1 +1 @@
Pellentesque laoreet laoreet dui ut volutpat.
Pellentesque laoreet laoreet dui ut volutpat. Sentence added.

View File

@ -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.

View File

@ -1 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, adipiscing elit.

View File

@ -1 +1,2 @@
Quisque viverra neque quis eros dapibus
Quisque viverra neque
quis eros dapibus