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

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

http://datanucleus-appengine.googlecode.com/
Java | 171 lines | 127 code | 25 blank | 19 comment | 0 complexity | 5ec745cb1fd1ea73bf6636dfcf75cf96 MD5 | raw file
  1/**********************************************************************
  2 Copyright (c) 2009 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.Entity;
 19import com.google.appengine.api.datastore.EntityNotFoundException;
 20import com.google.appengine.api.datastore.Key;
 21import com.google.appengine.api.datastore.KeyFactory;
 22import com.google.appengine.datanucleus.Utils;
 23import com.google.appengine.datanucleus.jdo.JDOTestCase;
 24import com.google.appengine.datanucleus.test.jdo.Flight;
 25import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
 26import com.google.appengine.datanucleus.test.jdo.HasOneToManyListJDO;
 27
 28import javax.jdo.JDOFatalUserException;
 29import javax.jdo.Query;
 30
 31/**
 32 * @author Max Ross <maxr@google.com>
 33 */
 34public class JDOQLDeleteTest extends JDOTestCase {
 35
 36  public void testDelete_Txn_MultipleEntityGroups() {
 37
 38    ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24, 25));
 39    ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24, 25));
 40
 41    Query q = pm.newQuery(Flight.class);
 42    beginTxn();
 43    try {
 44      q.deletePersistentAll();
 45      fail("expected exception");
 46    } catch (JDOFatalUserException e) {
 47      // good - can't delete books from multiple entity groups in a txn
 48    }
 49    rollbackTxn();
 50    assertEquals(2, countForClass(Flight.class));
 51  }
 52
 53  public void testDelete_Txn_OneEntityGroup() {
 54
 55    Key parentKey = KeyFactory.createKey("yar", 23);
 56    ds.put(Flight.newFlightEntity(parentKey, null, "jimmy", "bos", "mia", 23, 24, 25));
 57    ds.put(Flight.newFlightEntity(parentKey, null, "jimmy", "bos", "mia", 23, 24, 25));
 58
 59    Query q = pm.newQuery(Flight.class);
 60    beginTxn();
 61    assertEquals(2, q.deletePersistentAll());
 62    assertEquals(2, countForClass(Flight.class));
 63    commitTxn();
 64    assertEquals(0, countForClass(Flight.class));
 65  }
 66
 67  public void testDelete_NoTxn() {
 68    switchDatasource(PersistenceManagerFactoryName.nontransactional);
 69    ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24));
 70    ds.put(Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24));
 71
 72    Query q = pm.newQuery(Flight.class);
 73    assertEquals(2, q.deletePersistentAll());
 74    assertEquals(0, countForClass(Flight.class));
 75  }
 76
 77  public void testDeleteAncestorQuery_Txn() {
 78    Key parentKey = KeyFactory.createKey("yar", 23);
 79    Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
 80    Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
 81
 82    ds.put(pojo1);
 83    ds.put(pojo2);
 84
 85    Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
 86    beginTxn();
 87    assertEquals(2, q.deletePersistentAll(parentKey));
 88    commitTxn();
 89    assertEquals(0, countForClass(HasKeyAncestorKeyPkJDO.class));
 90  }
 91
 92  public void testDeleteAncestorQuery_TxnRollback() throws EntityNotFoundException {
 93    Key parentKey = KeyFactory.createKey("yar", 23);
 94    Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
 95    Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
 96
 97    ds.put(pojo1);
 98    ds.put(pojo2);
 99
100    Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
101    beginTxn();
102    assertEquals(2, q.deletePersistentAll(parentKey));
103    rollbackTxn();
104    assertEquals(2, countForClass(HasKeyAncestorKeyPkJDO.class));
105  }
106
107  public void testDeleteAncestorQuery_NoTxn() {
108    switchDatasource(PersistenceManagerFactoryName.nontransactional);
109    Key parentKey = KeyFactory.createKey("yar", 23);
110    Entity pojo1 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
111    Entity pojo2 = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentKey);
112
113    ds.put(pojo1);
114    ds.put(pojo2);
115
116    Query q = pm.newQuery(HasKeyAncestorKeyPkJDO.class, "ancestorKey == :p1");
117    assertEquals(2, q.deletePersistentAll(parentKey));
118    assertEquals(0, countForClass(HasKeyAncestorKeyPkJDO.class));
119  }
120
121  public void testBatchDelete_NoTxn() {
122    switchDatasource(PersistenceManagerFactoryName.nontransactional);
123    Entity e1 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
124    ds.put(e1);
125    Entity e2 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
126    ds.put(e2);
127    Entity e3 = Flight.newFlightEntity("jimmy", "bos", "mia", 23, 24);
128    ds.put(e3);
129
130    Key key = KeyFactory.createKey("yar", "does not exist");
131    Query q = pm.newQuery(Flight.class, "id == :ids");
132    assertEquals(2, q.deletePersistentAll(Utils.newArrayList(key, e1.getKey(), e2.getKey())));
133    assertEquals(1, countForClass(Flight.class));
134  }
135
136  public void testBatchDelete_Txn() {
137    Key parent = KeyFactory.createKey("yar", 23);
138    Entity e1 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
139    ds.put(e1);
140    Entity e2 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
141    ds.put(e2);
142    Entity e3 = Flight.newFlightEntity(parent, null, "jimmy", "bos", "mia", 23, 24, 25);
143    ds.put(e3);
144
145    beginTxn();
146    Query q = pm.newQuery(Flight.class, "id == :ids");
147    assertEquals(2, q.deletePersistentAll(Utils.newArrayList(parent, e1.getKey(), e2.getKey())));
148    assertEquals(3, countForClass(Flight.class));
149    commitTxn();
150    assertEquals(1, countForClass(Flight.class));
151  }
152
153  public void testDeleteCascades() {
154    HasOneToManyListJDO parent = new HasOneToManyListJDO();
155    Flight f = new Flight();
156    parent.getFlights().add(f);
157    beginTxn();
158    pm.makePersistent(parent);
159    commitTxn();
160    assertEquals(1, countForClass(Flight.class));
161    assertEquals(1, countForClass(HasOneToManyListJDO.class));
162    beginTxn();
163    Query q = pm.newQuery(HasOneToManyListJDO.class);
164    assertEquals(1, q.deletePersistentAll());
165    assertEquals(1, countForClass(Flight.class));
166    assertEquals(1, countForClass(HasOneToManyListJDO.class));
167    commitTxn();
168    assertEquals(0, countForClass(Flight.class));
169    assertEquals(0, countForClass(HasOneToManyListJDO.class));
170  }
171}