Skip to content

Commit 88e3122

Browse files
Merge branch '169-test-date-property' into 'dev'
Resolve "Test Date property" #169 See merge request objectbox/objectbox-java!139
2 parents 19193db + c95901c commit 88e3122

File tree

11 files changed

+144
-32
lines changed

11 files changed

+144
-32
lines changed

objectbox-java/src/main/java/io/objectbox/Property.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,16 @@ public PropertyQueryCondition<ENTITY> lessOrEqual(Date value) {
354354
return new LongCondition<>(this, LongCondition.Operation.LESS_OR_EQUAL, value);
355355
}
356356

357+
/** Creates an "IN (..., ..., ...)" condition for this property. */
358+
public PropertyQueryCondition<ENTITY> oneOf(Date[] value) {
359+
return new LongArrayCondition<>(this, LongArrayCondition.Operation.IN, value);
360+
}
361+
362+
/** Creates a "NOT IN (..., ..., ...)" condition for this property. */
363+
public PropertyQueryCondition<ENTITY> notOneOf(Date[] value) {
364+
return new LongArrayCondition<>(this, LongArrayCondition.Operation.NOT_IN, value);
365+
}
366+
357367
/**
358368
* Creates a "BETWEEN ... AND ..." condition for this property.
359369
* Finds objects with property value between and including the first and second value.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ public LongArrayCondition(Property<T> property, Operation op, long[] value) {
212212
this.value = value;
213213
}
214214

215+
public LongArrayCondition(Property<T> property, Operation op, Date[] value) {
216+
super(property);
217+
this.op = op;
218+
this.value = new long[value.length];
219+
for (int i = 0; i < value.length; i++) {
220+
this.value[i] = value[i].getTime();
221+
}
222+
}
223+
215224
@Override
216225
void applyCondition(QueryBuilder<T> builder) {
217226
switch (op) {

tests/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Tests for `objectbox-java`
2+
3+
## Naming convention for tests
4+
5+
All new tests which will be added to the `tests/objectbox-java-test` module must have the names of their methods in the
6+
following format: `{attribute}_{queryCondition}_{expectation}`
7+
8+
For ex. `date_lessAndGreater_works`
9+
10+
Note: due to historic reasons (JUnit 3) existing test methods may be named differently (with the `test` prefix).

tests/objectbox-java-test/src/main/java/io/objectbox/TestEntity.java

Lines changed: 17 additions & 3 deletions
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.
@@ -16,11 +16,13 @@
1616

1717
package io.objectbox;
1818

19-
import javax.annotation.Nullable;
2019
import java.util.Arrays;
20+
import java.util.Date;
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import javax.annotation.Nullable;
25+
2426
/** In "real" entity would be annotated with @Entity. */
2527
public class TestEntity {
2628

@@ -59,6 +61,7 @@ public class TestEntity {
5961
private long[] longArray;
6062
private float[] floatArray;
6163
private double[] doubleArray;
64+
private Date date;
6265

6366
transient boolean noArgsConstructorCalled;
6467

@@ -92,7 +95,8 @@ public TestEntity(long id,
9295
int[] intArray,
9396
long[] longArray,
9497
float[] floatArray,
95-
double[] doubleArray
98+
double[] doubleArray,
99+
Date date
96100
) {
97101
this.id = id;
98102
this.simpleBoolean = simpleBoolean;
@@ -117,6 +121,7 @@ public TestEntity(long id,
117121
this.longArray = longArray;
118122
this.floatArray = floatArray;
119123
this.doubleArray = doubleArray;
124+
this.date = date;
120125
if (STRING_VALUE_THROW_IN_CONSTRUCTOR.equals(simpleString)) {
121126
throw new RuntimeException(EXCEPTION_IN_CONSTRUCTOR_MESSAGE);
122127
}
@@ -324,6 +329,14 @@ public void setDoubleArray(@Nullable double[] doubleArray) {
324329
this.doubleArray = doubleArray;
325330
}
326331

332+
public Date getDate() {
333+
return date;
334+
}
335+
336+
public void setDate(Date date) {
337+
this.date = date;
338+
}
339+
327340
@Override
328341
public String toString() {
329342
return "TestEntity{" +
@@ -350,6 +363,7 @@ public String toString() {
350363
", longArray=" + Arrays.toString(longArray) +
351364
", floatArray=" + Arrays.toString(floatArray) +
352365
", doubleArray=" + Arrays.toString(doubleArray) +
366+
", date=" + date +
353367
", noArgsConstructorCalled=" + noArgsConstructorCalled +
354368
'}';
355369
}

tests/objectbox-java-test/src/main/java/io/objectbox/TestEntityCursor.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public Cursor<TestEntity> createCursor(io.objectbox.Transaction tx, long cursorH
7070
private final static int __ID_longArray = TestEntity_.longArray.id;
7171
private final static int __ID_floatArray = TestEntity_.floatArray.id;
7272
private final static int __ID_doubleArray = TestEntity_.doubleArray.id;
73+
private final static int __ID_date = TestEntity_.date.id;
7374

7475
public TestEntityCursor(io.objectbox.Transaction tx, long cursor, BoxStore boxStore) {
7576
super(tx, cursor, TestEntity_.__INSTANCE, boxStore);
@@ -150,17 +151,20 @@ public long put(TestEntity entity) {
150151
__id9, simpleByteArray, __id15, __id15 != 0 ? stringObjectMapConverter.convertToDatabaseValue(stringObjectMap) : null,
151152
__id16, __id16 != 0 ? flexPropertyConverter.convertToDatabaseValue(flexProperty) : null);
152153

154+
java.util.Date date = entity.getDate();
155+
int __id23 = date != null ? __ID_date : 0;
156+
153157
collect313311(cursor, 0, 0,
154158
0, null, 0, null,
155159
0, null, 0, null,
156160
__ID_simpleLong, entity.getSimpleLong(), __ID_simpleLongU, entity.getSimpleLongU(),
157-
INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(), __ID_simpleIntU, entity.getSimpleIntU(),
158-
__ID_simpleShort, entity.getSimpleShort(), __ID_simpleShortU, entity.getSimpleShortU(),
161+
__id23, __id23 != 0 ? date.getTime() : 0, INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(),
162+
__ID_simpleIntU, entity.getSimpleIntU(), __ID_simpleShort, entity.getSimpleShort(),
159163
__ID_simpleFloat, entity.getSimpleFloat(), __ID_simpleDouble, entity.getSimpleDouble());
160164

161165
long __assignedId = collect004000(cursor, entity.getId(), PUT_FLAG_COMPLETE,
162-
__ID_simpleByte, entity.getSimpleByte(), __ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0,
163-
0, 0, 0, 0);
166+
__ID_simpleShortU, entity.getSimpleShortU(), __ID_simpleByte, entity.getSimpleByte(),
167+
__ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0, 0, 0);
164168

165169
entity.setId(__assignedId);
166170

tests/objectbox-java-test/src/main/java/io/objectbox/TestEntity_.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public final class TestEntity_ implements EntityInfo<TestEntity> {
120120
public final static io.objectbox.Property<TestEntity> doubleArray =
121121
new io.objectbox.Property<>(__INSTANCE, 22, 23, double[].class, "doubleArray");
122122

123+
public final static io.objectbox.Property<TestEntity> date =
124+
new io.objectbox.Property<>(__INSTANCE, 23, 24, java.util.Date.class, "date");
125+
123126
@SuppressWarnings("unchecked")
124127
public final static io.objectbox.Property<TestEntity>[] __ALL_PROPERTIES = new io.objectbox.Property[]{
125128
id,
@@ -144,7 +147,8 @@ public final class TestEntity_ implements EntityInfo<TestEntity> {
144147
intArray,
145148
longArray,
146149
floatArray,
147-
doubleArray
150+
doubleArray,
151+
date
148152
};
149153

150154
public final static io.objectbox.Property<TestEntity> __ID_PROPERTY = id;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.Comparator;
29+
import java.util.Date;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
@@ -299,7 +300,10 @@ private void addTestEntity(ModelBuilder modelBuilder, @Nullable IndexType simple
299300
entityBuilder.property("floatArray", PropertyType.FloatVector).id(TestEntity_.floatArray.id, ++lastUid);
300301
entityBuilder.property("doubleArray", PropertyType.DoubleVector).id(TestEntity_.doubleArray.id, ++lastUid);
301302

302-
int lastId = TestEntity_.doubleArray.id;
303+
// Date property
304+
entityBuilder.property("date", PropertyType.Date).id(TestEntity_.date.id, ++lastUid);
305+
306+
int lastId = TestEntity_.date.id;
303307
entityBuilder.lastPropertyId(lastId, lastUid);
304308
addOptionalFlagsToTestEntity(entityBuilder);
305309
entityBuilder.entityDone();
@@ -352,6 +356,7 @@ protected TestEntity createTestEntity(@Nullable String simpleString, int nr) {
352356
entity.setLongArray(new long[]{-entity.getSimpleLong(), entity.getSimpleLong()});
353357
entity.setFloatArray(new float[]{-entity.getSimpleFloat(), entity.getSimpleFloat()});
354358
entity.setDoubleArray(new double[]{-entity.getSimpleDouble(), entity.getSimpleDouble()});
359+
entity.setDate(new Date(1000 + nr));
355360
return entity;
356361
}
357362

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void maxDataSize() {
291291
DbMaxDataSizeExceededException.class,
292292
() -> getTestEntityBox().put(testEntity2)
293293
);
294-
assertEquals("Exceeded user-set maximum by [bytes]: 528", maxDataExc.getMessage());
294+
assertEquals("Exceeded user-set maximum by [bytes]: 544", maxDataExc.getMessage());
295295

296296
// Remove to get below max data size, then put again.
297297
getTestEntityBox().remove(testEntity1);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
2424
import java.util.Collection;
25+
import java.util.Date;
2526
import java.util.List;
2627
import java.util.Map;
2728

@@ -85,6 +86,7 @@ public void testPutAndGet() {
8586
assertArrayEquals(new long[]{-valLong, valLong}, entity.getLongArray());
8687
assertArrayEquals(new float[]{-valFloat, valFloat}, entityRead.getFloatArray(), 0);
8788
assertArrayEquals(new double[]{-valDouble, valDouble}, entity.getDoubleArray(), 0);
89+
assertEquals(new Date(1000 + simpleInt), entity.getDate());
8890
}
8991

9092
// Note: There is a similar test using the Cursor API directly (which is deprecated) in CursorTest.
@@ -135,6 +137,7 @@ public void testPutAndGet_defaultOrNullValues() {
135137
assertNull(defaultEntity.getLongArray());
136138
assertNull(defaultEntity.getFloatArray());
137139
assertNull(defaultEntity.getDoubleArray());
140+
assertNull(defaultEntity.getDate());
138141
}
139142

140143
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void setUpBox() {
6060
* <li>longArray = [{-3000,3000}..{-3009,3009}]</li>
6161
* <li>floatArray = [{-400.0,400.0}..{-400.9,400.9}]</li>
6262
* <li>doubleArray = [{-2020.00,2020.00}..{-2020.09,2020.09}] (approximately)</li>
63+
* <li>date = [Date(3000)..Date(3009)]</li>
6364
*/
6465
public List<TestEntity> putTestEntitiesScalars() {
6566
return putTestEntities(10, null, 2000);

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

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@
3030
import io.objectbox.TestEntity;
3131
import io.objectbox.TestEntity_;
3232
import io.objectbox.TestUtils;
33-
import io.objectbox.config.DebugFlags;
3433
import io.objectbox.exception.DbExceptionListener;
3534
import io.objectbox.exception.NonUniqueResultException;
3635
import io.objectbox.query.QueryBuilder.StringOrder;
37-
import io.objectbox.relation.MyObjectBox;
38-
import io.objectbox.relation.Order;
39-
import io.objectbox.relation.Order_;
4036

4137

38+
import static io.objectbox.TestEntity_.date;
4239
import static io.objectbox.TestEntity_.simpleBoolean;
4340
import static io.objectbox.TestEntity_.simpleByteArray;
4441
import static io.objectbox.TestEntity_.simpleFloat;
@@ -1253,29 +1250,84 @@ public void testQueryAttempts() {
12531250
}
12541251

12551252
@Test
1256-
public void testDateParam() {
1257-
store.close();
1258-
assertTrue(store.deleteAllFiles());
1259-
store = MyObjectBox.builder().baseDirectory(boxStoreDir).debugFlags(DebugFlags.LOG_QUERY_PARAMETERS).build();
1260-
1253+
public void date_equal_and_setParameter_works() {
12611254
Date now = new Date();
1262-
Order order = new Order();
1263-
order.setDate(now);
1264-
Box<Order> box = store.boxFor(Order.class);
1265-
box.put(order);
1255+
TestEntity entity = new TestEntity();
1256+
entity.setDate(now);
1257+
Box<TestEntity> box = store.boxFor(TestEntity.class);
1258+
box.put(entity);
1259+
1260+
try (Query<TestEntity> query = box.query(TestEntity_.date.equal(0)).build()) {
1261+
assertEquals(0, query.count());
1262+
query.setParameter(TestEntity_.date, now);
1263+
assertEquals(1, query.count());
1264+
}
12661265

1267-
Query<Order> query = box.query().equal(Order_.date, 0).build();
1268-
assertEquals(0, query.count());
1266+
// Again, but using alias
1267+
try (Query<TestEntity> aliasQuery = box.query(TestEntity_.date.equal(0)).parameterAlias("date").build()) {
1268+
assertEquals(0, aliasQuery.count());
1269+
aliasQuery.setParameter("date", now);
1270+
assertEquals(1, aliasQuery.count());
1271+
}
1272+
}
12691273

1270-
query.setParameter(Order_.date, now);
1271-
assertEquals(1, query.count());
1274+
@Test
1275+
public void date_between_works() {
1276+
putTestEntitiesScalars();
1277+
try (Query<TestEntity> query = box.query(date.between(new Date(3002L), new Date(3008L))).build()) {
1278+
assertEquals(7, query.count());
1279+
}
1280+
}
12721281

1273-
// Again, but using alias
1274-
Query<Order> aliasQuery = box.query().equal(Order_.date, 0).parameterAlias("date").build();
1275-
assertEquals(0, aliasQuery.count());
1282+
@Test
1283+
public void date_lessAndGreater_works() {
1284+
putTestEntitiesScalars();
1285+
try (Query<TestEntity> query = box.query(date.less(new Date(3002L))).build()) {
1286+
assertEquals(2, query.count());
1287+
}
1288+
try (Query<TestEntity> query = box.query(date.lessOrEqual(new Date(3003L))).build()) {
1289+
assertEquals(4, query.count());
1290+
}
1291+
try (Query<TestEntity> query = box.query(date.greater(new Date(3008L))).build()) {
1292+
assertEquals(1, query.count());
1293+
}
1294+
try (Query<TestEntity> query = box.query(date.greaterOrEqual(new Date(3008L))).build()) {
1295+
assertEquals(2, query.count());
1296+
}
1297+
}
1298+
1299+
@Test
1300+
public void date_oneOf_works() {
1301+
putTestEntitiesScalars();
1302+
Date[] valuesDate = new Date[]{new Date(3002L), new Date(), new Date(0)};
1303+
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate)).build()) {
1304+
assertEquals(1, query.count());
1305+
}
1306+
Date[] valuesDate2 = new Date[]{new Date()};
1307+
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate2)).build()) {
1308+
assertEquals(0, query.count());
1309+
}
1310+
Date[] valuesDate3 = new Date[]{new Date(3002L), new Date(3009L)};
1311+
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate3)).build()) {
1312+
assertEquals(2, query.count());
1313+
}
1314+
}
12761315

1277-
aliasQuery.setParameter("date", now);
1278-
assertEquals(1, aliasQuery.count());
1316+
@Test
1317+
public void date_notOneOf_works() {
1318+
putTestEntitiesScalars();
1319+
Date[] valuesDate = new Date[]{new Date(3002L), new Date(), new Date(0)};
1320+
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate)).build()) {
1321+
assertEquals(9, query.count());
1322+
}
1323+
Date[] valuesDate2 = new Date[]{new Date()};
1324+
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate2)).build()) {
1325+
assertEquals(10, query.count());
1326+
}
1327+
Date[] valuesDate3 = new Date[]{new Date(3002L), new Date(3009L)};
1328+
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate3)).build()) {
1329+
assertEquals(8, query.count());
1330+
}
12791331
}
12801332

12811333
@Test

0 commit comments

Comments
 (0)