|
|
@@ -34,36 +34,36 @@ import static org.junit.Assert.fail; |
|
|
|
public class LazyResourceCollectionTest { |
|
|
|
|
|
|
|
private class StringResourceCollection implements ResourceCollection { |
|
|
|
List<StringResourceIterator> createdIterators = new ArrayList<>();
|
|
|
|
List<StringResourceIterator> createdIterators = new ArrayList<>(); |
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override |
|
|
|
public int size() { |
|
|
|
return 3;
|
|
|
|
return 3; |
|
|
|
} |
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override |
|
|
|
public Iterator<Resource> iterator() { |
|
|
|
StringResourceIterator it = new StringResourceIterator(); |
|
|
|
createdIterators.add(it); |
|
|
|
return it; |
|
|
|
} |
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override |
|
|
|
public boolean isFilesystemOnly() { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class StringResourceIterator implements Iterator<Resource> {
|
|
|
|
private class StringResourceIterator implements Iterator<Resource> { |
|
|
|
int cursor = 0; |
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override |
|
|
|
public void remove() { |
|
|
|
throw new UnsupportedOperationException(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override
|
|
|
|
public StringResource next() {
|
|
|
|
@Override |
|
|
|
public StringResource next() { |
|
|
|
if (cursor < 3) { |
|
|
|
cursor++; |
|
|
|
return new StringResource("r" + cursor); |
|
|
@@ -71,7 +71,7 @@ public class LazyResourceCollectionTest { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override |
|
|
|
public boolean hasNext() { |
|
|
|
return cursor < 3; |
|
|
|
} |
|
|
@@ -85,25 +85,25 @@ public class LazyResourceCollectionTest { |
|
|
|
|
|
|
|
Iterator<Resource> it = lazyCollection.iterator(); |
|
|
|
assertOneCreatedIterator(collectionTest); |
|
|
|
StringResourceIterator stringResourceIterator =
|
|
|
|
collectionTest.createdIterators.get(0);
|
|
|
|
StringResourceIterator stringResourceIterator = |
|
|
|
collectionTest.createdIterators.get(0); |
|
|
|
assertEquals("A resource was loaded without iterating", 1, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r1", it.next());
|
|
|
|
assertStringValue("r1", it.next()); |
|
|
|
assertOneCreatedIterator(collectionTest); |
|
|
|
assertEquals("Iterating once load more than 1 resource", 2, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r2", it.next());
|
|
|
|
assertStringValue("r2", it.next()); |
|
|
|
assertOneCreatedIterator(collectionTest); |
|
|
|
assertEquals("Iterating twice load more than 2 resources", 3, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r3", it.next());
|
|
|
|
assertStringValue("r3", it.next()); |
|
|
|
assertOneCreatedIterator(collectionTest); |
|
|
|
assertEquals("Iterating 3 times load more than 3 resources", 3, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
try { |
|
|
|
it.next(); |
|
|
@@ -131,37 +131,37 @@ public class LazyResourceCollectionTest { |
|
|
|
Iterator<Resource> it2 = lazyCollection.iterator(); |
|
|
|
assertOneCreatedIterator(collectionTest); |
|
|
|
|
|
|
|
StringResourceIterator stringResourceIterator =
|
|
|
|
collectionTest.createdIterators.get(0);
|
|
|
|
StringResourceIterator stringResourceIterator = |
|
|
|
collectionTest.createdIterators.get(0); |
|
|
|
assertEquals("A resource was loaded without iterating", 1, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r1", it1.next());
|
|
|
|
assertStringValue("r1", it1.next()); |
|
|
|
assertEquals("Iterating once load more than 1 resource", 2, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r1", it2.next());
|
|
|
|
assertStringValue("r1", it2.next()); |
|
|
|
assertEquals( |
|
|
|
"The second iterator did not lookup in the cache for a resource", 2,
|
|
|
|
stringResourceIterator.cursor);
|
|
|
|
"The second iterator did not lookup in the cache for a resource", 2, |
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r2", it2.next());
|
|
|
|
assertStringValue("r2", it2.next()); |
|
|
|
assertEquals("Iterating twice load more than 2 resources", 3, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r2", it1.next());
|
|
|
|
assertStringValue("r2", it1.next()); |
|
|
|
assertEquals( |
|
|
|
"The first iterator did not lookup in the cache for a resource", 3,
|
|
|
|
stringResourceIterator.cursor);
|
|
|
|
"The first iterator did not lookup in the cache for a resource", 3, |
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r3", it2.next());
|
|
|
|
assertStringValue("r3", it2.next()); |
|
|
|
assertEquals("Iterating 3 times load more than 3 resources", 3, |
|
|
|
stringResourceIterator.cursor);
|
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
assertStringValue("r3", it1.next());
|
|
|
|
assertStringValue("r3", it1.next()); |
|
|
|
assertEquals( |
|
|
|
"The first iterator did not lookup in the cache for a resource", 3,
|
|
|
|
stringResourceIterator.cursor);
|
|
|
|
"The first iterator did not lookup in the cache for a resource", 3, |
|
|
|
stringResourceIterator.cursor); |
|
|
|
|
|
|
|
try { |
|
|
|
it1.next(); |
|
|
@@ -177,8 +177,8 @@ public class LazyResourceCollectionTest { |
|
|
|
// ok |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void assertStringValue(String expected, Resource r) {
|
|
|
|
assertEquals(expected, r.as(StringResource.class).getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void assertStringValue(String expected, Resource r) { |
|
|
|
assertEquals(expected, r.as(StringResource.class).getValue()); |
|
|
|
} |
|
|
|
} |