|
1 | 1 | /* |
2 | | - * Copyright 2017 ObjectBox Ltd. All rights reserved. |
| 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
25 | 25 | import java.util.List; |
26 | 26 | import java.util.Map; |
27 | 27 |
|
| 28 | + |
28 | 29 | import static org.junit.Assert.assertArrayEquals; |
29 | 30 | import static org.junit.Assert.assertEquals; |
30 | 31 | import static org.junit.Assert.assertFalse; |
31 | 32 | import static org.junit.Assert.assertNotNull; |
32 | 33 | import static org.junit.Assert.assertNull; |
| 34 | +import static org.junit.Assert.assertThrows; |
33 | 35 | import static org.junit.Assert.assertTrue; |
34 | 36 |
|
35 | 37 | public class BoxTest extends AbstractObjectBoxTest { |
@@ -85,6 +87,27 @@ public void testPutAndGet() { |
85 | 87 | assertArrayEquals(new double[]{-valDouble, valDouble}, entity.getDoubleArray(), 0); |
86 | 88 | } |
87 | 89 |
|
| 90 | + // Note: There is a similar test using the Cursor API directly (which is deprecated) in CursorTest. |
| 91 | + @Test |
| 92 | + public void testPut_notAssignedId_fails() { |
| 93 | + TestEntity entity = new TestEntity(); |
| 94 | + // Set ID that was not assigned |
| 95 | + entity.setId(1); |
| 96 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> box.put(entity)); |
| 97 | + assertEquals(ex.getMessage(), "ID is higher or equal to internal ID sequence: 1 (vs. 1). Use ID 0 (zero) to insert new objects."); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testPut_assignedId_inserts() { |
| 102 | + long id = box.put(new TestEntity()); |
| 103 | + box.remove(id); |
| 104 | + // Put with previously assigned ID should insert |
| 105 | + TestEntity entity = new TestEntity(); |
| 106 | + entity.setId(id); |
| 107 | + box.put(entity); |
| 108 | + assertEquals(1L, box.count()); |
| 109 | + } |
| 110 | + |
88 | 111 | @Test |
89 | 112 | public void testPutAndGet_defaultOrNullValues() { |
90 | 113 | long id = box.put(new TestEntity()); |
|
0 commit comments