|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, Sun Microsystems, Inc. All rights reserved. |
2 |
* Copyright (c) 2008, Sun Microsystems, Inc. All rights reserved. |
| 3 |
* This program and the accompanying materials are made available under the |
3 |
* This program and the accompanying materials are made available under the |
| 4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| 5 |
* which accompanies this distribution. |
5 |
* which accompanies this distribution. |
|
Lines 8-22
Link Here
|
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
* |
9 |
* |
| 10 |
* Contributors: |
10 |
* Contributors: |
| 11 |
* DaraniY = 1.0 - Initialize contribution |
11 |
* 12/04/2008 - 2.0 Darani Yallapragada |
|
|
12 |
* - 248780: Initial contribution for JPA 2.0 |
| 13 |
* 04/22/2010 - 2.1 Michael O'Brien |
| 14 |
* - 248780: Refactor Cache Implementation surrounding evict() |
| 15 |
* http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/cache_api#Refactor_20100322 |
| 16 |
* Fix evict() to handle non-Entity classes |
| 17 |
* Refactor to get IdentityMapAccessor state through EMF reference |
| 18 |
* Refactor dependencies to use Interfaces instead of Impl subclasses |
| 19 |
* Handle no CMPPolicy case for getId() |
| 20 |
* Handle no associated descriptor for Class parameter |
| 21 |
* MappedSuperclasses passed to evict() cause implementing subclasses to be evicted |
| 22 |
* Throw an IAE for Interfaces and Embeddable classes passed to evict() |
| 12 |
******************************************************************************/ |
23 |
******************************************************************************/ |
| 13 |
package org.eclipse.persistence.testing.tests.jpa.advanced; |
24 |
package org.eclipse.persistence.testing.tests.jpa.advanced; |
| 14 |
|
25 |
|
|
|
26 |
import javax.persistence.Cache; |
| 15 |
import javax.persistence.EntityManager; |
27 |
import javax.persistence.EntityManager; |
| 16 |
import junit.framework.Test; |
28 |
import junit.framework.Test; |
| 17 |
import junit.framework.TestSuite; |
29 |
import junit.framework.TestSuite; |
| 18 |
import org.eclipse.persistence.testing.models.jpa.advanced.*; |
30 |
import org.eclipse.persistence.testing.models.jpa.advanced.*; |
|
|
31 |
import org.eclipse.persistence.testing.models.jpa.advanced.fetchgroup.ChestProtector; |
| 32 |
import org.eclipse.persistence.testing.models.jpa.advanced.fetchgroup.GoalieGear; |
| 33 |
import org.eclipse.persistence.testing.models.jpa.advanced.fetchgroup.Pads; |
| 34 |
import org.eclipse.persistence.testing.models.jpa.metamodel.Manufacturer; |
| 35 |
import org.eclipse.persistence.descriptors.ClassDescriptor; |
| 36 |
import org.eclipse.persistence.internal.jpa.CMP3Policy; |
| 19 |
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl; |
37 |
import org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl; |
|
|
38 |
import org.eclipse.persistence.internal.jpa.EntityManagerImpl; |
| 20 |
import org.eclipse.persistence.jpa.JpaCache; |
39 |
import org.eclipse.persistence.jpa.JpaCache; |
| 21 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
40 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
| 22 |
|
41 |
|
|
Lines 25-31
Link Here
|
| 25 |
*/ |
44 |
*/ |
| 26 |
public class CacheImplJUnitTest extends JUnitTestCase { |
45 |
public class CacheImplJUnitTest extends JUnitTestCase { |
| 27 |
|
46 |
|
| 28 |
|
|
|
| 29 |
public CacheImplJUnitTest() { |
47 |
public CacheImplJUnitTest() { |
| 30 |
super(); |
48 |
super(); |
| 31 |
} |
49 |
} |
|
Lines 39-44
Link Here
|
| 39 |
clearCache(); |
57 |
clearCache(); |
| 40 |
} |
58 |
} |
| 41 |
|
59 |
|
|
|
60 |
/** |
| 61 |
* Note: These tests are setup with ID generation - but it is not used. |
| 62 |
* We set the Id manually so we can test cache operations like eviction properly. |
| 63 |
* @return |
| 64 |
*/ |
| 42 |
public static Test suite() { |
65 |
public static Test suite() { |
| 43 |
TestSuite suite = new TestSuite(); |
66 |
TestSuite suite = new TestSuite(); |
| 44 |
suite.setName("CacheImplJUnitTest"); |
67 |
suite.setName("CacheImplJUnitTest"); |
|
Lines 50-56
Link Here
|
| 50 |
suite.addTest(new CacheImplJUnitTest("testEvictAll")); |
73 |
suite.addTest(new CacheImplJUnitTest("testEvictAll")); |
| 51 |
suite.addTest(new CacheImplJUnitTest("testEvictContains")); |
74 |
suite.addTest(new CacheImplJUnitTest("testEvictContains")); |
| 52 |
suite.addTest(new CacheImplJUnitTest("testCacheAPI")); |
75 |
suite.addTest(new CacheImplJUnitTest("testCacheAPI")); |
| 53 |
|
76 |
// 20100322: 248780: CacheImpl refactor for non-Entity classes |
|
|
77 |
suite.addTest(new CacheImplJUnitTest("testEvictClass_MappedSuperclass_RemovesAssignableSubclasses")); |
| 78 |
suite.addTest(new CacheImplJUnitTest("testEvictClassObject_MappedSuperclass_RemovesAssignableSubclasses")); |
| 79 |
suite.addTest(new CacheImplJUnitTest("testEvictClass_JavaLangClass_hasNoEffect")); |
| 80 |
suite.addTest(new CacheImplJUnitTest("testGetId_fromUnmanagedMappedSuperclass_handles_null_descriptor")); |
| 81 |
suite.addTest(new CacheImplJUnitTest("testGetId_fromUnsupportedJavaLangInteger_throwsIAE_on_null_descriptor")); |
| 82 |
// Run these tests last as they modify the state of the ClassDescriptor permanently to verify variant corner use cases |
| 83 |
suite.addTest(new CacheImplJUnitTest("testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_weaving_on")); |
| 84 |
suite.addTest(new CacheImplJUnitTest("testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_and_null_pk_with_weaving_on")); |
| 85 |
suite.addTest(new CacheImplJUnitTest("testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_and_null_pk_with_weaving_off")); |
| 86 |
// test null descriptor on closed entityManager |
| 54 |
return suite; |
87 |
return suite; |
| 55 |
} |
88 |
} |
| 56 |
|
89 |
|
|
Lines 97-102
Link Here
|
| 97 |
assertTrue("Employee not found in cache", cache.contains(employee)); |
130 |
assertTrue("Employee not found in cache", cache.contains(employee)); |
| 98 |
cache.evict(employee); |
131 |
cache.evict(employee); |
| 99 |
cache.putObject(employee); |
132 |
cache.putObject(employee); |
|
|
133 |
cache.print(); |
| 100 |
cache.removeObject(employee); |
134 |
cache.removeObject(employee); |
| 101 |
cache.removeObject(Employee.class, employee.getId()); |
135 |
cache.removeObject(Employee.class, employee.getId()); |
| 102 |
cache.clear(); |
136 |
cache.clear(); |
|
Lines 225-230
Link Here
|
| 225 |
} finally { |
259 |
} finally { |
| 226 |
closeEntityManager(em); |
260 |
closeEntityManager(em); |
| 227 |
} |
261 |
} |
|
|
262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* In this test we attempt to remove the MappedSuperclass (superclass) from the entity cache. |
| 266 |
* The resulting action removes all implementing subclasses - which has the same effect |
| 267 |
* as just removing the implementing Entity in the first case. |
| 268 |
* |
| 269 |
* Hierarchy: |
| 270 |
* HockeyGear (Abstract Entity) |
| 271 |
* +----- GoalieGear (Concrete MappedSuperclass) --- (we will evict this one) |
| 272 |
* +----- Chest Protector (Concrete Entity) ---- cacheable (but this will actually be evicted) |
| 273 |
* +----- Pads (Concrete Entity) ---- cacheable (as well as this one) |
| 274 |
*/ |
| 275 |
public void testEvictClass_MappedSuperclass_RemovesAssignableSubclasses() { |
| 276 |
if (! isJPA10()) { |
| 277 |
int ID_PADS = ID_TEST_BASE + 100; |
| 278 |
int ID_CHESTPROTECTOR = ID_PADS++; |
| 279 |
final EntityManager em = createEntityManager("default1"); |
| 280 |
// Persist both implementing subclasses of GoalieGear |
| 281 |
beginTransaction(em); |
| 282 |
// HockeyGear(Abstract Entity) << GoalieGear (Concrete MappedSuperclass) << ChestProtector (Concrete Entity) |
| 283 |
ChestProtector e1 = new ChestProtector(); |
| 284 |
e1.setDescription("chest_protector"); |
| 285 |
e1.setSerialNumber(new Integer(ID_PADS)); |
| 286 |
em.persist(e1); |
| 287 |
commitTransaction(em); |
| 288 |
// closeEntityManager(em); // do not close the entityManager between transactions or you will get bug# 307445 |
| 289 |
|
| 290 |
beginTransaction(em); |
| 291 |
// HockeyGear(Abstract Entity) << GoalieGear (Concrete MappedSuperclass) << ChestProtector (Concrete Entity) |
| 292 |
Pads p1 = new Pads(); |
| 293 |
p1.setDescription("pads"); |
| 294 |
p1.setSerialNumber(new Integer(ID_CHESTPROTECTOR)); |
| 295 |
em.persist(p1); |
| 296 |
commitTransaction(em); |
| 297 |
closeEntityManager(em); |
| 298 |
|
| 299 |
EntityManager em1 = createEntityManager("default1"); |
| 300 |
EntityManager em2 = createEntityManager("default1"); |
| 301 |
try { |
| 302 |
ChestProtector e2 = (ChestProtector) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")) |
| 303 |
.getServerSession().getIdentityMapAccessor().getFromIdentityMap(e1); |
| 304 |
Pads p2 = (Pads) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")) |
| 305 |
.getServerSession().getIdentityMapAccessor().getFromIdentityMap(p1); |
| 306 |
// change the entity in the cache (only) - later a find() will get the unmodified version in the database |
| 307 |
e2.setDescription("new_chest_protector"); |
| 308 |
p2.setDescription("new_pads"); |
| 309 |
|
| 310 |
String expected_chestProtector = em1.find(GoalieGear.class, ID_CHESTPROTECTOR).getDescription(); |
| 311 |
String expected_pads = em1.find(GoalieGear.class, ID_PADS).getDescription(); |
| 312 |
|
| 313 |
// evict the inheriting entities via their MappedSuperclass |
| 314 |
getEntityManagerFactory("default1").getCache().evict(GoalieGear.class); |
| 315 |
|
| 316 |
// read the inheriting entities directly from the database (because they are no longer in the cache - hopefully) |
| 317 |
GoalieGear g5 = em2.find(GoalieGear.class, ID_CHESTPROTECTOR); |
| 318 |
GoalieGear p5 = em2.find(GoalieGear.class, ID_PADS); |
| 319 |
|
| 320 |
// Check that we are not actually getting the supposed "to be evicted" cached version (that was modified) |
| 321 |
String actual_chestProtector = g5.getDescription(); |
| 322 |
String actual_pads = p5.getDescription(); |
| 323 |
// verify that assignable subclasses (ChestProtector and Pads) are removed from the cache |
| 324 |
assertNotSame("Assertion Error - There should be no modified cached entity instance in the cache - the entity read from the database should be different. " |
| 325 |
, expected_chestProtector, actual_chestProtector); |
| 326 |
assertNotSame("Assertion Error - There should be no modified cached entity instance in the cache - the entity read from the database should be different. " |
| 327 |
, expected_pads, actual_pads); |
| 328 |
} finally { |
| 329 |
closeEntityManager(em1); |
| 330 |
closeEntityManager(em2); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 228 |
|
334 |
|
|
|
335 |
/** |
| 336 |
* In this test we attempt to remove the MappedSuperclass (superclass) from the entity cache. |
| 337 |
* The resulting action removes all implementing subclasses - which has the same effect |
| 338 |
* as just removing the implementing Entity in the first case. |
| 339 |
* |
| 340 |
* Hierarchy: |
| 341 |
* HockeyGear (Abstract Entity) |
| 342 |
* +----- GoalieGear (Concrete MappedSuperclass) --- (we will evict this one) |
| 343 |
* +----- Chest Protector (Concrete Entity) ---- cacheable (but this will actually be evicted) |
| 344 |
* +----- Pads (Concrete Entity) ---- cacheable (as well as this one) |
| 345 |
*/ |
| 346 |
public void testEvictClassObject_MappedSuperclass_RemovesAssignableSubclasses() { |
| 347 |
if (! isJPA10()) { |
| 348 |
int ID_PADS = ID_TEST_BASE + 200; |
| 349 |
int ID_CHESTPROTECTOR = ID_PADS++; |
| 350 |
EntityManager em = createEntityManager("default1"); |
| 351 |
// Persist both implementing subclasses of GoalieGear |
| 352 |
beginTransaction(em); |
| 353 |
// HockeyGear(Abstract Entity) << GoalieGear (Concrete MappedSuperclass) << ChestProtector (Concrete Entity) |
| 354 |
ChestProtector e1 = new ChestProtector(); |
| 355 |
e1.setDescription("chest_protector"); |
| 356 |
e1.setSerialNumber(new Integer(ID_PADS)); |
| 357 |
em.persist(e1); |
| 358 |
commitTransaction(em); |
| 359 |
// closeEntityManager(em); // do not close the entityManager between transactions or you will get bug# 307445 |
| 360 |
|
| 361 |
beginTransaction(em); |
| 362 |
Pads p1 = new Pads(); |
| 363 |
p1.setDescription("pads"); |
| 364 |
p1.setSerialNumber(new Integer(ID_CHESTPROTECTOR)); |
| 365 |
em.persist(p1); |
| 366 |
commitTransaction(em); |
| 367 |
closeEntityManager(em); |
| 368 |
|
| 369 |
EntityManager em1 = createEntityManager("default1"); |
| 370 |
EntityManager em2 = createEntityManager("default1"); |
| 371 |
try { |
| 372 |
GoalieGear c2 = (GoalieGear) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")) |
| 373 |
.getServerSession().getIdentityMapAccessor().getFromIdentityMap(e1); |
| 374 |
Pads p2 = (Pads) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")) |
| 375 |
.getServerSession().getIdentityMapAccessor().getFromIdentityMap(p1); |
| 376 |
// change the entity in the cache (only) - later a find() will get the unmodified version in the database |
| 377 |
c2.setDescription("new_chest_protector"); |
| 378 |
p2.setDescription("new_pads"); |
| 379 |
|
| 380 |
String expected_chestProtector = em1.find(GoalieGear.class, ID_CHESTPROTECTOR).getDescription(); |
| 381 |
String expected_pads = em1.find(GoalieGear.class, ID_PADS).getDescription(); |
| 382 |
|
| 383 |
// evict the inheriting entity (by Id) via its' MappedSuperclass |
| 384 |
getEntityManagerFactory("default1").getCache().evict(GoalieGear.class, c2.getSerialNumber()); // ChestProtector |
| 385 |
getEntityManagerFactory("default1").getCache().evict(GoalieGear.class, p2.getSerialNumber()); // Pad |
| 386 |
|
| 387 |
// read the inheriting entities directly from the database (because they are no longer in the cache - hopefully) |
| 388 |
GoalieGear c5 = em2.find(GoalieGear.class, ID_CHESTPROTECTOR); |
| 389 |
GoalieGear p5 = em2.find(GoalieGear.class, ID_PADS); |
| 390 |
|
| 391 |
// Check that we are not actually getting the supposed "to be evicted" cached version (that was modified) |
| 392 |
String actual_chestProtector = c5.getDescription(); |
| 393 |
String actual_pads = p5.getDescription(); |
| 394 |
// verify that assignable subclasses (ChestProtector and Pads) are removed from the cache |
| 395 |
assertNotSame("Assertion Error - There should be no modified cached entity instance in the cache - the entity read from the database should be different. " |
| 396 |
, expected_chestProtector, actual_chestProtector); |
| 397 |
assertNotSame("Assertion Error - There should be no modified cached entity instance in the cache - the entity read from the database should be different. " |
| 398 |
, expected_pads, actual_pads); |
| 399 |
} finally { |
| 400 |
closeEntityManager(em1); |
| 401 |
closeEntityManager(em2); |
| 402 |
} |
| 403 |
} |
| 229 |
} |
404 |
} |
|
|
405 |
|
| 406 |
|
| 407 |
//org.eclipse.persistence.testing.models.jpa.jpaadvancedproperties.Customer |
| 408 |
|
| 409 |
public void testEvictClass_JavaLangClass_hasNoEffect() { |
| 410 |
if (! isJPA10()) { |
| 411 |
int ID = ID_TEST_BASE + 300; |
| 412 |
boolean _exceptionThrown = false; |
| 413 |
EntityManager em = createEntityManager("default1"); |
| 414 |
// Persist |
| 415 |
beginTransaction(em); |
| 416 |
// HockeyGear(Abstract Entity) << GoalieGear (Concrete MappedSuperclass) << ChestProtector (Concrete Entity) |
| 417 |
ChestProtector e1 = new ChestProtector(); |
| 418 |
e1.setDescription("gear"); |
| 419 |
e1.setSerialNumber(new Integer(ID)); |
| 420 |
em.persist(e1); |
| 421 |
commitTransaction(em); |
| 422 |
closeEntityManager(em); |
| 423 |
|
| 424 |
EntityManager em1 = createEntityManager("default1"); |
| 425 |
EntityManager em2 = createEntityManager("default1"); |
| 426 |
try { |
| 427 |
GoalieGear e2 = (GoalieGear) ((EntityManagerFactoryImpl) getEntityManagerFactory("default1")) |
| 428 |
.getServerSession().getIdentityMapAccessor().getFromIdentityMap(e1); |
| 429 |
// change the entity in the cache (only) - later a find() will get the unmodified version in the database |
| 430 |
e2.setDescription("new_gear"); |
| 431 |
String expected = em1.find(GoalieGear.class, ID).getDescription(); |
| 432 |
// The following dows not throws an IAE on a plain java (Integer) class - it will leave the cached entities as-is in the cache |
| 433 |
getEntityManagerFactory("default1").getCache().evict(Integer.class); |
| 434 |
// read the inheriting entities directly from the cache (because they were not evicted) |
| 435 |
GoalieGear g5 = em2.find(GoalieGear.class, ID); |
| 436 |
String actual = g5.getDescription(); |
| 437 |
assertSame("Assertion Error - There should be a modified cached entity instance in the cache - the entity is not actually read from the database" |
| 438 |
, expected, actual); |
| 439 |
} catch (IllegalArgumentException iae) { |
| 440 |
_exceptionThrown = true; |
| 441 |
} finally { |
| 442 |
closeEntityManager(em1); |
| 443 |
closeEntityManager(em2); |
| 444 |
assertFalse("IllegalArgumentException was thrown.", _exceptionThrown); |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
// The xml model version is not managed |
| 450 |
public void testGetId_fromUnmanagedMappedSuperclass_handles_null_descriptor() { |
| 451 |
if (!isJPA10()) { |
| 452 |
int ID = ID_TEST_BASE + 400; |
| 453 |
boolean _exceptionThrown = false; |
| 454 |
EntityManager em = createEntityManager();//"default1"); |
| 455 |
// Persist both implementing subclasses of GoalieGear |
| 456 |
beginTransaction(em); |
| 457 |
// CacheableTrueMappedSuperclass (MappedSuperclass with Id) << SubCacheableFalseEntity (Entity) |
| 458 |
org.eclipse.persistence.testing.models.jpa.xml.cacheable.SubCacheableFalseEntity anEntity |
| 459 |
= new org.eclipse.persistence.testing.models.jpa.xml.cacheable.SubCacheableFalseEntity(); |
| 460 |
org.eclipse.persistence.testing.models.jpa.xml.cacheable.CacheableTrueMappedSuperclass aMappedSuperclass |
| 461 |
= new org.eclipse.persistence.testing.models.jpa.xml.cacheable.CacheableTrueMappedSuperclass(); |
| 462 |
anEntity.setId(ID); |
| 463 |
// Can we place non-persistable objects into only the cache (non-entities)? |
| 464 |
// We do not need to persist this entity for this specific test |
| 465 |
//em.persist(anEntity); |
| 466 |
commitTransaction(em); |
| 467 |
closeEntityManager(em); |
| 468 |
|
| 469 |
EntityManager em1 = createEntityManager("default1"); |
| 470 |
EntityManager em2 = createEntityManager("default1"); |
| 471 |
Cache aJPACache = getEntityManagerFactory("default1").getCache(); |
| 472 |
JpaCache anEclipseLinkCache = (JpaCache)getEntityManagerFactory("default1").getCache(); |
| 473 |
Object anId = null; |
| 474 |
try { |
| 475 |
anId = anEclipseLinkCache.getId(anEntity); |
| 476 |
} catch (IllegalArgumentException iae) { |
| 477 |
_exceptionThrown = true; |
| 478 |
} finally { |
| 479 |
closeEntityManager(em1); |
| 480 |
closeEntityManager(em2); |
| 481 |
assertTrue("IllegalArgumentException should have been thrown on the unmanaged entity lacking a descriptor.", _exceptionThrown); |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
public void testGetId_fromUnsupportedJavaLangInteger_throwsIAE_on_null_descriptor() { |
| 487 |
if (!isJPA10()) { |
| 488 |
boolean _exceptionThrown = false; |
| 489 |
EntityManager em1 = createEntityManager("default1"); |
| 490 |
JpaCache anEclipseLinkCache = (JpaCache)getEntityManagerFactory("default1").getCache(); |
| 491 |
try { |
| 492 |
anEclipseLinkCache.getId(new Integer(1)); |
| 493 |
} catch (IllegalArgumentException iae) { |
| 494 |
_exceptionThrown = true; |
| 495 |
} finally { |
| 496 |
closeEntityManager(em1); |
| 497 |
assertTrue("IllegalArgumentException should have been thrown on a getId() call on an Integer which obviously has no descriptor.", _exceptionThrown); |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* In order to test handling of a null CMP3Policy on the descriptor during a cache.getId() call, |
| 504 |
* we must have an Id on an abstract MappedSuperclass root that is defined via sessions.xml. |
| 505 |
* Or we can simulate a null CMP3Policy by clearing it - so that the alternate code handling is run |
| 506 |
*/ |
| 507 |
public void testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_weaving_on() { |
| 508 |
if (!isJPA10()) { |
| 509 |
int ID = ID_TEST_BASE + 500; |
| 510 |
Object originalId = null; |
| 511 |
EntityManager em = createEntityManager();//"default1"); |
| 512 |
// Persist both implementing subclasses of GoalieGear |
| 513 |
beginTransaction(em); |
| 514 |
// CacheableTrueMappedSuperclass (MappedSuperclass with Id) << SubCacheableFalseEntity (Entity) |
| 515 |
org.eclipse.persistence.testing.models.jpa.cacheable.SubCacheableFalseEntity anEntity = new org.eclipse.persistence.testing.models.jpa.cacheable.SubCacheableFalseEntity(); |
| 516 |
org.eclipse.persistence.testing.models.jpa.cacheable.CacheableTrueMappedSuperclass aMappedSuperclass = new org.eclipse.persistence.testing.models.jpa.cacheable.CacheableTrueMappedSuperclass(); |
| 517 |
anEntity.setId(ID); |
| 518 |
// Can we place non-persistable objects into only the cache (non-entities)? |
| 519 |
em.persist(anEntity); |
| 520 |
commitTransaction(em); |
| 521 |
closeEntityManager(em); |
| 522 |
|
| 523 |
EntityManager em1 = createEntityManager("default1"); |
| 524 |
Cache aJPACache = getEntityManagerFactory("default1").getCache(); |
| 525 |
JpaCache anEclipseLinkCache = (JpaCache)getEntityManagerFactory("default1").getCache(); |
| 526 |
Object anId = null; |
| 527 |
originalId = anEntity.getId(); |
| 528 |
|
| 529 |
// clear the CMP3Policy to simulate a native pojo |
| 530 |
ClassDescriptor cdesc = ((EntityManagerImpl)em1).getSession().getClassDescriptor(anEntity.getClass()); |
| 531 |
CMP3Policy policy = (CMP3Policy) (cdesc.getCMPPolicy()); |
| 532 |
assertNotNull("CMP3Policy should exist", policy); |
| 533 |
cdesc.setCMPPolicy(null); |
| 534 |
try { |
| 535 |
// This call will test the weaving [on] section of getId() |
| 536 |
anId = anEclipseLinkCache.getId(anEntity); |
| 537 |
assertNotNull("Id instance should not be null", anId); |
| 538 |
assertTrue("Id instance should be of type Integer", anId instanceof Integer); |
| 539 |
assertEquals(((Integer)anId).intValue(), ID); |
| 540 |
assertEquals(anId, originalId); |
| 541 |
} finally { |
| 542 |
closeEntityManager(em1); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
public void testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_and_null_pk_with_weaving_on() { |
| 548 |
if (!isJPA10()) { |
| 549 |
//int ID = ID_TEST_BASE + 600; |
| 550 |
Integer ID = null; // keep object as null for unset testing |
| 551 |
Object originalId = null; |
| 552 |
EntityManager em = createEntityManager("metamodel1"); |
| 553 |
beginTransaction(em); |
| 554 |
// org.eclipse.persistence.testing.models.jpa.metamodel.Person(Concrete MappedSuperclass) <-- Corporation(Abstract MappedSuperclass) <-- Manufacturer (Entity) |
| 555 |
Manufacturer anEntity = new Manufacturer(); |
| 556 |
//anEntity.setId(ID); |
| 557 |
// Can we place non-persistable objects into only the cache (non-entities)? |
| 558 |
em.persist(anEntity); |
| 559 |
commitTransaction(em); |
| 560 |
closeEntityManager(em); |
| 561 |
|
| 562 |
EntityManager em1 = createEntityManager("metamodel1"); |
| 563 |
Cache aJPACache = getEntityManagerFactory("metamodel1").getCache(); |
| 564 |
JpaCache anEclipseLinkCache = (JpaCache)getEntityManagerFactory("metamodel1").getCache(); |
| 565 |
Object anId = null; |
| 566 |
// The originalId will be set by automatic sequence generation - if we require it |
| 567 |
originalId = anEntity.getId(); |
| 568 |
|
| 569 |
// clear the CMP3Policy to simulate a native pojo |
| 570 |
ClassDescriptor cdesc = ((EntityManagerImpl)em1).getSession().getClassDescriptor(anEntity.getClass()); |
| 571 |
CMP3Policy policy = (CMP3Policy) (cdesc.getCMPPolicy()); |
| 572 |
assertNotNull("CMP3Policy should exist prior to having been cleared manually for this test", policy); |
| 573 |
cdesc.setCMPPolicy(null); |
| 574 |
try { |
| 575 |
anId = anEclipseLinkCache.getId(anEntity); |
| 576 |
assertNotNull("Id instance should not be null", anId); |
| 577 |
assertTrue("Id instance should be of type Integer", anId instanceof Integer); |
| 578 |
assertEquals(anId, originalId); |
| 579 |
// set the CMPPolicy back for out of order testing |
| 580 |
cdesc.setCMPPolicy(policy); |
| 581 |
} finally { |
| 582 |
closeEntityManager(em1); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
// We need a MappedSuperclass that is defined by a persistence.xml where eclipselink.weaving="false" |
| 588 |
// This test requires that weaving is off in persistence.xml for metamodel1 |
| 589 |
public void testGetId_fromNativeMappedSuperclass_handles_null_cmp3policy_and_null_pk_with_weaving_off() { |
| 590 |
if (!isJPA10()) { |
| 591 |
int ID = ID_TEST_BASE + 700; |
| 592 |
//Integer ID = null; // keep object as null for unset testing |
| 593 |
Object originalId = null; |
| 594 |
EntityManager em = createEntityManager("metamodel1"); |
| 595 |
beginTransaction(em); |
| 596 |
Manufacturer anEntity = new Manufacturer(); |
| 597 |
anEntity.setName("test"); |
| 598 |
anEntity.setId(ID); |
| 599 |
// Can we place non-persistable objects into only the cache (non-entities)? |
| 600 |
em.persist(anEntity); |
| 601 |
commitTransaction(em); |
| 602 |
//closeEntityManager(em); // keep the EM open |
| 603 |
|
| 604 |
EntityManager em1 = createEntityManager("metamodel1"); |
| 605 |
Cache aJPACache = getEntityManagerFactory("metamodel1").getCache(); |
| 606 |
JpaCache anEclipseLinkCache = (JpaCache)getEntityManagerFactory("metamodel1").getCache(); |
| 607 |
Object anId = null; |
| 608 |
// The originalId will be set by automatic sequence generation - if we require it |
| 609 |
originalId = anEntity.getId(); |
| 610 |
|
| 611 |
// clear the CMP3Policy to simulate a native pojo |
| 612 |
ClassDescriptor cdesc = ((EntityManagerImpl)em).getSession().getClassDescriptor(anEntity.getClass()); |
| 613 |
assertNotNull("ClassDescriptor for Entity must not be null", cdesc); // check that the entityManager is not null |
| 614 |
CMP3Policy policy = (CMP3Policy) (cdesc.getCMPPolicy()); |
| 615 |
assertNotNull("CMP3Policy should exist prior to having been cleared manually for this test", policy); |
| 616 |
cdesc.setCMPPolicy(null); |
| 617 |
try { |
| 618 |
anId = anEclipseLinkCache.getId(anEntity); |
| 619 |
assertNotNull("Id instance should not be null", anId); |
| 620 |
assertTrue("Id instance should be of type Integer", anId instanceof Integer); |
| 621 |
assertEquals(anId, ID); |
| 622 |
assertEquals(anId, originalId); |
| 623 |
// set the CMPPolicy back for out of order testing |
| 624 |
cdesc.setCMPPolicy(policy); |
| 625 |
} finally { |
| 626 |
closeEntityManager(em1); |
| 627 |
closeEntityManager(em); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
// 20100422: 248780: CacheImpl refactor for non-Entity classes |
| 633 |
public static int ID_TEST_BASE = 1136; // change this value during iterative testing |
| 230 |
} |
634 |
} |