@Test public void determineTransactionManagerWithNoBeanFactory() { PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, null); assertSame(transactionManager, ti.determineTransactionManager(new DefaultTransactionAttribute())); }
@Test public void matchAlwaysTransactionAttributeSource() throws Exception { MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource(); TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null); assertNotNull(ta); assertTrue(TransactionDefinition.PROPAGATION_REQUIRED == ta.getPropagationBehavior()); tas.setTransactionAttribute(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_SUPPORTS)); ta = tas.getTransactionAttribute(IOException.class.getMethod("getMessage"), IOException.class); assertNotNull(ta); assertTrue(TransactionDefinition.PROPAGATION_SUPPORTS == ta.getPropagationBehavior()); }
@Test public void determineTransactionManagerWithEmptyQualifierAndDefaultName() { BeanFactory beanFactory = mock(BeanFactory.class); PlatformTransactionManager defaultTransactionManager = associateTransactionManager(beanFactory, "defaultTransactionManager"); TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName( "defaultTransactionManager", beanFactory); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier(""); assertSame(defaultTransactionManager, ti.determineTransactionManager(attribute)); }
@Test public void putTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); Object key = new Object(); cache.put(key, "123"); assertNull(target.get(key)); this.txManager.commit(status); assertEquals("123", target.get(key, String.class)); }
@Test public void determineTransactionManagerWithQualifierUnknown() { BeanFactory beanFactory = mock(BeanFactory.class); TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier("fooTransactionManager"); thrown.expect(NoSuchBeanDefinitionException.class); thrown.expectMessage("'fooTransactionManager'"); ti.determineTransactionManager(attribute); }
@Test public void determineTransactionManagerDefaultSeveralTimes() { BeanFactory beanFactory = mock(BeanFactory.class); TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory); PlatformTransactionManager txManager = mock(PlatformTransactionManager.class); given(beanFactory.getBean(PlatformTransactionManager.class)).willReturn(txManager); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); PlatformTransactionManager actual = ti.determineTransactionManager(attribute); assertSame(txManager, actual); // Call again, should be cached PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute); assertSame(txManager, actual2); verify(beanFactory, times(1)).getBean(PlatformTransactionManager.class); }
@Test public void determineTransactionManagerWithBeanNameSeveralTimes() { BeanFactory beanFactory = mock(BeanFactory.class); TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName( "fooTransactionManager", beanFactory); PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager"); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); PlatformTransactionManager actual = ti.determineTransactionManager(attribute); assertSame(txManager, actual); // Call again, should be cached PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute); assertSame(txManager, actual2); verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class); }
@Test public void determineTransactionManagerWithQualifierAndDefaultName() { BeanFactory beanFactory = mock(BeanFactory.class); associateTransactionManager(beanFactory, "defaultTransactionManager"); TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName( "defaultTransactionManager", beanFactory); PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager"); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier("fooTransactionManager"); assertSame(fooTransactionManager, ti.determineTransactionManager(attribute)); }
@Test public void evictTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.evict(key); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); }
@Test public void determineTransactionManagerWithQualifierAndDefault() { BeanFactory beanFactory = mock(BeanFactory.class); PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, beanFactory); PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager"); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier("fooTransactionManager"); assertSame(fooTransactionManager, ti.determineTransactionManager(attribute)); }
@Test public void clearTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.clear(); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); } }
@Test public void determineTransactionManagerWithQualifierSeveralTimes() { BeanFactory beanFactory = mock(BeanFactory.class); TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory); PlatformTransactionManager txManager = associateTransactionManager(beanFactory, "fooTransactionManager"); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier("fooTransactionManager"); PlatformTransactionManager actual = ti.determineTransactionManager(attribute); assertSame(txManager, actual); // Call again, should be cached PlatformTransactionManager actual2 = ti.determineTransactionManager(attribute); assertSame(txManager, actual2); verify(beanFactory, times(1)).containsBean("fooTransactionManager"); verify(beanFactory, times(1)).getBean("fooTransactionManager", PlatformTransactionManager.class); }
/** * Test that TransactionStatus.setRollbackOnly works. */ @Test public void programmaticRollback() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); Method m = getNameMethod; MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(m, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); given(ptm.getTransaction(txatt)).willReturn(status); final String name = "jenny"; TestBean tb = new TestBean() { @Override public String getName() { TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus(); txStatus.setRollbackOnly(); return name; } }; ITestBean itb = (ITestBean) advised(tb, ptm, tas); // verification!? assertTrue(name.equals(itb.getName())); verify(ptm).commit(status); }
@Test public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception { Object testEntity = new Object(); // This one's for the tx (shared) EntityManager sharedEm = mock(EntityManager.class); given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction()); // This is the application-specific one EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm); LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit(); MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo()); pui.setTransactionType(PersistenceUnitTransactionType.JTA); JpaTransactionManager jpatm = new JpaTransactionManager(); jpatm.setEntityManagerFactory(cefb.getObject()); TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute()); EntityManagerFactory emf = cefb.getObject(); assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject()); assertNotSame("EMF must be proxied", mockEmf, emf); EntityManager em = emf.createEntityManager(); em.joinTransaction(); assertFalse(em.contains(testEntity)); jpatm.commit(txStatus); cefb.destroy(); verify(mockEm).joinTransaction(); verify(mockEm).contains(testEntity); verify(mockEmf).close(); }
/** * Check that a transaction is created and committed using * CallbackPreferringPlatformTransactionManager. */ @Test public void transactionShouldSucceedWithCallbackPreference() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager(); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); assertSame(txatt, ptm.getDefinition()); assertFalse(ptm.getStatus().isRollbackOnly()); }
@Test public void testDefaultTransactionAttributeToString() { DefaultTransactionAttribute source = new DefaultTransactionAttribute(); source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); source.setTimeout(10); source.setReadOnly(true); TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(source.toString()); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertEquals(ta, source); assertEquals(TransactionDefinition.PROPAGATION_SUPPORTS, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.ISOLATION_REPEATABLE_READ, ta.getIsolationLevel()); assertEquals(10, ta.getTimeout()); assertTrue(ta.isReadOnly()); assertTrue(ta.rollbackOn(new RuntimeException())); assertFalse(ta.rollbackOn(new Exception())); source.setTimeout(9); assertNotSame(ta, source); source.setTimeout(10); assertEquals(ta, source); }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceedWithNotNew() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); // verification!? itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
@Test public void transactionExceptionPropagatedWithCallbackPreference() throws Throwable { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(exceptionalMethod, txatt); MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager(); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); try { itb.exceptional(new OptimisticLockingFailureException("")); fail("Should have thrown OptimisticLockingFailureException"); } catch (OptimisticLockingFailureException ex) { // expected } checkTransactionStatus(false); assertSame(txatt, ptm.getDefinition()); assertFalse(ptm.getStatus().isRollbackOnly()); }
/** * Check that two transactions are created and committed. */ @Test public void twoTransactionsShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource(); tas1.register(getNameMethod, txatt); MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource(); tas2.register(setNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] {tas1, tas2}); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); itb.setName("myName"); checkTransactionStatus(false); verify(ptm, times(2)).commit(status); }