/tests/com/google/appengine/datanucleus/jpa/JPAOneToManyPolymorphicSetTest.java
Java | 358 lines | 313 code | 25 blank | 20 comment | 0 complexity | 793537a15856fa6956e621b3848eaf5e MD5 | raw file
1/********************************************************************** 2Copyright (c) 2011 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 org.datanucleus.util.NucleusLogger; 19 20import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirBottomLongPkSet; 21import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirBottomSet; 22import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirBottomUnencodedStringPkSet; 23import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirMiddleLongPkSet; 24import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirMiddleSet; 25import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirMiddleUnencodedStringPkSet; 26import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirTopLongPkSet; 27import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirTopSet; 28import com.google.appengine.datanucleus.test.jpa.BidirectionalSingleTableChildSetJPA.BidirTopUnencodedStringPkSet; 29import com.google.appengine.datanucleus.test.jpa.HasPolymorphicRelationsSetJPA.HasOneToManyKeyPkSetJPA; 30import com.google.appengine.datanucleus.test.jpa.HasPolymorphicRelationsSetJPA.HasOneToManyLongPkSetJPA; 31import com.google.appengine.datanucleus.test.jpa.HasPolymorphicRelationsSetJPA.HasOneToManySetJPA; 32import com.google.appengine.datanucleus.test.jpa.HasPolymorphicRelationsSetJPA.HasOneToManyUnencodedStringPkSetJPA; 33import com.google.appengine.datanucleus.test.jpa.UnidirectionalSingeTableChildJPA.UnidirBottom; 34import com.google.appengine.datanucleus.test.jpa.UnidirectionalSingeTableChildJPA.UnidirMiddle; 35 36import static com.google.appengine.datanucleus.PolymorphicTestUtils.getEntityKind; 37 38public class JPAOneToManyPolymorphicSetTest extends JPAOneToManyPolymorphicTestCase { 39 40 public void testInsert_NewParentAndChild() throws Exception { 41 testInsert_NewParentAndChild(TXN_START_END); 42 } 43 public void testInsert_NewParentAndChild_NoTxn() throws Exception { 44 testInsert_NewParentAndChild(NEW_EM_START_END); 45 } 46 private void testInsert_NewParentAndChild(StartEnd startEnd) throws Exception { 47 testInsert_NewParentAndChild(new BidirTopSet(), new HasOneToManySetJPA(), 48 startEnd, UnidirLevel.Middle, 1, 1); 49 testInsert_NewParentAndChild(new BidirBottomSet(), new HasOneToManySetJPA(), 50 startEnd, UnidirLevel.Top, 2, 2); 51 testInsert_NewParentAndChild(new BidirMiddleSet(), new HasOneToManySetJPA(), 52 startEnd, UnidirLevel.Bottom, 3, 3); 53 } 54 55 public void testInsert_ExistingParentNewChild() throws Exception { 56 NucleusLogger.GENERAL.info(">> testInsert_ExistingParentNewChild START"); 57 testInsert_ExistingParentNewChild(TXN_START_END); 58 NucleusLogger.GENERAL.info(">> testInsert_ExistingParentNewChild END"); 59 } 60 public void testInsert_ExistingParentNewChild_NoTxn() throws Exception { 61 testInsert_ExistingParentNewChild(NEW_EM_START_END); 62 } 63 private void testInsert_ExistingParentNewChild(StartEnd startEnd) throws Exception { 64 testInsert_ExistingParentNewChild(new BidirMiddleSet(), new HasOneToManySetJPA(), 65 startEnd, UnidirLevel.Bottom, 1, 1); 66 testInsert_ExistingParentNewChild(new BidirBottomSet(), new HasOneToManySetJPA(), 67 startEnd, UnidirLevel.Middle, 2, 2); 68 testInsert_ExistingParentNewChild(new BidirTopSet(), new HasOneToManySetJPA(), 69 startEnd, UnidirLevel.Top, 3, 3); 70 } 71 72 public void testUpdate_UpdateChildWithMerge() throws Exception { 73 testUpdate_UpdateChildWithMerge(TXN_START_END); 74 } 75 public void testUpdate_UpdateChildWithMerge_NoTxn() throws Exception { 76 testUpdate_UpdateChildWithMerge(NEW_EM_START_END); 77 } 78 private void testUpdate_UpdateChildWithMerge(StartEnd startEnd) throws Exception { 79 testUpdate_UpdateChildWithMerge(new BidirBottomSet(), new HasOneToManySetJPA(), 80 startEnd, UnidirLevel.Top, 1, 1); 81 testUpdate_UpdateChildWithMerge(new BidirMiddleSet(), new HasOneToManySetJPA(), 82 startEnd, UnidirLevel.Middle, 2, 2); 83 testUpdate_UpdateChildWithMerge(new BidirTopSet(), new HasOneToManySetJPA(), 84 startEnd, UnidirLevel.Bottom, 3, 3); 85 } 86 87 public void testUpdate_UpdateChild() throws Exception { 88 testUpdate_UpdateChild(TXN_START_END); 89 } 90 public void testUpdate_UpdateChild_NoTxn() throws Exception { 91 testUpdate_UpdateChild(NEW_EM_START_END); 92 } 93 private void testUpdate_UpdateChild(StartEnd startEnd) throws Exception { 94 testUpdate_UpdateChild(new BidirBottomSet(), new HasOneToManySetJPA(), 95 startEnd, UnidirLevel.Middle, 1, 1); 96 testUpdate_UpdateChild(new BidirMiddleSet(), new HasOneToManySetJPA(), 97 startEnd, UnidirLevel.Bottom, 2, 2); 98 testUpdate_UpdateChild(new BidirTopSet(), new HasOneToManySetJPA(), 99 startEnd, UnidirLevel.Top, 3, 3); 100 } 101 102 public void testUpdate_NullOutChildren() throws Exception { 103 testUpdate_NullOutChildren(TXN_START_END); 104 } 105 public void testUpdate_NullOutChildren_NoTxn() throws Exception { 106 testUpdate_NullOutChildren(NEW_EM_START_END); 107 } 108 private void testUpdate_NullOutChildren(StartEnd startEnd) throws Exception { 109 testUpdate_NullOutChildren(new BidirBottomSet(), new HasOneToManySetJPA(), 110 startEnd, UnidirLevel.Middle, 1); 111 testUpdate_NullOutChildren(new BidirMiddleSet(), new HasOneToManySetJPA(), 112 startEnd, UnidirLevel.Top, 2); 113 testUpdate_NullOutChildren(new BidirTopSet(), new HasOneToManySetJPA(), 114 startEnd, UnidirLevel.Bottom, 3); 115 } 116 117 public void testUpdate_ClearOutChildren() throws Exception { 118 testUpdate_ClearOutChildren(TXN_START_END); 119 } 120 public void testUpdate_ClearOutChildren_NoTxn() throws Exception { 121 testUpdate_ClearOutChildren(NEW_EM_START_END); 122 } 123 private void testUpdate_ClearOutChildren(StartEnd startEnd) throws Exception { 124 testUpdate_ClearOutChildren(new BidirMiddleSet(), new HasOneToManySetJPA(), 125 startEnd, UnidirLevel.Top, 1); 126 testUpdate_ClearOutChildren(new BidirTopSet(), new HasOneToManySetJPA(), 127 startEnd, UnidirLevel.Middle, 2); 128 testUpdate_ClearOutChildren(new BidirBottomSet(), new HasOneToManySetJPA(), 129 startEnd, UnidirLevel.Bottom, 3); 130 } 131 132 public void testFind() throws Exception { 133 testFind(TXN_START_END); 134 } 135 public void testFind_NoTxn() throws Exception { 136 testFind(NEW_EM_START_END); 137 } 138 private void testFind(StartEnd startEnd) throws Exception { 139 testFind(HasOneToManySetJPA.class, BidirTopSet.class, 140 startEnd, UnidirLevel.Top, getEntityKind(BidirTopSet.class)); 141 testFind(HasOneToManySetJPA.class, BidirMiddleSet.class, 142 startEnd, UnidirLevel.Bottom, getEntityKind(BidirTopSet.class)); 143 testFind(HasOneToManySetJPA.class, BidirBottomSet.class, 144 startEnd, UnidirLevel.Middle, getEntityKind(BidirTopSet.class)); 145 } 146 147 public void testQuery() throws Exception { 148 testQuery(TXN_START_END); 149 } 150 public void testQuery_NoTxn() throws Exception { 151 testQuery(NEW_EM_START_END); 152 } 153 private void testQuery(StartEnd startEnd) throws Exception { 154 testQuery(HasOneToManySetJPA.class, BidirMiddleSet.class, 155 startEnd, UnidirLevel.Top, getEntityKind(BidirTopSet.class)); 156 testQuery(HasOneToManySetJPA.class, BidirTopSet.class, 157 startEnd, UnidirLevel.Middle, getEntityKind(BidirTopSet.class)); 158 testQuery(HasOneToManySetJPA.class, BidirBottomSet.class, 159 startEnd, UnidirLevel.Bottom, getEntityKind(BidirTopSet.class)); 160 } 161 162 public void testChildFetchedLazily() throws Exception { 163 testChildFetchedLazily(HasOneToManySetJPA.class, BidirTopSet.class, 164 UnidirLevel.Middle, getEntityKind(BidirTopSet.class)); 165 testChildFetchedLazily(HasOneToManySetJPA.class, BidirMiddleSet.class, 166 UnidirLevel.Top, getEntityKind(BidirTopSet.class)); 167 testChildFetchedLazily(HasOneToManySetJPA.class, BidirBottomSet.class, 168 UnidirLevel.Bottom, getEntityKind(BidirTopSet.class)); 169 } 170 171 public void testDeleteParentDeletesChild() throws Exception { 172 testDeleteParentDeletesChild(TXN_START_END); 173 } 174 public void testDeleteParentDeletesChild_NoTxn() throws Exception { 175 testDeleteParentDeletesChild(NEW_EM_START_END); 176 } 177 private void testDeleteParentDeletesChild(StartEnd startEnd) throws Exception { 178 testDeleteParentDeletesChild(HasOneToManySetJPA.class, BidirBottomSet.class, 179 startEnd, UnidirLevel.Top, getEntityKind(BidirTopSet.class)); 180 testDeleteParentDeletesChild(HasOneToManySetJPA.class, BidirMiddleSet.class, 181 startEnd, UnidirLevel.Bottom, getEntityKind(BidirTopSet.class)); 182 testDeleteParentDeletesChild(HasOneToManySetJPA.class, BidirTopSet.class, 183 startEnd, UnidirLevel.Middle, getEntityKind(BidirTopSet.class)); 184 } 185 186 public void testRemoveObject() throws Exception { 187 testRemoveObject(TXN_START_END); 188 } 189 public void testRemoveObject_NoTxn() throws Exception { 190 testRemoveObject(NEW_EM_START_END); 191 } 192 private void testRemoveObject(StartEnd startEnd) throws Exception { 193 testRemoveObject(new HasOneToManySetJPA(), new BidirTopSet(nextNamedKey()), 194 new BidirTopSet(nextNamedKey()), startEnd, UnidirLevel.Top, UnidirLevel.Bottom, 1); 195 testRemoveObject(new HasOneToManySetJPA(), new BidirBottomSet(nextNamedKey()), 196 new BidirTopSet(nextNamedKey()), startEnd, UnidirLevel.Middle, UnidirLevel.Top, 2); 197 testRemoveObject(new HasOneToManySetJPA(), new BidirMiddleSet(nextNamedKey()), 198 new BidirTopSet(nextNamedKey()), startEnd, UnidirLevel.Bottom, UnidirLevel.Middle, 3); 199 } 200 201 public void testChangeParent() throws Exception { 202 testChangeParent(new HasOneToManySetJPA(), new HasOneToManySetJPA(), TXN_START_END); 203 } 204 public void testChangeParent_NoTxn() throws Exception { 205 testChangeParent(new HasOneToManySetJPA(), new HasOneToManySetJPA(), NEW_EM_START_END); 206 } 207 public void testNewParentNewChild_NamedKeyOnChild() throws Exception { 208 testNewParentNewChild_NamedKeyOnChild(new HasOneToManySetJPA(), TXN_START_END); 209 } 210 public void testNewParentNewChild_NamedKeyOnChild_NoTxn() throws Exception { 211 testNewParentNewChild_NamedKeyOnChild(new HasOneToManySetJPA(), NEW_EM_START_END); 212 } 213 public void testAddAlreadyPersistedChildToParent_NoTxnSameEm() { 214 testAddAlreadyPersistedChildToParent_NoTxnSameEm(new HasOneToManySetJPA()); 215 } 216 public void testAddAlreadyPersistedChildToParent_NoTxnDifferentEm() { 217 testAddAlreadyPersistedChildToParent_NoTxnDifferentEm(new HasOneToManySetJPA()); 218 } 219 public void testFetchOfOneToManyParentWithKeyPk() throws Exception { 220 testFetchOfOneToManyParentWithKeyPk(new HasOneToManyKeyPkSetJPA(), TXN_START_END); 221 } 222 public void testFetchOfOneToManyParentWithKeyPk_NoTxn() throws Exception { 223 testFetchOfOneToManyParentWithKeyPk(new HasOneToManyKeyPkSetJPA(), NEW_EM_START_END); 224 } 225 public void testFetchOfOneToManyParentWithLongPk() throws Exception { 226 testFetchOfOneToManyParentWithLongPk(new HasOneToManyLongPkSetJPA(), TXN_START_END); 227 } 228 public void testFetchOfOneToManyParentWithLongPk_NoTxn() throws Exception { 229 testFetchOfOneToManyParentWithLongPk(new HasOneToManyLongPkSetJPA(), NEW_EM_START_END); 230 } 231 public void testFetchOfOneToManyParentWithUnencodedStringPk() throws Exception { 232 testFetchOfOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkSetJPA(), 233 TXN_START_END); 234 } 235 public void testFetchOfOneToManyParentWithUnencodedStringPk_NoTxn() throws Exception { 236 testFetchOfOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkSetJPA(), 237 NEW_EM_START_END); 238 } 239 240 public void testAddChildToOneToManyParentWithLongPk() throws Exception { 241 testAddChildToOneToManyParentWithLongPk(TXN_START_END); 242 } 243 public void testAddChildToOneToManyParentWithLongPk_NoTxn() throws Exception { 244 testAddChildToOneToManyParentWithLongPk(NEW_EM_START_END); 245 } 246 private void testAddChildToOneToManyParentWithLongPk(StartEnd startEnd) throws Exception { 247 testAddChildToOneToManyParentWithLongPk(new HasOneToManyLongPkSetJPA(), new BidirBottomLongPkSet(), 248 startEnd, UnidirLevel.Top, "B"); 249 testAddChildToOneToManyParentWithLongPk(new HasOneToManyLongPkSetJPA(), new BidirMiddleLongPkSet(), 250 startEnd, UnidirLevel.Bottom, "M"); 251 testAddChildToOneToManyParentWithLongPk(new HasOneToManyLongPkSetJPA(), new BidirTopLongPkSet(), 252 startEnd, UnidirLevel.Middle, "T"); 253 } 254 255 public void testAddChildToOneToManyParentWithUnencodedStringPk() throws Exception { 256 testAddChildToOneToManyParentWithUnencodedStringPk(TXN_START_END); 257 } 258 public void testAddChildToOneToManyParentWithUnencodedStringPk_NoTxn() throws Exception { 259 testAddChildToOneToManyParentWithUnencodedStringPk(NEW_EM_START_END); 260 } 261 private void testAddChildToOneToManyParentWithUnencodedStringPk(StartEnd startEnd) throws Exception { 262 testAddChildToOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkSetJPA(), 263 new BidirBottomUnencodedStringPkSet(), startEnd, UnidirLevel.Top, "C"); 264 testAddChildToOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkSetJPA(), 265 new BidirTopUnencodedStringPkSet(), startEnd, UnidirLevel.Middle, "A"); 266 testAddChildToOneToManyParentWithUnencodedStringPk(new HasOneToManyUnencodedStringPkSetJPA(), 267 new BidirMiddleUnencodedStringPkSet(), startEnd, UnidirLevel.Bottom, "B"); 268 } 269 270 public void testAddQueriedParentToBidirChild() throws Exception { 271 testAddQueriedParentToBidirChild(TXN_START_END); 272 } 273 public void testAddQueriedParentToBidirChild_NoTxn() throws Exception { 274 testAddQueriedParentToBidirChild(NEW_EM_START_END); 275 } 276 private void testAddQueriedParentToBidirChild(StartEnd startEnd) throws Exception { 277 testAddQueriedParentToBidirChild(new HasOneToManySetJPA(), new BidirMiddleSet(), 278 startEnd); 279 testAddQueriedParentToBidirChild(new HasOneToManySetJPA(), new BidirTopSet(), 280 startEnd); 281 testAddQueriedParentToBidirChild(new HasOneToManySetJPA(), new BidirBottomSet(), 282 startEnd); 283 } 284 public void testAddFetchedParentToBidirChild() throws Exception { 285 testAddFetchedParentToBidirChild(TXN_START_END); 286 } 287 public void testAddFetchedParentToBidirChild_NoTxn() throws Exception { 288 testAddFetchedParentToBidirChild(NEW_EM_START_END); 289 } 290 public void testAddFetchedParentToBidirChild(StartEnd startEnd) throws Exception { 291 testAddFetchedParentToBidirChild(new HasOneToManySetJPA(), new BidirBottomSet(), 292 startEnd); 293 testAddFetchedParentToBidirChild(new HasOneToManySetJPA(), new BidirTopSet(), 294 startEnd); 295 testAddFetchedParentToBidirChild(new HasOneToManySetJPA(), new BidirMiddleSet(), 296 startEnd); 297 } 298 299 public void testOnlyOneParentPutOnParentAndChildUpdate() throws Throwable { 300 testOnlyOneParentPutOnParentAndChildUpdate(TXN_START_END); 301 } 302 public void testOnlyOneParentPutOnParentAndChildUpdate_NoTxn() throws Throwable { 303 testOnlyOneParentPutOnParentAndChildUpdate(NEW_EM_START_END); 304 } 305 private void testOnlyOneParentPutOnParentAndChildUpdate(StartEnd startEnd) throws Throwable { 306 testOnlyOneParentPutOnParentAndChildUpdate(new HasOneToManySetJPA(), new BidirTopSet(), 307 startEnd); 308 testOnlyOneParentPutOnParentAndChildUpdate(new HasOneToManySetJPA(), new BidirMiddleSet(), 309 startEnd); 310 testOnlyOneParentPutOnParentAndChildUpdate(new HasOneToManySetJPA(), new BidirBottomSet(), 311 startEnd); 312 } 313 314 public void testOnlyOnePutOnChildUpdate() throws Throwable { 315 testOnlyOnePutOnChildUpdate(TXN_START_END); 316 } 317 public void testOnlyOnePutOnChildUpdate_NoTxn() throws Throwable { 318 testOnlyOnePutOnChildUpdate(NEW_EM_START_END); 319 } 320 private void testOnlyOnePutOnChildUpdate(StartEnd startEnd) throws Throwable { 321 testOnlyOnePutOnChildUpdate(new HasOneToManySetJPA(), new BidirTopSet(), 322 startEnd); 323 testOnlyOnePutOnChildUpdate(new HasOneToManySetJPA(), new BidirMiddleSet(), 324 startEnd); 325 testOnlyOnePutOnChildUpdate(new HasOneToManySetJPA(), new BidirBottomSet(), 326 startEnd); 327 } 328 329 public void testOnlyOneParentPutOnChildDelete() throws Throwable { 330 // 1 put to remove the keys 331 int expectedUpdatePuts = 1; 332 testOnlyOneParentPutOnChildDelete(new HasOneToManySetJPA(), new BidirTopSet(), 333 TXN_START_END, expectedUpdatePuts); 334 } 335 336 public void testOnlyOneParentPutOnChildDelete_NoTxn() throws Throwable { 337 // updates are atomic when non-tx, so get 1 after each collection clear, and 1 for the update. 338 int expectedUpdatePuts = 5; 339 testOnlyOneParentPutOnChildDelete(new HasOneToManySetJPA(), new BidirTopSet(), 340 NEW_EM_START_END, expectedUpdatePuts); 341 } 342 343 @Override 344 protected void registerSubclasses() { 345 // Make sure all subclasses of UnidirTop, BidirTopSet are known. Only the meta data 346 // of the top class in the inheritance tree (element type of the collections) 347 // is known otherwise when getting the pojo. 348 getExecutionContext().getStoreManager().addClass(UnidirMiddle.class.getName(), 349 getExecutionContext().getClassLoaderResolver()); 350 getExecutionContext().getStoreManager().addClass(UnidirBottom.class.getName(), 351 getExecutionContext().getClassLoaderResolver()); 352 getExecutionContext().getStoreManager().addClass(BidirMiddleSet.class.getName(), 353 getExecutionContext().getClassLoaderResolver()); 354 getExecutionContext().getStoreManager().addClass(BidirBottomSet.class.getName(), 355 getExecutionContext().getClassLoaderResolver()); 356 } 357 358}