From 4a52f5e6ce06cb6717510c6a975d5490be627c98 Mon Sep 17 00:00:00 2001 From: Keuin Date: Tue, 12 Jan 2021 15:03:23 +0800 Subject: Add unit test for Sha256Identifier and ObjectCollection --- .../util/backup/incremental/ObjectCollection.java | 45 ++++++++++++----- .../incremental/ObjectCollectionFactory.java | 17 +++---- .../util/backup/incremental/ObjectElement.java | 59 ++++++++++++++++++++++ .../incremental/identifier/ObjectIdentifier.java | 1 + .../incremental/identifier/Sha256Identifier.java | 4 ++ .../identifier/SingleHashIdentifier.java | 1 + 6 files changed, 105 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java (limited to 'src/main') diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java index d5d766a..e0ef4f7 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollection.java @@ -1,31 +1,50 @@ package com.keuin.kbackupfabric.util.backup.incremental; -import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; - -import java.util.Objects; -import java.util.Set; +import java.util.*; public class ObjectCollection { private final String name; - private final Set elements; - private final Set subCollections; + private final Map elements; + private final Map subCollections; - ObjectCollection(String name, Set elements, Set subCollections) { + ObjectCollection(String name, Set elements, Map subCollections) { this.name = Objects.requireNonNull(name); - this.elements = Objects.requireNonNull(elements); - this.subCollections = Objects.requireNonNull(subCollections); + this.elements = new HashMap<>(); + for (ObjectElement e : elements) { + Objects.requireNonNull(e); + if (this.elements.put(e.getName(), e) != null) { + throw new IllegalStateException("elements conflict with the same name"); + } + } + this.subCollections = new HashMap<>(Objects.requireNonNull(subCollections)); } public String getName() { return name; } - public Set getElements() { - return elements; + public Set getElementSet() { + return new HashSet<>(elements.values()); + } + + public Map getElementMap() { + return Collections.unmodifiableMap(elements); + } + + public ObjectElement getElement(String name) { + return elements.get(name); + } + + public Set getSubCollectionSet() { + return new HashSet<>(subCollections.values()); + } + + public Map getSubCollectionMap() { + return Collections.unmodifiableMap(subCollections); } - public Set getSubCollections() { - return subCollections; + public ObjectCollection getSubCollection(String name) { + return subCollections.get(name); } @Override diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java index 8b974db..be9e426 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectCollectionFactory.java @@ -7,10 +7,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Objects; -import java.util.Set; +import java.util.*; /** * Incremental backup is implemented as git-like file collection. @@ -26,19 +23,21 @@ public class ObjectCollectionFactory { } public ObjectCollection fromDirectory(File directory) throws IOException { - final Set subFiles = new HashSet<>(); - final Set subCollections = new HashSet<>(); + final Set subFiles = new HashSet<>(); + final Map subCollections = new HashMap<>(); if (!Objects.requireNonNull(directory).isDirectory()) throw new IllegalArgumentException("given file is not a directory"); - for (Iterator iter = Files.walk(directory.toPath()).iterator(); iter.hasNext();) { + for (Iterator iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext();) { Path path = iter.next(); + if (Files.isSameFile(path, directory.toPath())) + continue; File file = path.toFile(); if (file.isDirectory()) { - subCollections.add(fromDirectory(file)); + subCollections.put(file.getName(), fromDirectory(file)); } else { - subFiles.add(identifierFactory.fromFile(file)); + subFiles.add(new ObjectElement(file.getName(), identifierFactory.fromFile(file))); } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java new file mode 100644 index 0000000..1232fb9 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/ObjectElement.java @@ -0,0 +1,59 @@ +package com.keuin.kbackupfabric.util.backup.incremental; + +import com.keuin.kbackupfabric.util.backup.incremental.identifier.ObjectIdentifier; + +import java.util.Objects; + +/** + * Representing a file in a ObjectCollection. + * Immutable. + */ +public class ObjectElement { + private final String name; + private final ObjectIdentifier identifier; + + public ObjectElement(String name, ObjectIdentifier identifier) { + Objects.requireNonNull(name); + Objects.requireNonNull(identifier); + this.name = name; + this.identifier = identifier; + } + + /** + * Get file name. + * @return the file name. + */ + public String getName() { + return name; + } + + /** + * Get file identifier, which is considered to be different between files with different contents. + * @return the identifier. + */ + public ObjectIdentifier getIdentifier() { + return identifier; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ObjectElement that = (ObjectElement) o; + return name.equals(that.name) && + identifier.equals(that.identifier); + } + + @Override + public int hashCode() { + return Objects.hash(name, identifier); + } + + @Override + public String toString() { + return "ObjectElement{" + + "name='" + name + '\'' + + ", identifier=" + identifier + + '}'; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java index 62798e1..6744616 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java @@ -4,6 +4,7 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; * The identifier distinguishing files in the object collection. * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. * All identifiers should be immutable and implement their own equals method. + * Immutable. */ public interface ObjectIdentifier { String getIdentification(); diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java index 9f56b0e..64716ed 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java @@ -7,6 +7,10 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Objects; +/** + * Identifier based on sha256. + * Immutable. + */ public class Sha256Identifier extends SingleHashIdentifier { private static final int SHA256_LENGTH = 32; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java index 62ba47c..3b96f79 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -8,6 +8,7 @@ import java.util.Arrays; /** * A simple identifier based on a single hash function. + * Immutable. */ public abstract class SingleHashIdentifier implements ObjectIdentifier { -- cgit v1.2.3