Skip to content

Commit 2b187ff

Browse files
Tests: test put with (not) assigned IDs and Box API
1 parent d3b2fad commit 2b187ff

File tree

1 file changed

+24
-1
lines changed
  • tests/objectbox-java-test/src/test/java/io/objectbox

1 file changed

+24
-1
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/BoxTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,11 +25,13 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28+
2829
import static org.junit.Assert.assertArrayEquals;
2930
import static org.junit.Assert.assertEquals;
3031
import static org.junit.Assert.assertFalse;
3132
import static org.junit.Assert.assertNotNull;
3233
import static org.junit.Assert.assertNull;
34+
import static org.junit.Assert.assertThrows;
3335
import static org.junit.Assert.assertTrue;
3436

3537
public class BoxTest extends AbstractObjectBoxTest {
@@ -85,6 +87,27 @@ public void testPutAndGet() {
8587
assertArrayEquals(new double[]{-valDouble, valDouble}, entity.getDoubleArray(), 0);
8688
}
8789

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+
88111
@Test
89112
public void testPutAndGet_defaultOrNullValues() {
90113
long id = box.put(new TestEntity());

0 commit comments

Comments
 (0)