Java Code Examples for org.springframework.test.annotation.Rollback#value()

The following examples show how to use org.springframework.test.annotation.Rollback#value() . 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
/**
 * Determine whether or not to rollback transactions by default for the
 * supplied {@linkplain TestContext test context}.
 * <p>Supports {@link Rollback @Rollback} or {@link Commit @Commit} at the
 * class-level.
 * @param testContext the test context for which the default rollback flag
 * should be retrieved
 * @return the <em>default rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the default rollback flag
 */
protected final boolean isDefaultRollback(TestContext testContext) throws Exception {
	Class<?> testClass = testContext.getTestClass();
	Rollback rollback = AnnotatedElementUtils.findMergedAnnotation(testClass, Rollback.class);
	boolean rollbackPresent = (rollback != null);

	if (rollbackPresent) {
		boolean defaultRollback = rollback.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Retrieved default @Rollback(%s) for test class [%s].",
					defaultRollback, testClass.getName()));
		}
		return defaultRollback;
	}

	// else
	return true;
}
 
Example 2
Source File: TransactionalTestExecutionListener.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Determine whether or not to rollback transactions for the supplied
 * {@linkplain TestContext test context} by taking into consideration the
 * {@linkplain #isDefaultRollback(TestContext) default rollback} flag and a
 * possible method-level override via the {@link Rollback @Rollback}
 * annotation.
 * @param testContext the test context for which the rollback flag
 * should be retrieved
 * @return the <em>rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the rollback flag
 */
protected final boolean isRollback(TestContext testContext) throws Exception {
	boolean rollback = isDefaultRollback(testContext);
	Rollback rollbackAnnotation =
			AnnotatedElementUtils.findMergedAnnotation(testContext.getTestMethod(), Rollback.class);
	if (rollbackAnnotation != null) {
		boolean rollbackOverride = rollbackAnnotation.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.",
					rollbackOverride, rollback, testContext));
		}
		rollback = rollbackOverride;
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"No method-level @Rollback override: using default rollback [%s] for test context %s.",
					rollback, testContext));
		}
	}
	return rollback;
}
 
Example 3
Source File: TransactionalTestExecutionListener.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Determine whether or not to rollback transactions by default for the
 * supplied {@linkplain TestContext test context}.
 * <p>Supports {@link Rollback @Rollback} or {@link Commit @Commit} at the
 * class-level.
 * @param testContext the test context for which the default rollback flag
 * should be retrieved
 * @return the <em>default rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the default rollback flag
 */
protected final boolean isDefaultRollback(TestContext testContext) throws Exception {
	Class<?> testClass = testContext.getTestClass();
	Rollback rollback = AnnotatedElementUtils.findMergedAnnotation(testClass, Rollback.class);
	boolean rollbackPresent = (rollback != null);

	if (rollbackPresent) {
		boolean defaultRollback = rollback.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Retrieved default @Rollback(%s) for test class [%s].",
					defaultRollback, testClass.getName()));
		}
		return defaultRollback;
	}

	// else
	return true;
}
 
Example 4
Source File: TransactionalTestExecutionListener.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Determine whether or not to rollback transactions for the supplied
 * {@linkplain TestContext test context} by taking into consideration the
 * {@linkplain #isDefaultRollback(TestContext) default rollback} flag and a
 * possible method-level override via the {@link Rollback @Rollback}
 * annotation.
 * @param testContext the test context for which the rollback flag
 * should be retrieved
 * @return the <em>rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the rollback flag
 */
protected final boolean isRollback(TestContext testContext) throws Exception {
	boolean rollback = isDefaultRollback(testContext);
	Rollback rollbackAnnotation =
			AnnotatedElementUtils.findMergedAnnotation(testContext.getTestMethod(), Rollback.class);
	if (rollbackAnnotation != null) {
		boolean rollbackOverride = rollbackAnnotation.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.",
					rollbackOverride, rollback, testContext));
		}
		rollback = rollbackOverride;
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"No method-level @Rollback override: using default rollback [%s] for test context %s.",
					rollback, testContext));
		}
	}
	return rollback;
}
 
Example 5
Source File: TransactionalTestExecutionListener.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine whether or not to rollback transactions by default for the
 * supplied {@linkplain TestContext test context}.
 * <p>Supports {@link Rollback @Rollback} or
 * {@link TransactionConfiguration @TransactionConfiguration} at the
 * class-level.
 * @param testContext the test context for which the default rollback flag
 * should be retrieved
 * @return the <em>default rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the default rollback flag
 */
@SuppressWarnings("deprecation")
protected final boolean isDefaultRollback(TestContext testContext) throws Exception {
	Class<?> testClass = testContext.getTestClass();
	Rollback rollback = findAnnotation(testClass, Rollback.class);
	boolean rollbackPresent = (rollback != null);
	TransactionConfigurationAttributes txConfigAttributes = retrieveConfigurationAttributes(testContext);

	if (rollbackPresent && txConfigAttributes != defaultTxConfigAttributes) {
		throw new IllegalStateException(String.format("Test class [%s] is annotated with both @Rollback "
				+ "and @TransactionConfiguration, but only one is permitted.", testClass.getName()));
	}

	if (rollbackPresent) {
		boolean defaultRollback = rollback.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format("Retrieved default @Rollback(%s) for test class [%s].", defaultRollback,
				testClass.getName()));
		}
		return defaultRollback;
	}

	// else
	return txConfigAttributes.isDefaultRollback();
}
 
Example 6
Source File: TransactionalTestExecutionListener.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Determine whether or not to rollback transactions for the supplied
 * {@linkplain TestContext test context} by taking into consideration the
 * {@linkplain #isDefaultRollback(TestContext) default rollback} flag and a
 * possible method-level override via the {@link Rollback @Rollback}
 * annotation.
 * @param testContext the test context for which the rollback flag
 * should be retrieved
 * @return the <em>rollback</em> flag for the supplied test context
 * @throws Exception if an error occurs while determining the rollback flag
 */
protected final boolean isRollback(TestContext testContext) throws Exception {
	boolean rollback = isDefaultRollback(testContext);
	Rollback rollbackAnnotation = findAnnotation(testContext.getTestMethod(), Rollback.class);
	if (rollbackAnnotation != null) {
		boolean rollbackOverride = rollbackAnnotation.value();
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.",
				rollbackOverride, rollback, testContext));
		}
		rollback = rollbackOverride;
	}
	else {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"No method-level @Rollback override: using default rollback [%s] for test context %s.", rollback,
				testContext));
		}
	}
	return rollback;
}