PageRenderTime 57ms CodeModel.GetById 35ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 287 lines | 215 code | 45 blank | 27 comment | 0 complexity | 485643e78f1e161dc30f82112d0fdab2 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.KeyFactory;
 21import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
 22import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
 23import com.google.appengine.datanucleus.test.jdo.HasOneToOneJDO;
 24import com.google.appengine.datanucleus.test.jdo.HasOneToOneParentJDO;
 25import com.google.appengine.datanucleus.test.jdo.HasOneToOnesWithDifferentCascadesJDO;
 26import com.google.appengine.datanucleus.test.jdo.HasStringAncestorKeyPkJDO;
 27import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
 28
 29import static com.google.appengine.datanucleus.TestUtils.assertKeyParentEquals;
 30
 31import javax.jdo.JDOFatalUserException;
 32
 33/**
 34 * @author Max Ross <maxr@google.com>
 35 */
 36public class JDOImplicitEntityGroupTest extends JDOTestCase {
 37
 38  public void testOneToOnePersistCascadeAll() throws EntityNotFoundException {
 39    HasOneToOneJDO parent = new HasOneToOneJDO();
 40    HasOneToOneParentJDO child = new HasOneToOneParentJDO();
 41    parent.setHasParent(child);
 42    child.setParent(parent);
 43
 44    beginTxn();
 45    pm.makePersistent(parent);
 46    commitTxn();
 47
 48    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
 49    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 50
 51    beginTxn();
 52    parent = pm.getObjectById(HasOneToOneJDO.class, parent.getId());
 53    assertNotNull(parent.getHasParent());
 54    assertNotNull(parent.getHasParent().getParent());
 55    commitTxn();
 56  }
 57
 58  public void testOneToOnePersistChildWithAncestorCascadeAll() throws EntityNotFoundException {
 59    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
 60    HasStringAncestorStringPkJDO child = new HasStringAncestorStringPkJDO();
 61    parent.setCascadeAllChild(child);
 62
 63    beginTxn();
 64    pm.makePersistent(parent);
 65    commitTxn();
 66
 67    beginTxn();
 68    parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
 69    assertNotNull(parent.getCascadeAllChild());
 70    child = pm.getObjectById(HasStringAncestorStringPkJDO.class, child.getId());
 71    assertEquals(parent.getId(), child.getAncestorId());
 72    commitTxn();
 73
 74    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
 75    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 76  }
 77
 78  public void testOneToOnePersistChildWithKeyAncestorCascadeAll() throws EntityNotFoundException {
 79    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
 80    HasKeyAncestorStringPkJDO child = new HasKeyAncestorStringPkJDO();
 81    parent.setCascadeAllChildWithKeyAncestor(child);
 82
 83    beginTxn();
 84    pm.makePersistent(parent);
 85    commitTxn();
 86
 87    beginTxn();
 88    parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
 89    child = pm.getObjectById(HasKeyAncestorStringPkJDO.class, child.getKey());
 90    assertEquals(parent.getId(), KeyFactory.keyToString(child.getAncestorKey()));
 91    assertNotNull(parent.getCascadeAllChildWithKeyAncestor());
 92    commitTxn();
 93
 94    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
 95    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 96  }
 97
 98  public void testOneToOnePersistCascadePersist() throws EntityNotFoundException {
 99    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
100    HasStringAncestorKeyPkJDO child = new HasStringAncestorKeyPkJDO();
101    parent.setCascadePersistChild(child);
102
103    beginTxn();
104    pm.makePersistent(parent);
105    commitTxn();
106
107    beginTxn();
108    parent = pm.getObjectById(HasOneToOnesWithDifferentCascadesJDO.class, parent.getId());
109    child = pm.getObjectById(HasStringAncestorKeyPkJDO.class, child.getKey());
110    assertEquals(parent.getId(), child.getAncestorKey());
111    assertNotNull(parent.getCascadePersistChild());
112    commitTxn();
113
114    Entity childEntity = ds.get(child.getKey());
115    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
116  }
117
118  public void testOneToOnePersistCascadeRemove() throws EntityNotFoundException {
119    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
120    HasKeyAncestorKeyPkJDO child = new HasKeyAncestorKeyPkJDO();
121    parent.setCascadeRemoveChild(child);
122
123    beginTxn();
124    pm.makePersistent(child);
125    commitTxn();
126    beginTxn();
127    try {
128      // this fails because it attempts to establish a parent
129      // for an entity that was originally persisted without a parent.
130      pm.makePersistent(parent);
131      fail("expected exception");
132    } catch (JDOFatalUserException e) {
133      // good
134      rollbackTxn();
135    }
136  }
137
138  public void testOneToOnePersistCascadeAll_OverrideParentManually() {
139    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
140
141    HasOneToOnesWithDifferentCascadesJDO anotherParent = new HasOneToOnesWithDifferentCascadesJDO();
142    beginTxn();
143    pm.makePersistent(anotherParent);
144    commitTxn();
145
146    HasStringAncestorStringPkJDO child = new HasStringAncestorStringPkJDO(anotherParent.getId());
147    parent.setCascadeAllChild(child);
148
149    beginTxn();
150    try {
151      pm.makePersistent(parent);
152      fail("expected exception");
153    } catch (JDOFatalUserException e) {
154      // good
155    } finally {
156      rollbackTxn();
157    }
158  }
159
160  public void testOneToOnePersistCascadePersist_OverrideParentManually() {
161    HasOneToOnesWithDifferentCascadesJDO parent = new HasOneToOnesWithDifferentCascadesJDO();
162
163    HasOneToOnesWithDifferentCascadesJDO anotherParent = new HasOneToOnesWithDifferentCascadesJDO();
164    beginTxn();
165    pm.makePersistent(anotherParent);
166    commitTxn();
167
168    HasStringAncestorKeyPkJDO child = new HasStringAncestorKeyPkJDO();
169    child.setAncestorKey(anotherParent.getId());
170    parent.setCascadePersistChild(child);
171
172    beginTxn();
173    try {
174      pm.makePersistent(parent);
175      fail("expected exception");
176    } catch (JDOFatalUserException e) {
177      // good
178    } finally {
179      rollbackTxn();
180    }
181  }
182
183  public void testOneToOneRemoveParentCascadeAll() {
184    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
185    ds.put(parentEntity);
186    Entity childEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentEntity.getKey());
187    ds.put(childEntity);
188    parentEntity.setProperty("cascadeall", childEntity.getKey());
189    ds.put(parentEntity);
190
191    beginTxn();
192    HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
193        HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
194    assertNotNull(parent.getCascadeAllChild());
195    pm.deletePersistent(parent);
196    commitTxn();
197
198    try {
199      ds.get(parentEntity.getKey());
200      fail("expected enfe");
201    } catch (EntityNotFoundException e) {
202      // good
203    }
204
205    try {
206      ds.get(childEntity.getKey());
207      fail("expected enfe");
208    } catch (EntityNotFoundException e) {
209      // good
210    }
211  }
212
213  public void testOneToOneRemoveChildCascadeAll() throws EntityNotFoundException {
214    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
215    ds.put(parentEntity);
216    Entity childEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), parentEntity.getKey());
217    ds.put(childEntity);
218    parentEntity.setProperty("cascadeall", childEntity.getKey());
219    ds.put(parentEntity);
220
221    beginTxn();
222    HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
223        HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
224    assertNotNull(parent.getCascadeAllChild());
225    assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeAllChild().getId());
226    pm.deletePersistent(parent.getCascadeAllChild());
227    commitTxn();
228
229    parentEntity = ds.get(parentEntity.getKey());
230    assertEquals(childEntity.getKey(), parentEntity.getProperty("cascadeall"));
231    try {
232      ds.get(childEntity.getKey());
233      fail("expected enfe");
234    } catch (EntityNotFoundException e) {
235      // good
236    }
237  }
238
239  public void testOneToOneRemoveChildCascadeRemove() throws EntityNotFoundException {
240    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJDO.class.getSimpleName());
241    ds.put(parentEntity);
242    Entity childEntity = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), parentEntity.getKey());
243    ds.put(childEntity);
244    parentEntity.setProperty("cascaderemove", childEntity.getKey());
245    ds.put(parentEntity);
246
247    beginTxn();
248    HasOneToOnesWithDifferentCascadesJDO parent = pm.getObjectById(
249        HasOneToOnesWithDifferentCascadesJDO.class, KeyFactory.keyToString(parentEntity.getKey()));
250    assertNotNull(parent.getCascadeRemoveChild());
251    assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeRemoveChild().getKey());
252    pm.deletePersistent(parent.getCascadeRemoveChild());
253    commitTxn();
254
255    parentEntity = ds.get(parentEntity.getKey());
256    assertEquals(childEntity.getKey(), parentEntity.getProperty("cascaderemove"));
257    try {
258      ds.get(childEntity.getKey());
259      fail("expected enfe");
260    } catch (EntityNotFoundException e) {
261      // good
262    }
263  }
264
265  public void testChildGoesIntoEntityGroupOfExistingParent() throws EntityNotFoundException {
266    HasOneToOneJDO parent = new HasOneToOneJDO();
267
268    beginTxn();
269    pm.makePersistent(parent);
270    commitTxn();
271
272    beginTxn();
273    HasOneToOneParentJDO child = new HasOneToOneParentJDO();
274    parent.setHasParent(child);
275    child.setParent(parent);
276
277    pm.makePersistent(parent);
278    commitTxn();
279    beginTxn();
280    parent = pm.getObjectById(HasOneToOneJDO.class, parent.getId());
281    assertNotNull(parent.getHasParent());
282    commitTxn();
283
284    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getKey()));
285    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
286  }
287}