PageRenderTime 90ms CodeModel.GetById 33ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 329 lines | 276 code | 27 blank | 26 comment | 0 complexity | 1580e926fadeb769c6c298220b9d34ff MD5 | raw file
  1/*
  2 * /**********************************************************************
  3 * Copyright (c) 2009 Google Inc.
  4 *
  5 * Licensed under the Apache License, Version 2.0 (the "License");
  6 * you may not use this file except in compliance with the License.
  7 * You may obtain a copy of the License at
  8 *
  9 * http://www.apache.org/licenses/LICENSE-2.0
 10 *
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 * **********************************************************************/
 17package com.google.appengine.datanucleus.jdo;
 18
 19import com.google.appengine.api.datastore.Entity;
 20import com.google.appengine.api.datastore.EntityNotFoundException;
 21import com.google.appengine.api.datastore.KeyFactory;
 22import com.google.appengine.datanucleus.Utils;
 23import com.google.appengine.datanucleus.test.jdo.BidirectionalChildListJDO;
 24import com.google.appengine.datanucleus.test.jdo.DetachableJDO;
 25import com.google.appengine.datanucleus.test.jdo.DetachableWithMultiValuePropsJDO;
 26import com.google.appengine.datanucleus.test.jdo.Flight;
 27import com.google.appengine.datanucleus.test.jdo.HasOneToManyListJDO;
 28
 29import java.io.ByteArrayInputStream;
 30import java.io.ByteArrayOutputStream;
 31import java.io.IOException;
 32import java.io.ObjectInputStream;
 33import java.io.ObjectOutputStream;
 34import java.io.Serializable;
 35import java.util.Date;
 36import java.util.List;
 37
 38import javax.jdo.JDOHelper;
 39import javax.jdo.JDOObjectNotFoundException;
 40import javax.jdo.ObjectState;
 41
 42/**
 43 * @author Max Ross <maxr@google.com>
 44 */
 45public class JDOAttachDetachTest extends JDOTestCase {
 46
 47  private <T extends Serializable> T toBytesAndBack(T obj)
 48      throws IOException, ClassNotFoundException {
 49    ByteArrayOutputStream baos = new ByteArrayOutputStream();
 50    ObjectOutputStream oos = new ObjectOutputStream(baos);
 51    oos.writeObject(obj);
 52
 53    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
 54    ObjectInputStream ois = new ObjectInputStream(bais);
 55    return (T) ois.readObject();
 56  }
 57
 58  public void testSimpleSerializeWithTxns()
 59      throws IOException, ClassNotFoundException, EntityNotFoundException {
 60    pm.setDetachAllOnCommit(true);
 61    beginTxn();
 62    DetachableJDO pojo = new DetachableJDO();
 63    pojo.setVal("yar");
 64    Date now = new Date();
 65    pojo.setDate(now);
 66    pm.makePersistent(pojo);
 67    commitTxn();
 68    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
 69    assertEquals(Date.class, pojo.getDate().getClass());
 70    pm.close();
 71    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
 72    pm = pmf.getPersistenceManager();
 73    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
 74
 75    pojo = toBytesAndBack(pojo);
 76
 77    assertEquals("yar", pojo.getVal());
 78    assertEquals(now, pojo.getDate());
 79    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
 80    beginTxn();
 81    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
 82    pojo.setVal("not yar");
 83    Date newDate = new Date(pojo.getDate().getTime() + 1);
 84    pojo.getDate().setTime(newDate.getTime());
 85    assertEquals(ObjectState.DETACHED_DIRTY, JDOHelper.getObjectState(pojo));
 86    pm.makePersistent(pojo);
 87    commitTxn();
 88    Entity e = ds.get(KeyFactory.createKey(DetachableJDO.class.getSimpleName(), pojo.getId()));
 89    assertEquals("not yar", e.getProperty("val"));
 90    assertEquals(newDate, e.getProperty("date"));
 91  }
 92
 93  public void testSimpleSerializeWithoutTxns() throws Exception {
 94    switchDatasource(PersistenceManagerFactoryName.nontransactional);
 95    pm.setDetachAllOnCommit(true);
 96    DetachableJDO pojo = new DetachableJDO();
 97    pojo.setVal("yar");
 98    Date now = new Date();
 99    pojo.setDate(now);
100    pm.makePersistent(pojo);
101
102    // DN3 changes this from P_NEW to DETACHED_CLEAN since detachAllOnCommit
103    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
104
105    pm.close();
106    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
107    assertEquals(Date.class, pojo.getDate().getClass());
108    pm = pmf.getPersistenceManager();
109    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
110
111    pojo = toBytesAndBack(pojo);
112
113    assertEquals("yar", pojo.getVal());
114    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
115    pojo.setVal("not yar");
116    Date newDate = new Date(pojo.getDate().getTime() + 1);
117    pojo.getDate().setTime(newDate.getTime());
118    assertEquals(ObjectState.DETACHED_DIRTY, JDOHelper.getObjectState(pojo));
119    pm.makePersistent(pojo);
120    pm.close();
121    Entity e = ds.get(KeyFactory.createKey(DetachableJDO.class.getSimpleName(), pojo.getId()));
122    assertEquals("not yar", e.getProperty("val"));
123    assertEquals(newDate, e.getProperty("date"));
124  }
125
126  public void testSerializeWithMultiValueProps() throws Exception {
127    pm.setDetachAllOnCommit(true);
128    beginTxn();
129    DetachableWithMultiValuePropsJDO pojo = new DetachableWithMultiValuePropsJDO();
130    pojo.setStrList(Utils.newArrayList("c", "d"));
131    pm.makePersistent(pojo);
132    commitTxn();
133    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
134    pm.close();
135    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
136    pm = pmf.getPersistenceManager();
137    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
138
139    pojo = toBytesAndBack(pojo);
140
141    assertEquals(Utils.newArrayList("c", "d"), pojo.getStrList());
142    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
143    beginTxn();
144    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
145    // reattach to the pm - this turns our regular list field into a managed
146    // list field
147    pojo = pm.makePersistent(pojo);
148    assertEquals(ObjectState.PERSISTENT_CLEAN, JDOHelper.getObjectState(pojo));
149    pojo.getStrList().add("e");
150    commitTxn();
151    Entity e = ds.get(KeyFactory.createKey(DetachableWithMultiValuePropsJDO.class.getSimpleName(), pojo.getId()));
152    assertEquals(3, ((List<String>)e.getProperty("strList")).size());
153    assertEquals(Utils.newArrayList("c", "d", "e"), e.getProperty("strList"));
154  }
155
156  public void testSerializeWithOneToMany_AddBidirectionalChildToDetached() throws Exception {
157    pm.setDetachAllOnCommit(true);
158    beginTxn();
159    HasOneToManyListJDO pojo = new HasOneToManyListJDO();
160    pojo.setVal("yar");
161    BidirectionalChildListJDO bidir = new BidirectionalChildListJDO();
162    bidir.setChildVal("yar2");
163    pojo.addBidirChild(bidir);
164    pm.makePersistent(pojo);
165    commitTxn();
166    pm.close();
167    pm = pmf.getPersistenceManager();
168
169    pojo = toBytesAndBack(pojo);
170    assertEquals("yar", pojo.getVal());
171    assertEquals(1, pojo.getBidirChildren().size());
172    BidirectionalChildListJDO bidir2 = new BidirectionalChildListJDO();
173    bidir2.setChildVal("yar3");
174    pojo.addBidirChild(bidir2);
175    bidir2.setParent(pojo);
176    beginTxn();
177    pojo = pm.makePersistent(pojo);
178    commitTxn();
179    Entity e = ds.get(KeyFactory.stringToKey(bidir2.getId()));
180    assertEquals(KeyFactory.stringToKey(pojo.getId()), e.getKey().getParent());
181  }
182
183  public void testSerializeWithOneToMany_AddUnidirectionalChildToDetached() throws Exception {
184    pm.setDetachAllOnCommit(true);
185    beginTxn();
186    HasOneToManyListJDO pojo = new HasOneToManyListJDO();
187    pojo.setVal("yar");
188    Flight flight = new Flight();
189    flight.setName("harry");
190    pojo.addFlight(flight);
191    pm.makePersistent(pojo);
192    commitTxn();
193    pm.close();
194    pm = pmf.getPersistenceManager();
195
196    pojo = toBytesAndBack(pojo);
197    assertEquals("yar", pojo.getVal());
198    assertEquals(1, pojo.getFlights().size());
199    Flight flight2 = new Flight();
200    flight2.setName("not harry");
201    pojo.addFlight(flight2);
202    beginTxn();
203    pojo = pm.makePersistent(pojo);
204    commitTxn();
205    Entity e = ds.get(KeyFactory.stringToKey(flight2.getId()));
206    assertEquals(KeyFactory.stringToKey(pojo.getId()), e.getKey().getParent());
207  }
208
209  public void testSerializeWithOneToMany_AddChildToReattached() throws Exception {
210    pm.setDetachAllOnCommit(true);
211    beginTxn();
212    HasOneToManyListJDO pojo = new HasOneToManyListJDO();
213    pojo.setVal("yar");
214    BidirectionalChildListJDO bidir = new BidirectionalChildListJDO();
215    bidir.setChildVal("yar2");
216    pojo.addBidirChild(bidir);
217    pm.makePersistent(pojo);
218    commitTxn();
219    pm.close();
220    pm = pmf.getPersistenceManager();
221
222    pojo = toBytesAndBack(pojo);
223    assertEquals("yar", pojo.getVal());
224    assertEquals(1, pojo.getBidirChildren().size());
225    beginTxn();
226    pojo = pm.makePersistent(pojo);
227    BidirectionalChildListJDO bidir2 = new BidirectionalChildListJDO();
228    bidir.setChildVal("yar3");
229    pojo.addBidirChild(bidir2);
230    bidir2.setParent(pojo);
231    commitTxn();
232    Entity e = ds.get(KeyFactory.stringToKey(bidir2.getId()));
233    assertEquals(KeyFactory.stringToKey(pojo.getId()), e.getKey().getParent());
234  }
235
236  public void testDeleteDetachedObject_NoTxn() {
237    switchDatasource(PersistenceManagerFactoryName.nontransactional);
238    DetachableJDO pojo = new DetachableJDO();
239    pojo.setVal("yar");
240    Date now = new Date();
241    pojo.setDate(now);
242    pm.makePersistent(pojo);
243    pm.close();
244    pm = pmf.getPersistenceManager();
245    pojo = pm.detachCopy(pm.getObjectById(pojo.getClass(), pojo.getId()));
246    pm.close();
247    pm = pmf.getPersistenceManager();
248    pm.deletePersistent(pojo);
249    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
250    pm.close();
251    pm = pmf.getPersistenceManager();
252    try {
253      pm.getObjectById(pojo.getClass(), pojo.getId());
254      fail("expected exception");
255    } catch (JDOObjectNotFoundException e) {
256      // good
257    }
258  }
259
260  public void testDeleteDetachedNewObject_NoTxn() {
261    switchDatasource(PersistenceManagerFactoryName.nontransactional);
262    DetachableJDO pojo = new DetachableJDO();
263    pojo.setVal("yar");
264    Date now = new Date();
265    pojo.setDate(now);
266    pm.makePersistent(pojo);
267    pojo = pm.detachCopy(pojo);
268    pm.close();
269    pm = pmf.getPersistenceManager();
270    pm.deletePersistent(pojo);
271    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
272    pm.close();
273    pm = pmf.getPersistenceManager();
274    try {
275      pm.getObjectById(pojo.getClass(), pojo.getId());
276      fail("expected exception");
277    } catch (JDOObjectNotFoundException e) {
278      // good
279    }
280  }
281
282  public void testDeleteDetachedObject_Txn() {
283    beginTxn();
284    DetachableJDO pojo = new DetachableJDO();
285    pojo.setVal("yar");
286    Date now = new Date();
287    pojo.setDate(now);
288    pm.makePersistent(pojo);
289    commitTxn();
290    beginTxn();
291    pojo = pm.detachCopy(pm.getObjectById(pojo.getClass(), pojo.getId()));
292    commitTxn();
293    beginTxn();
294    pm.deletePersistent(pojo);
295    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
296    commitTxn();
297    beginTxn();
298    try {
299      pm.getObjectById(pojo.getClass(), pojo.getId());
300      fail("expected exception");
301    } catch (JDOObjectNotFoundException e) {
302      // good
303    }
304    rollbackTxn();
305  }
306
307  public void testDeleteDetachedNewObject_Txn() {
308    beginTxn();
309    DetachableJDO pojo = new DetachableJDO();
310    pojo.setVal("yar");
311    Date now = new Date();
312    pojo.setDate(now);
313    pm.makePersistent(pojo);
314    pojo = pm.detachCopy(pojo);
315    commitTxn();
316    beginTxn();
317    pm.deletePersistent(pojo);
318    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
319    commitTxn();
320    beginTxn();
321    try {
322      pm.getObjectById(pojo.getClass(), pojo.getId());
323      fail("expected exception");
324    } catch (JDOObjectNotFoundException e) {
325      // good
326    }
327    rollbackTxn();
328  }
329}