PageRenderTime 77ms CodeModel.GetById 20ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 301 lines | 250 code | 24 blank | 27 comment | 0 complexity | 3aab4e25910c6fe626aac8edb4bb8d9b 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.Key;
 21import com.google.appengine.api.datastore.KeyFactory;
 22import com.google.appengine.api.datastore.Query;
 23import com.google.appengine.datanucleus.test.jdo.Flight;
 24import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO;
 25import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorStringPkJDO;
 26import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
 27import com.google.appengine.datanucleus.test.jdo.HasStringAncestorKeyPkJDO;
 28import com.google.appengine.datanucleus.test.jdo.HasStringAncestorStringPkJDO;
 29
 30
 31import javax.jdo.JDOFatalUserException;
 32
 33/**
 34 * @author Max Ross <maxr@google.com>
 35 */
 36public class JDOAncestorTest extends JDOTestCase {
 37
 38  public void testInsert_IdGen() {
 39    Entity flightEntity = Flight.newFlightEntity("max", "bos", "mia", 3, 4);
 40    ds.put(flightEntity);
 41    Key flightKey = flightEntity.getKey();
 42    HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(KeyFactory.keyToString(flightKey));
 43    makePersistentInTxn(ha, TXN_START_END);
 44    Key keyWithParent = KeyFactory.stringToKey(ha.getId());
 45    assertEquals(flightKey, keyWithParent.getParent());
 46    // now we'll issue an ancestor query directly against the datastore and see
 47    // if our object comes back.
 48    Query q = new Query(ha.getClass().getSimpleName());
 49    q.setAncestor(flightKey);
 50    Entity result = ds.prepare(q).asSingleEntity();
 51    assertEquals(flightKey, result.getKey().getParent());
 52  }
 53
 54  public void testInsert_NamedKey() {
 55    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
 56    ds.put(flightEntity);
 57    Key flightKey = flightEntity.getKey();
 58    Key key = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key", flightKey).getKey();
 59    HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(null, KeyFactory.keyToString(key));
 60    makePersistentInTxn(ha, TXN_START_END);
 61    Key keyWithParent = KeyFactory.stringToKey(ha.getId());
 62    assertEquals(flightKey, keyWithParent.getParent());
 63    // now we'll issue an ancestor query directly against the datastore and see
 64    // if our object comes back.
 65    Query q = new Query(ha.getClass().getSimpleName());
 66    q.setAncestor(flightKey);
 67    Entity result = ds.prepare(q).asSingleEntity();
 68    assertEquals(flightKey, result.getKey().getParent());
 69    assertEquals("named key", result.getKey().getName());
 70    assertEquals("parent named key", result.getKey().getParent().getName());
 71  }
 72
 73  public void testInsert_SetAncestorAndPk() {
 74    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
 75    ds.put(flightEntity);
 76    Key flightKey = flightEntity.getKey();
 77    HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(
 78        KeyFactory.keyToString(flightKey),
 79        KeyFactory.keyToString(KeyFactory.createKey(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key")));
 80    beginTxn();
 81    try {
 82      pm.makePersistent(ha);
 83      fail("expected exception");
 84    } catch (JDOFatalUserException e) {
 85      // good
 86      rollbackTxn();
 87    }
 88  }
 89
 90  public void testFetch() {
 91    Entity flightEntity = Flight.newFlightEntity("max", "bos", "mia", 3, 4);
 92    ds.put(flightEntity);
 93    Entity hasAncestorEntity = new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), flightEntity.getKey());
 94    ds.put(hasAncestorEntity);
 95
 96    beginTxn();
 97    HasStringAncestorStringPkJDO ha = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(hasAncestorEntity.getKey()));
 98    assertEquals(KeyFactory.keyToString(flightEntity.getKey()), ha.getAncestorId());
 99    commitTxn();
100  }
101
102  public void testFetchWithNamedKey() {
103    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
104    ds.put(flightEntity);
105    Entity hasAncestorEntity =
106        new Entity(HasStringAncestorStringPkJDO.class.getSimpleName(), "named key", flightEntity.getKey());
107    ds.put(hasAncestorEntity);
108
109    beginTxn();
110    HasStringAncestorStringPkJDO ha = pm.getObjectById(HasStringAncestorStringPkJDO.class, KeyFactory.keyToString(hasAncestorEntity.getKey()));
111    assertEquals(KeyFactory.keyToString(flightEntity.getKey()), ha.getAncestorId());
112    assertEquals("named key", KeyFactory.stringToKey(ha.getId()).getName());
113    assertEquals("parent named key", KeyFactory.stringToKey(ha.getId()).getParent().getName());
114    commitTxn();
115  }
116
117  public void testInsertWithNullAncestor() {
118    HasStringAncestorStringPkJDO ha = new HasStringAncestorStringPkJDO(null);
119    makePersistentInTxn(ha, TXN_START_END);
120    Key keyWithParent = KeyFactory.stringToKey(ha.getId());
121    assertNull(keyWithParent.getParent());
122  }
123
124  public void testKeyPKKeyAncestor_NamedKey() throws EntityNotFoundException {
125    HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
126    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
127    ds.put(flightEntity);
128    Key flightKey = flightEntity.getKey();
129    Key pojoKey = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), "child named key", flightKey).getKey();
130    pojo.setKey(pojoKey);
131    beginTxn();
132    pm.makePersistent(pojo);
133    commitTxn();
134    ds.get(pojoKey);
135    beginTxn();
136    pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, pojoKey);
137    assertEquals(pojo.getAncestorKey(), pojoKey.getParent());
138    commitTxn();
139  }
140
141  public void testKeyPKKeyAncestor_NamedKeyWrongKind() throws EntityNotFoundException {
142    HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
143    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
144    ds.put(flightEntity);
145    Key flightKey = flightEntity.getKey();
146    Key pojoKey = new Entity("blarg", "child named key", flightKey).getKey();
147    pojo.setKey(pojoKey);
148    beginTxn();
149    try {
150      pm.makePersistent(pojo);
151      fail("expected exception");
152    } catch (JDOFatalUserException e) {
153      // good
154      rollbackTxn();
155    }
156  }
157
158  public void testKeyPKKeyAncestor_IdGen() throws EntityNotFoundException {
159    HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
160    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
161    ds.put(flightEntity);
162    Key flightKey = flightEntity.getKey();
163    pojo.setAncestorKey(flightKey);
164    beginTxn();
165    pm.makePersistent(pojo);
166    commitTxn();
167    ds.get(pojo.getKey());
168    beginTxn();
169    pojo = pm.getObjectById(HasKeyAncestorKeyPkJDO.class, pojo.getKey());
170    assertEquals(pojo.getAncestorKey(), pojo.getKey().getParent());
171    commitTxn();
172  }
173
174  public void testKeyPKKeyAncestor_SetAncestorAndKey() throws EntityNotFoundException {
175    HasKeyAncestorKeyPkJDO pojo = new HasKeyAncestorKeyPkJDO();
176    Entity flightEntity = Flight.newFlightEntity("parent named key", "max", "bos", "mia", 3, 4);
177    ds.put(flightEntity);
178    Key flightKey = flightEntity.getKey();
179    pojo.setAncestorKey(flightKey);
180    Key pojoKey = new Entity(HasKeyAncestorKeyPkJDO.class.getSimpleName(), "child named key", flightKey).getKey();
181    pojo.setKey(pojoKey);
182    beginTxn();
183    try {
184      pm.makePersistent(pojo);
185      fail("expected exception");
186    } catch (JDOFatalUserException e) {
187      // good
188      rollbackTxn();
189    }
190  }
191
192  public void testInsertWithKeyPkAndAncestor() throws EntityNotFoundException {
193    Entity e = new Entity("yam");
194    ds.put(e);
195    HasKeyPkJDO hk1 = new HasKeyPkJDO();
196    hk1.setAncestorKey(e.getKey());
197    beginTxn();
198    pm.makePersistent(hk1);
199    Key key = hk1.getKey();
200    Key ancestorKey = hk1.getAncestorKey();
201    assertNotNull(key);
202    commitTxn();
203    Entity reloaded = ds.get(hk1.getKey());
204    assertEquals(ancestorKey, reloaded.getKey().getParent());
205  }
206
207  public void testInsertWithKeyPkAndStringAncestor_IdGen() throws EntityNotFoundException {
208    Entity e = new Entity("yam");
209    ds.put(e);
210    HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
211    hk1.setAncestorKey(KeyFactory.keyToString(e.getKey()));
212    beginTxn();
213    pm.makePersistent(hk1);
214    Key key = hk1.getKey();
215    String ancestorKey = hk1.getAncestorKey();
216    commitTxn();
217    Entity reloaded = ds.get(key);
218    assertEquals(ancestorKey, KeyFactory.keyToString(reloaded.getKey().getParent()));
219  }
220
221  public void testInsertWithKeyPkAndStringAncestor_NamedKey() throws EntityNotFoundException {
222    Entity e = new Entity("yam");
223    ds.put(e);
224    HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
225    Key key = new Entity(HasStringAncestorKeyPkJDO.class.getSimpleName(), "named key", e.getKey()).getKey();
226    hk1.setKey(key);
227    beginTxn();
228    pm.makePersistent(hk1);
229    assertEquals(e.getKey(), KeyFactory.stringToKey(hk1.getAncestorKey()));
230    String ancestorKey = hk1.getAncestorKey();
231    commitTxn();
232    Entity reloaded = ds.get(key);
233    assertEquals(ancestorKey, KeyFactory.keyToString(reloaded.getKey().getParent()));
234  }
235
236  public void testInsertWithKeyPkAndStringAncestor_SetKeyAndAncestor() throws EntityNotFoundException {
237    Entity e = new Entity("yam");
238    ds.put(e);
239    HasStringAncestorKeyPkJDO hk1 = new HasStringAncestorKeyPkJDO();
240    Key key = KeyFactory.createKey(HasStringAncestorKeyPkJDO.class.getSimpleName(), "named key");
241    hk1.setKey(key);
242    hk1.setAncestorKey(KeyFactory.keyToString(e.getKey()));
243    beginTxn();
244    try {
245      pm.makePersistent(hk1);
246      fail("expected exception");
247    } catch (JDOFatalUserException ex) {
248      // good
249      rollbackTxn();
250    }
251  }
252
253  public void testInsertWithStringPkAndKeyAncestor_IdGen() throws EntityNotFoundException {
254    Entity e = new Entity("yam");
255    ds.put(e);
256    HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
257    hk1.setAncestorKey(e.getKey());
258    beginTxn();
259    pm.makePersistent(hk1);
260    String key = hk1.getKey();
261    Key ancestorKey = hk1.getAncestorKey();
262    commitTxn();
263    Entity reloaded = ds.get(KeyFactory.stringToKey(key));
264    assertEquals(ancestorKey, reloaded.getKey().getParent());
265  }
266
267  public void testInsertWithStringPkAndKeyAncestor_NamedKey() throws EntityNotFoundException {
268    Entity e = new Entity("yam");
269    ds.put(e);
270    HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
271    Key keyToSet =
272        new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), "yar", e.getKey()).getKey();
273    hk1.setKey(KeyFactory.keyToString(keyToSet));
274    beginTxn();
275    pm.makePersistent(hk1);
276    String key = hk1.getKey();
277    assertEquals(e.getKey(), hk1.getAncestorKey());
278    commitTxn();
279    Entity reloaded = ds.get(KeyFactory.stringToKey(key));
280    assertEquals(e.getKey(), reloaded.getKey().getParent());
281  }
282
283  public void testInsertWithStringPkAndKeyAncestor_SetAncestorAndPk() throws EntityNotFoundException {
284    Entity parentEntity = new Entity("yam");
285    ds.put(parentEntity);
286    HasKeyAncestorStringPkJDO hk1 = new HasKeyAncestorStringPkJDO();
287    Key keyToSet =
288        new Entity(HasKeyAncestorStringPkJDO.class.getSimpleName(), "yar", parentEntity.getKey()).getKey();
289    hk1.setKey(KeyFactory.keyToString(keyToSet));
290    hk1.setAncestorKey(keyToSet);
291    beginTxn();
292    try {
293      pm.makePersistent(hk1);
294      fail("expected exception");
295    } catch (JDOFatalUserException e) {
296      // good
297      rollbackTxn();
298    }
299  }
300
301}