PageRenderTime 77ms CodeModel.GetById 56ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://datanucleus-appengine.googlecode.com/
Java | 187 lines | 141 code | 24 blank | 22 comment | 7 complexity | 90ec8a0da9d7516ef977c89cf571c3e0 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 Google Inc
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package com.google.appengine.datanucleus.query;
 17
 18import com.google.appengine.api.datastore.Cursor;
 19import com.google.appengine.api.datastore.Entity;
 20import com.google.appengine.api.datastore.FetchOptions;
 21import com.google.appengine.api.datastore.Key;
 22import com.google.appengine.api.datastore.KeyFactory;
 23import com.google.appengine.api.datastore.QueryResultIterator;
 24import com.google.appengine.datanucleus.Utils;
 25import com.google.appengine.datanucleus.jdo.JDOTestCase;
 26import com.google.appengine.datanucleus.test.jdo.Flight;
 27import com.google.appengine.datanucleus.test.jpa.Book;
 28
 29
 30import java.util.Arrays;
 31import java.util.Iterator;
 32import java.util.List;
 33import java.util.Map;
 34
 35import javax.jdo.Query;
 36
 37/**
 38 * @author Max Ross <max.ross@gmail.com>
 39 */
 40public class JDOQLCursorTest extends JDOTestCase {
 41
 42  public void testGetCursor_List() {
 43    Entity e1 = Flight.newFlightEntity("harold", "bos", "mia", 23, 24);
 44    Entity e2 = Flight.newFlightEntity("harold", "bos", "mia", 23, 24);
 45    Entity e3 = Flight.newFlightEntity("harold", "bos", "mia", 23, 24);
 46    ds.put(Arrays.asList(e1, e2, e3));
 47
 48    Map<String, Object> extensionMap = Utils.newHashMap();
 49
 50    beginTxn();
 51    Query q = pm.newQuery(Flight.class);
 52    q.setRange(0, 1);
 53    List<Flight> flights = (List<Flight>) q.execute();
 54    assertEquals(1, flights.size());
 55    assertEquals(e1.getKey(), KeyFactory.stringToKey(flights.get(0).getId()));
 56    Cursor c = JDOCursorHelper.getCursor(flights);
 57    assertNotNull(c);
 58
 59    extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, c);
 60    q.setExtensions(extensionMap);
 61    flights = (List<Flight>) q.execute();
 62    assertEquals(1, flights.size());
 63    assertEquals(e2.getKey(), KeyFactory.stringToKey(flights.get(0).getId()));
 64    assertNotNull(JDOCursorHelper.getCursor(flights));
 65
 66    extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, c.toWebSafeString());
 67    q.setExtensions(extensionMap);
 68    flights = (List<Flight>) q.execute();
 69    assertEquals(1, flights.size());
 70    assertEquals(e2.getKey(), KeyFactory.stringToKey(flights.get(0).getId()));
 71    c = JDOCursorHelper.getCursor(flights);
 72    assertNotNull(c);
 73
 74    extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, c);
 75    q.setExtensions(extensionMap);
 76    flights = (List<Flight>) q.execute();
 77    assertEquals(1, flights.size());
 78    assertEquals(e3.getKey(), KeyFactory.stringToKey(flights.get(0).getId()));
 79    assertNotNull(JDOCursorHelper.getCursor(flights));
 80
 81    extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, c.toWebSafeString());
 82    q.setExtensions(extensionMap);
 83    flights = (List<Flight>) q.execute();
 84    assertEquals(1, flights.size());
 85    assertEquals(e3.getKey(), KeyFactory.stringToKey(flights.get(0).getId()));
 86    assertNotNull(JDOCursorHelper.getCursor(flights));
 87    commitTxn();
 88  }
 89  
 90  public void testCursorEquality() {
 91    List<Key> keys = Utils.newArrayList();
 92    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 93    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 94    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 95
 96    com.google.appengine.api.datastore.Query query = new com.google.appengine.api.datastore.Query("Book");
 97    QueryResultIterator entityIter = ds.prepare(query).asQueryResultIterator();
 98    List<Cursor> lowLevelCursors = Utils.newArrayList();
 99    lowLevelCursors.add(entityIter.getCursor());
100    entityIter.next();
101    lowLevelCursors.add(entityIter.getCursor());
102    entityIter.next();
103    lowLevelCursors.add(entityIter.getCursor());
104    entityIter.next();
105    lowLevelCursors.add(entityIter.getCursor());
106
107    beginTxn();
108    Query q = pm.newQuery("select from " + Book.class.getName());
109    Iterator<Book> bookIter = asIterator(q);
110    List<Cursor> ormCursors = Utils.newArrayList();
111    ormCursors.add(JDOCursorHelper.getCursor(bookIter));
112    bookIter.next();
113    ormCursors.add(JDOCursorHelper.getCursor(bookIter));
114    bookIter.next();
115    ormCursors.add(JDOCursorHelper.getCursor(bookIter));
116    bookIter.next();
117    ormCursors.add(JDOCursorHelper.getCursor(bookIter));
118    commitTxn();
119    
120    assertEquals(lowLevelCursors, ormCursors);
121
122    for (int i = 0; i < lowLevelCursors.size(); i++) {
123      Cursor lowLevelCursor = lowLevelCursors.get(i);
124      @SuppressWarnings("deprecation")
125      List<Entity> list = ds.prepare(query).asList(FetchOptions.Builder.withCursor(lowLevelCursor));
126      assertEquals(3 - i, list.size());
127    }
128  }
129
130  public void testGetCursor_Iterator() {
131    List<Key> keys = Utils.newArrayList();
132    for (int i = 0; i < 10; i++) {
133      keys.add(ds.put(Book.newBookEntity("auth" + i, "34", "yar")));
134    }
135
136    beginTxn();
137    Query q = pm.newQuery("select from " + Book.class.getName());
138    Iterator<Book> bookIter = asIterator(q);
139    assertCursorResults(JDOCursorHelper.getCursor(bookIter), keys);
140    int index = 0;
141    while (bookIter.hasNext()) {
142      bookIter.next();
143      assertCursorResults(JDOCursorHelper.getCursor(bookIter), keys.subList(++index, keys.size()));
144    }
145    // setting a max result means we call asQueryResultList(), and there are no
146    // intermediate cursors available from a List.
147    q.setRange(0, 1);
148
149    bookIter = asIterator(q);
150    assertNull(JDOCursorHelper.getCursor((asIterator(q))));
151    while (bookIter.hasNext()) {
152      bookIter.next();
153      assertNull(JDOCursorHelper.getCursor((asIterator(q))));
154    }
155  }
156
157  private <T> Iterator<T> asIterator(Query q) {
158    return ((List) q.execute()).iterator();
159  }
160
161  public void testGetCursor_Iterable() {
162    List<Key> keys = Utils.newArrayList();
163    for (int i = 0; i < 10; i++) {
164      keys.add(ds.put(Book.newBookEntity("auth" + i, "34", "yar")));
165    }
166
167    beginTxn();
168    Query q = pm.newQuery("select from " + Book.class.getName());
169    // no limit specified so what we get back doesn't have an end cursor
170    List<Book> bookList = (List<Book>) q.execute();
171    assertNull(JDOCursorHelper.getCursor(bookList));
172    for (@SuppressWarnings("unused") Book b : bookList) {
173      // no cursor
174      assertNull(JDOCursorHelper.getCursor(bookList));
175    }
176  }
177
178  private void assertCursorResults(Cursor cursor, List<Key> expectedKeys) {
179    Query q = pm.newQuery("select from " + Book.class.getName());
180    q.addExtension(JDOCursorHelper.QUERY_CURSOR_PROPERTY_NAME, cursor);
181    List<Key> keys = Utils.newArrayList();
182    for (Object b : (Iterable) q.execute()) {
183      keys.add(KeyFactory.stringToKey(((Book) b).getId()));
184    }
185    assertEquals(expectedKeys, keys);
186  }
187}