/tests/com/google/appengine/datanucleus/query/JPQLDeleteTest.java
Java | 197 lines | 151 code | 26 blank | 20 comment | 0 complexity | 6bad587d03f22c303f15f6b33179f45e MD5 | raw file
1/********************************************************************** 2 Copyright (c) 2009 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.query; 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.DatastoreManager; 23import com.google.appengine.datanucleus.Utils; 24import com.google.appengine.datanucleus.jpa.JPATestCase; 25import com.google.appengine.datanucleus.test.jpa.Book; 26import com.google.appengine.datanucleus.test.jpa.HasKeyAncestorKeyPkJPA; 27import com.google.appengine.datanucleus.test.jpa.HasOneToManyListJPA; 28 29 30import javax.persistence.PersistenceException; 31import javax.persistence.Query; 32 33/** 34 * @author Max Ross <maxr@google.com> 35 */ 36public class JPQLDeleteTest extends JPATestCase { 37 38 public void testDelete_Txn_MultipleEntityGroups() { 39 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890")); 40 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891")); 41 42 Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b"); 43 beginTxn(); 44 try { 45 q.executeUpdate(); 46 fail("expected exception"); 47 } catch (PersistenceException e) { 48 // good - can't delete books from multiple entity groups in a txn 49 } 50 rollbackTxn(); 51 assertEquals(2, countForClass(Book.class)); 52 } 53 54 public void testDelete_Txn_OneEntityGroup() { 55 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890")); 56 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891")); 57 58 Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b"); 59 beginTxn(); 60 try { 61 q.executeUpdate(); 62 fail("expected exception"); 63 } catch (PersistenceException e) { 64 // good - can't delete books from multiple entity groups in a txn 65 } 66 rollbackTxn(); 67 assertEquals(2, countForClass(Book.class)); 68 } 69 70 public void testDelete_NoTxn() { 71 switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed); 72 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67890")); 73 ds.put(Book.newBookEntity("Bar Book", "Joe Blow", "67891")); 74 75 Query q = em.createQuery("DELETE FROM " + Book.class.getName() + " b"); 76 assertEquals(2, q.executeUpdate()); 77 assertEquals(0, countForClass(Book.class)); 78 } 79 80 public void testDeleteAncestorQuery_Txn() { 81 Key parentKey = KeyFactory.createKey("yar", 23); 82 Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 83 Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 84 85 ds.put(pojo1); 86 ds.put(pojo2); 87 88 Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1"); 89 q.setParameter("p1", parentKey); 90 beginTxn(); 91 assertEquals(2, q.executeUpdate()); 92 commitTxn(); 93 assertEquals(0, countForClass(HasKeyAncestorKeyPkJPA.class)); 94 } 95 96 public void testDeleteAncestorQuery_TxnRollback() throws EntityNotFoundException { 97 Key parentKey = KeyFactory.createKey("yar", 23); 98 Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 99 Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 100 101 ds.put(pojo1); 102 ds.put(pojo2); 103 104 Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1"); 105 q.setParameter("p1", parentKey); 106 beginTxn(); 107 assertEquals(2, q.executeUpdate()); 108 rollbackTxn(); 109 assertEquals(2, countForClass(HasKeyAncestorKeyPkJPA.class)); 110 } 111 112 public void testDeleteAncestorQuery_NoTxn() { 113 switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed); 114 Key parentKey = KeyFactory.createKey("yar", 23); 115 Entity pojo1 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 116 Entity pojo2 = new Entity(HasKeyAncestorKeyPkJPA.class.getSimpleName(), parentKey); 117 118 ds.put(pojo1); 119 ds.put(pojo2); 120 121 Query q = em.createQuery("DELETE FROM " + HasKeyAncestorKeyPkJPA.class.getName() + " b WHERE ancestorKey = :p1"); 122 q.setParameter("p1", parentKey); 123 assertEquals(2, q.executeUpdate()); 124 assertEquals(0, countForClass(HasKeyAncestorKeyPkJPA.class)); 125 } 126 127 public void testBatchDelete_NoTxn_FastButInaccurate() { 128 switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed); 129 Entity e1 = Book.newBookEntity("title", "auth", "123432", -40); 130 ds.put(e1); 131 Entity e2 = Book.newBookEntity("title", "auth", "123432", -40); 132 ds.put(e2); 133 Entity e3 = Book.newBookEntity("title", "auth", "123432", -40); 134 ds.put(e3); 135 136 Key key = KeyFactory.createKey("yar", "does not exist"); 137 Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids"); 138 q.setParameter("ids", Utils.newArrayList(key, e1.getKey(), e2.getKey())); 139 assertEquals(3, q.executeUpdate()); 140 assertEquals(1, countForClass(Book.class)); 141 } 142 143 public void testBatchDelete_NoTxn_SlowButAccurate() { 144 switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed); 145 Entity e1 = Book.newBookEntity("title", "auth", "123432", -40); 146 ds.put(e1); 147 Entity e2 = Book.newBookEntity("title", "auth", "123432", -40); 148 ds.put(e2); 149 Entity e3 = Book.newBookEntity("title", "auth", "123432", -40); 150 ds.put(e3); 151 152 Key key = KeyFactory.createKey("yar", "does not exist"); 153 Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids"); 154 q.setHint(DatastoreManager.QUERYEXT_SLOW_BUT_MORE_ACCURATE_JPQL_DELETE, true); 155 q.setParameter("ids", Utils.newArrayList(key, e1.getKey(), e2.getKey())); 156 assertEquals(2, q.executeUpdate()); 157 assertEquals(1, countForClass(Book.class)); 158 } 159 160 public void testBatchDelete_Txn() { 161 Key parent = KeyFactory.createKey("yar", 23); 162 Entity e1 = Book.newBookEntity(parent, "title", "auth", "123432", -40); 163 ds.put(e1); 164 Entity e2 = Book.newBookEntity(parent, "title", "auth", "123432", -40); 165 ds.put(e2); 166 Entity e3 = Book.newBookEntity(parent, "title", "auth", "123432", -40); 167 ds.put(e3); 168 169 beginTxn(); 170 Query q = em.createQuery("delete from " + Book.class.getName() + " b where id = :ids"); 171 q.setParameter("ids", Utils.newArrayList(parent, e1.getKey(), e2.getKey())); 172 assertEquals(3, q.executeUpdate()); 173 assertEquals(3, countForClass(Book.class)); 174 commitTxn(); 175 assertEquals(1, countForClass(Book.class)); 176 } 177 178 public void testDeleteDoesNotCascade() { 179 HasOneToManyListJPA parent = new HasOneToManyListJPA(); 180 Book b = new Book(); 181 b.setAuthor("author"); 182 parent.getBooks().add(b); 183 beginTxn(); 184 em.persist(parent); 185 commitTxn(); 186 assertEquals(1, countForClass(Book.class)); 187 assertEquals(1, countForClass(HasOneToManyListJPA.class)); 188 beginTxn(); 189 Query q = em.createQuery("delete from " + HasOneToManyListJPA.class.getName() + " b"); 190 assertEquals(1, q.executeUpdate()); 191 assertEquals(1, countForClass(Book.class)); 192 assertEquals(1, countForClass(HasOneToManyListJPA.class)); 193 commitTxn(); 194 assertEquals(1, countForClass(Book.class)); 195 assertEquals(0, countForClass(HasOneToManyListJPA.class)); 196 } 197}