PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 241 lines | 180 code | 27 blank | 34 comment | 0 complexity | fee6a5915dbd76e5e8408dc185b9b3c9 MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.jdo;
 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.test.jdo.Flight;
 23import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
 24import com.google.appengine.datanucleus.test.jdo.HasVersionWithFieldJDO;
 25import com.google.appengine.datanucleus.test.jdo.KitchenSink;
 26
 27import javax.jdo.JDOHelper;
 28import javax.jdo.JDOObjectNotFoundException;
 29import javax.jdo.JDOOptimisticVerificationException;
 30
 31/**
 32 * @author Max Ross <maxr@google.com>
 33 */
 34public class JDODeleteTest extends JDOTestCase {
 35
 36  private static final String DEFAULT_VERSION_PROPERTY_NAME = "VERSION";
 37
 38  public void testSimpleDelete() {
 39    Key key = ds.put(KitchenSink.newKitchenSinkEntity(null));
 40
 41    String keyStr = KeyFactory.keyToString(key);
 42    beginTxn();
 43    KitchenSink ks = pm.getObjectById(KitchenSink.class, keyStr);
 44    assertNotNull(ks);
 45    pm.deletePersistent(ks);
 46    commitTxn();
 47    beginTxn();
 48    try {
 49      pm.getObjectById(KitchenSink.class, keyStr);
 50      fail("expected onfe");
 51    } catch (JDOObjectNotFoundException onfe) {
 52      // good
 53    } finally {
 54      rollbackTxn();
 55    }
 56  }
 57
 58  public void testSimpleDelete_NamedKey() {
 59    Key key = ds.put(KitchenSink.newKitchenSinkEntity("named key", null));
 60
 61    String keyStr = KeyFactory.keyToString(key);
 62    beginTxn();
 63    KitchenSink ks = pm.getObjectById(KitchenSink.class, keyStr);
 64    assertNotNull(ks);
 65    assertEquals("named key", KeyFactory.stringToKey(ks.key).getName());
 66    pm.deletePersistent(ks);
 67    commitTxn();
 68    beginTxn();
 69    try {
 70      pm.getObjectById(KitchenSink.class, keyStr);
 71      fail("expected onfe");
 72    } catch (JDOObjectNotFoundException onfe) {
 73      // good
 74    } finally {
 75      rollbackTxn();
 76    }
 77  }
 78
 79  public void testOptimisticLocking_Update_NoField() {
 80    switchDatasource(PersistenceManagerFactoryName.nontransactional);
 81    Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
 82    Key key = ds.put(flightEntity);
 83
 84    String keyStr = KeyFactory.keyToString(key);
 85    beginTxn();
 86    Flight flight = pm.getObjectById(Flight.class, keyStr);
 87
 88    flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
 89    // we update the flight directly in the datastore right before we delete
 90    ds.put(flightEntity);
 91    try {
 92      pm.deletePersistent(flight);
 93      fail("expected optimistic exception");
 94    } catch (JDOOptimisticVerificationException jove) {
 95      // good
 96    } finally {
 97      rollbackTxn();
 98    }
 99  }
100
101  public void testOptimisticLocking_Delete_NoField() {
102    switchDatasource(PersistenceManagerFactoryName.nontransactional);
103    Entity flightEntity = Flight.newFlightEntity("1", "yam", "bam", 1, 2);
104    Key key = ds.put(flightEntity);
105
106    String keyStr = KeyFactory.keyToString(key);
107    beginTxn();
108    Flight flight = pm.getObjectById(Flight.class, keyStr);
109
110    flight.setName("2");
111    flightEntity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 2L);
112    // we remove the flight from the datastore right before delete
113    ds.delete(key);
114    try {
115      pm.deletePersistent(flight);
116      fail("expected optimistic exception");
117    } catch (JDOOptimisticVerificationException jove) {
118      // good
119    } finally {
120      rollbackTxn();
121    }
122  }
123
124  public void testOptimisticLocking_Update_HasVersionField() {
125    switchDatasource(PersistenceManagerFactoryName.nontransactional);
126    Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
127    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
128    Key key = ds.put(entity);
129
130    String keyStr = KeyFactory.keyToString(key);
131    beginTxn();
132    HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
133
134    hvwf.setValue("value");
135    commitTxn();
136    beginTxn();
137    hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
138    assertEquals(2L, hvwf.getVersion());
139    // make sure the version gets bumped
140    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 3L);
141
142    // we update the entity directly in the datastore right before delete
143    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 7L);
144    ds.put(entity);
145    try {
146      pm.deletePersistent(hvwf);
147      fail("expected optimistic exception");
148    } catch (JDOOptimisticVerificationException jove) {
149      // good
150    } finally {
151      rollbackTxn();
152    }
153    // make sure the version didn't change on the model object
154    assertEquals(2L, JDOHelper.getVersion(hvwf));
155  }
156
157  public void testOptimisticLocking_Delete_HasVersionField() {
158    switchDatasource(PersistenceManagerFactoryName.nontransactional);
159    Entity entity = new Entity(HasVersionWithFieldJDO.class.getSimpleName());
160    entity.setProperty(DEFAULT_VERSION_PROPERTY_NAME, 1L);
161    Key key = ds.put(entity);
162
163    String keyStr = KeyFactory.keyToString(key);
164    beginTxn();
165    HasVersionWithFieldJDO hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, keyStr);
166
167    // delete the entity in the datastore right before we delete
168    ds.delete(key);
169    try {
170      pm.deletePersistent(hvwf);
171      fail("expected optimistic exception");
172    } catch (JDOOptimisticVerificationException jove) {
173      // good
174    } finally {
175      rollbackTxn();
176    }
177
178    // make sure the version didn't change on the model object
179    assertEquals(1L, JDOHelper.getVersion(hvwf));
180  }
181
182  public void testNonTransactionalDelete() throws EntityNotFoundException {
183    switchDatasource(PersistenceManagerFactoryName.nontransactional);
184
185    Key key = ds.put(Flight.newFlightEntity("1", "yam", "bam", 1, 2));
186    Flight f = pm.getObjectById(Flight.class, KeyFactory.keyToString(key));
187    pm.deletePersistent(f);
188    pm.close();
189    try {
190      ds.get(key);
191      fail("expected enfe");
192    } catch (EntityNotFoundException enfe) {
193      // good
194    }
195    pm = pmf.getPersistenceManager();
196  }
197
198  public void testDeleteHasAncestorPkField() {
199    Entity e =
200        new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), KeyFactory.createKey("Yam", 24));
201    ds.put(e);
202    beginTxn();
203    HasKeyAncestorKeyPkJDO pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, e.getKey());
204    pm.deletePersistent(pojo);
205    commitTxn();
206  }
207
208  public void testDeletePersistentNew() {
209    beginTxn();
210    KitchenSink ks = KitchenSink.newKitchenSink();
211    pm.makePersistent(ks);
212    String keyStr = ks.key;
213    pm.deletePersistent(ks);
214    commitTxn();
215    beginTxn();
216    try {
217      pm.getObjectById(KitchenSink.class, keyStr);
218      fail("expected onfe");
219    } catch (JDOObjectNotFoundException onfe) {
220      // good
221    } finally {
222      rollbackTxn();
223    }
224  }
225
226  public void testDeletePersistentNew_NoTxn() {
227    switchDatasource(PersistenceManagerFactoryName.nontransactional);
228    KitchenSink ks = KitchenSink.newKitchenSink();
229    pm.makePersistent(ks);
230    String keyStr = ks.key;
231    pm.deletePersistent(ks);
232    pm.close();
233    pm = pmf.getPersistenceManager();
234    try {
235      pm.getObjectById(KitchenSink.class, keyStr);
236      fail("expected onfe");
237    } catch (JDOObjectNotFoundException onfe) {
238      // good
239    }
240  }
241}