/tests/com/google/appengine/datanucleus/jpa/JPAInsertionTest.java
Java | 116 lines | 86 code | 12 blank | 18 comment | 0 complexity | fb4cb082595a659e8bb84b234ea34909 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.KeyFactory; 21import com.google.appengine.datanucleus.test.jpa.Book; 22import com.google.appengine.datanucleus.test.jpa.HasKeyPkJPA; 23 24 25/** 26 * @author Max Ross <maxr@google.com> 27 */ 28public class JPAInsertionTest extends JPATestCase { 29 30 public void testSimpleInsert() throws EntityNotFoundException { 31 Book b1 = new Book(); 32 b1.setAuthor("jimmy"); 33 b1.setIsbn("isbn"); 34 b1.setTitle("the title"); 35 assertNull(b1.getId()); 36 beginTxn(); 37 em.persist(b1); 38 assertNull(b1.getId()); 39 commitTxn(); 40 assertNotNull(b1.getId()); 41 Entity entity = ds.get(KeyFactory.stringToKey(b1.getId())); 42 assertNotNull(entity); 43 assertEquals("jimmy", entity.getProperty("author")); 44 assertEquals("isbn", entity.getProperty("isbn")); 45 assertEquals("the title", entity.getProperty("title")); 46 assertEquals(Book.class.getSimpleName(), entity.getKind()); 47 } 48 49 public void testSimpleInsert_Merge() throws EntityNotFoundException { 50 Book b1 = new Book(); 51 b1.setAuthor("jimmy"); 52 b1.setIsbn("isbn"); 53 b1.setTitle("the title"); 54 assertNull(b1.getId()); 55 beginTxn(); 56 em.merge(b1); 57 commitTxn(); 58 assertNotNull(b1.getId()); 59 Entity entity = ds.get(KeyFactory.stringToKey(b1.getId())); 60 assertNotNull(entity); 61 assertEquals("jimmy", entity.getProperty("author")); 62 assertEquals("isbn", entity.getProperty("isbn")); 63 assertEquals("the title", entity.getProperty("title")); 64 assertEquals(Book.class.getSimpleName(), entity.getKind()); 65 } 66 67 public void testSimpleInsertWithNamedKey() throws EntityNotFoundException { 68 Book b1 = new Book("named key"); 69 b1.setAuthor("jimmy"); 70 b1.setIsbn("isbn"); 71 b1.setTitle("the title"); 72 beginTxn(); 73 em.persist(b1); 74 commitTxn(); 75 assertEquals("named key", KeyFactory.stringToKey(b1.getId()).getName()); 76 Entity entity = ds.get(KeyFactory.stringToKey(b1.getId())); 77 assertNotNull(entity); 78 assertEquals("jimmy", entity.getProperty("author")); 79 assertEquals("isbn", entity.getProperty("isbn")); 80 assertEquals("the title", entity.getProperty("title")); 81 assertEquals(Book.class.getSimpleName(), entity.getKind()); 82 assertEquals("named key", entity.getKey().getName()); 83 } 84 85 public void testInsertWithKeyPk() { 86 HasKeyPkJPA hk = new HasKeyPkJPA(); 87 88 beginTxn(); 89 em.persist(hk); 90 commitTxn(); 91 92 assertNotNull(hk.getId()); 93 assertNull(hk.getAncestorId()); 94 } 95 96 public void testInsertWithNamedKeyPk() { 97 HasKeyPkJPA hk = new HasKeyPkJPA(); 98 hk.setId(KeyFactory.createKey(HasKeyPkJPA.class.getSimpleName(), "name")); 99 beginTxn(); 100 em.persist(hk); 101 commitTxn(); 102 103 assertNotNull(hk.getId()); 104 assertEquals("name", hk.getId().getName()); 105 } 106 107 public void testInsertWithFlushBeforeCommit() { 108 Book b1 = new Book(); 109 beginTxn(); 110 em.persist(b1); 111 assertNull(b1.getId()); 112 em.flush(); 113 assertNotNull(b1.getId()); 114 commitTxn(); 115 } 116}