reg.setTransactionKey("a");
EntityManager em1a = contexts.getCurrentPersistenceContext(emf1, props1, useCount, cbk);
reg.setTransactionKey("b");
EntityManager em1b = contexts.getCurrentPersistenceContext(emf1, props2, useCount, cbk);
Skeleton.getSkeleton(emf1).assertCalledExactNumberOfTimes(new MethodCall(EntityManagerFactory.class, "createEntityManager", props1), 1);
Skeleton.getSkeleton(emf1).assertCalledExactNumberOfTimes(new MethodCall(EntityManagerFactory.class, "createEntityManager", props2), 1);
assertNotSame("We should not get the same delegate!", em1a, em1b);
Skeleton.getSkeleton(em1a).assertNotCalled(new MethodCall(EntityManager.class, "close"));
Skeleton.getSkeleton(em1b).assertNotCalled(new MethodCall(EntityManager.class, "close"));
assertEquals("Expected two creations", 2, useCount.get());
Skeleton.getSkeleton(cbk).assertSkeletonNotCalled();
reg.setTransactionKey("a");
EntityManager em2a = contexts.getCurrentPersistenceContext(emf2, props1, useCount, cbk);
reg.setTransactionKey("b");
EntityManager em2b = contexts.getCurrentPersistenceContext(emf2, props2, useCount, cbk);
Skeleton.getSkeleton(emf2).assertCalledExactNumberOfTimes(new MethodCall(EntityManagerFactory.class, "createEntityManager", props1), 1);
Skeleton.getSkeleton(emf2).assertCalledExactNumberOfTimes(new MethodCall(EntityManagerFactory.class, "createEntityManager", props2), 1);
assertNotSame("We should get the same delegate!", em2a, em2b);
Skeleton.getSkeleton(em2a).assertNotCalled(new MethodCall(EntityManager.class, "close"));
Skeleton.getSkeleton(em2b).assertNotCalled(new MethodCall(EntityManager.class, "close"));
assertEquals("Expected four creations", 4, useCount.get());
Skeleton.getSkeleton(cbk).assertSkeletonNotCalled();
reg.setTransactionKey("b");
reg.afterCompletion("b");
Skeleton.getSkeleton(em1a).assertNotCalled(new MethodCall(EntityManager.class, "close"));
Skeleton.getSkeleton(em1b).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
Skeleton.getSkeleton(em2a).assertNotCalled(new MethodCall(EntityManager.class, "close"));
Skeleton.getSkeleton(em2b).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
assertEquals("Expected two uncreations", 2, useCount.get());
Skeleton.getSkeleton(cbk).assertCalledExactNumberOfTimes(new MethodCall(DestroyCallback.class,
"callback"), 2);
reg.setTransactionKey("a");
reg.afterCompletion("a");
Skeleton.getSkeleton(em1a).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
Skeleton.getSkeleton(em1b).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
Skeleton.getSkeleton(em2a).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
Skeleton.getSkeleton(em2b).assertCalledExactNumberOfTimes(new MethodCall(EntityManager.class, "close"), 1);
assertEquals("Expected no remaining instances", 0, useCount.get());
Skeleton.getSkeleton(cbk).assertCalledExactNumberOfTimes(new MethodCall(DestroyCallback.class,
"callback"), 4);
}