1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.rmi.RemoteException ; 20 import java.util.Collections ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 import javax.servlet.ServletException ; 25 26 import junit.framework.TestCase; 27 28 import org.springframework.mail.MailSendException; 29 import org.springframework.transaction.TransactionDefinition; 30 31 36 public class RuleBasedTransactionAttributeTests extends TestCase { 37 38 public void testDefaultRule() { 39 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(); 40 assertTrue(rta.rollbackOn(new RuntimeException ())); 41 assertTrue(rta.rollbackOn(new MailSendException(""))); 42 assertTrue(!rta.rollbackOn(new Exception ())); 43 assertTrue(!rta.rollbackOn(new ServletException ())); 44 } 45 46 50 public void testRuleForRollbackOnChecked() { 51 List l = new LinkedList (); 52 l.add(new RollbackRuleAttribute("javax.servlet.ServletException")); 53 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 54 55 assertTrue(rta.rollbackOn(new RuntimeException ())); 56 assertTrue(rta.rollbackOn(new MailSendException(""))); 57 assertTrue(!rta.rollbackOn(new Exception ())); 58 assertTrue(rta.rollbackOn(new ServletException ())); 60 } 61 62 public void testRuleForCommitOnUnchecked() { 63 List l = new LinkedList (); 64 l.add(new NoRollbackRuleAttribute("org.springframework.mail.MailSendException")); 65 l.add(new RollbackRuleAttribute("javax.servlet.ServletException")); 66 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 67 68 assertTrue(rta.rollbackOn(new RuntimeException ())); 69 assertTrue(!rta.rollbackOn(new MailSendException(""))); 71 assertTrue(!rta.rollbackOn(new Exception ())); 72 assertTrue(rta.rollbackOn(new ServletException ())); 74 } 75 76 public void testRuleForSelectiveRollbackOnCheckedWithString() { 77 List l = new LinkedList (); 78 l.add(new RollbackRuleAttribute("java.rmi.RemoteException")); 79 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 80 testRuleForSelectiveRollbackOnChecked(rta); 81 } 82 83 public void testRuleForSelectiveRollbackOnCheckedWithClass() { 84 List l = Collections.singletonList(new RollbackRuleAttribute(RemoteException .class)); 85 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 86 testRuleForSelectiveRollbackOnChecked(rta); 87 } 88 89 private void testRuleForSelectiveRollbackOnChecked(RuleBasedTransactionAttribute rta) { 90 assertTrue(rta.rollbackOn(new RuntimeException ())); 91 assertTrue(!rta.rollbackOn(new Exception ())); 93 assertTrue(rta.rollbackOn(new RemoteException ())); 95 } 96 97 101 public void testRuleForCommitOnSubclassOfChecked() { 102 List l = new LinkedList (); 103 l.add(new RollbackRuleAttribute("java.lang.Exception")); 106 l.add(new NoRollbackRuleAttribute("ServletException")); 107 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 108 109 assertTrue(rta.rollbackOn(new RuntimeException ())); 110 assertTrue(rta.rollbackOn(new Exception ())); 111 assertFalse(rta.rollbackOn(new ServletException ())); 113 } 114 115 public void testRollbackNever() { 116 List l = new LinkedList (); 117 l.add(new NoRollbackRuleAttribute("Throwable")); 118 RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l); 119 120 assertTrue(!rta.rollbackOn(new Throwable ())); 121 assertTrue(!rta.rollbackOn(new RuntimeException ())); 122 assertTrue(!rta.rollbackOn(new MailSendException(""))); 123 assertTrue(!rta.rollbackOn(new Exception ())); 124 assertTrue(!rta.rollbackOn(new ServletException ())); 125 } 126 127 } 128 | Popular Tags |