PageRenderTime 61ms CodeModel.GetById 28ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 403 lines | 350 code | 34 blank | 19 comment | 0 complexity | f31f816892445348593fab2a57f423e6 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.Book;
 25import com.google.appengine.datanucleus.test.jpa.HasMultiValuePropsJPA;
 26
 27
 28import java.util.ArrayList;
 29import java.util.HashSet;
 30import java.util.LinkedHashSet;
 31import java.util.LinkedList;
 32import java.util.List;
 33import java.util.Set;
 34import java.util.SortedSet;
 35import java.util.TreeSet;
 36
 37/**
 38 * @author Erick Armbrust <earmbrust@google.com>
 39 * @author Max Ross <maxr@google.com>
 40 */
 41public class JPAUpdateTest extends JPATestCase {
 42
 43  public void testUpdateAfterFetch() throws EntityNotFoundException {
 44    Key key = ds.put(Book.newBookEntity("jimmy", "12345", "the title"));
 45
 46    String keyStr = KeyFactory.keyToString(key);
 47    beginTxn();
 48    Book book = em.find(Book.class, keyStr);
 49
 50    assertEquals(keyStr, book.getId());
 51    assertEquals("jimmy", book.getAuthor());
 52    assertEquals("12345", book.getIsbn());
 53    assertEquals("the title", book.getTitle());
 54
 55    book.setIsbn("56789");
 56    commitTxn();
 57
 58    Entity bookCheck = ds.get(key);
 59    assertEquals("jimmy", bookCheck.getProperty("author"));
 60    assertEquals("56789", bookCheck.getProperty("isbn"));
 61    assertEquals("the title", bookCheck.getProperty("title"));
 62  }
 63
 64  public void testUpdateAfterSave() throws EntityNotFoundException {
 65    Book b = new Book();
 66    b.setAuthor("max");
 67    b.setIsbn("22333");
 68    b.setTitle("yam");
 69
 70    beginTxn();
 71    em.persist(b);
 72    commitTxn();
 73
 74    assertNotNull(b.getId());
 75
 76    beginTxn();
 77    b.setTitle("not yam");
 78    em.merge(b);
 79    commitTxn();
 80
 81    Entity bookCheck = ds.get(KeyFactory.stringToKey(b.getId()));
 82    assertEquals("max", bookCheck.getProperty("author"));
 83    assertEquals("22333", bookCheck.getProperty("isbn"));
 84    assertEquals("not yam", bookCheck.getProperty("title"));
 85  }
 86
 87  public void testUpdateList_Add() throws EntityNotFoundException {
 88    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
 89    List<String> list = Utils.newArrayList("a", "b");
 90    pojo.setStrList(list);
 91    beginTxn();
 92    em.persist(pojo);
 93    commitTxn();
 94    em.close();
 95    em = emf.createEntityManager();
 96    beginTxn();
 97    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
 98    pojo.getStrList().add("zoom");
 99    commitTxn();
100    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
101    assertEquals(3, ((List<?>)e.getProperty("strList")).size());
102  }
103
104  public void testUpdateList_Reset() throws EntityNotFoundException {
105    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
106    List<String> list = Utils.newArrayList("a", "b");
107    pojo.setStrList(list);
108    beginTxn();
109    em.persist(pojo);
110    commitTxn();
111    em.close();
112    em = emf.createEntityManager();
113    beginTxn();
114    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
115    list = Utils.newArrayList("a", "b", "zoom");
116    pojo.setStrList(list);
117    commitTxn();
118    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
119    assertEquals(3, ((List<?>)e.getProperty("strList")).size());
120  }
121
122  public void testUpdateCollection_Add() throws EntityNotFoundException {
123    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
124    List<Integer> list = Utils.newArrayList(2, 3);
125    pojo.setIntColl(list);
126    beginTxn();
127    em.persist(pojo);
128    commitTxn();
129    em.close();
130    em = emf.createEntityManager();
131    beginTxn();
132    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
133    pojo.getIntColl().add(4);
134    commitTxn();
135    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
136    assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
137  }
138
139  public void testUpdateCollection_Reset() throws EntityNotFoundException {
140    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
141    List<Integer> list = Utils.newArrayList(2, 3);
142    pojo.setIntColl(list);
143    beginTxn();
144    em.persist(pojo);
145    commitTxn();
146    em.close();
147    em = emf.createEntityManager();
148    beginTxn();
149    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
150    list = Utils.newArrayList(2, 3, 4);
151    pojo.setIntColl(list);
152    commitTxn();
153    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
154    assertEquals(3, ((List<?>)e.getProperty("intColl")).size());
155  }
156
157  public void testUpdateArrayList_Add() throws EntityNotFoundException {
158    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
159    ArrayList<String> list = Utils.newArrayList("a", "b");
160    pojo.setStrArrayList(list);
161    beginTxn();
162    em.persist(pojo);
163    commitTxn();
164    em.close();
165    em = emf.createEntityManager();
166    beginTxn();
167    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
168    pojo.getStrArrayList().add("zoom");
169    commitTxn();
170    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
171    assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
172  }
173
174  public void testUpdateArrayList_Reset() throws EntityNotFoundException {
175    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
176    ArrayList<String> list = Utils.newArrayList("a", "b");
177    pojo.setStrArrayList(list);
178    beginTxn();
179    em.persist(pojo);
180    commitTxn();
181    em.close();
182    em = emf.createEntityManager();
183    beginTxn();
184    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
185    list = Utils.newArrayList("a", "b", "zoom");
186    pojo.setStrArrayList(list);
187    commitTxn();
188    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
189    assertEquals(3, ((List<?>)e.getProperty("strArrayList")).size());
190  }
191
192  public void testUpdateLinkedList_Add() throws EntityNotFoundException {
193    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
194    LinkedList<String> list = Utils.newLinkedList("a", "b");
195    pojo.setStrLinkedList(list);
196    beginTxn();
197    em.persist(pojo);
198    commitTxn();
199    em.close();
200    em = emf.createEntityManager();
201    beginTxn();
202    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
203    pojo.getStrLinkedList().add("zoom");
204    commitTxn();
205    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
206    assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
207  }
208
209  public void testUpdateLinkedList_Reset() throws EntityNotFoundException {
210    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
211    LinkedList<String> list = Utils.newLinkedList("a", "b");
212    pojo.setStrLinkedList(list);
213    beginTxn();
214    em.persist(pojo);
215    commitTxn();
216    em.close();
217    em = emf.createEntityManager();
218    beginTxn();
219    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
220    list = Utils.newLinkedList("a", "b", "zoom");
221    pojo.setStrLinkedList(list);
222    commitTxn();
223    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
224    assertEquals(3, ((List<?>)e.getProperty("strLinkedList")).size());
225  }
226
227  public void testUpdateSet_Add() throws EntityNotFoundException {
228    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
229    Set<String> set = Utils.newHashSet("a", "b");
230    pojo.setStrSet(set);
231    beginTxn();
232    em.persist(pojo);
233    commitTxn();
234    em.close();
235    em = emf.createEntityManager();
236    beginTxn();
237    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
238    pojo.getStrSet().add("zoom");
239    commitTxn();
240    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
241    assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
242  }
243
244  public void testUpdateSet_Reset() throws EntityNotFoundException {
245    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
246    Set<String> set = Utils.newHashSet("a", "b");
247    pojo.setStrSet(set);
248    beginTxn();
249    em.persist(pojo);
250    commitTxn();
251    em.close();
252    em = emf.createEntityManager();
253    beginTxn();
254    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
255    set = Utils.newHashSet("a", "b", "zoom");
256    pojo.setStrSet(set);
257    commitTxn();
258    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
259    assertEquals(3, ((List<?>)e.getProperty("strSet")).size());
260  }
261
262  public void testUpdateHashSet_Add() throws EntityNotFoundException {
263    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
264    HashSet<String> set = Utils.newHashSet("a", "b");
265    pojo.setStrHashSet(set);
266    beginTxn();
267    em.persist(pojo);
268    commitTxn();
269    em.close();
270    em = emf.createEntityManager();
271    beginTxn();
272    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
273    pojo.getStrHashSet().add("zoom");
274    commitTxn();
275    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
276    assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
277  }
278
279  public void testUpdateHashSet_Reset() throws EntityNotFoundException {
280    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
281    HashSet<String> set = Utils.newHashSet("a", "b");
282    pojo.setStrHashSet(set);
283    beginTxn();
284    em.persist(pojo);
285    commitTxn();
286    em.close();
287    em = emf.createEntityManager();
288    beginTxn();
289    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
290    set = Utils.newHashSet("a", "b", "zoom");
291    pojo.setStrHashSet(set);
292    commitTxn();
293    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
294    assertEquals(3, ((List<?>)e.getProperty("strHashSet")).size());
295  }
296
297  public void testUpdateLinkedHashSet_Add() throws EntityNotFoundException {
298    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
299    LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
300    pojo.setStrLinkedHashSet(set);
301    beginTxn();
302    em.persist(pojo);
303    commitTxn();
304    em.close();
305    em = emf.createEntityManager();
306    beginTxn();
307    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
308    pojo.getStrLinkedHashSet().add("zoom");
309    commitTxn();
310    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
311    assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
312  }
313
314  public void testUpdateLinkedHashSet_Reset() throws EntityNotFoundException {
315    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
316    LinkedHashSet<String> set = Utils.newLinkedHashSet("a", "b");
317    pojo.setStrLinkedHashSet(set);
318    beginTxn();
319    em.persist(pojo);
320    commitTxn();
321    em.close();
322    em = emf.createEntityManager();
323    beginTxn();
324    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
325    set = Utils.newLinkedHashSet("a", "b", "zoom");
326    pojo.setStrLinkedHashSet(set);
327    commitTxn();
328    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
329    assertEquals(3, ((List<?>)e.getProperty("strLinkedHashSet")).size());
330  }
331
332  public void testUpdateTreeSet_Add() throws EntityNotFoundException {
333    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
334    TreeSet<String> set = Utils.newTreeSet("a", "b");
335    pojo.setStrTreeSet(set);
336    beginTxn();
337    em.persist(pojo);
338    commitTxn();
339    em.close();
340    em = emf.createEntityManager();
341    beginTxn();
342    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
343    pojo.getStrTreeSet().add("zoom");
344    commitTxn();
345    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
346    assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
347  }
348
349  public void testUpdateTreeSet_Reset() throws EntityNotFoundException {
350    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
351    TreeSet<String> set = Utils.newTreeSet("a", "b");
352    pojo.setStrTreeSet(set);
353    beginTxn();
354    em.persist(pojo);
355    commitTxn();
356    em.close();
357    em = emf.createEntityManager();
358    beginTxn();
359    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
360    set = Utils.newTreeSet("a", "b", "zoom");
361    pojo.setStrTreeSet(set);
362    commitTxn();
363    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
364    assertEquals(3, ((List<?>)e.getProperty("strTreeSet")).size());
365  }
366
367  public void testUpdateSortedSet_Add() throws EntityNotFoundException {
368    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
369    SortedSet<String> set = Utils.newTreeSet("a", "b");
370    pojo.setStrSortedSet(set);
371    beginTxn();
372    em.persist(pojo);
373    commitTxn();
374    em.close();
375    em = emf.createEntityManager();
376    beginTxn();
377    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
378    pojo.getStrSortedSet().add("zoom");
379    commitTxn();
380    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
381    assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
382  }
383
384  public void testUpdateSortedSet_Reset() throws EntityNotFoundException {
385    HasMultiValuePropsJPA pojo = new HasMultiValuePropsJPA();
386    SortedSet<String> set = Utils.newTreeSet("a", "b");
387    pojo.setStrSortedSet(set);
388    beginTxn();
389    em.persist(pojo);
390    commitTxn();
391    em.close();
392    em = emf.createEntityManager();
393    beginTxn();
394    pojo = em.find(HasMultiValuePropsJPA.class, pojo.getId());
395    set = Utils.newTreeSet("a", "b", "zoom");
396    pojo.setStrSortedSet(set);
397    commitTxn();
398    Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId()));
399    assertEquals(3, ((List<?>)e.getProperty("strSortedSet")).size());
400  }
401
402
403}