PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 261 lines | 190 code | 38 blank | 33 comment | 0 complexity | 33e65d666004870d4a9e378a6ac2a5f1 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.UnidirectionalOneToManySubclassesJDO.SubChild;
 22import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SubParentWithSubChild;
 23import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SubParentWithSuperChild;
 24import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperChild;
 25import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperParentWithSubChild;
 26import com.google.appengine.datanucleus.test.jdo.UnidirectionalOneToManySubclassesJDO.SuperParentWithSuperChild;
 27
 28
 29import java.util.Collections;
 30
 31/**
 32 * @author Max Ross <max.ross@gmail.com>
 33 */
 34public class JDOUnidirectionalOneToManySubclassTest extends JDOTestCase {
 35
 36  public void testSubParentWithSubChild() throws EntityNotFoundException {
 37    // insertion
 38    SubParentWithSubChild parent = new SubParentWithSubChild();
 39    parent.setSuperParentString("super parent string");
 40    parent.setSubParentString("sub parent string");
 41    SubChild subChild = new SubChild();
 42    subChild.setAString("a string");
 43    subChild.setBString("b string");
 44    parent.getSuperParentSubChildren().add(subChild);
 45
 46    beginTxn();
 47    pm.makePersistent(parent);
 48    commitTxn();
 49    Entity parentEntity =
 50        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
 51    Entity superParentSubChildEntity = ds.get(subChild.getId());
 52    assertEquals(3, parentEntity.getProperties().size());
 53    assertEquals("super parent string", parentEntity.getProperty("superParentString"));
 54    assertEquals("sub parent string", parentEntity.getProperty("subParentString"));
 55    assertEquals(Collections.singletonList(superParentSubChildEntity.getKey()),
 56                 parentEntity.getProperty("subChildren"));
 57
 58    assertEquals(2, superParentSubChildEntity.getProperties().size());
 59    assertEquals("a string", superParentSubChildEntity.getProperty("aString"));
 60    assertEquals("b string", superParentSubChildEntity.getProperty("bString"));
 61
 62    // lookup
 63    beginTxn();
 64    parent = pm.getObjectById(parent.getClass(), parent.getId());
 65    assertEquals("super parent string", parent.getSuperParentString());
 66    assertEquals("sub parent string", parent.getSubParentString());
 67    assertEquals(1, parent.getSuperParentSubChildren().size());
 68    assertEquals(subChild.getId(), parent.getSuperParentSubChildren().get(0).getId());
 69    commitTxn();
 70
 71    beginTxn();
 72    subChild = pm.getObjectById(subChild.getClass(), subChild.getId());
 73    assertEquals("a string", subChild.getAString());
 74    assertEquals("b string", subChild.getBString());
 75    commitTxn();
 76
 77    // cascade delete
 78    beginTxn();
 79    pm.deletePersistent(parent);
 80    commitTxn();
 81
 82    assertEquals(0, countForClass(parent.getClass()));
 83    assertEquals(0, countForClass(subChild.getClass()));
 84  }
 85
 86  public void testSubParentWithSuperChild() throws EntityNotFoundException {
 87    // insertion
 88    SubParentWithSuperChild parent = new SubParentWithSuperChild();
 89    parent.setSuperParentString("super parent string");
 90    parent.setSubParentString("sub parent string");
 91
 92    SuperChild superChild = new SuperChild();
 93    superChild.setAString("a string");
 94    parent.getSuperParentSuperChildren().add(superChild);
 95
 96    beginTxn();
 97    pm.makePersistent(parent);
 98    commitTxn();
 99    Entity parentEntity =
100        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
101    Entity superParentSuperChildEntity = ds.get(superChild.getId());
102    assertEquals(3, parentEntity.getProperties().size());
103    assertEquals("super parent string", parentEntity.getProperty("superParentString"));
104    assertEquals("sub parent string", parentEntity.getProperty("subParentString"));
105    assertEquals(Collections.singletonList(superParentSuperChildEntity.getKey()),
106                 parentEntity.getProperty("superChildren"));
107
108    assertEquals(1, superParentSuperChildEntity.getProperties().size());
109    assertEquals("a string", superParentSuperChildEntity.getProperty("aString"));
110
111    // lookup
112    beginTxn();
113    parent = pm.getObjectById(parent.getClass(), parent.getId());
114    assertEquals("super parent string", parent.getSuperParentString());
115    assertEquals("sub parent string", parent.getSubParentString());
116    assertEquals(1, parent.getSuperParentSuperChildren().size());
117    assertEquals(superChild.getId(), parent.getSuperParentSuperChildren().get(0).getId());
118    commitTxn();
119
120    beginTxn();
121    superChild = pm.getObjectById(superChild.getClass(), superChild.getId());
122    assertEquals("a string", superChild.getAString());
123    commitTxn();
124
125    // cascade delete
126    beginTxn();
127    pm.deletePersistent(parent);
128    commitTxn();
129
130    assertEquals(0, countForClass(parent.getClass()));
131    assertEquals(0, countForClass(superChild.getClass()));
132  }
133
134  public void testSuperParentWithSuperChild() throws EntityNotFoundException {
135    // insertion
136    SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
137    parent.setSuperParentString("super parent string");
138
139    SuperChild superChild = new SuperChild();
140    superChild.setAString("a string");
141    parent.getSuperParentSuperChildren().add(superChild);
142
143    beginTxn();
144    pm.makePersistent(parent);
145    commitTxn();
146    Entity parentEntity =
147        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
148    Entity superParentSuperChildEntity = ds.get(superChild.getId());
149    assertEquals(2, parentEntity.getProperties().size());
150    assertEquals("super parent string", parentEntity.getProperty("superParentString"));
151    assertEquals(Collections.singletonList(superParentSuperChildEntity.getKey()),
152                 parentEntity.getProperty("superChildren"));
153    assertEquals(1, superParentSuperChildEntity.getProperties().size());
154    assertEquals("a string", superParentSuperChildEntity.getProperty("aString"));
155
156    // lookup
157    beginTxn();
158    parent = pm.getObjectById(parent.getClass(), parent.getId());
159    assertEquals("super parent string", parent.getSuperParentString());
160    assertEquals(1, parent.getSuperParentSuperChildren().size());
161    assertEquals(superChild.getId(), parent.getSuperParentSuperChildren().get(0).getId());
162    commitTxn();
163
164    beginTxn();
165    superChild = pm.getObjectById(superChild.getClass(), superChild.getId());
166    assertEquals("a string", superChild.getAString());
167    commitTxn();
168
169    // cascade delete
170    beginTxn();
171    pm.deletePersistent(parent);
172    commitTxn();
173
174    assertEquals(0, countForClass(parent.getClass()));
175    assertEquals(0, countForClass(superChild.getClass()));
176  }
177
178  public void testSuperParentWithSubChild() throws EntityNotFoundException {
179    // insertion
180    SuperParentWithSubChild parent = new SuperParentWithSubChild();
181    parent.setSuperParentString("super parent string");
182
183    SubChild subChild = new SubChild();
184    subChild.setAString("a string");
185    subChild.setBString("b string");
186    parent.getSuperParentSubChildren().add(subChild);
187
188    beginTxn();
189    pm.makePersistent(parent);
190    commitTxn();
191    Entity parentEntity =
192        ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId()));
193    Entity superParentSubChildEntity = ds.get(subChild.getId());
194    assertEquals(2, parentEntity.getProperties().size());
195    assertEquals("super parent string", parentEntity.getProperty("superParentString"));
196    assertEquals(Collections.singletonList(superParentSubChildEntity.getKey()),
197                 parentEntity.getProperty("subChildren"));
198
199    assertEquals(2, superParentSubChildEntity.getProperties().size());
200    assertEquals("a string", superParentSubChildEntity.getProperty("aString"));
201    assertEquals("b string", superParentSubChildEntity.getProperty("bString"));
202
203    // lookup
204    beginTxn();
205    parent = pm.getObjectById(parent.getClass(), parent.getId());
206    assertEquals("super parent string", parent.getSuperParentString());
207    assertEquals(1, parent.getSuperParentSubChildren().size());
208    assertEquals(subChild.getId(), parent.getSuperParentSubChildren().get(0).getId());
209    commitTxn();
210
211    beginTxn();
212    subChild = pm.getObjectById(subChild.getClass(), subChild.getId());
213    assertEquals("a string", subChild.getAString());
214    assertEquals("b string", subChild.getBString());
215    commitTxn();
216
217    // cascade delete
218    beginTxn();
219    pm.deletePersistent(parent);
220    commitTxn();
221
222    assertEquals(0, countForClass(parent.getClass()));
223    assertEquals(0, countForClass(subChild.getClass()));
224  }
225
226  public void testWrongChildType() {
227    SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
228    parent.setSuperParentString("a string");
229    SubChild child = new SubChild();
230    parent.getSuperParentSuperChildren().add(child);
231
232    beginTxn();
233    try {
234      pm.makePersistent(parent);
235      fail("expected exception");
236    } catch (UnsupportedOperationException uoe) {
237      // good
238    }
239    rollbackTxn();
240  }
241
242  public void testWrongChildType_Update() throws InstantiationException, IllegalAccessException {
243    // need a non-txn datasource so we can access multiple entity groups
244    switchDatasource(PersistenceManagerFactoryName.nontransactional);
245    SubChild child = SubChild.class.newInstance();
246    SuperParentWithSuperChild parent = new SuperParentWithSuperChild();
247    parent.setSuperParentString("a string");
248    beginTxn();
249    pm.makePersistent(parent);
250    commitTxn();
251    parent = pm.getObjectById(parent.getClass(), parent.getId());
252
253    try {
254      parent.getSuperParentSuperChildren().add(child);
255      pm.close();
256      fail("expected exception");
257    } catch (UnsupportedOperationException uoe) {
258      // good
259    }
260  }
261}