PageRenderTime 34ms CodeModel.GetById 9ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jpa/JPAAbstractBaseClassTest.java

http://datanucleus-appengine.googlecode.com/
Java | 247 lines | 185 code | 39 blank | 23 comment | 0 complexity | 3e28601601c9f76f118a01f09eba4595 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.jpa;
 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.jpa.AbstractBaseClassesJPA.Concrete1;
 23import com.google.appengine.datanucleus.test.jpa.AbstractBaseClassesJPA.Concrete2;
 24import com.google.appengine.datanucleus.test.jpa.AbstractBaseClassesJPA.Concrete3;
 25import com.google.appengine.datanucleus.test.jpa.AbstractBaseClassesJPA.Concrete4;
 26
 27
 28/**
 29 * @author Max Ross <max.ross@gmail.com>
 30 */
 31public class JPAAbstractBaseClassTest extends JPATestCase {
 32
 33  public void testConcrete() throws EntityNotFoundException {
 34    Concrete1 concrete = new Concrete1();
 35    concrete.setBase1Str("base 1");
 36    concrete.setConcrete1Str("concrete");
 37    Concrete3 concrete3 = new Concrete3();
 38    concrete3.setStr("str3");
 39    concrete.setConcrete3(concrete3);
 40    Concrete4 concrete4a = new Concrete4();
 41    concrete4a.setStr("str4a");
 42    Concrete4 concrete4b = new Concrete4();
 43    concrete4b.setStr("str4b");
 44    concrete.setConcrete4(Utils.newArrayList(concrete4a, concrete4b));
 45
 46    beginTxn();
 47    em.persist(concrete);
 48    commitTxn();
 49
 50    Entity concreteEntity = ds.get(KeyFactory.createKey(kindForObject(concrete), concrete.getId()));
 51    Entity concrete3Entity = ds.get(concrete3.getId());
 52    Entity concrete4aEntity = ds.get(concrete4a.getId());
 53    Entity concrete4bEntity = ds.get(concrete4b.getId());
 54
 55    assertEquals(4, concreteEntity.getProperties().size());
 56    assertEquals("base 1", concreteEntity.getProperty("base1Str"));
 57    assertEquals("concrete", concreteEntity.getProperty("concrete1Str"));
 58    assertEquals(concrete3Entity.getKey(), concreteEntity.getProperty("concrete3_id"));
 59    assertEquals(Utils.newArrayList(concrete4aEntity.getKey(), concrete4bEntity.getKey()),
 60                 concreteEntity.getProperty("concrete4"));
 61
 62    assertEquals(1, concrete3Entity.getProperties().size());
 63    assertEquals("str3", concrete3Entity.getProperty("str"));
 64
 65    assertEquals(1, concrete4aEntity.getProperties().size());
 66    assertEquals("str4a", concrete4aEntity.getProperty("str"));
 67
 68    assertEquals(1, concrete4bEntity.getProperties().size());
 69    assertEquals("str4b", concrete4bEntity.getProperty("str"));
 70
 71    beginTxn();
 72    concrete = em.find(concrete.getClass(), concrete.getId());
 73    assertEquals("base 1", concrete.getBase1Str());
 74    assertEquals("concrete", concrete.getConcrete1Str());
 75    assertEquals(concrete3.getId(), concrete.getConcrete3().getId());
 76    assertEquals(concrete3.getStr(), concrete.getConcrete3().getStr());
 77    assertEquals(2, concrete.getConcrete4().size());
 78    assertEquals(concrete4a.getId(), concrete.getConcrete4().get(0).getId());
 79    assertEquals(concrete4a.getStr(), concrete.getConcrete4().get(0).getStr());
 80    assertEquals(concrete4b.getId(), concrete.getConcrete4().get(1).getId());
 81    assertEquals(concrete4b.getStr(), concrete.getConcrete4().get(1).getStr());
 82
 83    concrete.setBase1Str("not base 1");
 84    concrete.setConcrete1Str("not concrete");
 85
 86    concrete.getConcrete3().setStr("blam3");
 87    concrete.getConcrete4().get(0).setStr("blam4");
 88    commitTxn();
 89
 90    concreteEntity = ds.get(KeyFactory.createKey(kindForObject(concrete), concrete.getId()));
 91    concrete3Entity = ds.get(concrete3.getId());
 92    concrete4aEntity = ds.get(concrete4a.getId());
 93
 94    assertEquals(4, concreteEntity.getProperties().size());
 95    assertEquals("not base 1", concreteEntity.getProperty("base1Str"));
 96    assertEquals("not concrete", concreteEntity.getProperty("concrete1Str"));
 97    assertEquals(concrete3Entity.getKey(), concreteEntity.getProperty("concrete3_id"));
 98    assertEquals(Utils.newArrayList(concrete4aEntity.getKey(), concrete4bEntity.getKey()),
 99                 concreteEntity.getProperty("concrete4"));
100
101    assertEquals(1, concrete3Entity.getProperties().size());
102    assertEquals("blam3", concrete3Entity.getProperty("str"));
103
104    assertEquals(1, concrete4aEntity.getProperties().size());
105    assertEquals("blam4", concrete4aEntity.getProperty("str"));
106    try {
107      ds.get(concrete4b.getId());
108    } catch (EntityNotFoundException enfe) {
109      // good
110    }
111    beginTxn();
112    concrete = em.find(concrete.getClass(), concrete.getId());
113    assertEquals("not base 1", concrete.getBase1Str());
114    assertEquals("not concrete", concrete.getConcrete1Str());
115    assertEquals(concrete3.getId(), concrete.getConcrete3().getId());
116    assertEquals("blam3", concrete.getConcrete3().getStr());
117    assertEquals(2, concrete.getConcrete4().size());
118    assertEquals(concrete4a.getId(), concrete.getConcrete4().get(0).getId());
119    assertEquals("blam4", concrete.getConcrete4().get(0).getStr());
120
121    assertNotNull(em.createQuery(
122        "select from " + concrete.getClass().getName() + " b where base1Str = 'not base 1'").getSingleResult());
123    assertNotNull(em.createQuery(
124        "select from " + concrete.getClass().getName() + " b where concrete1Str = 'not concrete'").getSingleResult());
125
126    em.remove(concrete);
127    commitTxn();
128
129    assertEquals(0, countForClass(concrete.getClass()));
130    assertEquals(0, countForClass(concrete3.getClass()));
131    assertEquals(0, countForClass(concrete4a.getClass()));
132  }
133
134  // This test fails under the runtime enhancer when run right after the test
135  // above.  It does fine when run on its own and under the compile-time
136  // enhancer.  Sadness.
137  public void testConcrete2() throws EntityNotFoundException {
138    Concrete2 concrete = new Concrete2();
139    concrete.setBase1Str("base 1");
140    concrete.setBase2Str("base 2");
141    concrete.setConcrete2Str("concrete");
142    Concrete3 concrete3 = new Concrete3();
143    concrete3.setStr("str3");
144    concrete.setConcrete3(concrete3);
145    Concrete4 concrete4a = new Concrete4();
146    concrete4a.setStr("str4a");
147    Concrete4 concrete4b = new Concrete4();
148    concrete4b.setStr("str4b");
149    concrete.setConcrete4(Utils.newArrayList(concrete4a, concrete4b));
150
151    beginTxn();
152    em.persist(concrete);
153    commitTxn();
154
155    Entity concreteEntity = ds.get(KeyFactory.createKey(kindForObject(concrete), concrete.getId()));
156    Entity concrete3Entity = ds.get(concrete3.getId());
157    Entity concrete4aEntity = ds.get(concrete4a.getId());
158    Entity concrete4bEntity = ds.get(concrete4b.getId());
159
160    assertEquals(5, concreteEntity.getProperties().size());
161    assertEquals("base 1", concreteEntity.getProperty("base1Str"));
162    assertEquals("base 2", concreteEntity.getProperty("base2Str"));
163    assertEquals("concrete", concreteEntity.getProperty("concrete2Str"));
164    assertEquals(concrete3Entity.getKey(), concreteEntity.getProperty("concrete3_id"));
165    assertEquals(Utils.newArrayList(concrete4aEntity.getKey(), concrete4bEntity.getKey()),
166                 concreteEntity.getProperty("concrete4"));
167
168    assertEquals(1, concrete3Entity.getProperties().size());
169    assertEquals("str3", concrete3Entity.getProperty("str"));
170
171    assertEquals(1, concrete4aEntity.getProperties().size());
172    assertEquals("str4a", concrete4aEntity.getProperty("str"));
173
174    assertEquals(1, concrete4bEntity.getProperties().size());
175    assertEquals("str4b", concrete4bEntity.getProperty("str"));
176
177    beginTxn();
178    concrete = em.find(concrete.getClass(), concrete.getId());
179    assertEquals("base 1", concrete.getBase1Str());
180    assertEquals("base 2", concrete.getBase2Str());
181    assertEquals("concrete", concrete.getConcrete2Str());
182
183    assertEquals(concrete3.getId(), concrete.getConcrete3().getId());
184    assertEquals(concrete3.getStr(), concrete.getConcrete3().getStr());
185    assertEquals(2, concrete.getConcrete4().size());
186    assertEquals(concrete4a.getId(), concrete.getConcrete4().get(0).getId());
187    assertEquals(concrete4a.getStr(), concrete.getConcrete4().get(0).getStr());
188    assertEquals(concrete4b.getId(), concrete.getConcrete4().get(1).getId());
189    assertEquals(concrete4b.getStr(), concrete.getConcrete4().get(1).getStr());
190
191    concrete.setBase1Str("not base 1");
192    concrete.setBase2Str("not base 2");
193    concrete.setConcrete2Str("not concrete");
194
195    concrete.getConcrete3().setStr("blam3");
196    concrete.getConcrete4().get(0).setStr("blam4");
197    commitTxn();
198
199    concreteEntity = ds.get(KeyFactory.createKey(kindForObject(concrete), concrete.getId()));
200    concrete3Entity = ds.get(concrete3.getId());
201    concrete4aEntity = ds.get(concrete4a.getId());
202
203    assertEquals(5, concreteEntity.getProperties().size());
204    assertEquals("not base 1", concreteEntity.getProperty("base1Str"));
205    assertEquals("not base 2", concreteEntity.getProperty("base2Str"));
206    assertEquals("not concrete", concreteEntity.getProperty("concrete2Str"));
207    assertEquals(concrete3Entity.getKey(), concreteEntity.getProperty("concrete3_id"));
208    assertEquals(Utils.newArrayList(concrete4aEntity.getKey(), concrete4bEntity.getKey()),
209                 concreteEntity.getProperty("concrete4"));
210
211    assertEquals(1, concrete3Entity.getProperties().size());
212    assertEquals("blam3", concrete3Entity.getProperty("str"));
213
214    assertEquals(1, concrete4aEntity.getProperties().size());
215    assertEquals("blam4", concrete4aEntity.getProperty("str"));
216    try {
217      ds.get(concrete4b.getId());
218    } catch (EntityNotFoundException enfe) {
219      // good
220    }
221
222    beginTxn();
223    concrete = em.find(concrete.getClass(), concrete.getId());
224    assertEquals("not base 1", concrete.getBase1Str());
225    assertEquals("not base 2", concrete.getBase2Str());
226    assertEquals("not concrete", concrete.getConcrete2Str());
227    assertEquals(concrete3.getId(), concrete.getConcrete3().getId());
228    assertEquals("blam3", concrete.getConcrete3().getStr());
229    assertEquals(2, concrete.getConcrete4().size());
230    assertEquals(concrete4a.getId(), concrete.getConcrete4().get(0).getId());
231    assertEquals("blam4", concrete.getConcrete4().get(0).getStr());
232
233    assertNotNull(em.createQuery(
234        "select from " + concrete.getClass().getName() + " b where base1Str = 'not base 1'").getSingleResult());
235    assertNotNull(em.createQuery(
236        "select from " + concrete.getClass().getName() + " b where base2Str = 'not base 2'").getSingleResult());
237    assertNotNull(em.createQuery(
238        "select from " + concrete.getClass().getName() + " b where concrete2Str = 'not concrete'").getSingleResult());
239
240    em.remove(concrete);
241    commitTxn();
242
243    assertEquals(0, countForClass(concrete.getClass()));
244    assertEquals(0, countForClass(concrete3.getClass()));
245    assertEquals(0, countForClass(concrete4a.getClass()));
246  }
247}