PageRenderTime 68ms CodeModel.GetById 49ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/query/LazyResultTest.java

http://datanucleus-appengine.googlecode.com/
Java | 284 lines | 215 code | 39 blank | 30 comment | 0 complexity | e9510789125374e7ce92551a3a64537c MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.query;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.datanucleus.DatastoreTestCase;
 20import com.google.appengine.datanucleus.Utils;
 21import com.google.appengine.datanucleus.Utils.Function;
 22
 23
 24import java.util.Collections;
 25import java.util.Iterator;
 26import java.util.ListIterator;
 27
 28/**
 29 * @author Max Ross <maxr@google.com>
 30 */
 31public class LazyResultTest extends DatastoreTestCase {
 32
 33  public void testEquality() {
 34    LazyResult lr1 = new LazyResult<Object>(Collections.<Entity>emptyList(), null, false);
 35    LazyResult lr2 = new LazyResult<Object>(Collections.<Entity>emptyList(), null, false);
 36    assertTrue(lr1.equals(lr1));
 37    assertFalse(lr1.equals(lr2));
 38  }
 39
 40  private static final Function<Entity, Object> NULL_FUNC = new Function<Entity, Object>() {
 41    public Object apply(Entity entity) {
 42      return entity;
 43    }
 44  };
 45
 46  private static class CountingIterable implements Iterable<Entity> {
 47    private final Iterable<Entity> iterable;
 48    private int nextCount = 0;
 49
 50    private CountingIterable(Iterable<Entity> iterable) {
 51      this.iterable = iterable;
 52    }
 53
 54    public Iterator<Entity> iterator() {
 55      return new CountingIterator(iterable.iterator());
 56    }
 57
 58    private class CountingIterator implements Iterator<Entity> {
 59      private final Iterator<Entity> iter;
 60
 61      private CountingIterator(Iterator<Entity> iter) {
 62        this.iter = iter;
 63      }
 64
 65      public boolean hasNext() {
 66        return iter.hasNext();
 67      }
 68
 69      public Entity next() {
 70        nextCount++;
 71        return iter.next();
 72      }
 73
 74      public void remove() {
 75        throw new UnsupportedOperationException();
 76      }
 77    }
 78  }
 79
 80  public void testSize_FreshIterator() {
 81    CountingIterable iterable = new CountingIterable(Utils.<Entity>newArrayList());
 82    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
 83    assertEquals(0, lr.size());
 84    assertEquals(0, iterable.nextCount);
 85
 86    Entity e = null;
 87    iterable = new CountingIterable(Utils.newArrayList(e));
 88    lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
 89    assertEquals(1, lr.size());
 90    assertEquals(1, iterable.nextCount);
 91
 92    iterable = new CountingIterable(Utils.newArrayList(e, e));
 93    lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
 94    assertEquals(2, lr.size());
 95    assertEquals(2, iterable.nextCount);
 96  }
 97
 98  public void testSize_PartiallyConsumedIterator() {
 99    Entity e = null;
100    CountingIterable iterable = new CountingIterable(Utils.newArrayList(e, e, e));
101    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
102    lr.resolveNext();
103    assertEquals(1, iterable.nextCount);
104    assertEquals(3, lr.size());
105    assertEquals(3, iterable.nextCount);
106  }
107
108  public void testSize_ExhaustedIterator() {
109    Entity e = null;
110    CountingIterable iterable = new CountingIterable(Utils.newArrayList(e, e));
111    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
112    lr.resolveNext();
113    lr.resolveNext();
114    assertEquals(2, iterable.nextCount);
115    assertEquals(2, lr.size());
116    assertEquals(2, iterable.nextCount);
117  }
118
119  public void testGet_FreshIterator() {
120    CountingIterable iterable = new CountingIterable(Utils.<Entity>newArrayList());
121    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
122    try {
123      lr.get(0);
124      fail("expected index out of bounds exception");
125    } catch (IndexOutOfBoundsException e) {
126      // good
127    }
128    assertEquals(0, iterable.nextCount);
129
130    Entity e1 = new Entity("yar");
131    Entity e2 = new Entity("yar");
132    iterable = new CountingIterable(Utils.newArrayList(e1, e2));
133    lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
134    assertEquals(e1, lr.get(0));
135    assertEquals(1, iterable.nextCount);
136    assertEquals(e2, lr.get(1));
137    assertEquals(2, iterable.nextCount);
138
139    try {
140      lr.get(3);
141      fail("expected index out of bounds exception");
142    } catch (IndexOutOfBoundsException e) {
143      // good
144    }
145    assertEquals(2, iterable.nextCount);
146  }
147
148  public void testGet_PartiallyConsumedIterator() {
149    Entity e1 = new Entity("yar");
150    Entity e2 = new Entity("yar");
151    CountingIterable iterable = new CountingIterable(Utils.<Entity>newArrayList(e1, e2));
152    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
153    lr.resolveNext();
154    assertEquals(1, iterable.nextCount);
155    assertEquals(e1, lr.get(0));
156    assertEquals(1, iterable.nextCount);
157    assertEquals(e2, lr.get(1));
158    assertEquals(2, iterable.nextCount);
159
160    try {
161      lr.get(3);
162      fail("expected index out of bounds exception");
163    } catch (IndexOutOfBoundsException e) {
164      // good
165    }
166    assertEquals(2, iterable.nextCount);
167  }
168
169  public void testGet_ExhaustedIterator() {
170    Entity e1 = new Entity("yar");
171    Entity e2 = new Entity("yar");
172    CountingIterable iterable = new CountingIterable(Utils.<Entity>newArrayList(e1, e2));
173    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
174    lr.resolveNext();
175    lr.resolveNext();
176    assertEquals(2, iterable.nextCount);
177    assertEquals(e1, lr.get(0));
178    assertEquals(2, iterable.nextCount);
179    assertEquals(e2, lr.get(1));
180    assertEquals(2, iterable.nextCount);
181
182    try {
183      lr.get(3);
184      fail("expected index out of bounds exception");
185    } catch (IndexOutOfBoundsException e) {
186      // good
187    }
188    assertEquals(2, iterable.nextCount);
189  }
190
191  // This implicitly tests the iterator() method as well since iterator() just
192  // delegates to listIterator()
193  public void testListIterator() {
194    CountingIterable iterable = new CountingIterable(Utils.<Entity>newArrayList());
195    LazyResult lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
196    assertFalse(lr.listIterator().hasNext());
197
198    Entity e1 = new Entity("yar1");
199    Entity e2 = new Entity("yar2");
200    Entity e3 = new Entity("yar3");
201    Entity e4 = new Entity("yar4");
202    iterable = new CountingIterable(Utils.<Entity>newArrayList(e1, e2, e3, e4));
203    lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
204
205    ListIterator listIter = lr.listIterator();
206    assertTrue(listIter.hasNext());
207    assertSame(e1, listIter.next());
208    assertEquals(1, iterable.nextCount);
209    assertSame(e1, lr.get(0));
210    assertEquals(1, iterable.nextCount);
211
212    assertEquals(e2, listIter.next());
213    assertEquals(2, iterable.nextCount);
214    // Calls to the iterator make more data available to get() so nextCount
215    // does not increment.
216    assertSame(e2, lr.get(1));
217    assertEquals(2, iterable.nextCount);
218
219    // now we work our way backwards
220    assertEquals(2, listIter.nextIndex());
221    assertEquals(1, listIter.previousIndex());
222    assertTrue(listIter.hasPrevious());
223    assertTrue(listIter.hasNext());
224    assertSame(e2, listIter.previous());
225    assertTrue(listIter.hasNext());
226    assertEquals(3, iterable.nextCount);
227    assertTrue(listIter.hasPrevious());
228
229    assertEquals(1, listIter.nextIndex());
230    assertEquals(0, listIter.previousIndex());
231    assertSame(e1, listIter.previous());
232    assertEquals(3, iterable.nextCount);
233    assertFalse(listIter.hasPrevious());
234    assertTrue(listIter.hasNext());
235    assertEquals(-1, listIter.previousIndex());
236
237    // now we go forwards again
238    assertSame(e1, listIter.next());
239    assertEquals(1, listIter.nextIndex());
240    assertEquals(0, listIter.previousIndex());
241    assertEquals(3, iterable.nextCount);
242    assertTrue(listIter.hasPrevious());
243    assertTrue(listIter.hasNext());
244
245    assertSame(e2, listIter.next());
246    assertEquals(2, listIter.nextIndex());
247    assertEquals(1, listIter.previousIndex());
248    assertEquals(3, iterable.nextCount);
249    assertTrue(listIter.hasPrevious());
250    assertTrue(listIter.hasNext());
251    assertEquals(3, iterable.nextCount);
252
253    assertSame(e3, listIter.next());
254    assertEquals(3, iterable.nextCount);
255    assertEquals(3, listIter.nextIndex());
256    assertEquals(2, listIter.previousIndex());
257    assertTrue(listIter.hasPrevious());
258    assertTrue(listIter.hasNext());
259    // the call to hasNext() results in a fetch
260    assertEquals(4, iterable.nextCount);
261
262    assertSame(e4, listIter.next());
263    assertEquals(4, listIter.nextIndex());
264    assertEquals(3, listIter.previousIndex());
265    assertEquals(4, iterable.nextCount);
266    assertTrue(listIter.hasPrevious());
267    assertFalse(listIter.hasNext());
268
269    iterable = new CountingIterable(Utils.<Entity>newArrayList(e1, e2));
270    lr = new LazyResult<Object>(iterable, NULL_FUNC, false);
271    listIter = lr.listIterator();
272    assertTrue(listIter.hasNext());
273    assertEquals(e1, listIter.next());
274    assertEquals(1, iterable.nextCount);
275
276    // Call to get makes more data available to the iterator.
277    lr.get(1);
278    assertEquals(2, iterable.nextCount);
279    assertTrue(listIter.hasNext());
280    assertSame(e2, listIter.next());
281    assertEquals(2, iterable.nextCount);
282  }
283
284}