コード例 #1
0
  /*
   * Test method for
   * 'org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetEntityManager(EntityManagerFactory)'
   */
  @Test
  public void testDoGetEntityManager() {
    // test null assertion
    try {
      EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null);
      fail("expected exception");
    } catch (IllegalArgumentException ex) {
      // it's okay
    }
    EntityManagerFactory factory = mock(EntityManagerFactory.class);

    // no tx active
    assertNull(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null));
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
  }
コード例 #2
0
  @Test
  public void testDoGetEntityManagerWithTx() throws Exception {
    try {
      EntityManagerFactory factory = mock(EntityManagerFactory.class);
      EntityManager manager = mock(EntityManager.class);

      TransactionSynchronizationManager.initSynchronization();
      given(factory.createEntityManager()).willReturn(manager);

      // no tx active
      assertSame(manager, EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null));
      assertSame(
          manager,
          ((EntityManagerHolder) TransactionSynchronizationManager.unbindResource(factory))
              .getEntityManager());
    } finally {
      TransactionSynchronizationManager.clearSynchronization();
    }

    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
  }