PageRenderTime 60ms CodeModel.GetById 2ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 392 lines | 286 code | 62 blank | 44 comment | 0 complexity | 693f19a0420a16a1e2d175d3f49e19b0 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 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.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.Utils;
 22import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToManySubclassesJDO.Example1;
 23import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToManySubclassesJDO.Example2;
 24import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToManySubclassesJDO.Example3;
 25import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToManySubclassesJDO.Example4;
 26
 27
 28/**
 29 * @author Max Ross <max.ross@gmail.com>
 30 */
 31public class JDOBidirectionalOneToManySubclassTest extends JDOTestCase {
 32
 33  public void testExample1Subclass() throws EntityNotFoundException {
 34    // insertion
 35    Example1.B parent = new Example1.B();
 36    parent.setAString("a string");
 37    parent.setBString("b string");
 38
 39    Example1.X child = new Example1.X();
 40    child.setXString("x string");
 41    parent.getChildren().add(child);
 42
 43    beginTxn();
 44    pm.makePersistent(parent);
 45    commitTxn();
 46    Entity parentEntity =
 47        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
 48    Entity childEntity = ds.get(child.getId());
 49    assertEquals(3, parentEntity.getProperties().size());
 50    assertEquals("a string", parentEntity.getProperty("aString"));
 51    assertEquals("b string", parentEntity.getProperty("bString"));
 52    assertEquals(Utils.newArrayList(childEntity.getKey()), parentEntity.getProperty("children"));
 53
 54    assertEquals(1, childEntity.getProperties().size());
 55    assertEquals("x string", childEntity.getProperty("xString"));
 56
 57    // lookup
 58    beginTxn();
 59    parent = pm.getObjectById(parent.getClass(), parent.getId());
 60    assertEquals("a string", parent.getAString());
 61    assertEquals("b string", parent.getBString());
 62    assertEquals(1, parent.getChildren().size());
 63    assertEquals(child.getId(), parent.getChildren().get(0).getId());
 64    commitTxn();
 65
 66    beginTxn();
 67    child = pm.getObjectById(child.getClass(), child.getId());
 68    assertEquals("x string", child.getXString());
 69    commitTxn();
 70
 71    // cascade delete
 72    beginTxn();
 73    pm.deletePersistent(parent);
 74    commitTxn();
 75
 76    assertEquals(0, countForClass(parent.getClass()));
 77    assertEquals(0, countForClass(child.getClass()));
 78  }
 79
 80  public void testExample1Superclass() throws EntityNotFoundException {
 81    // insertion
 82    Example1.A parent = new Example1.A();
 83    parent.setAString("a string");
 84
 85    Example1.X child = new Example1.X();
 86    child.setXString("x string");
 87    parent.getChildren().add(child);
 88
 89    beginTxn();
 90    pm.makePersistent(parent);
 91    commitTxn();
 92    Entity parentEntity =
 93        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
 94    Entity childEntity = ds.get(child.getId());
 95
 96    assertEquals(2, parentEntity.getProperties().size());
 97    assertEquals("a string", parentEntity.getProperty("aString"));
 98    assertEquals(Utils.newArrayList(childEntity.getKey()), parentEntity.getProperty("children"));
 99    assertEquals(1, childEntity.getProperties().size());
100    assertEquals("x string", childEntity.getProperty("xString"));
101
102    // lookup
103    beginTxn();
104    parent = pm.getObjectById(parent.getClass(), parent.getId());
105    assertEquals("a string", parent.getAString());
106    assertEquals(1, parent.getChildren().size());
107    assertEquals(child.getId(), parent.getChildren().get(0).getId());
108    commitTxn();
109
110    beginTxn();
111    child = pm.getObjectById(child.getClass(), child.getId());
112    assertEquals("x string", child.getXString());
113    commitTxn();
114
115    // cascade delete
116    beginTxn();
117    pm.deletePersistent(parent);
118    commitTxn();
119
120    assertEquals(0, countForClass(parent.getClass()));
121    assertEquals(0, countForClass(child.getClass()));
122  }
123
124  public void testExample2Subclass() throws EntityNotFoundException {
125    // insertion
126    Example2.B parent = new Example2.B();
127    parent.setAString("a string");
128    parent.setBString("b string");
129
130    Example2.Y child = new Example2.Y();
131    child.setXString("x string");
132    child.setYString("y string");
133    parent.getChildren().add(child);
134
135    beginTxn();
136    pm.makePersistent(parent);
137    commitTxn();
138    Entity parentEntity =
139        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
140    Entity childEntity = ds.get(child.getId());
141
142    assertEquals(3, parentEntity.getProperties().size());
143    assertEquals("a string", parentEntity.getProperty("aString"));
144    assertEquals("b string", parentEntity.getProperty("bString"));
145    assertEquals(Utils.newArrayList(childEntity.getKey()), parentEntity.getProperty("children"));
146    assertEquals(2, childEntity.getProperties().size());
147    assertEquals("x string", childEntity.getProperty("xString"));
148    assertEquals("y string", childEntity.getProperty("yString"));
149
150    // lookup
151    beginTxn();
152    parent = pm.getObjectById(parent.getClass(), parent.getId());
153    assertEquals("a string", parent.getAString());
154    assertEquals("b string", parent.getBString());
155    assertEquals(1, parent.getChildren().size());
156    assertEquals(child.getId(), parent.getChildren().get(0).getId());
157    commitTxn();
158
159    beginTxn();
160    child = pm.getObjectById(child.getClass(), child.getId());
161    assertEquals("x string", child.getXString());
162    assertEquals("y string", child.getYString());
163    commitTxn();
164
165    // cascade delete
166    beginTxn();
167    pm.deletePersistent(parent);
168    commitTxn();
169
170    assertEquals(0, countForClass(parent.getClass()));
171    assertEquals(0, countForClass(child.getClass()));
172  }
173
174  public void testExample2Superclass() throws EntityNotFoundException {
175    // insertion
176    Example2.A parent = new Example2.A();
177    parent.setAString("a string");
178
179    beginTxn();
180    pm.makePersistent(parent);
181    commitTxn();
182    Entity parentEntity =
183        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
184    assertEquals(1, parentEntity.getProperties().size());
185    assertEquals("a string", parentEntity.getProperty("aString"));
186
187    // lookup
188    beginTxn();
189    parent = pm.getObjectById(parent.getClass(), parent.getId());
190    assertEquals("a string", parent.getAString());
191    commitTxn();
192
193    // delete
194    beginTxn();
195    pm.deletePersistent(parent);
196    commitTxn();
197
198    assertEquals(0, countForClass(parent.getClass()));
199  }
200
201  public void testExample3Subclass() throws EntityNotFoundException {
202    // insertion
203    Example3.B parent = new Example3.B();
204    parent.setAString("a string");
205    parent.setBString("b string");
206
207    Example3.Y child = new Example3.Y();
208    child.setXString("x string");
209    child.setYString("y string");
210    parent.getChildren().add(child);
211
212    beginTxn();
213    pm.makePersistent(parent);
214    commitTxn();
215    Entity parentEntity =
216        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
217    Entity childEntity = ds.get(child.getId());
218
219    assertEquals(3, parentEntity.getProperties().size());
220    assertEquals("a string", parentEntity.getProperty("aString"));
221    assertEquals("b string", parentEntity.getProperty("bString"));
222    assertEquals(Utils.newArrayList(childEntity.getKey()), parentEntity.getProperty("children"));
223    assertEquals(2, childEntity.getProperties().size());
224    assertEquals("x string", childEntity.getProperty("xString"));
225    assertEquals("y string", childEntity.getProperty("yString"));
226
227    // lookup
228    beginTxn();
229    parent = pm.getObjectById(parent.getClass(), parent.getId());
230    assertEquals("a string", parent.getAString());
231    assertEquals("b string", parent.getBString());
232    assertEquals(1, parent.getChildren().size());
233    assertEquals(child.getId(), parent.getChildren().get(0).getId());
234    commitTxn();
235
236    beginTxn();
237    child = pm.getObjectById(child.getClass(), child.getId());
238    assertEquals("x string", child.getXString());
239    assertEquals("y string", child.getYString());
240    commitTxn();
241
242    // cascade delete
243    beginTxn();
244    pm.deletePersistent(parent);
245    commitTxn();
246
247    assertEquals(0, countForClass(parent.getClass()));
248    assertEquals(0, countForClass(child.getClass()));
249  }
250
251  public void testExample3Superclass() throws EntityNotFoundException {
252    // insertion
253    Example3.A parent = new Example3.A();
254    parent.setAString("a string");
255
256    beginTxn();
257    pm.makePersistent(parent);
258    commitTxn();
259    Entity parentEntity =
260        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
261    assertEquals(1, parentEntity.getProperties().size());
262    assertEquals("a string", parentEntity.getProperty("aString"));
263
264    // lookup
265    beginTxn();
266    parent = pm.getObjectById(parent.getClass(), parent.getId());
267    assertEquals("a string", parent.getAString());
268    commitTxn();
269
270    // delete
271    beginTxn();
272    pm.deletePersistent(parent);
273    commitTxn();
274
275    assertEquals(0, countForClass(parent.getClass()));
276  }
277
278  public void testExample4Subclass() throws EntityNotFoundException {
279    // insertion
280    Example4.B parent = new Example4.B();
281    parent.setAString("a string");
282    parent.setBString("b string");
283
284    Example4.Y child = new Example4.Y();
285    child.setXString("x string");
286    child.setYString("y string");
287    parent.getChildren().add(child);
288
289    beginTxn();
290    pm.makePersistent(parent);
291    commitTxn();
292    Entity parentEntity =
293        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
294    Entity childEntity = ds.get(child.getId());
295
296    assertEquals(3, parentEntity.getProperties().size());
297    assertEquals("a string", parentEntity.getProperty("aString"));
298    assertEquals("b string", parentEntity.getProperty("bString"));
299    assertEquals(Utils.newArrayList(childEntity.getKey()), parentEntity.getProperty("children"));
300    assertEquals(2, childEntity.getProperties().size());
301    assertEquals("x string", childEntity.getProperty("xString"));
302    assertEquals("y string", childEntity.getProperty("yString"));
303
304    // lookup
305    beginTxn();
306    parent = pm.getObjectById(parent.getClass(), parent.getId());
307    assertEquals("a string", parent.getAString());
308    assertEquals("b string", parent.getBString());
309    assertEquals(1, parent.getChildren().size());
310    assertEquals(child.getId(), parent.getChildren().get(0).getId());
311    commitTxn();
312
313    beginTxn();
314    child = pm.getObjectById(child.getClass(), child.getId());
315    assertEquals("x string", child.getXString());
316    assertEquals("y string", child.getYString());
317    commitTxn();
318
319    // cascade delete
320    beginTxn();
321    pm.deletePersistent(parent);
322    commitTxn();
323
324    assertEquals(0, countForClass(parent.getClass()));
325    assertEquals(0, countForClass(child.getClass()));
326  }
327
328  public void testExample4Superclass() throws EntityNotFoundException {
329    // insertion
330    Example4.A parent = new Example4.A();
331    parent.setAString("a string");
332
333    beginTxn();
334    pm.makePersistent(parent);
335    commitTxn();
336    Entity parentEntity =
337        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
338    assertEquals(2, parentEntity.getProperties().size());
339    assertEquals("a string", parentEntity.getProperty("aString"));
340    assertTrue(parentEntity.hasProperty("children"));
341    assertNull(parentEntity.getProperty("children"));
342
343    // lookup
344    beginTxn();
345    parent = pm.getObjectById(parent.getClass(), parent.getId());
346    assertEquals("a string", parent.getAString());
347    commitTxn();
348
349    // delete
350    beginTxn();
351    pm.deletePersistent(parent);
352    commitTxn();
353
354    assertEquals(0, countForClass(parent.getClass()));
355  }
356
357  public void testWrongChildType() {
358    Example1.A parent = new Example1.A();
359    parent.setAString("a string");
360    Example1.Y child = new Example1.Y();
361    child.setXString("x string");
362    parent.getChildren().add(child);
363
364    beginTxn();
365    try {
366      pm.makePersistent(parent);
367      fail("expected exception");
368    } catch (UnsupportedOperationException uoe) {
369      // good
370    }
371    rollbackTxn();
372  }
373
374  public void testWrongChildType_Update() {
375    Example1.A parent = new Example1.A();
376    parent.setAString("a string");
377    beginTxn();
378    pm.makePersistent(parent);
379    commitTxn();
380    beginTxn();
381    Example1.Y child = new Example1.Y();
382    child.setXString("x string");
383    parent.getChildren().add(child);
384
385    try {
386      commitTxn();
387      fail("expected exception");
388    } catch (UnsupportedOperationException uoe) {
389      // good
390    }
391  }
392}