strangedb/src/test/java/org/warp/jcwdb/AppTest.java

119 lines
3.0 KiB
Java

package org.warp.jcwdb;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import org.junit.Test;
import org.warp.jcwdb.EntryReference;
import org.warp.jcwdb.JCWDatabase;
import org.warp.jcwdb.LightList;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
/**
*
* @throws IOException
*/
@Test
public void shouldReadCustomClasses() throws IOException
{
CustomClass customClass = new CustomClass();
customClass.primitive = 1;
customClass.string = "test";
Path path = Files.createTempFile("", ".db"), idx = Files.createTempFile("", ".idx");
JCWDatabase db = new JCWDatabase(path, idx);
EntryReference<LightList<CustomClass>> ref = db.getRoot();
ref.editValue((root, saver) -> {
root.add(customClass);
});
db.close();
JCWDatabase db2 = new JCWDatabase(path, idx);
EntryReference<LightList<CustomClass>> ref2 = db2.getRoot();
CustomClass customClass2 = ref2.getValueReadOnly().getReadOnlyValue(0);
assertTrue(customClass.equals(customClass2));
assertTrue(customClass.string.equals(customClass2.string));
db2.close();
Files.delete(path);
Files.delete(idx);
}
public static class CustomClass {
public String string;
public int primitive;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + primitive;
result = prime * result + ((string == null) ? 0 : string.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomClass other = (CustomClass) obj;
if (primitive != other.primitive)
return false;
if (string == null) {
if (other.string != null)
return false;
} else if (!string.equals(other.string))
return false;
return true;
}
}
/**
*
* @throws IOException
*/
@Test
public void shouldActAsAnArrayListWithEqualObjects() throws IOException
{
Path path = Files.createTempFile("", ".db"), idx = Files.createTempFile("", ".idx");
JCWDatabase db = new JCWDatabase(path, idx);
EntryReference<LightList<String>> ref = db.getRoot();
LightList<String> list1 = ref.getValueReadOnly();
ArrayList<String> list2 = new ArrayList<String>();
String s = "a";
for (int i = 0; i < 10; i++) {
list1.add(s);
list2.add(s);
}
assertTrue(list1.size() == list2.size());
for (int i = 0; i < 10; i++) {
assertTrue(list1.get(i) == list2.get(i));
assertTrue(list2.get(i) == list1.get(i));
}
db.close();
Files.delete(path);
Files.delete(idx);
}
}