PageRenderTime 49ms CodeModel.GetById 1ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jpa/JPAImplicitEntityGroupTest.java

http://datanucleus-appengine.googlecode.com/
Java | 255 lines | 185 code | 45 blank | 25 comment | 0 complexity | e6418c015b47ad718b74bfa125e94d61 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.jpa;
 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.jpa.HasKeyAncestorKeyPkJPA;
 22import com.google.appengine.datanucleus.test.jpa.HasOneToOneJPA;
 23import com.google.appengine.datanucleus.test.jpa.HasOneToOneParentJPA;
 24import com.google.appengine.datanucleus.test.jpa.HasOneToOnesWithDifferentCascadesJPA;
 25import com.google.appengine.datanucleus.test.jpa.HasStringAncestorKeyPkJPA;
 26import com.google.appengine.datanucleus.test.jpa.HasStringAncestorStringPkJPA;
 27
 28import static com.google.appengine.datanucleus.TestUtils.assertKeyParentEquals;
 29import static com.google.appengine.datanucleus.TestUtils.assertKeyParentNull;
 30
 31
 32import javax.persistence.PersistenceException;
 33
 34/**
 35 * @author Max Ross <maxr@google.com>
 36 */
 37public class JPAImplicitEntityGroupTest extends JPATestCase {
 38
 39  public void testOneToOnePersistCascadeAll() throws EntityNotFoundException {
 40    HasOneToOneJPA parent = new HasOneToOneJPA();
 41    HasOneToOneParentJPA child = new HasOneToOneParentJPA();
 42    parent.setHasParent(child);
 43    child.setParent(parent);
 44
 45    beginTxn();
 46    em.persist(parent);
 47    commitTxn();
 48
 49    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
 50    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 51
 52    ds.get(KeyFactory.stringToKey(parent.getId()));
 53  }
 54
 55  public void testOneToOnePersistChildWithAncestorCascadeAll() throws EntityNotFoundException {
 56    HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
 57    HasStringAncestorStringPkJPA child = new HasStringAncestorStringPkJPA();
 58    parent.setCascadeAllChild(child);
 59
 60    beginTxn();
 61    em.persist(parent);
 62    commitTxn();
 63
 64    assertEquals(parent.getId(), child.getAncestorId());
 65
 66    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
 67    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 68
 69    ds.get(KeyFactory.stringToKey(parent.getId()));
 70  }
 71
 72  public void testOneToOnePersistCascadePersist() throws EntityNotFoundException {
 73    HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
 74    HasStringAncestorKeyPkJPA child = new HasStringAncestorKeyPkJPA();
 75    parent.setCascadePersistChild(child);
 76
 77    beginTxn();
 78    em.persist(parent);
 79    assertEquals(parent.getId(), child.getAncestorKey());
 80    commitTxn();
 81
 82    Entity childEntity = ds.get(child.getKey());
 83    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
 84  }
 85
 86  public void testOneToOnePersistCascadeRemove() throws EntityNotFoundException {
 87    switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_not_allowed);
 88
 89    HasKeyAncestorKeyPkJPA child = new HasKeyAncestorKeyPkJPA();
 90
 91    beginTxn();
 92    em.persist(child);
 93    commitTxn();
 94    HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
 95    parent.setCascadeRemoveChild(child);
 96    beginTxn();
 97    em.persist(parent);
 98    try {
 99      commitTxn();
100      fail("expected exception");
101    } catch (PersistenceException e) {
102      // good
103      rollbackTxn();
104    }
105
106    Entity childEntity = ds.get(child.getKey());
107    assertKeyParentNull(childEntity, childEntity.getKey());
108  }
109
110  public void testOneToOnePersistCascadeAll_OverrideParentManually() {
111    HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
112
113    HasOneToOnesWithDifferentCascadesJPA anotherParent = new HasOneToOnesWithDifferentCascadesJPA();
114    beginTxn();
115    em.persist(anotherParent);
116    commitTxn();
117
118    HasStringAncestorStringPkJPA child = new HasStringAncestorStringPkJPA(anotherParent.getId());
119    parent.setCascadeAllChild(child);
120
121    beginTxn();
122    em.persist(parent);
123    try {
124      commitTxn();
125      fail("expected exception");
126    } catch (PersistenceException e) {
127      // good
128    }
129  }
130
131  public void testOneToOnePersistCascadePersist_OverrideParentManually() {
132    HasOneToOnesWithDifferentCascadesJPA parent = new HasOneToOnesWithDifferentCascadesJPA();
133
134    HasOneToOnesWithDifferentCascadesJPA anotherParent = new HasOneToOnesWithDifferentCascadesJPA();
135    beginTxn();
136    em.persist(anotherParent);
137    commitTxn();
138
139    HasStringAncestorKeyPkJPA child = new HasStringAncestorKeyPkJPA();
140    child.setAncestorKey(anotherParent.getId());
141    parent.setCascadePersistChild(child);
142
143    beginTxn();
144    em.persist(parent);
145    try {
146      commitTxn();
147      fail("expected exception");
148    } catch (PersistenceException e) {
149      // good
150    }
151  }
152
153  public void testOneToOneRemoveParentCascadeAll() {
154    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
155    ds.put(parentEntity);
156    Entity childEntity = new Entity(HasStringAncestorStringPkJPA.class.getSimpleName(), parentEntity.getKey());
157    ds.put(childEntity);
158    parentEntity.setProperty("cascadeall", childEntity.getKey());
159    ds.put(parentEntity);
160
161    beginTxn();
162    HasOneToOnesWithDifferentCascadesJPA parent = em.find(
163        HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
164    assertNotNull(parent.getCascadeAllChild());
165    em.remove(parent);
166    commitTxn();
167
168    try {
169      ds.get(parentEntity.getKey());
170      fail("expected enfe");
171    } catch (EntityNotFoundException e) {
172      // good
173    }
174
175    try {
176      ds.get(childEntity.getKey());
177      fail("expected enfe");
178    } catch (EntityNotFoundException e) {
179      // good
180    }
181  }
182
183  public void testOneToOneRemoveChildCascadeAll() throws EntityNotFoundException {
184    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
185    ds.put(parentEntity);
186    Entity childEntity = new Entity(HasStringAncestorStringPkJPA.class.getSimpleName(), parentEntity.getKey());
187    ds.put(childEntity);
188    parentEntity.setProperty("cascadeall", childEntity.getKey());
189    ds.put(parentEntity);
190
191    beginTxn();
192    HasOneToOnesWithDifferentCascadesJPA parent = em.find(
193        HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
194    assertNotNull(parent.getCascadeAllChild());
195    assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeAllChild().getId());
196    em.remove(parent.getCascadeAllChild());
197    commitTxn();
198
199    parentEntity = ds.get(parentEntity.getKey());
200    assertEquals(childEntity.getKey(), parentEntity.getProperty("cascadeall"));
201    try {
202      ds.get(childEntity.getKey());
203      fail("expected enfe");
204    } catch (EntityNotFoundException e) {
205      // good
206    }
207  }
208
209  public void testOneToOneRemoveChildCascadeRemove() throws EntityNotFoundException {
210    Entity parentEntity = new Entity(HasOneToOnesWithDifferentCascadesJPA.class.getSimpleName());
211    ds.put(parentEntity);
212    Entity childEntity = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentEntity.getKey());
213    ds.put(childEntity);
214    parentEntity.setProperty("cascaderemove", childEntity.getKey());
215    ds.put(parentEntity);
216
217    beginTxn();
218    HasOneToOnesWithDifferentCascadesJPA parent = em.find(
219        HasOneToOnesWithDifferentCascadesJPA.class, KeyFactory.keyToString(parentEntity.getKey()));
220    assertNotNull(parent.getCascadeRemoveChild());
221    assertKeyParentEquals(parent.getId(), childEntity, parent.getCascadeRemoveChild().getKey());
222    em.remove(parent.getCascadeRemoveChild());
223    commitTxn();
224
225    parentEntity = ds.get(parentEntity.getKey());
226    assertEquals(childEntity.getKey(), parentEntity.getProperty("cascaderemove"));
227    try {
228      ds.get(childEntity.getKey());
229      fail("expected enfe");
230    } catch (EntityNotFoundException e) {
231      // good
232    }
233  }
234
235  public void testChildGoesIntoEntityGroupOfExistingParent() throws EntityNotFoundException {
236    HasOneToOneJPA parent = new HasOneToOneJPA();
237
238    beginTxn();
239    em.persist(parent);
240    commitTxn();
241
242    HasOneToOneParentJPA child = new HasOneToOneParentJPA();
243    parent.setHasParent(child);
244    child.setParent(parent);
245
246    beginTxn();
247    em.merge(parent);
248    commitTxn();
249
250    Entity childEntity = ds.get(KeyFactory.stringToKey(child.getId()));
251    assertKeyParentEquals(parent.getId(), childEntity, childEntity.getKey());
252
253    ds.get(KeyFactory.stringToKey(parent.getId()));
254  }
255}