Skip to content

Commit 19821f9

Browse files
QueryBuilder: fix assert when closed, throw on and/or (#818)
1 parent 1b6abc3 commit 19821f9

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ public QueryBuilder<T> and() {
532532
}
533533

534534
private void combineOperator(Operator operator) {
535+
verifyHandle(); // Not using handle, but throw for consistency with other methods.
535536
if (lastCondition == 0) {
536537
throw new IllegalStateException("No previous condition. Use operators like and() and or() only between two conditions.");
537538
}

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

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,35 @@ public void testBuild() {
6565
assertNotNull(query);
6666
}
6767

68-
@Test(expected = IllegalStateException.class)
69-
public void testBuildTwice() {
70-
QueryBuilder<TestEntity> queryBuilder = box.query();
71-
for (int i = 0; i < 2; i++) {
72-
// calling any builder method after build should fail
73-
// note: not calling all variants for different types
74-
queryBuilder.isNull(TestEntity_.simpleString);
75-
queryBuilder.and();
76-
queryBuilder.notNull(TestEntity_.simpleString);
77-
queryBuilder.or();
78-
queryBuilder.equal(TestEntity_.simpleBoolean, true);
79-
queryBuilder.notEqual(TestEntity_.simpleBoolean, true);
80-
queryBuilder.less(TestEntity_.simpleInt, 42);
81-
queryBuilder.greater(TestEntity_.simpleInt, 42);
82-
queryBuilder.between(TestEntity_.simpleInt, 42, 43);
83-
queryBuilder.in(TestEntity_.simpleInt, new int[]{42});
84-
queryBuilder.notIn(TestEntity_.simpleInt, new int[]{42});
85-
queryBuilder.contains(TestEntity_.simpleString, "42", StringOrder.CASE_INSENSITIVE);
86-
queryBuilder.startsWith(TestEntity_.simpleString, "42", StringOrder.CASE_SENSITIVE);
87-
queryBuilder.order(TestEntity_.simpleInt);
88-
queryBuilder.build().find();
89-
}
68+
@Test
69+
public void useAfterBuild_fails() {
70+
QueryBuilder<TestEntity> builder = box.query();
71+
Query<TestEntity> query = builder.build();
72+
73+
// Calling any builder method after build should fail.
74+
// note: not calling all variants for different types.
75+
assertThrowsBuilderClosed(() -> builder.isNull(TestEntity_.simpleString));
76+
assertThrowsBuilderClosed(builder::and);
77+
assertThrowsBuilderClosed(() -> builder.notNull(TestEntity_.simpleString));
78+
assertThrowsBuilderClosed(builder::or);
79+
assertThrowsBuilderClosed(() -> builder.equal(TestEntity_.simpleBoolean, true));
80+
assertThrowsBuilderClosed(() -> builder.notEqual(TestEntity_.simpleBoolean, true));
81+
assertThrowsBuilderClosed(() -> builder.less(TestEntity_.simpleInt, 42));
82+
assertThrowsBuilderClosed(() -> builder.greater(TestEntity_.simpleInt, 42));
83+
assertThrowsBuilderClosed(() -> builder.between(TestEntity_.simpleInt, 42, 43));
84+
assertThrowsBuilderClosed(() -> builder.in(TestEntity_.simpleInt, new int[]{42}));
85+
assertThrowsBuilderClosed(() -> builder.notIn(TestEntity_.simpleInt, new int[]{42}));
86+
assertThrowsBuilderClosed(() -> builder.contains(TestEntity_.simpleString, "42", StringOrder.CASE_INSENSITIVE));
87+
assertThrowsBuilderClosed(() -> builder.startsWith(TestEntity_.simpleString, "42", StringOrder.CASE_SENSITIVE));
88+
assertThrowsBuilderClosed(() -> builder.order(TestEntity_.simpleInt));
89+
assertThrowsBuilderClosed(builder::build);
90+
91+
query.close();
92+
}
93+
94+
private void assertThrowsBuilderClosed(ThrowingRunnable runnable) {
95+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
96+
assertEquals("This QueryBuilder has already been closed. Please use a new instance.", ex.getMessage());
9097
}
9198

9299
@Test

0 commit comments

Comments
 (0)