PageRenderTime 156ms CodeModel.GetById 115ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 308 lines | 223 code | 47 blank | 38 comment | 0 complexity | 7f799bf3d3cb141f76008245fadcc1b8 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.test.jdo.BidirectionalOneToOneSubclassesJDO.Example1;
 22import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToOneSubclassesJDO.Example2;
 23import com.google.appengine.datanucleus.test.jdo.BidirectionalOneToOneSubclassesJDO.Example3;
 24
 25
 26/**
 27 * @author Max Ross <max.ross@gmail.com>
 28 */
 29public class JDOBidirectionalOneToOneSubclassTest extends JDOTestCase {
 30
 31  public void testExample1Subclass() throws EntityNotFoundException {
 32    // insertion
 33    Example1.B parent = new Example1.B();
 34    parent.setAString("a string");
 35    parent.setBString("b string");
 36
 37    Example1.X child = new Example1.X();
 38    child.setXString("x string");
 39    parent.setChild(child);
 40
 41    beginTxn();
 42    pm.makePersistent(parent);
 43    commitTxn();
 44    Entity parentEntity =
 45        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
 46    Entity childEntity = ds.get(child.getId());
 47
 48    assertEquals(3, parentEntity.getProperties().size());
 49    assertEquals("a string", parentEntity.getProperty("aString"));
 50    assertEquals("b string", parentEntity.getProperty("bString"));
 51    assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
 52    assertEquals(1, childEntity.getProperties().size());
 53    assertEquals("x string", childEntity.getProperty("xString"));
 54
 55    // lookup
 56    beginTxn();
 57    parent = pm.getObjectById(parent.getClass(), parent.getId());
 58    assertEquals("a string", parent.getAString());
 59    assertEquals("b string", parent.getBString());
 60    assertEquals(child.getId(), parent.getChild().getId());
 61    commitTxn();
 62
 63    beginTxn();
 64    child = pm.getObjectById(child.getClass(), child.getId());
 65    assertEquals("x string", child.getXString());
 66    commitTxn();
 67
 68    // cascade delete
 69    beginTxn();
 70    pm.deletePersistent(parent);
 71    commitTxn();
 72
 73    assertEquals(0, countForClass(parent.getClass()));
 74    assertEquals(0, countForClass(child.getClass()));
 75  }
 76
 77  public void testExample1Superclass() throws EntityNotFoundException {
 78    // insertion
 79    Example1.A parent = new Example1.A();
 80    parent.setAString("a string");
 81
 82    Example1.X child = new Example1.X();
 83    child.setXString("x string");
 84    parent.setChild(child);
 85
 86    beginTxn();
 87    pm.makePersistent(parent);
 88    commitTxn();
 89    Entity parentEntity =
 90        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
 91    Entity childEntity = ds.get(child.getId());
 92    assertEquals(2, parentEntity.getProperties().size());
 93    assertEquals("a string", parentEntity.getProperty("aString"));
 94    assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
 95    assertEquals(1, childEntity.getProperties().size());
 96    assertEquals("x string", childEntity.getProperty("xString"));
 97
 98    // lookup
 99    beginTxn();
100    parent = pm.getObjectById(parent.getClass(), parent.getId());
101    assertEquals("a string", parent.getAString());
102    assertEquals(child.getId(), parent.getChild().getId());
103    commitTxn();
104
105    beginTxn();
106    child = pm.getObjectById(child.getClass(), child.getId());
107    assertEquals("x string", child.getXString());
108    commitTxn();
109
110    // cascade delete
111    beginTxn();
112    pm.deletePersistent(parent);
113    commitTxn();
114
115    assertEquals(0, countForClass(parent.getClass()));
116    assertEquals(0, countForClass(child.getClass()));
117  }
118
119  public void testExample3Subclass() throws EntityNotFoundException {
120    // insertion
121    Example2.B parent = new Example2.B();
122    parent.setAString("a string");
123    parent.setBString("b string");
124
125    Example2.Y child = new Example2.Y();
126    child.setXString("x string");
127    child.setYString("y string");
128    parent.setChild(child);
129
130    beginTxn();
131    pm.makePersistent(parent);
132    commitTxn();
133    Entity parentEntity =
134        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
135    Entity childEntity = ds.get(child.getId());
136    assertEquals(3, parentEntity.getProperties().size());
137    assertEquals("a string", parentEntity.getProperty("aString"));
138    assertEquals("b string", parentEntity.getProperty("bString"));
139    assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
140    assertEquals(2, childEntity.getProperties().size());
141    assertEquals("x string", childEntity.getProperty("xString"));
142    assertEquals("y string", childEntity.getProperty("yString"));
143
144    // lookup
145    beginTxn();
146    parent = pm.getObjectById(parent.getClass(), parent.getId());
147    assertEquals("a string", parent.getAString());
148    assertEquals("b string", parent.getBString());
149    assertEquals(child.getId(), parent.getChild().getId());
150    commitTxn();
151
152    beginTxn();
153    child = pm.getObjectById(child.getClass(), child.getId());
154    assertEquals("x string", child.getXString());
155    assertEquals("y string", child.getYString());
156    commitTxn();
157
158    // cascade delete
159    beginTxn();
160    pm.deletePersistent(parent);
161    commitTxn();
162
163    assertEquals(0, countForClass(parent.getClass()));
164    assertEquals(0, countForClass(child.getClass()));
165  }
166
167  public void testExample3Superclass() throws EntityNotFoundException {
168    // insertion
169    Example2.A parent = new Example2.A();
170    parent.setAString("a string");
171
172    beginTxn();
173    pm.makePersistent(parent);
174    commitTxn();
175    Entity parentEntity =
176        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
177    assertEquals(1, parentEntity.getProperties().size());
178    assertEquals("a string", parentEntity.getProperty("aString"));
179
180    // lookup
181    beginTxn();
182    parent = pm.getObjectById(parent.getClass(), parent.getId());
183    assertEquals("a string", parent.getAString());
184    commitTxn();
185
186    // delete
187    beginTxn();
188    pm.deletePersistent(parent);
189    commitTxn();
190
191    assertEquals(0, countForClass(parent.getClass()));
192  }
193
194  public void testExample4Subclass() throws EntityNotFoundException {
195    // insertion
196    Example3.B parent = new Example3.B();
197    parent.setAString("a string");
198    parent.setBString("b string");
199
200    Example3.Y child = new Example3.Y();
201    child.setXString("x string");
202    child.setYString("y string");
203    parent.setChild(child);
204
205    beginTxn();
206    pm.makePersistent(parent);
207    commitTxn();
208    Entity parentEntity =
209        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
210    Entity childEntity = ds.get(child.getId());
211    assertEquals(3, parentEntity.getProperties().size());
212    assertEquals("a string", parentEntity.getProperty("aString"));
213    assertEquals("b string", parentEntity.getProperty("bString"));
214    assertEquals(childEntity.getKey(), parentEntity.getProperty("child_id_OID"));
215    assertEquals(2, childEntity.getProperties().size());
216    assertEquals("x string", childEntity.getProperty("xString"));
217    assertEquals("y string", childEntity.getProperty("yString"));
218
219    // lookup
220    beginTxn();
221    parent = pm.getObjectById(parent.getClass(), parent.getId());
222    assertEquals("a string", parent.getAString());
223    assertEquals("b string", parent.getBString());
224    assertEquals(child.getId(), parent.getChild().getId());
225    commitTxn();
226
227    beginTxn();
228    child = pm.getObjectById(child.getClass(), child.getId());
229    assertEquals("x string", child.getXString());
230    assertEquals("y string", child.getYString());
231    commitTxn();
232
233    // cascade delete
234    beginTxn();
235    pm.deletePersistent(parent);
236    commitTxn();
237
238    assertEquals(0, countForClass(parent.getClass()));
239    assertEquals(0, countForClass(child.getClass()));
240  }
241
242  public void testExample4Superclass() throws EntityNotFoundException {
243    // insertion
244    Example3.A parent = new Example3.A();
245    parent.setAString("a string");
246
247    beginTxn();
248    pm.makePersistent(parent);
249    commitTxn();
250    Entity parentEntity =
251        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
252    assertEquals(2, parentEntity.getProperties().size());
253    assertEquals("a string", parentEntity.getProperty("aString"));
254    assertTrue(parentEntity.hasProperty("child_id_OID"));
255    assertNull(parentEntity.getProperty("child_id_OID"));
256
257    // lookup
258    beginTxn();
259    parent = pm.getObjectById(parent.getClass(), parent.getId());
260    assertEquals("a string", parent.getAString());
261    commitTxn();
262
263    // delete
264    beginTxn();
265    pm.deletePersistent(parent);
266    commitTxn();
267
268    assertEquals(0, countForClass(parent.getClass()));
269  }
270
271  public void testWrongChildType() {
272    Example1.A parent = new Example1.A();
273    parent.setAString("a string");
274    Example1.Y child = new Example1.Y();
275    child.setXString("x string");
276    parent.setChild(child);
277
278    beginTxn();
279    try {
280      pm.makePersistent(parent);
281      fail("expected exception");
282    } catch (UnsupportedOperationException uoe) {
283      // good
284    }
285    rollbackTxn();
286  }
287
288  public void testWrongChildType_Update() {
289    Example1.A parent = new Example1.A();
290    parent.setAString("a string");
291    beginTxn();
292    pm.makePersistent(parent);
293    commitTxn();
294
295    beginTxn();
296    parent = pm.getObjectById(parent.getClass(), parent.getId());
297    Example1.Y child = new Example1.Y();
298    child.setXString("x string");
299    parent.setChild(child);
300
301    try {
302      commitTxn();
303      fail("expected exception");
304    } catch (UnsupportedOperationException uoe) {
305      // good
306    }
307  }
308}