2 * Sixth - System for data storage, computation, exploration and interaction.
3 * Author: Svjatoslav Agejenko.
4 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.datastore;
10 import eu.svjatoslav.sixth.data.store.DataStore;
11 import eu.svjatoslav.sixth.data.store.file.FileDataStore;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
17 import java.io.IOException;
18 import java.util.ArrayList;
20 public class DataStoreImplTest {
22 public static final String UTF_8 = "UTF-8";
28 public void setUp() throws Exception {
29 dataSourceFile = new File("test.byar");
30 if (dataSourceFile.exists())
31 if (!dataSourceFile.delete())
32 throw new RuntimeException("Cannot delete file: " + dataSourceFile.getCanonicalPath());
34 dataStore = new FileDataStore(dataSourceFile);
38 public void testBasicStorageAndRetrieval() throws IOException {
40 final int record1 = dataStore.createRecord("See on test".getBytes(UTF_8));
41 final int record2 = dataStore.createRecord("another test".getBytes(UTF_8));
43 Assert.assertEquals(0, record1);
44 Assert.assertEquals(1, record2);
46 Assert.assertEquals("another test", new String(dataStore.readRecord(record2),
48 Assert.assertEquals("See on test", new String(dataStore.readRecord(record1),
51 dataStore.updateRecord(record1, "!!!".getBytes(UTF_8));
53 Assert.assertEquals("!!!", new String(dataStore.readRecord(record1), UTF_8));
55 dataStore.updateRecord(record1, dataStore.readRecord(record2));
57 Assert.assertEquals("another test", new String(dataStore.readRecord(record1),
64 public void testDefragmentation() throws IOException {
65 final int record1 = dataStore.createRecord("1111".getBytes(UTF_8));
66 final int record2 = dataStore.createRecord("22".getBytes(UTF_8));
67 final int record3 = dataStore.createRecord("3333333".getBytes(UTF_8));
68 final int record4 = dataStore.createRecord("4".getBytes(UTF_8));
70 ((FileDataStore) dataStore).vacuum(0);
72 final int record5 = dataStore.createRecord("555".getBytes());
74 dataStore.deleteRecord(record1);
76 ((FileDataStore) dataStore).vacuum(0);
78 final int record6 = dataStore.createRecord("66".getBytes());
84 public void testEntriesTableIncreasing() throws IOException {
85 // dataStore.createRecord(String.valueOf("lalalala").getBytes());
87 for (int j = 0; j < 10; j++) {
88 final ArrayList<Integer> ids = new ArrayList<>();
90 for (int i = 0; i < 100; i++) {
91 final int id = dataStore.createRecord(String.valueOf(
92 "____________" + i).getBytes());
97 for (final int id : ids)
98 dataStore.deleteRecord(id);
100 ((FileDataStore) dataStore).vacuum(0);