1 16 17 package org.springframework.transaction.interceptor; 18 19 import javax.servlet.ServletException ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.mail.MailSendException; 24 import org.springframework.transaction.TransactionDefinition; 25 26 33 public class TransactionAttributeEditorTests extends TestCase { 34 35 public void testNull() { 36 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 37 pe.setAsText(null); 38 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 39 assertTrue(ta == null); 40 } 41 42 public void testEmptyString() { 43 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 44 pe.setAsText(""); 45 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 46 assertTrue(ta == null); 47 } 48 49 public void testValidPropagationCodeOnly() { 50 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 51 pe.setAsText("PROPAGATION_REQUIRED"); 52 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 53 assertTrue(ta != null); 54 assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED); 55 assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT); 56 assertTrue(!ta.isReadOnly()); 57 } 58 59 public void testInvalidPropagationCodeOnly() { 60 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 61 try { 62 pe.setAsText("XXPROPAGATION_REQUIRED"); 63 fail("Should have failed with bogus propagation code"); 64 } 65 catch (IllegalArgumentException ex) { 66 } 68 } 69 70 public void testValidPropagationCodeAndIsolationCode() { 71 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 72 pe.setAsText("PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED"); 73 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 74 assertTrue(ta != null); 75 assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED); 76 assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED); 77 } 78 79 public void testValidPropagationAndIsolationCodesAndInvalidRollbackRule() { 80 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 81 try { 82 pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX"); 83 fail("Should have failed with bogus rollback rule"); 84 } 85 catch (IllegalArgumentException ex) { 86 } 88 } 89 90 public void testValidPropagationCodeAndIsolationCodeAndRollbackRules1() { 91 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 92 pe.setAsText("PROPAGATION_MANDATORY,ISOLATION_REPEATABLE_READ,timeout_10,-ServletException,+MailSendException"); 93 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 94 assertNotNull(ta); 95 assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_MANDATORY); 96 assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ); 97 assertEquals(ta.getTimeout(), 10); 98 assertFalse(ta.isReadOnly()); 99 assertTrue(ta.rollbackOn(new RuntimeException ())); 100 assertFalse(ta.rollbackOn(new Exception ())); 101 assertTrue(ta.rollbackOn(new ServletException ())); 103 assertTrue(!ta.rollbackOn(new MailSendException(""))); 104 } 105 106 public void testValidPropagationCodeAndIsolationCodeAndRollbackRules2() { 107 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 108 pe.setAsText("+ServletException,readOnly,ISOLATION_READ_COMMITTED,-MailSendException,PROPAGATION_SUPPORTS"); 109 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 110 assertNotNull(ta); 111 assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS); 112 assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_READ_COMMITTED); 113 assertEquals(ta.getTimeout(), TransactionDefinition.TIMEOUT_DEFAULT); 114 assertTrue(ta.isReadOnly()); 115 assertTrue(ta.rollbackOn(new RuntimeException ())); 116 assertFalse(ta.rollbackOn(new Exception ())); 117 assertFalse(ta.rollbackOn(new ServletException ())); 119 assertTrue(ta.rollbackOn(new MailSendException(""))); 120 } 121 122 public void testDefaultTransactionAttributeToString() { 123 DefaultTransactionAttribute source = new DefaultTransactionAttribute(); 124 source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); 125 source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); 126 source.setTimeout(10); 127 source.setReadOnly(true); 128 129 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 130 pe.setAsText(source.toString()); 131 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 132 assertEquals(ta, source); 133 assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS); 134 assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ); 135 assertEquals(ta.getTimeout(), 10); 136 assertTrue(ta.isReadOnly()); 137 assertTrue(ta.rollbackOn(new RuntimeException ())); 138 assertFalse(ta.rollbackOn(new Exception ())); 139 140 source.setTimeout(9); 141 assertNotSame(ta, source); 142 source.setTimeout(10); 143 assertEquals(ta, source); 144 } 145 146 public void testRuleBasedTransactionAttributeToString() { 147 RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute(); 148 source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); 149 source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); 150 source.setTimeout(10); 151 source.setReadOnly(true); 152 source.getRollbackRules().add(new RollbackRuleAttribute("IllegalArgumentException")); 153 source.getRollbackRules().add(new NoRollbackRuleAttribute("IllegalStateException")); 154 155 TransactionAttributeEditor pe = new TransactionAttributeEditor(); 156 pe.setAsText(source.toString()); 157 TransactionAttribute ta = (TransactionAttribute) pe.getValue(); 158 assertEquals(ta, source); 159 assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS); 160 assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ); 161 assertEquals(ta.getTimeout(), 10); 162 assertTrue(ta.isReadOnly()); 163 assertTrue(ta.rollbackOn(new IllegalArgumentException ())); 164 assertFalse(ta.rollbackOn(new IllegalStateException ())); 165 166 source.getRollbackRules().clear(); 167 assertNotSame(ta, source); 168 source.getRollbackRules().add(new RollbackRuleAttribute("IllegalArgumentException")); 169 source.getRollbackRules().add(new NoRollbackRuleAttribute("IllegalStateException")); 170 assertEquals(ta, source); 171 } 172 173 } 174 | Popular Tags |