Java Code Examples for org.springframework.test.context.TestContext#getTestMethod()

The following examples show how to use org.springframework.test.context.TestContext#getTestMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionalTestExecutionListener.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * If a transaction is currently active for the supplied
 * {@linkplain TestContext test context}, this method will end the transaction
 * and run {@link AfterTransaction @AfterTransaction} methods.
 * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if
 * an error occurs while ending the transaction.
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	// If there was (or perhaps still is) a transaction...
	if (txContext != null) {
		TransactionStatus transactionStatus = txContext.getTransactionStatus();
		try {
			// If the transaction is still active...
			if (transactionStatus != null && !transactionStatus.isCompleted()) {
				txContext.endTransaction();
			}
		}
		finally {
			runAfterTransactionMethods(testContext);
		}
	}
}
 
Example 2
Source File: ExecutionListenerHelper.java    From flyway-test-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to build test execution information with test class and
 * method
 *
 * @param testContext of spring test environment
 *
 * @return String like &lt;Class Name&gt;[.&lt;Method Name&gt;]
 */
public static String getExecutionInformation(TestContext testContext) {
    String result = "";
    Class<?> testClass = testContext.getTestClass();

    result = testClass.getName();

    // now check for method
    Method m = null;

    try {
        m = testContext.getTestMethod();
    } catch (IllegalStateException ex) {
        // Do Nothing
    }

    if (m != null) {
        result = result + "." + m.getName();
    }

    return result;
}
 
Example 3
Source File: ReloadContextTestExecutionListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Forces context reload before test method if the annotation is present on the method or if the annotation is
 * present on the class and {@link ReloadContext.ClassMode} is set to <code>ClassMode.BEFORE_EACH_TEST_METHOD</code>
 * .
 */
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
	Class<?> testClass = testContext.getTestClass();
	Method testMethod = testContext.getTestMethod();
	final Class<ReloadContext> annotationType = ReloadContext.class;

	boolean methodReloadContext = testMethod.isAnnotationPresent(annotationType);
	boolean classReloadContext = testClass.isAnnotationPresent(annotationType);
	ReloadContext classReloadContextAnnotation = testClass.getAnnotation(annotationType);
	ClassMode classMode = classReloadContext ? classReloadContextAnnotation.classMode() : null;

	if (methodReloadContext || (classReloadContext && classMode == ClassMode.BEFORE_EACH_TEST_METHOD)) {
		reloadContext(testContext);
	}
}
 
Example 4
Source File: TransactionalTestExecutionListener.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * If a transaction is currently active for the supplied
 * {@linkplain TestContext test context}, this method will end the transaction
 * and run {@link AfterTransaction @AfterTransaction} methods.
 * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if
 * an error occurs while ending the transaction.
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	// If there was (or perhaps still is) a transaction...
	if (txContext != null) {
		TransactionStatus transactionStatus = txContext.getTransactionStatus();
		try {
			// If the transaction is still active...
			if (transactionStatus != null && !transactionStatus.isCompleted()) {
				txContext.endTransaction();
			}
		}
		finally {
			runAfterTransactionMethods(testContext);
		}
	}
}
 
Example 5
Source File: TransactionalTestExecutionListener.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * If a transaction is currently active for the supplied
 * {@linkplain TestContext test context}, this method will end the transaction
 * and run {@link AfterTransaction @AfterTransaction} methods.
 * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if
 * an error occurs while ending the transaction.
 */
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	// If there was (or perhaps still is) a transaction...
	if (txContext != null) {
		TransactionStatus transactionStatus = txContext.getTransactionStatus();
		try {
			// If the transaction is still active...
			if ((transactionStatus != null) && !transactionStatus.isCompleted()) {
				txContext.endTransaction();
			}
		}
		finally {
			runAfterTransactionMethods(testContext);
		}
	}
}
 
Example 6
Source File: UnitTestTransactionalTestExecutionListener.java    From wisp with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化数据源
 *
 * @param testContext
 */
private void initDatabase(TestContext testContext) {
    SqlConfig.Database database = null;
    Class<?> testClass = testContext.getTestClass();
    SqlConfig sqlConfigInClass = testClass.getAnnotation(SqlConfig.class);
    if (sqlConfigInClass != null) {
        database = sqlConfigInClass.database();
    }
    Method method = testContext.getTestMethod();
    SqlConfig sqlConfigInMethod = method.getAnnotation(SqlConfig.class);
    if (sqlConfigInMethod != null) {
        database = sqlConfigInMethod.database();
    }
    if (database != null) {
        UnitTestDataSource.selectDataSource(database);
    }
}
 
Example 7
Source File: UnitTestTransactionalTestExecutionListener.java    From wisp with Apache License 2.0 5 votes vote down vote up
private void initTestData(TestContext testContext) {
    List<String> sqlFiles = new ArrayList<String>();
    /**
     * 读取测试类指定的sql文件
     */
    Class<?> testClass = testContext.getTestClass();
    SqlConfig sqlConfigInClass = testClass.getAnnotation(SqlConfig.class);
    if (sqlConfigInClass != null) {
        String[] sqlFilesInClass = sqlConfigInClass.sqlFiles();
        if (ArrayUtils.isNotEmpty(sqlFilesInClass)) {
            sqlFiles.addAll(Arrays.asList(sqlFilesInClass));
        }
    }
    /**
     * 读取测试方法指定的sql文件
     */
    Method method = testContext.getTestMethod();
    SqlConfig sqlConfigInMethod = method.getAnnotation(SqlConfig.class);
    if (sqlConfigInMethod != null) {
        String[] sqlFilesInMethod = sqlConfigInMethod.sqlFiles();
        if (ArrayUtils.isNotEmpty(sqlFilesInMethod)) {
            sqlFiles.addAll(Arrays.asList(sqlFilesInMethod));
        }
    }
    /**
     * 执行sql
     */
    for (String sqlFile : sqlFiles) {
        LOGGER.info(String.format("execute sql file [%s]", sqlFile));
        this.executeSqlScript(testContext, sqlFile, false);
    }
}
 
Example 8
Source File: DBUnitTestExecutionListener.java    From flyway-test-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Only annotation with loadFiles will be executed
 */
public void beforeTestMethod(final TestContext testContext)
		throws Exception {
	// no we check for the DBResetForClass
	final Method testMethod = testContext.getTestMethod();

	final Annotation annotation = testMethod
			.getAnnotation(DBUnitSupport.class);
	if (annotation != null) {
		final DBUnitSupport dbUnitAnnotaton = (DBUnitSupport) annotation;

		loadFiles(testContext, dbUnitAnnotaton);
	}
}
 
Example 9
Source File: AbstractDirtiesContextTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod}
 * by dirtying the context if appropriate (i.e., according to the required modes).
 * @param testContext the test context whose application context should
 * potentially be marked as dirty; never {@code null}
 * @param requiredMethodMode the method mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @param requiredClassMode the class mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @throws Exception allows any exception to propagate
 * @since 4.2
 * @see #dirtyContext
 */
protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode,
		ClassMode requiredClassMode) throws Exception {

	Assert.notNull(testContext, "TestContext must not be null");
	Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null");
	Assert.notNull(requiredClassMode, "requiredClassMode must not be null");

	Class<?> testClass = testContext.getTestClass();
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class);
	DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class);
	boolean methodAnnotated = (methodAnn != null);
	boolean classAnnotated = (classAnn != null);
	MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null);
	ClassMode classMode = (classAnnotated ? classAnn.classMode() : null);

	if (logger.isDebugEnabled()) {
		String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After");
		logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] "
				+ "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext,
			classAnnotated, classMode, methodAnnotated, methodMode));
	}

	if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) {
		HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode());
		dirtyContext(testContext, hierarchyMode);
	}
}
 
Example 10
Source File: SqlScriptsTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Detect a default SQL script by implementing the algorithm defined in
 * {@link Sql#scripts}.
 */
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
	Class<?> clazz = testContext.getTestClass();
	Method method = testContext.getTestMethod();
	String elementType = (classLevel ? "class" : "method");
	String elementName = (classLevel ? clazz.getName() : method.toString());

	String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName());
	if (!classLevel) {
		resourcePath += "." + method.getName();
	}
	resourcePath += ".sql";

	String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
	ClassPathResource classPathResource = new ClassPathResource(resourcePath);

	if (classPathResource.exists()) {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]", prefixedResourcePath,
				elementType, elementName));
		}
		return prefixedResourcePath;
	}
	else {
		String msg = String.format("Could not detect default SQL script for test %s [%s]: "
				+ "%s does not exist. Either declare statements or scripts via @Sql or make the "
				+ "default SQL script available.", elementType, elementName, classPathResource);
		logger.error(msg);
		throw new IllegalStateException(msg);
	}
}
 
Example 11
Source File: TransactionalTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	final Method testMethod = testContext.getTestMethod();
	final Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	if (txContext != null) {
		throw new IllegalStateException("Cannot start a new transaction without ending the existing transaction.");
	}

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context "
					+ testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 12
Source File: TransactionalTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "Test method of supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction");

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute +
					"] found for test context " + testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
		Assert.state(tm != null,
				() -> "Failed to retrieve PlatformTransactionManager for @Transactional test: " + testContext);
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 13
Source File: OptimizedFlywayTestExecutionListener.java    From embedded-database-spring-test with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
    Method testMethod = testContext.getTestMethod();

    FlywayTest[] annotations = findFlywayTestAnnotations(testMethod);
    if (annotations.length > 1) {
        logger.warn("Optimized database loading is not supported when using multiple flyway test annotations");
    }
    for (FlywayTest annotation : annotations) {
        optimizedDbReset(testContext, testMethod, annotation);
    }
}
 
Example 14
Source File: AbstractDirtiesContextTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod}
 * by dirtying the context if appropriate (i.e., according to the required modes).
 * @param testContext the test context whose application context should
 * potentially be marked as dirty; never {@code null}
 * @param requiredMethodMode the method mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @param requiredClassMode the class mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @throws Exception allows any exception to propagate
 * @since 4.2
 * @see #dirtyContext
 */
protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode,
		ClassMode requiredClassMode) throws Exception {

	Assert.notNull(testContext, "TestContext must not be null");
	Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null");
	Assert.notNull(requiredClassMode, "requiredClassMode must not be null");

	Class<?> testClass = testContext.getTestClass();
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class);
	DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class);
	boolean methodAnnotated = (methodAnn != null);
	boolean classAnnotated = (classAnn != null);
	MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null);
	ClassMode classMode = (classAnnotated ? classAnn.classMode() : null);

	if (logger.isDebugEnabled()) {
		String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After");
		logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] "
				+ "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext,
			classAnnotated, classMode, methodAnnotated, methodMode));
	}

	if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) {
		HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode());
		dirtyContext(testContext, hierarchyMode);
	}
}
 
Example 15
Source File: SqlScriptsTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Detect a default SQL script by implementing the algorithm defined in
 * {@link Sql#scripts}.
 */
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
	Class<?> clazz = testContext.getTestClass();
	Method method = testContext.getTestMethod();
	String elementType = (classLevel ? "class" : "method");
	String elementName = (classLevel ? clazz.getName() : method.toString());

	String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName());
	if (!classLevel) {
		resourcePath += "." + method.getName();
	}
	resourcePath += ".sql";

	String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
	ClassPathResource classPathResource = new ClassPathResource(resourcePath);

	if (classPathResource.exists()) {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]",
					prefixedResourcePath, elementType, elementName));
		}
		return prefixedResourcePath;
	}
	else {
		String msg = String.format("Could not detect default SQL script for test %s [%s]: " +
				"%s does not exist. Either declare statements or scripts via @Sql or make the " +
				"default SQL script available.", elementType, elementName, classPathResource);
		logger.error(msg);
		throw new IllegalStateException(msg);
	}
}
 
Example 16
Source File: TransactionalTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	Method testMethod = testContext.getTestMethod();
	Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "Test method of supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction");

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute +
					"] found for test context " + testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
		Assert.state(tm != null,
				() -> "Failed to retrieve PlatformTransactionManager for @Transactional test: " + testContext);
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 17
Source File: AbstractDirtiesContextTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod}
 * by dirtying the context if appropriate (i.e., according to the required modes).
 * @param testContext the test context whose application context should
 * potentially be marked as dirty; never {@code null}
 * @param requiredMethodMode the method mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @param requiredClassMode the class mode required for a context to
 * be marked dirty in the current phase; never {@code null}
 * @throws Exception allows any exception to propagate
 * @since 4.2
 * @see #dirtyContext
 */
protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode,
		ClassMode requiredClassMode) throws Exception {

	Assert.notNull(testContext, "TestContext must not be null");
	Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null");
	Assert.notNull(requiredClassMode, "requiredClassMode must not be null");

	Class<?> testClass = testContext.getTestClass();
	Method testMethod = testContext.getTestMethod();
	Assert.notNull(testClass, "The test class of the supplied TestContext must not be null");
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class);
	DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class);
	boolean methodAnnotated = (methodAnn != null);
	boolean classAnnotated = (classAnn != null);
	MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null);
	ClassMode classMode = (classAnnotated ? classAnn.classMode() : null);

	if (logger.isDebugEnabled()) {
		String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After");
		logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] "
				+ "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext,
			classAnnotated, classMode, methodAnnotated, methodMode));
	}

	if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) {
		HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode());
		dirtyContext(testContext, hierarchyMode);
	}
}
 
Example 18
Source File: SqlScriptsTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Detect a default SQL script by implementing the algorithm defined in
 * {@link Sql#scripts}.
 */
private String detectDefaultScript(TestContext testContext, boolean classLevel) {
	Class<?> clazz = testContext.getTestClass();
	Method method = testContext.getTestMethod();
	String elementType = (classLevel ? "class" : "method");
	String elementName = (classLevel ? clazz.getName() : method.toString());

	String resourcePath = ClassUtils.convertClassNameToResourcePath(clazz.getName());
	if (!classLevel) {
		resourcePath += "." + method.getName();
	}
	resourcePath += ".sql";

	String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
	ClassPathResource classPathResource = new ClassPathResource(resourcePath);

	if (classPathResource.exists()) {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("Detected default SQL script \"%s\" for test %s [%s]",
					prefixedResourcePath, elementType, elementName));
		}
		return prefixedResourcePath;
	}
	else {
		String msg = String.format("Could not detect default SQL script for test %s [%s]: " +
				"%s does not exist. Either declare statements or scripts via @Sql or make the " +
				"default SQL script available.", elementType, elementName, classPathResource);
		logger.error(msg);
		throw new IllegalStateException(msg);
	}
}
 
Example 19
Source File: DBUnitTestExecutionListener.java    From flyway-test-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * Only annotation with saveIt will be executed
 */
public void afterTestMethod(final TestContext testContext) throws Exception {
	// no we check for the DBResetForClass
	final Method testMethod = testContext.getTestMethod();

	final Annotation annotation = testMethod
			.getAnnotation(DBUnitSupport.class);

	if (annotation != null) {
		final DBUnitSupport dbUnitAnnotaton = (DBUnitSupport) annotation;
		final String saveIt = dbUnitAnnotaton.saveFileAfterRun();
		final String[] tables = dbUnitAnnotaton.saveTableAfterRun();

		if (saveIt != null && saveIt.trim().length() > 0) {
			String executionInfo = "";
			if (logger.isDebugEnabled()) {
				executionInfo = ExecutionListenerHelper
						.getExecutionInformation(testContext);
				logger.debug("******** Start save information '"
						+ executionInfo + "' info file '" + saveIt + "'.");
			}
			final DataSource ds = getSaveDataSource(testContext);

			final IDatabaseConnection con = getConnection(ds, testContext);
               // Issue 16 fix leaking database connection - will look better with Java 7 Closable
               try {
                   final IDataSet dataSet = getDataSetToExport(tables, con);
                   final File fileToExport = getFileToExport(saveIt);

                   final FileWriter fileWriter = new FileWriter(fileToExport);
                   FlatXmlDataSet.write(dataSet, fileWriter);
               } finally {
                   if (logger.isDebugEnabled()) {
                       logger.debug("******** Close database connection " + con);
                   }
                   con.close();
               }
               if (logger.isDebugEnabled()) {
                   logger.debug("******** Finished save information '"
                           + executionInfo + "' info file '" + saveIt + "'.");
               }
           }
	}
}
 
Example 20
Source File: FlywayTestExecutionListener.java    From flyway-test-extensions with Apache License 2.0 3 votes vote down vote up
/**
 * Called from spring before a test method will be invoked.
 *
 * @param testContext
 *            default test context filled from spring
 *
 * @throws Exception
 *             if any error occurred
 */
public void beforeTestMethod(final TestContext testContext)
        throws Exception {
    final Method testMethod = testContext.getTestMethod();

    handleFlywayTestAnnotationForMethod(testContext, testMethod);
}