1 7 8 package org.springframework.transaction.annotation; 9 10 import java.io.IOException ; 11 import java.lang.reflect.Method ; 12 13 import junit.framework.TestCase; 14 15 import org.springframework.transaction.interceptor.NoRollbackRuleAttribute; 16 import org.springframework.transaction.interceptor.RollbackRuleAttribute; 17 import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; 18 import org.springframework.transaction.interceptor.TransactionAttribute; 19 20 23 public class AnnotationTransactionAttributeSourceTests extends TestCase { 24 25 public void testNullOrEmpty() throws Exception { 26 27 Method method = Empty.class.getMethod("getAge", (Class []) null); 28 29 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 30 assertNull(atas.getTransactionAttribute(method, null)); 31 32 assertNull(atas.getTransactionAttribute(method, null)); 34 } 35 36 40 public void testTransactionAttributeDeclaredOnClassMethod() throws Exception { 41 Method classMethod = TestBean1.class.getMethod("getAge", (Class []) null); 42 43 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 44 TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class); 45 46 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); 47 rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception .class)); 48 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 49 } 50 51 54 public void testTransactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception { 55 Method interfaceMethod = ITestBean2.class.getMethod("getAge", (Class []) null); 56 57 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 58 TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class); 59 60 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); 61 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 62 } 63 64 67 public void testTransactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod() throws Exception { 68 Method classMethod = TestBean3.class.getMethod("getAge", (Class []) null); 69 Method interfaceMethod = ITestBean3.class.getMethod("getAge", (Class []) null); 70 71 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 72 TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class); 73 assertEquals(TransactionAttribute.PROPAGATION_REQUIRES_NEW, actual.getPropagationBehavior()); 74 75 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); 76 rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception .class)); 77 rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException .class)); 78 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 79 } 80 81 82 public void testRollbackRulesAreApplied() throws Exception { 83 Method method = TestBean3.class.getMethod("getAge", (Class []) null); 84 85 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 86 TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean3.class); 87 88 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); 89 rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception")); 90 rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException .class)); 91 92 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 93 assertTrue(actual.rollbackOn(new Exception ())); 94 assertFalse(actual.rollbackOn(new IOException ())); 95 96 actual = atas.getTransactionAttribute(method, method.getDeclaringClass()); 97 98 rbta = new RuleBasedTransactionAttribute(); 99 rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception")); 100 rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException .class)); 101 102 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 103 assertTrue(actual.rollbackOn(new Exception ())); 104 assertFalse(actual.rollbackOn(new IOException ())); 105 } 106 107 111 public void testDefaultsToClassTransactionAttribute() throws Exception { 112 Method method = TestBean4.class.getMethod("getAge", (Class []) null); 113 114 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); 115 TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean4.class); 116 117 RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); 118 rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception .class)); 119 rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException .class)); 120 assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); 121 } 122 123 124 public interface ITestBean { 125 126 int getAge(); 127 128 void setAge(int age); 129 130 String getName(); 131 132 void setName(String name); 133 } 134 135 public interface ITestBean2 { 136 137 @Transactional 138 int getAge(); 139 140 void setAge(int age); 141 142 String getName(); 143 144 void setName(String name); 145 } 146 147 public interface ITestBean3 { 148 149 @Transactional() 150 int getAge(); 151 152 void setAge(int age); 153 154 String getName(); 155 156 void setName(String name); 157 } 158 159 160 public static class Empty implements ITestBean { 161 162 private String name; 163 164 private int age; 165 166 public Empty() { 167 } 168 169 public Empty(String name, int age) { 170 this.name = name; 171 this.age = age; 172 } 173 174 public String getName() { 175 return name; 176 } 177 178 public void setName(String name) { 179 this.name = name; 180 } 181 182 public int getAge() { 183 return age; 184 } 185 186 public void setAge(int age) { 187 this.age = age; 188 } 189 } 190 191 192 public static class TestBean1 implements ITestBean { 193 194 private String name; 195 196 private int age; 197 198 public TestBean1() { 199 } 200 201 public TestBean1(String name, int age) { 202 this.name = name; 203 this.age = age; 204 } 205 206 public String getName() { 207 return name; 208 } 209 210 public void setName(String name) { 211 this.name = name; 212 } 213 214 @Transactional(rollbackFor=Exception .class) 215 public int getAge() { 216 return age; 217 } 218 219 public void setAge(int age) { 220 this.age = age; 221 } 222 } 223 224 225 public static class TestBean2 implements ITestBean2 { 226 227 private String name; 228 229 private int age; 230 231 public TestBean2() { 232 } 233 234 public TestBean2(String name, int age) { 235 this.name = name; 236 this.age = age; 237 } 238 239 public String getName() { 240 return name; 241 } 242 243 public void setName(String name) { 244 this.name = name; 245 } 246 247 public int getAge() { 248 return age; 249 } 250 251 public void setAge(int age) { 252 this.age = age; 253 } 254 } 255 256 257 public static class TestBean3 implements ITestBean3 { 258 259 private String name; 260 261 private int age; 262 263 public TestBean3() { 264 } 265 266 public TestBean3(String name, int age) { 267 this.name = name; 268 this.age = age; 269 } 270 271 public String getName() { 272 return name; 273 } 274 275 public void setName(String name) { 276 this.name = name; 277 } 278 279 @Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception .class, noRollbackFor={IOException .class}) 280 public int getAge() { 281 return age; 282 } 283 284 public void setAge(int age) { 285 this.age = age; 286 } 287 } 288 289 290 @Transactional(rollbackFor=Exception .class, noRollbackFor={IOException .class}) 291 public static class TestBean4 implements ITestBean3 { 292 293 private String name; 294 295 private int age; 296 297 public TestBean4() { 298 } 299 300 public TestBean4(String name, int age) { 301 this.name = name; 302 this.age = age; 303 } 304 305 public String getName() { 306 return name; 307 } 308 309 public void setName(String name) { 310 this.name = name; 311 } 312 313 public int getAge() { 314 return age; 315 } 316 317 public void setAge(int age) { 318 this.age = age; 319 } 320 } 321 322 } 323 | Popular Tags |