fixed maven repository URL
[sixth-data.git] / src / test / java / eu / svjatoslav / sixth / datastore / DataStoreImplTest.java
1 /*
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.
5  * 
6 */
7
8 package eu.svjatoslav.sixth.datastore;
9
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;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.ArrayList;
19
20 public class DataStoreImplTest {
21
22     public static final String UTF_8 = "UTF-8";
23     File dataSourceFile;
24
25     DataStore dataStore;
26
27     @Before
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());
33
34         dataStore = new FileDataStore(dataSourceFile);
35     }
36
37     @Test
38     public void testBasicStorageAndRetrieval() throws IOException {
39
40         final int record1 = dataStore.createRecord("See on test".getBytes(UTF_8));
41         final int record2 = dataStore.createRecord("another test".getBytes(UTF_8));
42
43         Assert.assertEquals(0, record1);
44         Assert.assertEquals(1, record2);
45
46         Assert.assertEquals("another test", new String(dataStore.readRecord(record2),
47                 UTF_8));
48         Assert.assertEquals("See on test", new String(dataStore.readRecord(record1),
49                 UTF_8));
50
51         dataStore.updateRecord(record1, "!!!".getBytes(UTF_8));
52
53         Assert.assertEquals("!!!", new String(dataStore.readRecord(record1), UTF_8));
54
55         dataStore.updateRecord(record1, dataStore.readRecord(record2));
56
57         Assert.assertEquals("another test", new String(dataStore.readRecord(record1),
58                 UTF_8));
59
60         dataStore.close();
61     }
62
63     @Test
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));
69
70         ((FileDataStore) dataStore).vacuum(0);
71
72         final int record5 = dataStore.createRecord("555".getBytes());
73
74         dataStore.deleteRecord(record1);
75
76         ((FileDataStore) dataStore).vacuum(0);
77
78         final int record6 = dataStore.createRecord("66".getBytes());
79
80         dataStore.close();
81     }
82
83     @Test
84     public void testEntriesTableIncreasing() throws IOException {
85         // dataStore.createRecord(String.valueOf("lalalala").getBytes());
86
87         for (int j = 0; j < 10; j++) {
88             final ArrayList<Integer> ids = new ArrayList<>();
89
90             for (int i = 0; i < 100; i++) {
91                 final int id = dataStore.createRecord(String.valueOf(
92                         "____________" + i).getBytes());
93
94                 ids.add(id);
95             }
96
97             for (final int id : ids)
98                 dataStore.deleteRecord(id);
99
100             ((FileDataStore) dataStore).vacuum(0);
101         }
102     }
103
104 }