|
16 | 16 |
|
17 | 17 | package io.objectbox; |
18 | 18 |
|
| 19 | +import io.objectbox.exception.DbFullException; |
| 20 | +import io.objectbox.exception.DbMaxDataSizeExceededException; |
19 | 21 | import io.objectbox.exception.PagesCorruptException; |
20 | 22 | import io.objectbox.model.ValidateOnOpenMode; |
21 | 23 | import org.greenrobot.essentials.io.IoUtils; |
|
28 | 30 | import java.io.InputStream; |
29 | 31 | import java.nio.file.Files; |
30 | 32 | import java.nio.file.Path; |
31 | | -import java.util.Collections; |
32 | 33 | import java.util.HashSet; |
33 | 34 | import java.util.List; |
34 | 35 | import java.util.Set; |
|
38 | 39 | import static org.junit.Assert.assertEquals; |
39 | 40 | import static org.junit.Assert.assertNotNull; |
40 | 41 | import static org.junit.Assert.assertSame; |
| 42 | +import static org.junit.Assert.assertThrows; |
41 | 43 | import static org.junit.Assert.assertTrue; |
42 | 44 | import static org.junit.Assert.fail; |
43 | 45 |
|
44 | 46 | public class BoxStoreBuilderTest extends AbstractObjectBoxTest { |
45 | 47 |
|
46 | 48 | private BoxStoreBuilder builder; |
47 | 49 |
|
| 50 | + private static final String LONG_STRING = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; |
| 51 | + |
48 | 52 | @Override |
49 | 53 | protected BoxStore createBoxStore() { |
50 | 54 | // Standard setup of store not required |
@@ -167,6 +171,73 @@ public void readOnly() { |
167 | 171 | assertTrue(store.isReadOnly()); |
168 | 172 | } |
169 | 173 |
|
| 174 | + @Test |
| 175 | + public void maxSize_invalidValues_throw() { |
| 176 | + // Max data larger than max database size throws. |
| 177 | + builder.maxSizeInKByte(10); |
| 178 | + IllegalArgumentException exSmaller = assertThrows( |
| 179 | + IllegalArgumentException.class, |
| 180 | + () -> builder.maxDataSizeInKByte(11) |
| 181 | + ); |
| 182 | + assertEquals("maxDataSizeInKByte must be smaller than maxSizeInKByte.", exSmaller.getMessage()); |
| 183 | + |
| 184 | + // Max database size smaller than max data size throws. |
| 185 | + builder.maxDataSizeInKByte(9); |
| 186 | + IllegalArgumentException exLarger = assertThrows( |
| 187 | + IllegalArgumentException.class, |
| 188 | + () -> builder.maxSizeInKByte(8) |
| 189 | + ); |
| 190 | + assertEquals("maxSizeInKByte must be larger than maxDataSizeInKByte.", exLarger.getMessage()); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void maxFileSize() { |
| 195 | + builder = createBoxStoreBuilder(null); |
| 196 | + builder.maxSizeInKByte(30); // Empty file is around 12 KB, object below adds about 8 KB each. |
| 197 | + store = builder.build(); |
| 198 | + putTestEntity(LONG_STRING, 1); |
| 199 | + TestEntity testEntity2 = createTestEntity(LONG_STRING, 2); |
| 200 | + DbFullException dbFullException = assertThrows( |
| 201 | + DbFullException.class, |
| 202 | + () -> getTestEntityBox().put(testEntity2) |
| 203 | + ); |
| 204 | + assertEquals("Could not commit tx", dbFullException.getMessage()); |
| 205 | + |
| 206 | + // Re-open with larger size. |
| 207 | + store.close(); |
| 208 | + builder.maxSizeInKByte(40); |
| 209 | + store = builder.build(); |
| 210 | + testEntity2.setId(0); // Clear ID of object that failed to put. |
| 211 | + getTestEntityBox().put(testEntity2); |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void maxDataSize() { |
| 216 | + // Put until max data size is reached, but still below max database size. |
| 217 | + builder = createBoxStoreBuilder(null); |
| 218 | + builder.maxSizeInKByte(50); // Empty file is around 12 KB, each put adds about 8 KB. |
| 219 | + builder.maxDataSizeInKByte(1); |
| 220 | + store = builder.build(); |
| 221 | + |
| 222 | + TestEntity testEntity1 = putTestEntity(LONG_STRING, 1); |
| 223 | + TestEntity testEntity2 = createTestEntity(LONG_STRING, 2); |
| 224 | + DbMaxDataSizeExceededException maxDataExc = assertThrows( |
| 225 | + DbMaxDataSizeExceededException.class, |
| 226 | + () -> getTestEntityBox().put(testEntity2) |
| 227 | + ); |
| 228 | + assertEquals("Exceeded user-set maximum by [bytes]: 64", maxDataExc.getMessage()); |
| 229 | + |
| 230 | + // Remove to get below max data size, then put again. |
| 231 | + getTestEntityBox().remove(testEntity1); |
| 232 | + getTestEntityBox().put(testEntity2); |
| 233 | + |
| 234 | + // Alternatively, re-open with larger max data size. |
| 235 | + store.close(); |
| 236 | + builder.maxDataSizeInKByte(2); |
| 237 | + store = builder.build(); |
| 238 | + putTestEntity(LONG_STRING, 3); |
| 239 | + } |
| 240 | + |
170 | 241 | @Test |
171 | 242 | public void validateOnOpen() { |
172 | 243 | // Create a database first; we must create the model only once (ID/UID sequences would be different 2nd time) |
|
0 commit comments