1 16 17 package org.springframework.transaction.interceptor; 18 19 import java.lang.reflect.Method ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.transaction.TransactionDefinition; 24 25 32 public class TransactionAttributeSourceEditorTests extends TestCase { 33 34 public void testNull() throws Exception { 35 TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor(); 36 pe.setAsText(null); 37 TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue(); 38 39 Method m = Object .class.getMethod("hashCode", (Class []) null); 40 assertTrue(tas.getTransactionAttribute(m, null) == null); 41 } 42 43 public void testInvalid() throws Exception { 44 TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor(); 45 try { 46 pe.setAsText("foo=bar"); 47 fail(); 48 } 49 catch (IllegalArgumentException ex) { 50 } 52 } 53 54 public void testMatchesSpecific() throws Exception { 55 TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor(); 56 pe.setAsText( 58 "java.lang.Object.hashCode=PROPAGATION_REQUIRED\n" + 59 "java.lang.Object.equals=PROPAGATION_MANDATORY\n" + 60 "java.lang.Object.*it=PROPAGATION_SUPPORTS\n" + 61 "java.lang.Object.notify=PROPAGATION_SUPPORTS\n" + 62 "java.lang.Object.not*=PROPAGATION_REQUIRED"); 63 TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue(); 64 65 checkTransactionProperties(tas, Object .class.getMethod("hashCode", (Class []) null), 66 TransactionDefinition.PROPAGATION_REQUIRED); 67 checkTransactionProperties(tas, Object .class.getMethod("equals", new Class [] { Object .class }), 68 TransactionDefinition.PROPAGATION_MANDATORY); 69 checkTransactionProperties(tas, Object .class.getMethod("wait", (Class []) null), 70 TransactionDefinition.PROPAGATION_SUPPORTS); 71 checkTransactionProperties(tas, Object .class.getMethod("wait", new Class [] { long.class }), 72 TransactionDefinition.PROPAGATION_SUPPORTS); 73 checkTransactionProperties(tas, Object .class.getMethod("wait", new Class [] { long.class, int.class }), 74 TransactionDefinition.PROPAGATION_SUPPORTS); 75 checkTransactionProperties(tas, Object .class.getMethod("notify", (Class []) null), 76 TransactionDefinition.PROPAGATION_SUPPORTS); 77 checkTransactionProperties(tas, Object .class.getMethod("notifyAll", (Class []) null), 78 TransactionDefinition.PROPAGATION_REQUIRED); 79 checkTransactionProperties(tas, Object .class.getMethod("toString", (Class []) null), -1); 80 } 81 82 public void testMatchesAll() throws Exception { 83 TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor(); 84 pe.setAsText("java.lang.Object.*=PROPAGATION_REQUIRED"); 85 TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue(); 86 87 checkTransactionProperties(tas, Object .class.getMethod("hashCode", (Class []) null), 88 TransactionDefinition.PROPAGATION_REQUIRED); 89 checkTransactionProperties(tas, Object .class.getMethod("equals", new Class [] { Object .class }), 90 TransactionDefinition.PROPAGATION_REQUIRED); 91 checkTransactionProperties(tas, Object .class.getMethod("wait", (Class []) null), 92 TransactionDefinition.PROPAGATION_REQUIRED); 93 checkTransactionProperties(tas, Object .class.getMethod("wait", new Class [] { long.class }), 94 TransactionDefinition.PROPAGATION_REQUIRED); 95 checkTransactionProperties(tas, Object .class.getMethod("wait", new Class [] { long.class, int.class }), 96 TransactionDefinition.PROPAGATION_REQUIRED); 97 checkTransactionProperties(tas, Object .class.getMethod("notify", (Class []) null), 98 TransactionDefinition.PROPAGATION_REQUIRED); 99 checkTransactionProperties(tas, Object .class.getMethod("notifyAll", (Class []) null), 100 TransactionDefinition.PROPAGATION_REQUIRED); 101 checkTransactionProperties(tas, Object .class.getMethod("toString", (Class []) null), 102 TransactionDefinition.PROPAGATION_REQUIRED); 103 } 104 105 private void checkTransactionProperties(TransactionAttributeSource tas, Method method, int propagationBehavior) { 106 TransactionAttribute ta = tas.getTransactionAttribute(method, null); 107 if (propagationBehavior >= 0) { 108 assertTrue(ta != null); 109 assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT); 110 assertTrue(ta.getPropagationBehavior() == propagationBehavior); 111 } 112 else { 113 assertTrue(ta == null); 114 } 115 } 116 117 } 118 | Popular Tags |