Skip to content

Commit

Permalink
fix for LazyList.sublist (fixes greenrobot#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
greenrobot committed Jun 30, 2015
1 parent 17f3880 commit b3858b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions DaoCore/src/de/greenrobot/dao/query/LazyList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
* Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -343,7 +343,7 @@ public int size() {
public List<E> subList(int start, int end) {
checkCached();
for (int i = start; i < end; i++) {
entities.get(i);
get(i);
}
return entities.subList(start, end);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
* Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
*
* This file is part of greenDAO Generator.
*
Expand All @@ -18,8 +18,8 @@
package de.greenrobot.daotest.entity;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.LazyList;
import de.greenrobot.dao.test.AbstractDaoTest;
import de.greenrobot.daotest.TestEntity;
import de.greenrobot.daotest.TestEntityDao;
Expand Down Expand Up @@ -57,7 +57,7 @@ protected int getSimpleInteger(int i) {
return 100 + i;
}

protected void assertIds(ArrayList<TestEntity> list, LazyList<TestEntity> list2) {
protected void assertIds(List<TestEntity> list, List<TestEntity> list2) {
for (int i = 0; i < list.size(); i++) {
TestEntity entity = list.get(i);
TestEntity lazyEntity = list2.get(i);
Expand Down
19 changes: 18 additions & 1 deletion DaoTest/src/de/greenrobot/daotest/query/LazyListTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
* Copyright (C) 2011-2015 Markus Junginger, greenrobot (http://greenrobot.de)
*
* This file is part of greenDAO Generator.
*
Expand Down Expand Up @@ -63,6 +63,23 @@ public void testGetAll100Uncached() {
listLazy.close();
}

public void testSublist() {
ArrayList<TestEntity> list = insert(10);
LazyList<TestEntity> listLazy = dao.queryBuilder().orderAsc(Properties.SimpleInteger).build().listLazy();
assertIds(list.subList(2, 7), listLazy.subList(2, 7));
}

public void testSublistUncached() {
ArrayList<TestEntity> list = insert(10);
LazyList<TestEntity> listLazy = dao.queryBuilder().orderAsc(Properties.SimpleInteger).build().listLazyUncached();
try {
assertIds(list.subList(2,7), listLazy.subList(2,7));
} catch (DaoException e) {
assertEquals("This operation only works with cached lazy lists", e.getMessage());
}

}

public void testIterator() {
ArrayList<TestEntity> list = insert(100);
LazyList<TestEntity> listLazy = dao.queryBuilder().orderAsc(Properties.SimpleInteger).build().listLazy();
Expand Down

0 comments on commit b3858b8

Please sign in to comment.