PageRenderTime 33ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 304 lines | 239 code | 39 blank | 26 comment | 0 complexity | ec4b147d79428b1b6d42ee2cb4a893bc 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.jpa;
 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.datanucleus.TestUtils;
 23import com.google.appengine.datanucleus.Utils;
 24import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkJPA;
 25import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkSeparateIdFieldJPA;
 26import com.google.appengine.datanucleus.test.jpa.HasEncodedStringPkSeparateNameFieldJPA;
 27import com.google.appengine.datanucleus.test.jpa.HasKeyPkJPA;
 28import com.google.appengine.datanucleus.test.jpa.HasLongPkJPA;
 29import com.google.appengine.datanucleus.test.jpa.HasUnencodedStringPkJPA;
 30
 31
 32import java.util.List;
 33import java.util.Set;
 34
 35import javax.persistence.PersistenceException;
 36import javax.persistence.Query;
 37
 38/**
 39 * @author Max Ross <maxr@google.com>
 40 */
 41public class JPAPrimaryKeyTest extends JPATestCase {
 42  public void testLongPk() throws EntityNotFoundException {
 43    HasLongPkJPA pojo = new HasLongPkJPA();
 44    beginTxn();
 45    em.persist(pojo);
 46    commitTxn();
 47
 48    assertNotNull(pojo.getId());
 49    Entity e = ds.get(KeyFactory.createKey(HasLongPkJPA.class.getSimpleName(), pojo.getId()));
 50
 51    beginTxn();
 52    em.find(HasLongPkJPA.class, e.getKey().getId());
 53    em.find(HasLongPkJPA.class, e.getKey());
 54    em.find(HasLongPkJPA.class, KeyFactory.keyToString(e.getKey()));
 55    commitTxn();
 56  }
 57
 58  public void testLongPk_UserProvided() throws EntityNotFoundException {
 59    HasLongPkJPA pojo = new HasLongPkJPA();
 60    pojo.setId(33L);
 61    beginTxn();
 62    em.persist(pojo);
 63    commitTxn();
 64    // the fact that this doesn't throw an exception is the test
 65    ds.get(KeyFactory.createKey(HasLongPkJPA.class.getSimpleName(), 33));
 66  }
 67
 68  public void testUnencodedStringPk() throws EntityNotFoundException {
 69    HasUnencodedStringPkJPA pojo = new HasUnencodedStringPkJPA();
 70    pojo.setId("a name");
 71    beginTxn();
 72    em.persist(pojo);
 73    commitTxn();
 74
 75    assertEquals("a name", pojo.getId());
 76    Entity e = ds.get(KeyFactory.createKey(HasUnencodedStringPkJPA.class.getSimpleName(), pojo.getId()));
 77
 78    beginTxn();
 79    em.find(HasUnencodedStringPkJPA.class, e.getKey().getName());
 80    em.find(HasUnencodedStringPkJPA.class, e.getKey());
 81    em.find(HasUnencodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
 82    commitTxn();
 83  }
 84
 85  public void testUnencodedStringPk_NullValue() {
 86    HasUnencodedStringPkJPA pojo = new HasUnencodedStringPkJPA();
 87    beginTxn();
 88    em.persist(pojo);
 89    try {
 90      commitTxn();
 91      fail("expected exception");
 92    } catch (PersistenceException e) {
 93      // good
 94      rollbackTxn();
 95    }
 96  }
 97
 98  public void testEncodedStringPk() throws EntityNotFoundException {
 99    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
100    Key key = KeyFactory.createKey(HasEncodedStringPkJPA.class.getSimpleName(), "a name");
101    pojo.setId(KeyFactory.keyToString(key));
102    beginTxn();
103    em.persist(pojo);
104    commitTxn();
105
106    assertEquals(KeyFactory.keyToString(key), pojo.getId());
107    Entity e = ds.get(KeyFactory.stringToKey(pojo.getId()));
108
109    beginTxn();
110    em.find(HasEncodedStringPkJPA.class, e.getKey().getName());
111    em.find(HasEncodedStringPkJPA.class, e.getKey());
112    em.find(HasEncodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
113    commitTxn();
114  }
115
116  public void testEncodedStringPk_NullValue() throws EntityNotFoundException {
117    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
118    beginTxn();
119    em.persist(pojo);
120    commitTxn();
121    assertNotNull(pojo.getId());
122    Key key = KeyFactory.stringToKey(pojo.getId());
123    Entity e = ds.get(key);
124
125    beginTxn();
126    em.find(HasEncodedStringPkJPA.class, e.getKey().getId());
127    em.find(HasEncodedStringPkJPA.class, e.getKey());
128    em.find(HasEncodedStringPkJPA.class, KeyFactory.keyToString(e.getKey()));
129    commitTxn();
130  }
131
132  public void testEncodedStringPk_NonKeyValue() throws EntityNotFoundException {
133    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
134    pojo.setId("yar");
135    beginTxn();
136    em.persist(pojo);
137    try {
138      commitTxn();
139      fail("expected exception");
140    } catch (PersistenceException e) {
141      // good
142      rollbackTxn();
143    }
144  }
145
146  public void testEncodedStringPk_SeparateNameField() throws EntityNotFoundException {
147    HasEncodedStringPkSeparateNameFieldJPA pojo = new HasEncodedStringPkSeparateNameFieldJPA();
148    pojo.setName("a name");
149    beginTxn();
150    em.persist(pojo);
151    commitTxn();
152
153    assertEquals(TestUtils.createKey(pojo, "a name"), KeyFactory.stringToKey(pojo.getId()));
154    Entity e = ds.get(KeyFactory.stringToKey(pojo.getId()));
155
156    beginTxn();
157    pojo = em.find(HasEncodedStringPkSeparateNameFieldJPA.class, e.getKey().getName());
158    assertEquals("a name", pojo.getName());
159    em.find(HasEncodedStringPkSeparateNameFieldJPA.class, e.getKey());
160    assertEquals("a name", pojo.getName());
161    em.find(HasEncodedStringPkSeparateNameFieldJPA.class, KeyFactory.keyToString(e.getKey()));
162    assertEquals("a name", pojo.getName());
163    commitTxn();
164
165  }
166
167  public void testEncodedStringPk_SeparateIdField() throws EntityNotFoundException {
168    HasEncodedStringPkSeparateIdFieldJPA pojo = new HasEncodedStringPkSeparateIdFieldJPA();
169    pojo.setId(34L);
170    beginTxn();
171    em.persist(pojo);
172    commitTxn();
173    beginTxn();
174    assertNotNull(pojo.getId());
175    assertNotNull(pojo.getKey());
176    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
177    long id = e.getKey().getId();
178    assertEquals(34L, id);
179    pojo = em.find(HasEncodedStringPkSeparateIdFieldJPA.class, e.getKey().getId());
180    assertEquals(id, pojo.getId().longValue());
181    em.find(HasEncodedStringPkSeparateIdFieldJPA.class, e.getKey());
182    assertEquals(id, pojo.getId().longValue());
183    em.find(HasEncodedStringPkSeparateIdFieldJPA.class, KeyFactory.keyToString(e.getKey()));
184    assertEquals(id, pojo.getId().longValue());
185    commitTxn();
186
187  }
188
189  public void testKeyPk_UserProvidedId() throws EntityNotFoundException {
190    HasKeyPkJPA pojo = new HasKeyPkJPA();
191    pojo.setId(TestUtils.createKey(pojo, 34));
192    beginTxn();
193    em.persist(pojo);
194    commitTxn();
195    // the fact that this doesn't throw an exception is the test
196    ds.get(KeyFactory.createKey(HasKeyPkJPA.class.getSimpleName(), 34));
197  }
198
199  public void testCannotChangeLongPk() {
200    HasLongPkJPA pojo = new HasLongPkJPA();
201    beginTxn();
202    em.persist(pojo);
203    commitTxn();
204    beginTxn();
205    pojo = em.find(HasLongPkJPA.class, pojo.getId());
206    pojo.setId(88L);
207    try {
208      commitTxn();
209      fail("expected exception");
210    } catch (PersistenceException e) {
211      // good
212      rollbackTxn();
213    }
214  }
215  
216  public void testUnencodedStringPk_BatchGet() throws EntityNotFoundException {
217    switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
218    HasUnencodedStringPkJPA pojo = new HasUnencodedStringPkJPA();
219    beginTxn();
220    pojo.setId("yar1");
221    em.persist(pojo);
222    commitTxn();
223
224    HasUnencodedStringPkJPA pojo2 = new HasUnencodedStringPkJPA();
225    beginTxn();
226    pojo2.setId("yar2");
227    em.persist(pojo2);
228    commitTxn();
229
230    assertNotNull(pojo.getId());
231    assertNotNull(pojo2.getId());
232
233    beginTxn();
234    Query q = em.createQuery("select from " + HasUnencodedStringPkJPA.class.getName() + " b where id = :ids");
235    q.setParameter("ids", Utils.newArrayList(pojo.getId(), pojo2.getId()));
236    List<HasUnencodedStringPkJPA> pojos = (List<HasUnencodedStringPkJPA>) q.getResultList();
237    assertEquals(2, pojos.size());
238    // we should preserve order but right now we don't
239    Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
240    assertEquals(pks, Utils.newHashSet("yar1", "yar2"));
241    commitTxn();
242  }
243
244  public void testEncodedStringPk_BatchGet() throws EntityNotFoundException {
245    switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
246    HasEncodedStringPkJPA pojo = new HasEncodedStringPkJPA();
247    beginTxn();
248    String key1 = new KeyFactory.Builder("parent", 44)
249        .addChild(HasEncodedStringPkJPA.class.getSimpleName(), "yar1").getString();
250    pojo.setId(key1);
251    em.persist(pojo);
252    commitTxn();
253
254    HasEncodedStringPkJPA pojo2 = new HasEncodedStringPkJPA();
255    beginTxn();
256    String key2 = new KeyFactory.Builder("parent", 44)
257        .addChild(HasEncodedStringPkJPA.class.getSimpleName(), "yar2").getString();        
258    pojo2.setId(key2);
259    em.persist(pojo2);
260    commitTxn();
261
262    assertNotNull(pojo.getId());
263    assertNotNull(pojo2.getId());
264
265    beginTxn();
266    Query q = em.createQuery("select from " + HasEncodedStringPkJPA.class.getName() + " b where id = :ids");
267    q.setParameter("ids", Utils.newArrayList(pojo.getId(), pojo2.getId()));
268    List<HasEncodedStringPkJPA> pojos = (List<HasEncodedStringPkJPA>) q.getResultList();
269    assertEquals(2, pojos.size());
270    // we should preserve order but right now we don't
271    Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
272    assertEquals(pks, Utils.newHashSet(key1, key2));
273    commitTxn();
274  }
275
276  public void testUnencodedLongPk_BatchGet() throws EntityNotFoundException {
277    switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
278    HasLongPkJPA pojo = new HasLongPkJPA();
279    beginTxn();
280    pojo.setId(1L);
281    em.persist(pojo);
282    commitTxn();
283
284    HasLongPkJPA pojo2 = new HasLongPkJPA();
285    beginTxn();
286    pojo2.setId(2L);
287    em.persist(pojo2);
288    commitTxn();
289
290    assertNotNull(pojo.getId());
291    assertNotNull(pojo2.getId());
292
293    beginTxn();
294    Query q = em.createQuery("select from " + HasLongPkJPA.class.getName() + " b where id = :ids");
295    q.setParameter("ids", Utils.newArrayList(pojo.getId(), pojo2.getId()));
296    List<HasLongPkJPA> pojos = (List<HasLongPkJPA>) q.getResultList();
297    assertEquals(2, pojos.size());
298    // we should preserve order but right now we don't
299    Set<Long> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
300    assertEquals(pks, Utils.newHashSet(1L, 2L));
301    commitTxn();
302  }
303  
304}