strangedb/src/test/java/it/cavallium/strangedb/tests/ObjectListTests.java

140 lines
3.7 KiB
Java

package it.cavallium.strangedb.tests;
import it.cavallium.strangedb.java.annotations.DbDataType;
import it.cavallium.strangedb.java.annotations.DbField;
import it.cavallium.strangedb.java.database.DatabaseJava;
import it.cavallium.strangedb.java.database.IDatabaseTools;
import it.cavallium.strangedb.java.objects.EnhancedObject;
import it.cavallium.strangedb.java.objects.lists.EnhancedObjectStrangeDbList;
import it.cavallium.strangedb.java.objects.lists.ObjectStrangeDbList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertEquals;
public class ObjectListTests {
private Path path1;
private Path path2;
private Path path3;
private DatabaseJava db;
@Before
public void setUp() throws Exception {
path1 = Files.createTempFile("db-tests-data-", ".db");
path2 = Files.createTempFile("db-tests-blocks-", ".db");
path3 = Files.createTempFile("db-tests-references-", ".db");
db = new DatabaseJava(path1, path2, path3);
ListsRoot root = db.loadRoot(ListsRoot::new);
for (int i = 0; i < 5000; i++) {
root.objectList.add(new ObjectItem(i));
root.enhancedObjectList.add(new EnhancedObjectItem(db, i));
}
for (int i = 0; i < 5000; i++) {
if (i % 10 == 0) {
root.objectList.update(i, new ObjectItem(i));
root.enhancedObjectList.update(i, new EnhancedObjectItem(db, i));
}
}
for (int i = 0; i < 5000; i++) {
if (i % 11 == 0) {
root.objectList.set(i, new ObjectItem(i));
root.enhancedObjectList.set(i, new EnhancedObjectItem(db, i));
}
}
for (int i = 5000; i < 6000; i++) {
root.objectList.add(new ObjectItem(0));
root.enhancedObjectList.add(i, new EnhancedObjectItem(db, 0));
}
db.close();
}
@Test
public void shouldUpdateClass() throws IOException {
db = new DatabaseJava(path1, path2, path3);
ListsRoot root = db.loadRoot(ListsRoot::new);
new Thread(() -> {
try {
for (int i = 0; i < 5000; i++) {
String val = root.objectList.get(i).value;
assertEquals(val, "test_" + i);
val = root.enhancedObjectList.get(i).value;
assertEquals(val, "test_" + i);
}
for (int i = 5000; i < 6000; i++) {
assertEquals(root.objectList.get(i).value, "test_" + 0);
assertEquals(root.enhancedObjectList.get(i).value, "test_" + 0);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
db.close();
}
@After
public void tearDown() throws Exception {
Files.deleteIfExists(path1);
Files.deleteIfExists(path2);
Files.deleteIfExists(path3);
}
public static class ListsRoot extends EnhancedObject {
@DbField(id = 0,name = "objectList", type = DbDataType.ENHANCED_OBJECT)
public ObjectStrangeDbList<ObjectItem> objectList;
@DbField(id = 1,name = "enhancedObjectList", type = DbDataType.ENHANCED_OBJECT)
public EnhancedObjectStrangeDbList<EnhancedObjectItem> enhancedObjectList;
@Deprecated
public ListsRoot() {
}
public ListsRoot(IDatabaseTools tools) throws IOException {
super(tools);
objectList = new ObjectStrangeDbList<>(tools);
enhancedObjectList = new EnhancedObjectStrangeDbList<>(tools, EnhancedObjectItem.class);
}
}
public static class ObjectItem {
private String value;
public ObjectItem() {
}
private ObjectItem(int i) {
this.value = "test_" + i;
}
}
public static class EnhancedObjectItem extends EnhancedObject {
@DbField(id = 0, type = DbDataType.OBJECT, name = "value")
private String value;
@Deprecated
public EnhancedObjectItem() {
}
public EnhancedObjectItem(IDatabaseTools tools, int i) throws IOException {
super(tools);
this.value = "test_" + i;
}
}
}