PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 93 lines | 60 code | 14 blank | 19 comment | 0 complexity | ac17b9517994668487437d02de8fa408 MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2011 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 java.util.Set;
19
20import javax.jdo.JDOObjectNotFoundException;
21
22import com.google.appengine.datanucleus.test.jdo.HasDatastoreIdentityChildJDO;
23import com.google.appengine.datanucleus.test.jdo.HasDatastoreIdentityParentJDO;
24
25/**
26 * Tests for datastore-identity with JDO.
27 */
28public class JDODatastoreIdentityTest extends JDOTestCase {
29
30  public void testInsertUpdate_IdGen() {
31    HasDatastoreIdentityParentJDO pojo = new HasDatastoreIdentityParentJDO();
32    pojo.setName("First Name");
33
34    beginTxn();
35    pm.makePersistent(pojo);
36    commitTxn();
37    Object id = pm.getObjectId(pojo);
38    pm.evictAll();
39
40    beginTxn();
41    HasDatastoreIdentityParentJDO pojo2 = (HasDatastoreIdentityParentJDO)pm.getObjectById(id);
42    assertNotNull(pojo2);
43    assertEquals("First Name", pojo2.getName());
44    pojo2.setName("Second Name");
45    commitTxn();
46    pm.evictAll();
47
48    beginTxn();
49    pojo2 = (HasDatastoreIdentityParentJDO)pm.getObjectById(id);
50    assertNotNull(pojo2);
51    assertEquals("Second Name", pojo2.getName());
52
53    pm.deletePersistent(pojo2);
54    commitTxn();
55    pm.evictAll();
56
57    beginTxn();
58    try {
59      pojo2 = (HasDatastoreIdentityParentJDO)pm.getObjectById(id);
60      fail("Returned object even though it was deleted");
61    } catch (JDOObjectNotFoundException onfe) {
62      // expected
63    }
64    commitTxn();
65  }
66
67  public void testOneToMany() {
68    HasDatastoreIdentityParentJDO pojo = new HasDatastoreIdentityParentJDO();
69    pojo.setName("First Name");
70    HasDatastoreIdentityChildJDO child1 = new HasDatastoreIdentityChildJDO();
71    child1.setName("Child 1");
72    pojo.getChildren().add(child1);
73    HasDatastoreIdentityChildJDO child2 = new HasDatastoreIdentityChildJDO();
74    child2.setName("Child 2");
75    pojo.getChildren().add(child2);
76
77    pm.makePersistent(pojo);
78    Object id = pm.getObjectId(pojo);
79    pm.evictAll();
80
81    HasDatastoreIdentityParentJDO pojo2 = (HasDatastoreIdentityParentJDO)pm.getObjectById(id);
82    assertNotNull(pojo2);
83    assertEquals("First Name", pojo2.getName());
84    Set<HasDatastoreIdentityChildJDO> children = pojo2.getChildren();
85    assertNotNull(children);
86    assertEquals(2, children.size());
87    pm.evictAll();
88
89    pojo2 = (HasDatastoreIdentityParentJDO)pm.getObjectById(id);
90    assertNotNull(pojo2);
91    assertEquals("First Name", pojo2.getName());
92  }
93}