PageRenderTime 34ms CodeModel.GetById 16ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jdo/JDODatastoreBridgeTest.java

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