1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.lang.reflect.Field ; 20 import java.lang.reflect.Method ; 21 import java.util.Map ; 22 23 import javax.servlet.ServletException ; 24 25 import junit.framework.TestCase; 26 27 import org.springframework.beans.ITestBean; 28 import org.springframework.beans.TestBean; 29 import org.springframework.metadata.Attributes; 30 import org.springframework.metadata.commons.CommonsAttributes; 31 import org.springframework.metadata.support.MapAttributes; 32 import org.springframework.transaction.TransactionDefinition; 33 34 37 public class AttributesTransactionAttributeSourceTests extends TestCase { 38 39 public void testNullOrEmpty() throws Exception { 40 Method method = ITestBean.class.getMethod("getAge", (Class []) null); 41 42 MapAttributes mar = new MapAttributes(); 43 mar.register(method, null); 44 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(mar); 45 assertNull(atas.getTransactionAttribute(method, null)); 46 47 mar.register(method, new Object [0]); 48 assertNull(atas.getTransactionAttribute(method, null)); 49 assertNull(atas.getTransactionAttribute(method, null)); 51 } 52 53 public void testSingleTransactionAttribute() throws Exception { 54 Method method = ITestBean.class.getMethod("getAge", (Class []) null); 55 56 TransactionAttribute txAtt = new DefaultTransactionAttribute(); 57 58 MapAttributes ma = new MapAttributes(); 59 ma.register(method, new Object []{txAtt}); 60 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 61 TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass()); 62 assertEquals(txAtt, actual); 63 assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass())); 65 } 66 67 68 public void testTransactionAttributeAmongOthers() throws Exception { 69 Method method = TestBean.class.getMethod("getAge", (Class []) null); 70 71 TransactionAttribute txAtt = new DefaultTransactionAttribute(); 72 73 MapAttributes ma = new MapAttributes(); 74 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 75 ma.register(method, new Object []{new Object (), "", txAtt, "er"}); 76 TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass()); 77 assertEquals(txAtt, actual); 78 assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass())); 79 } 80 81 public void testOverloadedMethodsGetDistinctTransactionAttributes() throws Exception { 82 Method method1 = TestBeanWithOverloadedMethod.class.getMethod("getAge", (Class []) null); 83 Method method2 = TestBeanWithOverloadedMethod.class.getMethod("getAge", new Class []{int.class}); 84 85 TransactionAttribute txAtt1 = new DefaultTransactionAttribute(); 86 TransactionAttribute txAtt2 = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NEVER); 87 88 MapAttributes ma = new MapAttributes(); 89 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 90 ma.register(method1, new Object []{new Object (), "", txAtt1, "er"}); 91 ma.register(method2, new Object []{txAtt2}); 92 93 TransactionAttribute actual = atas.getTransactionAttribute(method1, TestBeanWithOverloadedMethod.class); 94 assertEquals(txAtt1, actual); 95 assertSame(txAtt1, atas.getTransactionAttribute(method1, method1.getDeclaringClass())); 96 97 TransactionAttribute actual2 = atas.getTransactionAttribute(method2, TestBeanWithOverloadedMethod.class); 98 assertEquals(txAtt2, actual2); 99 assertSame(txAtt2, atas.getTransactionAttribute(method2, method2.getDeclaringClass())); 100 } 101 102 106 public void testTransactionAttributeDeclaredOnClassMethod() throws Exception { 107 Method classMethod = TestBean.class.getMethod("getAge", (Class []) null); 108 Method interfaceMethod = ITestBean.class.getMethod("getAge", (Class []) null); 109 110 TransactionAttribute txAtt = new DefaultTransactionAttribute(); 111 112 MapAttributes ma = new MapAttributes(); 113 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 114 ma.register(classMethod, new Object []{new Object (), "", txAtt, "er"}); 115 TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class); 117 assertEquals(txAtt, actual); 118 } 119 120 public void testTransactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception { 121 Method interfaceMethod = ITestBean.class.getMethod("getAge", (Class []) null); 122 123 TransactionAttribute txAtt = new DefaultTransactionAttribute(); 124 125 MapAttributes ma = new MapAttributes(); 126 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 127 ma.register(interfaceMethod, new Object []{new Object (), "", txAtt, "er"}); 128 TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class); 130 assertEquals(txAtt, actual); 131 } 132 133 public void testTransactionAttributeDeclaredOnTargetClassMethodTakesPrecedenceOverAttributeDeclaredOnInterfaceMethod() throws Exception { 134 Method classMethod = TestBean.class.getMethod("getAge", (Class []) null); 135 Method interfaceMethod = ITestBean.class.getMethod("getAge", (Class []) null); 136 137 TransactionAttribute interfaceAtt = new DefaultTransactionAttribute(); 138 TransactionAttribute classAtt = new DefaultTransactionAttribute(); 139 140 MapAttributes ma = new MapAttributes(); 141 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 142 ma.register(interfaceMethod, new Object []{new Object (), "", interfaceAtt, "er"}); 143 ma.register(classMethod, new Object []{new Object (), "", classAtt, "er"}); 144 TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class); 146 assertEquals(classAtt, actual); 147 } 148 149 public void testRollbackRulesAreApplied() throws Exception { 150 Method method = TestBean.class.getMethod("getAge", (Class []) null); 151 152 MapAttributes ma = new MapAttributes(); 153 TransactionAttribute txAtt = new RuleBasedTransactionAttribute(); 154 RollbackRuleAttribute rr = new RollbackRuleAttribute("java.lang.Exception"); 155 RollbackRuleAttribute nrr = new NoRollbackRuleAttribute("ServletException"); 156 157 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 158 159 ma.register(method, new Object []{new Object (), "", txAtt, rr, nrr, "er"}); 160 TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass()); 161 assertEquals(txAtt, actual); 162 assertTrue(txAtt.rollbackOn(new Exception ())); 163 assertFalse(txAtt.rollbackOn(new ServletException ())); 164 165 assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass())); 166 } 167 168 172 public void testDefaultsToClassTransactionAttribute() throws Exception { 173 Method method = TestBean.class.getMethod("getAge", (Class []) null); 174 175 TransactionAttribute txAtt = new DefaultTransactionAttribute(); 176 MapAttributes ma = new MapAttributes(); 177 AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma); 178 ma.register(TestBean.class, new Object []{new Object (), "", txAtt, "er"}); 179 TransactionAttribute actual = atas.getTransactionAttribute(method, null); 180 assertEquals(txAtt, actual); 181 } 182 183 public void testSPR418UnboundedCacheSizeGrowth() throws Exception { 184 Attributes attributes = new CommonsAttributes(); 185 AttributesTransactionAttributeSource attributeSource = 186 new AttributesTransactionAttributeSource(attributes); 187 188 for (int i = 0; i < 100; i++) { 190 PrototypeBean bean = new PrototypeBean(); 191 Method m = bean.getClass().getMethod("doNothing", new Class [0]); 192 attributeSource.getTransactionAttribute(m, bean.getClass()); 193 Map cache = (Map ) getPrivateField(attributeSource, 194 AbstractFallbackTransactionAttributeSource.class, "cache"); 195 assertEquals("Cache size should not increase: i=" + i, 1, cache.size()); 196 } 197 } 198 199 private static Object getPrivateField(Object obj, Class clazz, 200 String fieldName) throws NoSuchFieldException , 201 IllegalAccessException { 202 Field field = clazz.getDeclaredField(fieldName); 203 field.setAccessible(true); 204 return field.get(obj); 205 } 206 207 208 public static class TestBeanWithOverloadedMethod extends TestBean { 209 210 public int getAge(int i) { 211 return i; 212 } 213 } 214 215 216 private static class PrototypeBean { 217 218 public void doNothing() { 219 } 220 } 221 222 } 223 | Popular Tags |