package org.warp.jcwdb.utils; import it.unimi.dsi.fastutil.longs.LongArrayList; import org.warp.jcwdb.ann.*; 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 java.util.function.Consumer; import static org.junit.Assert.*; public class TestUtils { public static WrappedDb wrapDb() { return new WrappedDb(); } public static class WrappedDb { private JCWDatabase 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 JCWDatabase openDatabase() throws IOException { return new JCWDatabase(tempDir.resolve(Paths.get("data.db")), tempDir.resolve(Paths.get("indices.idx")), true); } public void delete() throws IOException { db.close(); deleteDir(tempDir); } public JCWDatabase 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 SimplestClass(db); root.field9.field1 = true; } public void setRootClassProperties(RootClass root) throws IOException { root.set1(true); root.set2((byte)2); root.set3((short)3); root.set4((char)4); root.set5(5); root.set6(6); 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); SimplestClass simplestClass9 = new SimplestClass(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) { shouldGetPropertyBoolean(root); shouldGetPropertyByte(root); shouldGetPropertyShort(root); shouldGetPropertyCharacter(root); shouldGetPropertyInteger(root); shouldGetPropertyLong(root); shouldGetPropertyObject(root); shouldGetPropertyUID(root); shouldGetPropertyDBObject(root); } private void shouldGetFieldBoolean(RootClass root) { assertTrue(root.field1); } private void shouldGetPropertyBoolean(RootClass root) { assertTrue(root.get1()); } private void shouldGetFieldByte(RootClass root) { assertEquals(2, root.field2); } private void shouldGetPropertyByte(RootClass root) { assertEquals(2, root.get2()); } private void shouldGetFieldShort(RootClass root) { assertEquals(3, root.field3); } private void shouldGetPropertyShort(RootClass root) { assertEquals(3, root.get3()); } private void shouldGetFieldCharacter(RootClass root) { assertEquals(4, root.field4); } private void shouldGetPropertyCharacter(RootClass root) { assertEquals(4, root.get4()); } private void shouldGetFieldInteger(RootClass root) { assertEquals(5, root.field5); } private void shouldGetPropertyInteger(RootClass root) { assertEquals(5, root.get5()); } private void shouldGetFieldLong(RootClass root) { assertEquals(6, root.field6); } private void shouldGetPropertyLong(RootClass root) { assertEquals(6, root.get6()); } 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(SimplestClass 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 DBObject { @DBField(id = 0, type = DBDataType.BOOLEAN) public boolean field1; @DBField(id = 1, type = DBDataType.BYTE) public byte field2; @DBField(id = 2, type = DBDataType.SHORT) public short field3; @DBField(id = 3, type = DBDataType.CHAR) public char field4; @DBField(id = 4, type = DBDataType.INTEGER) public int field5; @DBField(id = 5, type = DBDataType.LONG) public long field6; @DBField(id = 6, type = DBDataType.OBJECT) public String field7; @DBField(id = 7, type = DBDataType.UID_LIST) public LongArrayList field8; @DBField(id = 8, type = DBDataType.DATABASE_OBJECT) public SimplestClass field9; public RootClass() { } public RootClass(JCWDatabase database) throws IOException { super(database); } @DBPropertyGetter(id = 0, type = DBDataType.BOOLEAN) public boolean get1() { return getProperty(); } @DBPropertyGetter(id = 1, type = DBDataType.BYTE) public byte get2() { return getProperty(); } @DBPropertyGetter(id = 2, type = DBDataType.SHORT) public short get3() { return getProperty(); } @DBPropertyGetter(id = 3, type = DBDataType.CHAR) public char get4() { return getProperty(); } @DBPropertyGetter(id = 4, type = DBDataType.INTEGER) public int get5() { return getProperty(); } @DBPropertyGetter(id = 5, type = DBDataType.LONG) public long get6() { return getProperty(); } @DBPropertyGetter(id = 6, type = DBDataType.OBJECT) public String get7() { return getProperty(); } @DBPropertyGetter(id = 7, type = DBDataType.UID_LIST) public LongArrayList get8() { return getProperty(); } @DBPropertyGetter(id = 8, type = DBDataType.DATABASE_OBJECT) public SimplestClass get9() { return getProperty(); } @DBPropertySetter(id = 0, type = DBDataType.BOOLEAN) public void set1(boolean val) { setProperty(val); } @DBPropertySetter(id = 1, type = DBDataType.BYTE) public void set2(byte val) { setProperty(val); } @DBPropertySetter(id = 2, type = DBDataType.SHORT) public void set3(short val) { setProperty(val); } @DBPropertySetter(id = 3, type = DBDataType.CHAR) public void set4(char val) { setProperty(val); } @DBPropertySetter(id = 4, type = DBDataType.INTEGER) public void set5(int val) { setProperty(val); } @DBPropertySetter(id = 5, type = DBDataType.LONG) public void set6(long val) { setProperty(val); } @DBPropertySetter(id = 6, type = DBDataType.OBJECT) public void set7(String val) { setProperty(val); } @DBPropertySetter(id = 7, type = DBDataType.UID_LIST) public void set8(LongArrayList val) { setProperty(val); } @DBPropertySetter(id = 8, type = DBDataType.DATABASE_OBJECT) public void set9(SimplestClass val) { setProperty(val); } public boolean test() { return true; } @Override public void initialize() throws IOException { } } }