Skip to content

Commit 1b6abc3

Browse files
Query: test usage when store is closed (#818)
1 parent c88c132 commit 1b6abc3

File tree

1 file changed

+51
-0
lines changed
  • tests/objectbox-java-test/src/test/java/io/objectbox/query

1 file changed

+51
-0
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Arrays;
3737
import java.util.Date;
3838
import java.util.List;
39+
import java.util.concurrent.RejectedExecutionException;
3940

4041
import static io.objectbox.TestEntity_.simpleBoolean;
4142
import static io.objectbox.TestEntity_.simpleByteArray;
@@ -45,6 +46,7 @@
4546
import static io.objectbox.TestEntity_.simpleShort;
4647
import static io.objectbox.TestEntity_.simpleString;
4748
import static io.objectbox.TestEntity_.simpleStringArray;
49+
import static io.objectbox.TestEntity_.stringObjectMap;
4850
import static org.junit.Assert.assertArrayEquals;
4951
import static org.junit.Assert.assertEquals;
5052
import static org.junit.Assert.assertFalse;
@@ -137,6 +139,55 @@ private void assertThrowsQueryIsClosed(ThrowingRunnable runnable) {
137139
assertEquals("This query is closed. Build and use a new one.", ex.getMessage());
138140
}
139141

142+
@Test
143+
public void useAfterStoreClose_failsIfUsingStore() {
144+
Query<TestEntity> query = box.query(
145+
simpleString.equal("")
146+
.and(stringObjectMap.containsKeyValue("", ""))
147+
.and(simpleInt.equal(0))
148+
.and(simpleInt.oneOf(new int[]{0}).alias("oneOf4"))
149+
.and(simpleLong.oneOf(new long[]{0}).alias("oneOf8"))
150+
.and(simpleInt.between(0, 0).alias("between"))
151+
.and(simpleString.oneOf(new String[]{""}).alias("oneOfS"))
152+
.and(simpleByteArray.equal(new byte[]{0}))
153+
).build();
154+
store.close();
155+
156+
assertThrowsStoreIsClosed(query::count);
157+
assertThrowsStoreIsClosed(query::find);
158+
assertThrowsStoreIsClosed(() -> query.find(0, 1));
159+
assertThrowsStoreIsClosed(query::findFirst);
160+
assertThrowsStoreIsClosed(query::findIds);
161+
assertThrowsStoreIsClosed(() -> query.findIds(0, 1));
162+
assertThrowsStoreIsClosed(query::findLazy);
163+
assertThrowsStoreIsClosed(query::findLazyCached);
164+
assertThrowsStoreIsClosed(query::findUnique);
165+
assertThrowsStoreIsClosed(query::remove);
166+
167+
// describe and setParameter continue to work as store is not accessed.
168+
assertFalse(query.describe().isEmpty());
169+
assertFalse(query.describeParameters().isEmpty());
170+
query.setParameter(simpleString, "value");
171+
query.setParameters(stringObjectMap, "a", "b");
172+
query.setParameter(simpleInt, 1);
173+
query.setParameters("oneOf4", new int[]{1, 2});
174+
query.setParameters("oneOf8", new long[]{1, 2});
175+
query.setParameters("between", 1, 2);
176+
query.setParameter(simpleInt, 1.0);
177+
query.setParameters("between", 1.0, 2.0);
178+
query.setParameters("oneOfS", new String[]{"a", "b"});
179+
query.setParameter(simpleByteArray, new byte[]{1, 2});
180+
181+
// Internal thread pool is shut down as part of closing store, should no longer accept new work.
182+
assertThrows(RejectedExecutionException.class, () -> query.subscribe().observer(data -> {
183+
}));
184+
}
185+
186+
private void assertThrowsStoreIsClosed(ThrowingRunnable runnable) {
187+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
188+
assertEquals("Store is closed", ex.getMessage());
189+
}
190+
140191
@Test
141192
public void testNullNotNull() {
142193
List<TestEntity> scalars = putTestEntitiesScalars();

0 commit comments

Comments
 (0)