package org.warp.jcwdb.tests; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.warp.jcwdb.ann.*; import org.warp.jcwdb.utils.TestUtils; import java.io.IOException; import static org.junit.Assert.*; public class DBDBObjectListTests { private TestUtils.WrappedDb db; private RootWithList root; @Before public void setUp() throws Exception { db = TestUtils.wrapDb().create((db) -> { db.get().registerClass(TestUtils.class, 0); db.get().registerClass(TestUtils.RootClass.class, 1); db.get().registerClass(Class.class, 2); System.out.println("Loading root"); root = db.get().loadRoot(RootWithList.class); }); root.list = new DBDBObjectList<>(db.get(), TestUtils.RootClass.class); for (int i = 0; i < 100; i++) { TestUtils.RootClass rootClass = new TestUtils.RootClass(db.get()); db.setRootClassValues(rootClass); root.list.add(rootClass); } db.closeAndReopen(); for (int i = 0; i < 100; i++) { TestUtils.RootClass rootClass = new TestUtils.RootClass(db.get()); db.setRootClassValues(rootClass); root.list.add(rootClass); } } @Test public void shouldMatchList() throws IOException { checkEmptyList(); assertEquals(200, root.list.size()); for (int i = 0; i < 200; i++) { db.testRootClassValues(root.list.get(i)); } } private void checkEmptyList() throws IOException { DBObjectList list = new DBObjectList<>(db.get()); assertEquals(null, list.getLast()); assertEquals(0, list.size()); assertTrue(list.isEmpty()); list.add("1"); assertEquals(1, list.size()); assertEquals("1", list.getLast()); assertFalse(list.isEmpty()); } @After public void tearDown() throws Exception { db.delete(); } public static class RootWithList extends DBObject { @DBField(id = 0, type = DBDataType.DATABASE_OBJECT) public DBDBObjectList list; public RootWithList() { super(); } public RootWithList(JCWDatabase database) throws IOException { super(database); } @Override public void initialize() throws IOException { list = new DBDBObjectList<>(database, TestUtils.RootClass.class); } } }