1 16 17 package org.springframework.test.annotation; 18 19 import java.lang.reflect.Method ; 20 import java.util.Map ; 21 22 import javax.sql.DataSource ; 23 24 import org.apache.commons.logging.Log; 25 26 import org.springframework.context.ApplicationContext; 27 import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; 28 import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; 29 import org.springframework.transaction.TransactionDefinition; 30 import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; 31 import org.springframework.transaction.interceptor.TransactionAttributeSource; 32 33 47 public abstract class AbstractAnnotationAwareTransactionalTests 48 extends AbstractTransactionalDataSourceSpringContextTests { 49 50 protected SimpleJdbcTemplate simpleJdbcTemplate; 51 52 private TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource(); 53 54 protected ProfileValueSource profileValueSource = SystemProfileValueSource.getInstance(); 55 56 57 @Override 58 public void setDataSource(DataSource dataSource) { 59 super.setDataSource(dataSource); 60 this.simpleJdbcTemplate = new SimpleJdbcTemplate(this.jdbcTemplate); 62 } 63 64 protected void findUniqueProfileValueSourceFromContext(ApplicationContext ac) { 67 Map beans = ac.getBeansOfType(ProfileValueSource.class); 68 if (beans.size() == 1) { 69 this.profileValueSource = (ProfileValueSource) beans.values().iterator().next(); 70 } 71 } 72 73 74 77 @Override 78 public void runBare() throws Throwable { 79 if (isDisabledInThisEnvironment(getName())) { 81 super.runBare(); 83 return; 84 } 85 86 final Method testMethod = getClass().getMethod(getName(), (Class []) null); 90 91 if (isDisabledInThisEnvironment(testMethod)) { 92 logger.info("**** " + getClass().getName() + "." + getName() + " disabled in this environment: " + 93 "Total disabled tests=" + getDisabledTestCount()); 94 recordDisabled(); 95 return; 96 } 97 98 TransactionDefinition explicitTransactionDefinition = 99 this.transactionAttributeSource.getTransactionAttribute(testMethod, getClass()); 100 if (explicitTransactionDefinition != null) { 101 logger.info("Custom transaction definition [" + explicitTransactionDefinition + " for test method " + getName()); 102 setTransactionDefinition(explicitTransactionDefinition); 103 } 104 else if (testMethod.isAnnotationPresent(NotTransactional.class)) { 105 preventTransaction(); 107 } 108 109 runTestTimed( 112 new TestExecutionCallback() { 113 public void run() throws Throwable { 114 try { 115 AbstractAnnotationAwareTransactionalTests.super.runBare(); 116 } 117 finally { 118 if (testMethod.isAnnotationPresent(DirtiesContext.class)) { 122 AbstractAnnotationAwareTransactionalTests.this.setDirty(); 123 } 124 } 125 } 126 }, 127 testMethod, 128 logger); 129 } 130 131 protected boolean isDisabledInThisEnvironment(Method testMethod) { 132 IfProfileValue inProfile = testMethod.getAnnotation(IfProfileValue.class); 133 if (inProfile == null) { 134 inProfile = getClass().getAnnotation(IfProfileValue.class); 135 } 136 137 if (inProfile != null) { 138 return !this.profileValueSource.get(inProfile.name()).equals(inProfile.value()); 140 } 141 else { 142 return false; 143 } 144 145 } 147 148 149 private static void runTestTimed(TestExecutionCallback tec, Method testMethod, Log logger) throws Throwable { 150 Timed timed = testMethod.getAnnotation(Timed.class); 151 152 if (timed == null) { 153 runTest(tec, testMethod, logger); 154 } 155 else { 156 long startTime = System.currentTimeMillis(); 157 try { 158 runTest(tec, testMethod, logger); 159 } 160 finally { 161 long elapsed = System.currentTimeMillis() - startTime; 162 if (elapsed > timed.millis()) { 163 fail("Took " + elapsed + " ms; limit was " + timed.millis()); 164 } 165 } 166 } 167 } 168 169 private static void runTest(TestExecutionCallback tec, Method testMethod, Log logger) throws Throwable { 170 ExpectedException ee = testMethod.getAnnotation(ExpectedException.class); 171 Repeat repeat = testMethod.getAnnotation(Repeat.class); 172 173 int runs = (repeat != null) ? repeat.value() : 1; 174 175 for (int i = 0; i < runs; i++) { 176 try { 177 if (i > 0 && logger != null) { 178 logger.info("Repetition " + i + " of test " + testMethod.getName()); 179 } 180 tec.run(); 181 if (ee != null) { 182 fail("Expected throwable of class " + ee.value()); 183 } 184 } 185 catch (Throwable t) { 186 if (ee == null) { 187 throw t; 188 } 189 if (ee.value().isAssignableFrom(t.getClass())) { 190 } 192 else { 193 throw t; 196 } 197 } 198 } 199 } 200 201 202 private static interface TestExecutionCallback { 203 204 void run() throws Throwable ; 205 } 206 207 } 208 | Popular Tags |