package it.cavallium.strangedb.utils; import it.cavallium.strangedb.annotations.*; import it.cavallium.strangedb.functionalinterfaces.RunnableWithIO; import it.unimi.dsi.fastutil.longs.LongArrayList; import it.cavallium.strangedb.database.Database; import it.cavallium.strangedb.EnhancedObject; import it.cavallium.strangedb.database.IDatabaseTools; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Comparator; import static org.junit.Assert.*; public class NTestUtils { public static WrappedDb wrapDb() { return new WrappedDb(); } public static class WrappedDb { private Database db; private Path tempDir; private RunnableWithIO r; private WrappedDb() { } public WrappedDb create() throws IOException { tempDir = Files.createTempDirectory("tests-"); db = openDatabase(); if (r != null) { r.run(); } return this; } public WrappedDb create(ConsumerWithIO r) throws IOException { this.r = () -> r.accept(WrappedDb.this); this.create(); return this; } private Database openDatabase() throws IOException { return new Database(tempDir.resolve(Paths.get("data.db")), tempDir.resolve(Paths.get("blocks.dat")), tempDir.resolve(Paths.get("references.dat"))); } public void delete() throws IOException { db.close(); deleteDir(tempDir); } public Database get() { return db; } private void deleteDir(Path p) throws IOException { Files.walk(p) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } public void closeAndReopen() throws IOException { db.close(); db = openDatabase(); r.run(); } public void setRootClassValues(RootClass root) throws IOException { setRootClassFields(root); setRootClassProperties(root); } public void setRootClassFields(RootClass root) throws IOException { root.field1 = true; root.field2 = 2; root.field3 = 3; root.field4 = 4; root.field5 = 5; root.field6 = 6; root.field7 = "Test"; root.field8 = new LongArrayList(); root.field8.add(0); root.field8.add(1); root.field8.add(2); root.field8.add(Long.MAX_VALUE/2); root.field8.add(Long.MIN_VALUE/2); root.field8.add(Long.MAX_VALUE); root.field8.add(Long.MIN_VALUE); root.field9 = new NSimplestClass(db); root.field9.field1 = true; } public void setRootClassProperties(RootClass root) throws IOException { root.set7("Test"); LongArrayList lArrayList = new LongArrayList(); lArrayList.add(0); lArrayList.add(1); lArrayList.add(2); lArrayList.add(Long.MAX_VALUE/2); lArrayList.add(Long.MIN_VALUE/2); lArrayList.add(Long.MAX_VALUE); lArrayList.add(Long.MIN_VALUE); root.set8(lArrayList); NSimplestClass simplestClass9 = new NSimplestClass(db); simplestClass9.field1 = true; root.set9(simplestClass9); } public void testRootClassValues(RootClass root) { testRootClassFields(root); testRootClassProperties(root); } public void testRootClassFields(RootClass root) { shouldGetFieldBoolean(root); shouldGetFieldByte(root); shouldGetFieldShort(root); shouldGetFieldCharacter(root); shouldGetFieldInteger(root); shouldGetFieldLong(root); shouldGetFieldObject(root); shouldGetFieldUID(root); shouldGetFieldDBObject(root); } public void testRootClassProperties(RootClass root) { shouldGetPropertyObject(root); shouldGetPropertyUID(root); shouldGetPropertyDBObject(root); } private void shouldGetFieldBoolean(RootClass root) { assertTrue(root.field1); } private void shouldGetFieldByte(RootClass root) { assertEquals(2, root.field2); } private void shouldGetFieldShort(RootClass root) { assertEquals(3, root.field3); } private void shouldGetFieldCharacter(RootClass root) { assertEquals(4, root.field4); } private void shouldGetFieldInteger(RootClass root) { assertEquals(5, root.field5); } private void shouldGetFieldLong(RootClass root) { assertEquals(6, root.field6); } private void shouldGetFieldObject(RootClass root) { shouldGetObject(root.field7); } private void shouldGetPropertyObject(RootClass root) { shouldGetObject(root.get7()); } private void shouldGetFieldDBObject(RootClass root) { assertTrue(root.field9.field1); } private void shouldGetPropertyDBObject(RootClass root) { assertTrue(root.get9().field1); } private void shouldGetObject(String val) { assertNotNull(val); assertEquals("Test", val); } private void shouldGetDBObject(NSimplestClass val) { assertNotNull(val); assertTrue(val.field1); } private void shouldGetFieldUID(RootClass root) { shouldGetUID(root.field8); } private void shouldGetPropertyUID(RootClass root) { shouldGetUID(root.get8()); } private void shouldGetUID(LongArrayList val) { assertNotNull(val); assertEquals(7, val.size()); assertEquals(0, val.getLong(0)); assertEquals(val.getLong(5), Long.MAX_VALUE); assertEquals(val.getLong(6), Long.MIN_VALUE); } public void onLoad(RunnableWithIO r) { this.r = r; } } public static class RootClass extends EnhancedObject { @DbPrimitiveField(id = 0, type = DbPrimitiveType.BOOLEAN) public boolean field1; @DbPrimitiveField(id = 1, type = DbPrimitiveType.BYTE) public byte field2; @DbPrimitiveField(id = 2, type = DbPrimitiveType.SHORT) public short field3; @DbPrimitiveField(id = 3, type = DbPrimitiveType.CHAR) public char field4; @DbPrimitiveField(id = 4, type = DbPrimitiveType.INTEGER) public int field5; @DbPrimitiveField(id = 5, type = DbPrimitiveType.LONG) public long field6; @DbField(id = 0, type = DbDataType.OBJECT) public String field7; @DbField(id = 1, type = DbDataType.REFERENCES_LIST) public LongArrayList field8; @DbField(id = 2, type = DbDataType.ENHANCED_OBJECT) public NSimplestClass field9; public RootClass() { } public RootClass(IDatabaseTools databaseTools) throws IOException { super(databaseTools); } @DbPropertyGetter(id = 0, type = DbDataType.OBJECT) public String get7() { return getProperty(); } @DbPropertyGetter(id = 1, type = DbDataType.REFERENCES_LIST) public LongArrayList get8() { return getProperty(); } @DbPropertyGetter(id = 2, type = DbDataType.ENHANCED_OBJECT) public NSimplestClass get9() { return getProperty(); } @DbPropertySetter(id = 0, type = DbDataType.OBJECT) public void set7(String val) { setProperty(val); } @DbPropertySetter(id = 1, type = DbDataType.REFERENCES_LIST) public void set8(LongArrayList val) { setProperty(val); } @DbPropertySetter(id = 2, type = DbDataType.ENHANCED_OBJECT) public void set9(NSimplestClass val) { setProperty(val); } public boolean test() { return true; } } }