private void addRollbackRuleAttributesTo(List<RollbackRuleAttribute> rollbackRules, String rollbackForValue) { String[] exceptionTypeNames = StringUtils.commaDelimitedListToStringArray(rollbackForValue); for (String typeName : exceptionTypeNames) { rollbackRules.add(new RollbackRuleAttribute(StringUtils.trimWhitespace(typeName))); } }
@Test(expected = IllegalArgumentException.class) public void ctorArgExceptionStringNameVersionWithNull() { new RollbackRuleAttribute((String) null); }
@Test(expected = IllegalArgumentException.class) public void ctorArgMustBeAThrowableClassWithNonThrowableType() { new RollbackRuleAttribute(StringBuffer.class); }
protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) { RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.setPropagationBehaviorName( RuleBasedTransactionAttribute.PREFIX_PROPAGATION + attributes.getEnum("value").toString()); List<RollbackRuleAttribute> rollbackRules = new ArrayList<>(); for (Class<?> rbRule : attributes.getClassArray("rollbackOn")) { rollbackRules.add(new RollbackRuleAttribute(rbRule)); } for (Class<?> rbRule : attributes.getClassArray("dontRollbackOn")) { rollbackRules.add(new NoRollbackRuleAttribute(rbRule)); } rbta.setRollbackRules(rollbackRules); return rbta; }
@Test(expected = IllegalArgumentException.class) public void ctorArgMustBeAThrowableClassWithNullThrowableType() { new RollbackRuleAttribute((Class<?>) null); }
@Test public void testRuleForSelectiveRollbackOnCheckedWithString() { List<RollbackRuleAttribute> l = new LinkedList<>(); l.add(new RollbackRuleAttribute(java.rmi.RemoteException.class.getName())); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); doTestRuleForSelectiveRollbackOnChecked(rta); }
@Test public void foundImmediatelyWithString() { RollbackRuleAttribute rr = new RollbackRuleAttribute(java.lang.Exception.class.getName()); assertEquals(0, rr.getDepth(new Exception())); }
@Test public void foundImmediatelyWithClass() { RollbackRuleAttribute rr = new RollbackRuleAttribute(Exception.class); assertEquals(0, rr.getDepth(new Exception())); }
@Test public void testRuleForSelectiveRollbackOnCheckedWithClass() { List<RollbackRuleAttribute> l = Collections.singletonList(new RollbackRuleAttribute(RemoteException.class)); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); doTestRuleForSelectiveRollbackOnChecked(rta); }
@Test public void ancestry() { RollbackRuleAttribute rr = new RollbackRuleAttribute(java.lang.Exception.class.getName()); // Exception -> Runtime -> NestedRuntime -> MyRuntimeException assertThat(rr.getDepth(new MyRuntimeException("")), equalTo(3)); }
@Test public void notFound() { RollbackRuleAttribute rr = new RollbackRuleAttribute(java.io.IOException.class.getName()); assertEquals(-1, rr.getDepth(new MyRuntimeException(""))); }
@Test public void testRuleForCommitOnUnchecked() { List<RollbackRuleAttribute> list = new LinkedList<>(); list.add(new NoRollbackRuleAttribute(MyRuntimeException.class.getName())); list.add(new RollbackRuleAttribute(IOException.class.getName())); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); assertTrue(rta.rollbackOn(new RuntimeException())); // Check default behaviour is overridden assertFalse(rta.rollbackOn(new MyRuntimeException(""))); assertFalse(rta.rollbackOn(new Exception())); // Check that default behaviour is overridden assertTrue(rta.rollbackOn(new IOException())); }
/** * Test one checked exception that should roll back. */ @Test public void testRuleForRollbackOnChecked() { List<RollbackRuleAttribute> list = new LinkedList<>(); list.add(new RollbackRuleAttribute(IOException.class.getName())); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); assertTrue(rta.rollbackOn(new RuntimeException())); assertTrue(rta.rollbackOn(new MyRuntimeException(""))); assertFalse(rta.rollbackOn(new Exception())); // Check that default behaviour is overridden assertTrue(rta.rollbackOn(new IOException())); }
/** * See <a href="http://forum.springframework.org/showthread.php?t=41350">this forum post</a>. */ @Test public void testConflictingRulesToDetermineExactContract() { List<RollbackRuleAttribute> list = new LinkedList<>(); list.add(new NoRollbackRuleAttribute(MyBusinessWarningException.class)); list.add(new RollbackRuleAttribute(MyBusinessException.class)); RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list); assertTrue(rta.rollbackOn(new MyBusinessException())); assertFalse(rta.rollbackOn(new MyBusinessWarningException())); }
@Test public void customClassAttributeDetected() throws Exception { Method method = TestBean5.class.getMethod("getAge"); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean5.class); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); }
/** * Test the important case where the invocation is on a proxied interface method * but the attribute is defined on the target class. */ @Test public void transactionAttributeDeclaredOnClassMethod() throws Exception { Method classMethod = ITestBean1.class.getMethod("getAge"); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); }
@Test public void customMethodAttributeDetected() throws Exception { Method method = TestBean6.class.getMethod("getAge"); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean6.class); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); }
@Test public void alwaysTrueForThrowable() { RollbackRuleAttribute rr = new RollbackRuleAttribute(java.lang.Throwable.class.getName()); assertTrue(rr.getDepth(new MyRuntimeException("")) > 0); assertTrue(rr.getDepth(new IOException()) > 0); assertTrue(rr.getDepth(new FatalBeanException(null,null)) > 0); assertTrue(rr.getDepth(new RuntimeException()) > 0); }
@Test public void customClassAttributeWithReadOnlyOverrideDetected() throws Exception { Method method = TestBean7.class.getMethod("getAge"); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean7.class); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); assertTrue(actual.isReadOnly()); }
@Test public void customMethodAttributeWithReadOnlyOverrideDetected() throws Exception { Method method = TestBean8.class.getMethod("getAge"); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean8.class); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); assertTrue(actual.isReadOnly()); }