Skip to content

Commit b7b97f4

Browse files
QueryBuilder: unify operator pending error, assert message in tests.
1 parent d713fcd commit b7b97f4

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.objectbox.relation.RelationInfo;
2626

2727
import javax.annotation.Nullable;
28-
import java.io.Closeable;
2928
import java.util.ArrayList;
3029
import java.util.Comparator;
3130
import java.util.Date;
@@ -355,10 +354,7 @@ public QueryBuilder<T> orderDesc(Property<T> property) {
355354
public QueryBuilder<T> order(Property<T> property, int flags) {
356355
verifyNotSubQuery();
357356
verifyHandle();
358-
if (combineNextWith != Operator.NONE) {
359-
throw new IllegalStateException(
360-
"An operator is pending. Use operators like and() and or() only between two conditions.");
361-
}
357+
checkNoOperatorPending();
362358
nativeOrder(handle, property.getId(), flags);
363359
return this;
364360
}
@@ -536,10 +532,15 @@ private void combineOperator(Operator operator) {
536532
if (lastCondition == 0) {
537533
throw new IllegalStateException("No previous condition. Use operators like and() and or() only between two conditions.");
538534
}
535+
checkNoOperatorPending();
536+
combineNextWith = operator;
537+
}
538+
539+
private void checkNoOperatorPending() {
539540
if (combineNextWith != Operator.NONE) {
540-
throw new IllegalStateException("Another operator is pending. Use operators like and() and or() only between two conditions.");
541+
throw new IllegalStateException(
542+
"Another operator is pending. Use operators like and() and or() only between two conditions.");
541543
}
542-
combineNextWith = operator;
543544
}
544545

545546
private void checkCombineCondition(long currentCondition) {

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,27 @@ public void testOr() {
867867
assertEquals(2007, entities.get(1).getSimpleInt());
868868
}
869869

870-
@Test(expected = IllegalStateException.class)
870+
@Test
871871
public void testOr_bad1() {
872-
box.query().or();
872+
assertNoPreviousCondition(() -> box.query().or());
873+
}
874+
875+
private void assertNoPreviousCondition(ThrowingRunnable runnable) {
876+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
877+
assertEquals("No previous condition. Use operators like and() and or() only between two conditions.",
878+
ex.getMessage());
873879
}
874880

875-
@Test(expected = IllegalStateException.class)
881+
@SuppressWarnings("resource") // Throws RuntimeException, so not closing Builder/Query is fine.
882+
@Test
876883
public void testOr_bad2() {
877-
box.query().equal(simpleInt, 1).or().build();
884+
assertIncompleteLogicCondition(() -> box.query().equal(simpleInt, 1).or().build());
885+
}
886+
887+
private void assertIncompleteLogicCondition(ThrowingRunnable runnable) {
888+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
889+
assertEquals("Incomplete logic condition. Use or()/and() between two conditions only.",
890+
ex.getMessage());
878891
}
879892

880893
@Test
@@ -887,24 +900,42 @@ public void testAnd() {
887900
assertEquals(2008, entities.get(0).getSimpleInt());
888901
}
889902

890-
@Test(expected = IllegalStateException.class)
903+
@Test
891904
public void testAnd_bad1() {
892-
box.query().and();
905+
assertNoPreviousCondition(() -> box.query().and());
893906
}
894907

895-
@Test(expected = IllegalStateException.class)
908+
@SuppressWarnings("resource") // Throws RuntimeException, so not closing Builder/Query is fine.
909+
@Test
896910
public void testAnd_bad2() {
897-
box.query().equal(simpleInt, 1).and().build();
911+
assertIncompleteLogicCondition(() -> box.query().equal(simpleInt, 1).and().build());
898912
}
899913

900-
@Test(expected = IllegalStateException.class)
914+
@SuppressWarnings("resource") // Throws RuntimeException, so not closing Builder/Query is fine.
915+
@Test
901916
public void testOrAfterAnd() {
902-
box.query().equal(simpleInt, 1).and().or().equal(simpleInt, 2).build();
917+
assertOperatorIsPending(() -> box.query()
918+
.equal(simpleInt, 1)
919+
.and()
920+
.or()
921+
.equal(simpleInt, 2)
922+
.build());
903923
}
904924

905-
@Test(expected = IllegalStateException.class)
925+
@SuppressWarnings("resource") // Throws RuntimeException, so not closing Builder/Query is fine.
926+
@Test
906927
public void testOrderAfterAnd() {
907-
box.query().equal(simpleInt, 1).and().order(simpleInt).equal(simpleInt, 2).build();
928+
assertOperatorIsPending(() -> box.query()
929+
.equal(simpleInt, 1)
930+
.and().order(simpleInt)
931+
.equal(simpleInt, 2)
932+
.build());
933+
}
934+
935+
private void assertOperatorIsPending(ThrowingRunnable runnable) {
936+
IllegalStateException ex = assertThrows(IllegalStateException.class, runnable);
937+
assertEquals("Another operator is pending. Use operators like and() and or() only between two conditions.",
938+
ex.getMessage());
908939
}
909940

910941
@Test

0 commit comments

Comments
 (0)