PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 182 lines | 131 code | 24 blank | 27 comment | 7 complexity | b4decbc25078577443eb443565ea6468 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.jpa.JPATestCase;
 26import com.google.appengine.datanucleus.test.jpa.Book;
 27
 28
 29import java.util.Arrays;
 30import java.util.Iterator;
 31import java.util.List;
 32
 33import javax.persistence.FlushModeType;
 34import javax.persistence.Query;
 35
 36
 37/**
 38 * @author Max Ross <max.ross@gmail.com>
 39 */
 40public class JPQLCursorTest extends JPATestCase {
 41
 42  public void testGetCursor_List() {
 43    Entity e1 = Book.newBookEntity("auth", "34", "yar");
 44    Entity e2 = Book.newBookEntity("auth", "34", "yar");
 45    Entity e3 = Book.newBookEntity("auth", "34", "yar");
 46    ds.put(Arrays.asList(e1, e2, e3));
 47
 48    beginTxn();
 49    Query q = em.createQuery("select from " + Book.class.getName() + " b");
 50    q.setMaxResults(1);
 51    List<Book> books = (List<Book>) q.getResultList();
 52    assertEquals(1, books.size());
 53    assertEquals(e1.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
 54    Cursor c = JPACursorHelper.getCursor(books);
 55    assertNotNull(c);
 56
 57    q.setHint(JPACursorHelper.CURSOR_HINT, c);
 58    books = (List<Book>) q.getResultList();
 59    assertEquals(1, books.size());
 60    assertEquals(e2.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
 61    assertNotNull(JPACursorHelper.getCursor(books));
 62
 63    q.setHint(JPACursorHelper.CURSOR_HINT, c.toWebSafeString());
 64    books = (List<Book>) q.getResultList();
 65    assertEquals(1, books.size());
 66    assertEquals(e2.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
 67    c = JPACursorHelper.getCursor(books);
 68    assertNotNull(c);
 69
 70    q.setHint(JPACursorHelper.CURSOR_HINT, c);
 71    books = (List<Book>) q.getResultList();
 72    assertEquals(1, books.size());
 73    assertEquals(e3.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
 74    assertNotNull(JPACursorHelper.getCursor(books));
 75
 76    q.setHint(JPACursorHelper.CURSOR_HINT, c.toWebSafeString());
 77    books = (List<Book>) q.getResultList();
 78    assertEquals(1, books.size());
 79    assertEquals(e3.getKey(), KeyFactory.stringToKey(books.get(0).getId()));
 80    assertNotNull(JPACursorHelper.getCursor(books));
 81
 82    commitTxn();
 83  }
 84
 85  public void testCursorEquality() {
 86    List<Key> keys = Utils.newArrayList();
 87    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 88    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 89    keys.add(ds.put(Book.newBookEntity("auth", "34", "yar")));
 90
 91    com.google.appengine.api.datastore.Query query = new com.google.appengine.api.datastore.Query("Book");
 92    QueryResultIterator entityIter = ds.prepare(query).asQueryResultIterator();
 93    List<Cursor> lowLevelCursors = Utils.newArrayList();
 94    lowLevelCursors.add(entityIter.getCursor());
 95    entityIter.next();
 96    lowLevelCursors.add(entityIter.getCursor());
 97    entityIter.next();
 98    lowLevelCursors.add(entityIter.getCursor());
 99    entityIter.next();
100    lowLevelCursors.add(entityIter.getCursor());
101
102    Query q = em.createQuery("select from " + Book.class.getName() + " b");
103    Iterator<Book> bookIter = q.getResultList().iterator();
104    List<Cursor> ormCursors = Utils.newArrayList();
105    ormCursors.add(JPACursorHelper.getCursor(bookIter));
106    bookIter.next();
107    ormCursors.add(JPACursorHelper.getCursor(bookIter));
108    bookIter.next();
109    ormCursors.add(JPACursorHelper.getCursor(bookIter));
110    bookIter.next();
111    ormCursors.add(JPACursorHelper.getCursor(bookIter));
112
113    assertEquals(lowLevelCursors, ormCursors);
114
115    for (int i = 0; i < lowLevelCursors.size(); i++) {
116      Cursor lowLevelCursor = lowLevelCursors.get(i);
117      @SuppressWarnings("deprecation")
118      List<Entity> list = ds.prepare(query).asList(FetchOptions.Builder.withCursor(lowLevelCursor));
119      assertEquals(3 - i, list.size());
120    }
121  }
122
123  public void testGetCursor_Iterator() {
124    List<Key> keys = Utils.newArrayList();
125    for (int i = 0; i < 10; i++) {
126      keys.add(ds.put(Book.newBookEntity("auth" + i, "34", "yar")));
127    }
128
129    beginTxn();
130    Query q = em.createQuery("select from " + Book.class.getName() + " b");
131    Iterator<Book> bookIter = q.getResultList().iterator();
132    assertCursorResults(JPACursorHelper.getCursor(bookIter), keys);
133    int index = 0;
134    while (bookIter.hasNext()) {
135      bookIter.next();
136      assertCursorResults(JPACursorHelper.getCursor(bookIter), keys.subList(++index, keys.size()));
137    }
138    // setting a max result means we call asQueryResultList(), and there are no
139    // intermediate cursors available from a List.
140    q.setMaxResults(1);
141
142    bookIter = q.getResultList().iterator();
143    assertNull(JPACursorHelper.getCursor((bookIter)));
144    while (bookIter.hasNext()) {
145      bookIter.next();
146      assertNull(JPACursorHelper.getCursor((bookIter)));
147    }
148  }
149
150  public void testGetCursor_Iterable() {
151    List<Key> keys = Utils.newArrayList();
152    for (int i = 0; i < 10; i++) {
153      keys.add(ds.put(Book.newBookEntity("auth" + i, "34", "yar")));
154    }
155
156    beginTxn();
157    Query q = em.createQuery("select from " + Book.class.getName() + " b");
158    // no limit specified so what we get back doesn't have an end cursor
159    List<Book> bookList = q.getResultList();
160    assertNull(JPACursorHelper.getCursor(bookList));
161    for (@SuppressWarnings("unused") Book b : bookList) {
162      // no cursor
163      assertNull(JPACursorHelper.getCursor(bookList));
164    }
165  }
166
167  private void assertCursorResults(Cursor cursor, List<Key> expectedKeys) {
168    Query q = em.createQuery("select from " + Book.class.getName() + " b");
169    q.setHint(JPACursorHelper.QUERY_CURSOR_PROPERTY_NAME, cursor);
170    // if FlushModeType is AUTO and there is another query in progress
171    // in this txn, that query result will get flushed before this one
172    // runs, and as a result the cursor will get bumped to the end of the
173    // result set, which is not what we want for this test.  We set the
174    // FlushModeType to COMMIT to prevent the flush from happening.
175    q.setFlushMode(FlushModeType.COMMIT);
176    List<Key> keys = Utils.newArrayList();
177    for (Object b : q.getResultList()) {
178      keys.add(KeyFactory.stringToKey(((Book) b).getId()));
179    }
180    assertEquals(expectedKeys, keys);
181  }
182}