Added cluster tests.
This commit is contained in:
parent
b92aedf872
commit
7ccb0494ee
Binary file not shown.
After Width: | Height: | Size: 803 B |
Binary file not shown.
After Width: | Height: | Size: 795 B |
|
@ -1,5 +1,6 @@
|
||||||
package nl.andrewlalis.crystalkeep.model.serialization;
|
package nl.andrewlalis.crystalkeep.model.serialization;
|
||||||
|
|
||||||
|
import nl.andrewlalis.crystalkeep.model.Cluster;
|
||||||
import nl.andrewlalis.crystalkeep.model.Shard;
|
import nl.andrewlalis.crystalkeep.model.Shard;
|
||||||
import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard;
|
import nl.andrewlalis.crystalkeep.model.shards.LoginCredentialsShard;
|
||||||
import nl.andrewlalis.crystalkeep.model.shards.TextShard;
|
import nl.andrewlalis.crystalkeep.model.shards.TextShard;
|
||||||
|
@ -10,6 +11,7 @@ import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -41,7 +43,7 @@ public class ClusterSerializerTest {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
s1.getName(),
|
s1.getName(),
|
||||||
new String(Arrays.copyOfRange(data, 4, 4 + s1.getName().length())),
|
new String(Arrays.copyOfRange(data, 4, 4 + s1.getName().length())),
|
||||||
"First letter of shard name does not match expected."
|
"Shard name does not match expected."
|
||||||
);
|
);
|
||||||
|
|
||||||
Shard loadedS1 = ClusterSerializer.readShard(new ByteArrayInputStream(data));
|
Shard loadedS1 = ClusterSerializer.readShard(new ByteArrayInputStream(data));
|
||||||
|
@ -51,4 +53,50 @@ public class ClusterSerializerTest {
|
||||||
byte[] data2 = bos.toByteArray();
|
byte[] data2 = bos.toByteArray();
|
||||||
assertArrayEquals(data, data2, "Serialized data from a shard should not change.");
|
assertArrayEquals(data, data2, "Serialized data from a shard should not change.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static List<Cluster> testClusterIOData() {
|
||||||
|
List<Cluster> clusters = new ArrayList<>();
|
||||||
|
clusters.add(new Cluster("test"));
|
||||||
|
|
||||||
|
Cluster c1 = new Cluster("c1");
|
||||||
|
c1.addCluster(new Cluster("c2"));
|
||||||
|
clusters.add(c1);
|
||||||
|
|
||||||
|
Cluster c2 = new Cluster("c2");
|
||||||
|
c2.addShard(new TextShard("testing", LocalDateTime.now(), "Hello world!"));
|
||||||
|
clusters.add(c2);
|
||||||
|
|
||||||
|
Cluster c3 = new Cluster("c3");
|
||||||
|
Cluster c3Nested = new Cluster("nested");
|
||||||
|
c3Nested.addShard(new LoginCredentialsShard("login", LocalDateTime.now(), "andrew", "secret password"));
|
||||||
|
c3.addCluster(c3Nested);
|
||||||
|
clusters.add(c3);
|
||||||
|
return clusters;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("testClusterIOData")
|
||||||
|
public void testClusterIO(Cluster c) throws IOException {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
ClusterSerializer.writeCluster(c, bos);
|
||||||
|
byte[] data = bos.toByteArray();
|
||||||
|
assertNotEquals(0, data.length, "Serialized cluster should never result in empty bytes.");
|
||||||
|
assertEquals(
|
||||||
|
c.getName().length(),
|
||||||
|
ByteUtils.toInt(Arrays.copyOfRange(data, 0, 4)),
|
||||||
|
"Serialized cluster name length does not match expected."
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
c.getName(),
|
||||||
|
new String(Arrays.copyOfRange(data, 4, 4 + c.getName().length())),
|
||||||
|
"Cluster name does not match expected."
|
||||||
|
);
|
||||||
|
|
||||||
|
Cluster loaded = ClusterSerializer.readCluster(new ByteArrayInputStream(data), null);
|
||||||
|
assertEquals(c, loaded, "Loaded cluster should equal original cluster.");
|
||||||
|
bos.reset();
|
||||||
|
ClusterSerializer.writeCluster(loaded, bos);
|
||||||
|
byte[] data2 = bos.toByteArray();
|
||||||
|
assertArrayEquals(data, data2, "Serialized cluster should not change.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue