|
22 | 22 | import io.objectbox.DebugFlags;
|
23 | 23 | import io.objectbox.TestEntity;
|
24 | 24 | import io.objectbox.TestEntity_;
|
| 25 | +import io.objectbox.TestUtils; |
25 | 26 | import io.objectbox.exception.DbExceptionListener;
|
26 | 27 | import io.objectbox.exception.NonUniqueResultException;
|
27 | 28 | import io.objectbox.query.QueryBuilder.StringOrder;
|
@@ -255,17 +256,69 @@ public void testLongNotIn() {
|
255 | 256 | }
|
256 | 257 |
|
257 | 258 | @Test
|
258 |
| - public void testOffsetLimit() { |
| 259 | + public void offset_limit_find() { |
259 | 260 | putTestEntitiesScalars();
|
260 | 261 | Query<TestEntity> query = box.query().greater(simpleInt, 2002).less(simpleShort, 2108).build();
|
261 | 262 | assertEquals(5, query.count());
|
| 263 | + |
262 | 264 | assertEquals(4, query.find(1, 0).size());
|
263 | 265 | assertEquals(1, query.find(4, 0).size());
|
264 | 266 | assertEquals(2, query.find(0, 2).size());
|
265 | 267 | List<TestEntity> list = query.find(1, 2);
|
266 | 268 | assertEquals(2, list.size());
|
267 | 269 | assertEquals(2004, list.get(0).getSimpleInt());
|
268 | 270 | assertEquals(2005, list.get(1).getSimpleInt());
|
| 271 | + |
| 272 | + OffsetLimitFunction find = (offset, limit) -> query.find(offset, limit).size(); |
| 273 | + assertOffsetLimitEdgeCases(find); |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void offset_limit_findIds() { |
| 278 | + putTestEntitiesScalars(); |
| 279 | + Query<TestEntity> query = box.query().greater(simpleInt, 2002).less(simpleShort, 2108).build(); |
| 280 | + assertEquals(5, query.count()); |
| 281 | + |
| 282 | + assertEquals(4, query.findIds(1, 0).length); |
| 283 | + assertEquals(1, query.findIds(4, 0).length); |
| 284 | + assertEquals(2, query.findIds(0, 2).length); |
| 285 | + long[] list = query.findIds(1, 2); |
| 286 | + assertEquals(2, list.length); |
| 287 | + assertEquals(5, list[0]); |
| 288 | + assertEquals(6, list[1]); |
| 289 | + |
| 290 | + OffsetLimitFunction findIds = (offset, limit) -> query.findIds(offset, limit).length; |
| 291 | + assertOffsetLimitEdgeCases(findIds); |
| 292 | + } |
| 293 | + |
| 294 | + private interface OffsetLimitFunction { |
| 295 | + int applyAndCount(long offset, long limit); |
| 296 | + } |
| 297 | + |
| 298 | + private void assertOffsetLimitEdgeCases(OffsetLimitFunction function) { |
| 299 | + // Max value |
| 300 | + if (TestUtils.is32BitJVM()) { |
| 301 | + // When running 32-bit ObjectBox limit and offset max is limited to 32-bit unsigned integer. |
| 302 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 303 | + () -> function.applyAndCount(Long.MAX_VALUE, Long.MAX_VALUE)); |
| 304 | + assertEquals("Invalid offset (9223372036854775807): exceeds the maximum value allowed on this platform (4294967295)", |
| 305 | + ex.getMessage()); |
| 306 | + // Ensure max allowed value works. |
| 307 | + // Note: currently offset + limit must not exceed 32-bit unsigned integer max. |
| 308 | + assertEquals(0, function.applyAndCount(Integer.MAX_VALUE * 2L + 1, 0)); |
| 309 | + assertEquals(5, function.applyAndCount(0, Integer.MAX_VALUE * 2L + 1)); |
| 310 | + } else { |
| 311 | + // 64-bit JVM |
| 312 | + assertEquals(0, function.applyAndCount(Long.MAX_VALUE, Long.MAX_VALUE)); |
| 313 | + } |
| 314 | + |
| 315 | + // Min value |
| 316 | + IllegalArgumentException exOffset = assertThrows(IllegalArgumentException.class, |
| 317 | + () -> function.applyAndCount(Long.MIN_VALUE, 0)); |
| 318 | + assertEquals("Invalid offset (-9223372036854775808): must be zero or positive", exOffset.getMessage()); |
| 319 | + IllegalArgumentException exLimit = assertThrows(IllegalArgumentException.class, |
| 320 | + () -> function.applyAndCount(0, Long.MIN_VALUE)); |
| 321 | + assertEquals("Invalid limit (-9223372036854775808): must be zero or positive", exLimit.getMessage()); |
269 | 322 | }
|
270 | 323 |
|
271 | 324 | @Test
|
|
0 commit comments