PageRenderTime 97ms CodeModel.GetById 41ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 275 lines | 218 code | 35 blank | 22 comment | 0 complexity | 4bf04c68093f9fee7390aeb9a69f4f1f 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.KeyFactory;
 21import com.google.appengine.datanucleus.TestUtils;
 22import com.google.appengine.datanucleus.Utils;
 23import com.google.appengine.datanucleus.test.jdo.Flight;
 24import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO;
 25import com.google.appengine.datanucleus.test.jdo.HasPromotedTypesJDO;
 26import com.google.appengine.datanucleus.test.jdo.HasVersionNoFieldJDO;
 27import com.google.appengine.datanucleus.test.jdo.HasVersionWithFieldJDO;
 28import com.google.appengine.datanucleus.test.jdo.KitchenSink;
 29import com.google.appengine.datanucleus.test.jdo.Name;
 30import com.google.appengine.datanucleus.test.jdo.Person;
 31
 32
 33import java.util.Arrays;
 34import java.util.HashMap;
 35import java.util.Map;
 36
 37import javax.jdo.JDOFatalUserException;
 38import javax.jdo.JDOHelper;
 39import javax.jdo.PersistenceManager;
 40import javax.jdo.PersistenceManagerFactory;
 41
 42/**
 43 * @author Max Ross <maxr@google.com>
 44 */
 45public class JDOInsertionTest extends JDOTestCase {
 46
 47  private static final String DEFAULT_VERSION_PROPERTY_NAME = "VERSION";
 48
 49  public void testSimpleInsert() throws EntityNotFoundException {
 50    Flight f1 = new Flight();
 51    f1.setOrigin("BOS");
 52    f1.setDest("MIA");
 53    f1.setMe(2);
 54    f1.setYou(4);
 55    f1.setName("Harold");
 56    assertNull(f1.getId());
 57    makePersistentInTxn(f1, TXN_START_END);
 58    assertNotNull(f1.getId());
 59    Entity entity = ds.get(KeyFactory.stringToKey(f1.getId()));
 60    assertNotNull(entity);
 61    assertEquals("BOS", entity.getProperty("origin"));
 62    assertEquals("MIA", entity.getProperty("dest"));
 63    assertEquals("Harold", entity.getProperty("name"));
 64    assertEquals(2L, entity.getProperty("me"));
 65    assertEquals(4L, entity.getProperty("you"));
 66    assertEquals(1L, entity.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
 67    assertEquals(Flight.class.getSimpleName(), entity.getKind());
 68  }
 69
 70  public void testSimpleInsertWithNamedKey() throws EntityNotFoundException {
 71    Flight f = new Flight();
 72    f.setId(KeyFactory.keyToString(KeyFactory.createKey(Flight.class.getSimpleName(), "foo")));
 73    assertNotNull(f.getId());
 74    makePersistentInTxn(f, TXN_START_END);
 75    Entity entity = ds.get(KeyFactory.stringToKey(f.getId()));
 76    assertNotNull(entity);
 77    assertEquals("foo", entity.getKey().getName());
 78  }
 79
 80  public void testKitchenSinkInsert() throws EntityNotFoundException {
 81    Map<String, String> props = new HashMap<String, String>();
 82    props.put("datanucleus.appengine.BigDecimalsEncoding", "String");
 83    switchDatasource(PersistenceManagerFactoryName.originalStorageVersion, props);
 84    KitchenSink ks = KitchenSink.newKitchenSink();
 85    assertNull(ks.key);
 86    makePersistentInTxn(ks, TXN_START_END);
 87    assertNotNull(ks.key);
 88
 89    Entity entity = ds.get(KeyFactory.stringToKey(ks.key));
 90    assertNotNull(entity);
 91    assertEquals(KitchenSink.class.getSimpleName(), entity.getKind());
 92
 93    Entity sameEntity = KitchenSink.newKitchenSinkEntity(KeyFactory.stringToKey(ks.key));
 94    assertFalse(entity.getProperties().keySet().contains("an extra property"));
 95    entity.setProperty("an extra property", "yar!");
 96    assertEquals(sameEntity.getProperties(), entity.getProperties());
 97  }
 98
 99  public void testKitchenSinkInsertWithNulls() throws EntityNotFoundException {
100    KitchenSink allNulls = new KitchenSink();
101    makePersistentInTxn(allNulls, TXN_START_END);
102    Entity entityWithNulls = ds.get(KeyFactory.stringToKey(allNulls.key));
103    assertNotNull(entityWithNulls);
104
105    // now create a KitchenSink with non-null values
106    KitchenSink noNulls = KitchenSink.newKitchenSink();
107    makePersistentInTxn(noNulls, TXN_START_END);
108    Entity entityWithoutNulls = ds.get(KeyFactory.stringToKey(noNulls.key));
109    assertNotNull(entityWithoutNulls);
110
111    assertEquals(entityWithNulls.getProperties().keySet(), entityWithoutNulls.getProperties().keySet());
112  }
113
114  public void testVersionInserts() throws EntityNotFoundException {
115    HasVersionNoFieldJDO hv = new HasVersionNoFieldJDO();
116    makePersistentInTxn(hv, TXN_START_END);
117
118    Entity entity = ds.get(
119        KeyFactory.createKey(HasVersionNoFieldJDO.class.getSimpleName(), hv.getId()));
120    assertNotNull(entity);
121    assertEquals(1L, entity.getProperty("myversioncolumn"));
122
123    HasVersionWithFieldJDO hvwf = new HasVersionWithFieldJDO();
124    beginTxn();
125    pm.makePersistent(hvwf);
126    Long id = hvwf.getId();
127    assertNotNull(id);
128    commitTxn();
129    beginTxn();
130    hvwf = pm.getObjectById(HasVersionWithFieldJDO.class, id);
131    entity = ds.get(TestUtils.createKey(hvwf, id));
132    assertNotNull(entity);
133    assertEquals(1L, entity.getProperty(DEFAULT_VERSION_PROPERTY_NAME));
134    assertEquals(1L, hvwf.getVersion());
135    commitTxn();
136  }
137
138  public void testInsertWithKeyPk() {
139    HasKeyPkJDO hk = new HasKeyPkJDO();
140
141    beginTxn();
142    pm.makePersistent(hk);
143
144    assertNotNull(hk.getKey());
145    assertNull(hk.getAncestorKey());
146    commitTxn();
147  }
148
149  public void testInsertWithNamedKeyPk() {
150    HasKeyPkJDO hk = new HasKeyPkJDO();
151    hk.setKey(KeyFactory.createKey(HasKeyPkJDO.class.getSimpleName(), "name"));
152    makePersistentInTxn(hk, TXN_START_END);
153
154    assertNotNull(hk.getKey());
155    assertEquals("name", hk.getKey().getName());
156  }
157
158  public void testEmbeddable() throws EntityNotFoundException {
159    Person p = new Person();
160    p.setName(new Name());
161    p.getName().setFirst("jimmy");
162    p.getName().setLast("jam");
163    p.setAnotherName(new Name());
164    p.getAnotherName().setFirst("anotherjimmy");
165    p.getAnotherName().setLast("anotherjam");
166    makePersistentInTxn(p, TXN_START_END);
167
168    assertNotNull(p.getId());
169
170    Entity entity = ds.get(KeyFactory.createKey(Person.class.getSimpleName(), p.getId()));
171    assertNotNull(entity);
172    assertEquals("jimmy", entity.getProperty("first"));
173    assertEquals("jam", entity.getProperty("last"));
174    assertEquals("anotherjimmy", entity.getProperty("anotherFirst"));
175    assertEquals("anotherjam", entity.getProperty("anotherLast"));
176  }
177
178  public void testNullEmbeddable() throws EntityNotFoundException {
179    Person p = new Person();
180    p.setName(new Name());
181    p.getName().setFirst("jimmy");
182    p.getName().setLast("jam");
183    makePersistentInTxn(p, TXN_START_END);
184
185    assertNotNull(p.getId());
186
187    Entity entity = ds.get(KeyFactory.createKey(Person.class.getSimpleName(), p.getId()));
188    assertNotNull(entity);
189    assertEquals("jimmy", entity.getProperty("first"));
190    assertEquals("jam", entity.getProperty("last"));
191    assertNull(entity.getProperty("anotherFirst"));
192    assertNull(entity.getProperty("anotherLast"));
193  }
194
195  public void testFetchOfCachedPojo() throws Exception {
196    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(
197        PersistenceManagerFactoryName.nontransactional.name());
198    PersistenceManager pm = pmf.getPersistenceManager();
199    // TODO(maxr,bslatkin): Remove this next line once this test
200    // is actually working properly.
201    pm.currentTransaction().setRetainValues(false);
202    
203    try {
204      HasPromotedTypesJDO pojo = new HasPromotedTypesJDO();
205      pojo.setIntVal(33);
206      pojo.setIntegerVal(34);
207      pojo.setIntArray(new int[] {0, 1, 2});
208      pojo.setIntegerArray(new Integer[] {3, 4, 5});
209      pojo.setIntList(Utils.newArrayList(3, 4, 5));
210      pojo.setIntSet(Utils.newHashSet(3, 4, 5));
211      pojo.setIntLinkedList(Utils.newLinkedList(31, 41, 51));
212      pojo.setIntLinkedHashSet(Utils.newLinkedHashSet(31, 41, 51));
213      pojo.setLongPrimVal(35L);
214      pojo.setLongVal(36L);
215      pojo.setLongPrimArray(new long[] {6L, 7L, 8L});
216      pojo.setLongArray(new Long[] {9L, 10L, 11L});
217      pojo.setLongList(Utils.newArrayList(12L, 13L, 14L));
218      pojo.setLongSet(Utils.newHashSet(12L, 13L, 14L));
219      pojo.setLongLinkedList(Utils.newLinkedList(121L, 131L, 141L));
220      pojo.setLongLinkedHashSet(Utils.newLinkedHashSet(121L, 131L, 141L));
221      pm.currentTransaction().begin();
222      pm.makePersistent(pojo);
223      pm.currentTransaction().commit();
224
225      pojo = pm.getObjectById(HasPromotedTypesJDO.class, pojo.getId());
226      assertEquals(33, pojo.getIntVal());
227      assertEquals(Integer.valueOf(34), pojo.getIntegerVal());
228      assertTrue(Arrays.equals(new int[] {0, 1, 2}, pojo.getIntArray()));
229      assertTrue(Arrays.equals(new Integer[] {3, 4, 5}, pojo.getIntegerArray()));
230      assertEquals(Utils.newArrayList(3, 4, 5), pojo.getIntList());
231      assertEquals(Utils.newHashSet(3, 4, 5), pojo.getIntSet());
232      assertEquals(Utils.newLinkedList(31, 41, 51), pojo.getIntLinkedList());
233      assertEquals(Utils.newLinkedHashSet(31, 41, 51), pojo.getIntLinkedHashSet());
234      assertEquals(35L, pojo.getLongPrimVal());
235      assertEquals(Long.valueOf(36L), pojo.getLongVal());
236      assertTrue(Arrays.equals(new long[] {6L, 7L, 8L}, pojo.getLongPrimArray()));
237      assertTrue(Arrays.equals(new Long[] {9L, 10L, 11L}, pojo.getLongArray()));
238      assertEquals(Utils.newArrayList(12L, 13L, 14L), pojo.getLongList());
239      assertEquals(Utils.newHashSet(12L, 13L, 14L), pojo.getLongSet());
240      assertEquals(Utils.newLinkedList(121L, 131L, 141L), pojo.getLongLinkedList());
241      assertEquals(Utils.newLinkedHashSet(121L, 131L, 141L), pojo.getLongLinkedHashSet());
242    } finally {
243      pm.close();
244      pmf.close();
245    }
246  }
247
248  public void testInsertMultipleEntityGroups() {
249    Flight f1 = new Flight();
250    f1.setOrigin("BOS");
251    f1.setDest("MIA");
252    f1.setMe(2);
253    f1.setYou(4);
254    f1.setName("Harold");
255
256    Flight f2 = new Flight();
257    f2.setOrigin("BOS");
258    f2.setDest("MIA");
259    f2.setMe(2);
260    f2.setYou(4);
261    f2.setName("Harold");
262
263    beginTxn();
264    pm.makePersistent(f1);
265    try {
266      pm.makePersistent(f2);
267      fail("expected exception");
268    } catch (JDOFatalUserException iae) {
269      // good
270    } finally {
271      rollbackTxn();
272    }
273  }
274}
275