PageRenderTime 45ms CodeModel.GetById 16ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/com/google/appengine/datanucleus/jpa/JPADatastoreBridgeTest.java

http://datanucleus-appengine.googlecode.com/
Java | 105 lines | 73 code | 12 blank | 20 comment | 3 complexity | b63bd476cf25cb2b1aab0b1d074630ef 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.jpa;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.FetchOptions;
 20import com.google.appengine.api.datastore.Query;
 21import com.google.appengine.api.datastore.QueryResultIterable;
 22import com.google.appengine.api.datastore.QueryResultList;
 23import com.google.appengine.datanucleus.JPADatastoreBridge;
 24import com.google.appengine.datanucleus.test.jpa.Book;
 25
 26
 27import java.util.List;
 28
 29/**
 30 * @author Max Ross <max.ross@gmail.com>
 31 */
 32public class JPADatastoreBridgeTest extends JPATestCase {
 33
 34  public void testConvertQueryResultList_Empty() {
 35    QueryResultList<Entity> result =
 36        ds.prepare(new Query("blarg")).asQueryResultList(FetchOptions.Builder.withLimit(10));
 37
 38    JPADatastoreBridge bridge = new JPADatastoreBridge();
 39    List<Book> books = bridge.toJPAResult(em, Book.class, result);
 40    assertEquals(0, books.size());
 41  }
 42
 43  public void testConvertQueryResultList() {
 44    for (int i = 0; i < 5; i++) {
 45      Entity e = Book.newBookEntity("harold" + i, "isbn", "the title", 2004);
 46      ds.put(e);
 47    }
 48    QueryResultList<Entity> result =
 49        ds.prepare(new Query("Book")).asQueryResultList(FetchOptions.Builder.withLimit(10));
 50
 51    JPADatastoreBridge bridge = new JPADatastoreBridge();
 52    List<Book> books = bridge.toJPAResult(em, Book.class, result);
 53    assertEquals(5, books.size());
 54    String id = books.get(0).getId();
 55    // make sure these books are connected
 56    beginTxn();
 57    books.get(0).setTitle("different title");
 58    commitTxn();
 59    beginTxn();
 60    Book f = em.find(Book.class, id);
 61    assertEquals("different title", f.getTitle());
 62    commitTxn();
 63    deleteAll();
 64  }
 65
 66  public void testConvertQueryResultIterable() {
 67    for (int i = 0; i < 5; i++) {
 68      Entity e = Book.newBookEntity("harold" + i, "isbn", "the title", 2004);
 69      ds.put(e);
 70    }
 71    QueryResultIterable<Entity> result =
 72        ds.prepare(new Query("Book")).asQueryResultIterable();
 73
 74    JPADatastoreBridge bridge = new JPADatastoreBridge();
 75    List<Book> books = bridge.toJPAResult(em, Book.class, result);
 76    assertEquals(5, books.size());
 77    String id = books.get(0).getId();
 78    // make sure these books are connected
 79    beginTxn();
 80    books.get(0).setTitle("different title");
 81    commitTxn();
 82    beginTxn();
 83    Book f = em.find(Book.class, id);
 84    assertEquals("different title", f.getTitle());
 85    commitTxn();
 86    deleteAll();
 87  }
 88
 89  public void testAccessResultsAfterClose() {
 90    for (int i = 0; i < 3; i++) {
 91      Entity e = Book.newBookEntity("this", "that", "the other");
 92      ds.put(e);
 93    }
 94    QueryResultIterable<Entity> result =
 95        ds.prepare(new Query("Book")).asQueryResultIterable();
 96    beginTxn();
 97    JPADatastoreBridge bridge = new JPADatastoreBridge();
 98    List<Book> books = bridge.toJPAResult(em, Book.class, result);
 99    commitTxn();
100    em.close();
101    assertEquals(3, books.size());
102    deleteAll();
103  }
104
105}