Delete test annotations in spring-orm
This commit deletes all test annotations from the spring-orm module in order to reduce unnecessary confusion between these "copies" and the real, current versions of such classes in the spring-test module. Furthermore, the legacy abstract JUnit 3.8 base classes and test cases have been refactored accordingly.
This commit is contained in:
@@ -28,10 +28,8 @@ import javax.persistence.Query;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.orm.jpa.domain.DriversLicense;
|
||||
import org.springframework.orm.jpa.domain.Person;
|
||||
import org.springframework.test.annotation.ExpectedException;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.test.annotation.Repeat;
|
||||
import org.springframework.test.annotation.Timed;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
@@ -42,39 +40,40 @@ import org.springframework.util.SerializationTestUtils;
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
extends AbstractEntityManagerFactoryIntegrationTests {
|
||||
public abstract class AbstractContainerEntityManagerFactoryIntegrationTests extends
|
||||
AbstractEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
|
||||
assertTrue(Proxy.isProxyClass(entityManagerFactory.getClass()));
|
||||
assertTrue("Must have introduced config interface",
|
||||
entityManagerFactory instanceof EntityManagerFactoryInfo);
|
||||
assertTrue("Must have introduced config interface", entityManagerFactory instanceof EntityManagerFactoryInfo);
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
|
||||
//assertEquals("Person", emfi.getPersistenceUnitName());
|
||||
// assertEquals("Person", emfi.getPersistenceUnitName());
|
||||
assertNotNull("PersistenceUnitInfo must be available", emfi.getPersistenceUnitInfo());
|
||||
assertNotNull("Raw EntityManagerFactory must be available", emfi.getNativeEntityManagerFactory());
|
||||
}
|
||||
|
||||
public void testStateClean() {
|
||||
assertEquals("Should be no people from previous transactions",
|
||||
0, countRowsInTable("person"));
|
||||
assertEquals("Should be no people from previous transactions", 0, countRowsInTable("person"));
|
||||
}
|
||||
|
||||
@Repeat(5)
|
||||
public void testJdbcTx1() throws Exception {
|
||||
public void testJdbcTx1_1() {
|
||||
testJdbcTx2();
|
||||
}
|
||||
|
||||
@Timed(millis=273)
|
||||
public void testJdbcTx2() throws InterruptedException {
|
||||
//Thread.sleep(2000);
|
||||
public void testJdbcTx1_2() {
|
||||
testJdbcTx2();
|
||||
}
|
||||
|
||||
public void testJdbcTx1_3() {
|
||||
testJdbcTx2();
|
||||
}
|
||||
|
||||
public void testJdbcTx2() {
|
||||
assertEquals("Any previous tx must have been rolled back", 0, countRowsInTable("person"));
|
||||
//insertPerson("foo");
|
||||
executeSqlScript("/org/springframework/orm/jpa/insertPerson.sql", false);
|
||||
}
|
||||
|
||||
//@NotTransactional
|
||||
@SuppressWarnings({ "unused", "unchecked" })
|
||||
public void testEntityManagerProxyIsProxy() {
|
||||
assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
|
||||
@@ -86,21 +85,31 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
|
||||
}
|
||||
|
||||
@ExpectedException(RuntimeException.class)
|
||||
public void testBogusQuery() {
|
||||
Query query = sharedEntityManager.createQuery("It's raining toads");
|
||||
// required in OpenJPA case
|
||||
query.executeUpdate();
|
||||
try {
|
||||
Query query = sharedEntityManager.createQuery("It's raining toads");
|
||||
// required in OpenJPA case
|
||||
query.executeUpdate();
|
||||
fail("Should have thrown a RuntimeException");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
/* expected */
|
||||
}
|
||||
}
|
||||
|
||||
@ExpectedException(EntityNotFoundException.class)
|
||||
public void testGetReferenceWhenNoRow() {
|
||||
Person notThere = sharedEntityManager.getReference(Person.class, 666);
|
||||
try {
|
||||
Person notThere = sharedEntityManager.getReference(Person.class, 666);
|
||||
|
||||
// We may get here (as with Hibernate).
|
||||
// Either behaviour is valid: throw exception on first access
|
||||
// or on getReference itself.
|
||||
notThere.getFirstName();
|
||||
// We may get here (as with Hibernate).
|
||||
// Either behaviour is valid: throw exception on first access
|
||||
// or on getReference itself.
|
||||
notThere.getFirstName();
|
||||
fail("Should have thrown an EntityNotFoundException");
|
||||
}
|
||||
catch (EntityNotFoundException e) {
|
||||
/* expected */
|
||||
}
|
||||
}
|
||||
|
||||
public void testLazyLoading() {
|
||||
@@ -125,7 +134,6 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
}
|
||||
finally {
|
||||
deleteFromTables(new String[] { "person", "drivers_license" });
|
||||
//setComplete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,12 +165,12 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
}
|
||||
}
|
||||
|
||||
// public void testAspectJInjectionOfConfigurableEntity() {
|
||||
// Person p = new Person();
|
||||
// System.err.println(p);
|
||||
// assertNotNull("Was injected", p.getTestBean());
|
||||
// assertEquals("Ramnivas", p.getTestBean().getName());
|
||||
// }
|
||||
// public void testAspectJInjectionOfConfigurableEntity() {
|
||||
// Person p = new Person();
|
||||
// System.err.println(p);
|
||||
// assertNotNull("Was injected", p.getTestBean());
|
||||
// assertEquals("Ramnivas", p.getTestBean().getName());
|
||||
// }
|
||||
|
||||
public void testInstantiateAndSaveWithSharedEmProxy() {
|
||||
testInstantiateAndSave(sharedEntityManager);
|
||||
@@ -194,7 +202,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
}
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testQueryNoPersonsNotTransactional() {
|
||||
EntityManager em = entityManagerFactory.createEntityManager();
|
||||
@@ -225,7 +233,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
}
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testQueryNoPersonsSharedNotTransactional() {
|
||||
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
|
||||
@@ -238,7 +246,9 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// IllegalStateException expected, but PersistenceException thrown by Hibernate
|
||||
// IllegalStateException expected, but PersistenceException thrown by
|
||||
// Hibernate
|
||||
System.err.println(ex);
|
||||
assertTrue(ex.getMessage().indexOf("closed") != -1);
|
||||
}
|
||||
q = em.createQuery("select p from Person as p");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,16 +16,15 @@
|
||||
|
||||
package org.springframework.orm.jpa;
|
||||
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TransactionRequiredException;
|
||||
|
||||
import org.springframework.orm.jpa.domain.Person;
|
||||
import org.springframework.orm.jpa.AbstractEntityManagerFactoryIntegrationTests;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
@@ -38,7 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void testEntityManagerIsProxy() {
|
||||
assertTrue("EntityManagerFactory is proxied", Proxy.isProxyClass(entityManagerFactory.getClass()));
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.springframework.orm.jpa;
|
||||
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceException;
|
||||
@@ -27,8 +27,8 @@ import javax.persistence.TransactionRequiredException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.orm.jpa.domain.Person;
|
||||
import org.springframework.test.annotation.ExpectedException;
|
||||
import org.springframework.test.annotation.NotTransactional;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Integration tests using in-memory database for container-managed JPA
|
||||
@@ -39,12 +39,12 @@ import org.springframework.test.annotation.NotTransactional;
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntityManagerFactoryIntegrationTests {
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void testExceptionTranslationWithDialectFoundOnIntroducedEntityManagerInfo() throws Exception {
|
||||
doTestExceptionTranslationWithDialectFound(((EntityManagerFactoryInfo) entityManagerFactory).getJpaDialect());
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void testExceptionTranslationWithDialectFoundOnEntityManagerFactoryBean() throws Exception {
|
||||
AbstractEntityManagerFactoryBean aefb =
|
||||
(AbstractEntityManagerFactoryBean) applicationContext.getBean("&entityManagerFactory");
|
||||
@@ -80,9 +80,14 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
|
||||
}
|
||||
|
||||
// This would be legal, at least if not actually _starting_ a tx
|
||||
@ExpectedException(IllegalStateException.class)
|
||||
public void testEntityManagerProxyRejectsProgrammaticTxManagement() {
|
||||
createContainerManagedEntityManager().getTransaction();
|
||||
try {
|
||||
createContainerManagedEntityManager().getTransaction();
|
||||
fail("Should have thrown an IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
/* expected */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -93,10 +98,15 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
|
||||
createContainerManagedEntityManager().joinTransaction();
|
||||
}
|
||||
|
||||
@NotTransactional
|
||||
@ExpectedException(TransactionRequiredException.class)
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public void testContainerEntityManagerProxyRejectsJoinTransactionWithoutTransaction() {
|
||||
createContainerManagedEntityManager().joinTransaction();
|
||||
try {
|
||||
createContainerManagedEntityManager().joinTransaction();
|
||||
fail("Should have thrown a TransactionRequiredException");
|
||||
}
|
||||
catch (TransactionRequiredException e) {
|
||||
/* expected */
|
||||
}
|
||||
}
|
||||
|
||||
public void testInstantiateAndSave() {
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.hibernate.ejb.HibernateEntityManagerFactory;
|
||||
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
|
||||
import org.springframework.orm.jpa.domain.Person;
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
|
||||
/**
|
||||
* Hibernate-specific JPA tests.
|
||||
@@ -33,8 +32,8 @@ import org.springframework.test.annotation.IfProfileValue;
|
||||
* @author Juergen Hoeller
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
// Essentially @Ignore-d since AnnotationBeanConfigurerAspect cannot be found
|
||||
@IfProfileValue(name="test-group", value="broken")
|
||||
// TODO Decide what to do with broken HibernateEntityManagerFactoryIntegrationTests.
|
||||
// See isDisabledInThisEnvironment() for details.
|
||||
@SuppressWarnings("deprecation")
|
||||
public class HibernateEntityManagerFactoryIntegrationTests extends
|
||||
AbstractContainerEntityManagerFactoryIntegrationTests {
|
||||
@@ -42,6 +41,15 @@ public class HibernateEntityManagerFactoryIntegrationTests extends
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Always returns {@code true}, thereby disabling this entire test class
|
||||
* since AnnotationBeanConfigurerAspect cannot be found.
|
||||
*/
|
||||
@Override
|
||||
protected boolean isDisabledInThisEnvironment(String testMethodName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setSessionFactory(SessionFactory sessionFactory) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
@@ -51,7 +59,6 @@ public class HibernateEntityManagerFactoryIntegrationTests extends
|
||||
return HIBERNATE_CONFIG_LOCATIONS;
|
||||
}
|
||||
|
||||
|
||||
public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl() {
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
|
||||
assertTrue(emfi.getNativeEntityManagerFactory() instanceof HibernateEntityManagerFactory);
|
||||
@@ -78,7 +85,7 @@ public class HibernateEntityManagerFactoryIntegrationTests extends
|
||||
public void testConfigurablePerson() {
|
||||
Query q = this.sessionFactory.getCurrentSession().createQuery("select p from ContextualPerson as p");
|
||||
assertEquals(0, q.list().size());
|
||||
//assertNotNull(new ContextualPerson().entityManager); TODO
|
||||
// assertNotNull(new ContextualPerson().entityManager); TODO
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,6 +27,8 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>
|
||||
* Convenient superclass for JUnit 3.8 based tests depending on a Spring
|
||||
* context. The test instance itself is populated by Dependency Injection.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,8 @@ import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>
|
||||
* Abstract JUnit 3.8 test class that holds and exposes a single Spring
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,6 +25,8 @@ import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>
|
||||
* Superclass for JUnit 3.8 test cases using Spring
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContexts}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,7 +31,9 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
/**
|
||||
* Subclass of AbstractTransactionalSpringContextTests that adds some convenience
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>Subclass of AbstractTransactionalSpringContextTests that adds some convenience
|
||||
* functionality for JDBC access. Expects a {@link javax.sql.DataSource} bean
|
||||
* to be defined in the Spring application context.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,9 @@ import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
/**
|
||||
* Convenient base class for JUnit 3.8 based tests that should occur in a
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>Convenient base class for JUnit 3.8 based tests that should occur in a
|
||||
* transaction, but normally will roll the transaction back on the completion of
|
||||
* each test.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,7 +21,9 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Superclass for JUnit 3.8 based tests that allows conditional test execution
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>Superclass for JUnit 3.8 based tests that allows conditional test execution
|
||||
* at the individual test method level. The
|
||||
* {@link #isDisabledInThisEnvironment(String) isDisabledInThisEnvironment()}
|
||||
* method is invoked before the execution of each test method. Subclasses can
|
||||
|
||||
@@ -18,54 +18,30 @@ package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
|
||||
import org.springframework.transaction.interceptor.TransactionAttributeSource;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Java 5 specific subclass of
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>Java 5 specific subclass of
|
||||
* {@link AbstractTransactionalDataSourceSpringContextTests}, obeying annotations
|
||||
* for transaction control.
|
||||
* </p>
|
||||
* <p>
|
||||
* For example, test methods can be annotated with the regular Spring
|
||||
*
|
||||
* <p>For example, test methods can be annotated with the regular Spring
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional}
|
||||
* annotation (e.g., to force execution in a read-only transaction) or with the
|
||||
* {@link NotTransactional @NotTransactional} annotation to prevent any
|
||||
* transaction being created at all. In addition, individual test methods can be
|
||||
* annotated with {@link Rollback @Rollback} to override the
|
||||
* {@link #isDefaultRollback() default rollback} settings.
|
||||
* </p>
|
||||
* <p>
|
||||
* The following list constitutes all annotations currently supported by
|
||||
* AbstractAnnotationAwareTransactionalTests:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>{@link DirtiesContext @DirtiesContext}</li>
|
||||
* <li>{@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration}</li>
|
||||
* <li>{@link IfProfileValue @IfProfileValue}</li>
|
||||
* <li>{@link ExpectedException @ExpectedException}</li>
|
||||
* <li>{@link Timed @Timed}</li>
|
||||
* <li>{@link Repeat @Repeat}</li>
|
||||
* <li>{@link org.springframework.transaction.annotation.Transactional @Transactional}</li>
|
||||
* <li>{@link NotTransactional @NotTransactional}</li>
|
||||
* <li>{@link Rollback @Rollback}</li>
|
||||
* </ul>
|
||||
* annotation — for example, to force execution in a read-only transaction
|
||||
* or to prevent any transaction from being created at all by setting the propagation
|
||||
* level to {@code NOT_SUPPORTED}.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
* @deprecated as of Spring 3.0, in favor of using the listener-based test context framework
|
||||
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
|
||||
* @deprecated as of Spring 3.0, in favor of using the listener-based TestContext framework
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class AbstractAnnotationAwareTransactionalTests extends
|
||||
@@ -73,15 +49,6 @@ public abstract class AbstractAnnotationAwareTransactionalTests extends
|
||||
|
||||
private final TransactionAttributeSource transactionAttributeSource = new AnnotationTransactionAttributeSource();
|
||||
|
||||
/**
|
||||
* {@link ProfileValueSource} available to subclasses but primarily intended
|
||||
* for use in {@link #isDisabledInThisEnvironment(Method)}.
|
||||
* <p>Set to {@link SystemProfileValueSource} by default for backwards
|
||||
* compatibility; however, the value may be changed in the
|
||||
* {@link #AbstractAnnotationAwareTransactionalTests(String)} constructor.
|
||||
*/
|
||||
protected ProfileValueSource profileValueSource = SystemProfileValueSource.getInstance();
|
||||
|
||||
|
||||
/**
|
||||
* Default constructor for AbstractAnnotationAwareTransactionalTests, which
|
||||
@@ -93,37 +60,11 @@ public abstract class AbstractAnnotationAwareTransactionalTests extends
|
||||
|
||||
/**
|
||||
* Constructs a new AbstractAnnotationAwareTransactionalTests instance with
|
||||
* the specified JUnit {@code name} and retrieves the configured (or
|
||||
* default) {@link ProfileValueSource}.
|
||||
* the specified JUnit {@code name}.
|
||||
* @param name the name of the current test
|
||||
* @see ProfileValueUtils#retrieveProfileValueSource(Class)
|
||||
*/
|
||||
public AbstractAnnotationAwareTransactionalTests(String name) {
|
||||
super(name);
|
||||
this.profileValueSource = ProfileValueUtils.retrieveProfileValueSource(getClass());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Search for a unique {@link ProfileValueSource} in the supplied
|
||||
* {@link ApplicationContext}. If found, the
|
||||
* {@code profileValueSource} for this test will be set to the unique
|
||||
* {@link ProfileValueSource}.
|
||||
* @param applicationContext the ApplicationContext in which to search for
|
||||
* the ProfileValueSource; may not be {@code null}
|
||||
* @deprecated Use {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
protected void findUniqueProfileValueSourceFromContext(ApplicationContext applicationContext) {
|
||||
Assert.notNull(applicationContext, "Can not search for a ProfileValueSource in a null ApplicationContext.");
|
||||
ProfileValueSource uniqueProfileValueSource = null;
|
||||
Map<?, ?> beans = applicationContext.getBeansOfType(ProfileValueSource.class);
|
||||
if (beans.size() == 1) {
|
||||
uniqueProfileValueSource = (ProfileValueSource) beans.values().iterator().next();
|
||||
}
|
||||
if (uniqueProfileValueSource != null) {
|
||||
this.profileValueSource = uniqueProfileValueSource;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,55 +81,19 @@ public abstract class AbstractAnnotationAwareTransactionalTests extends
|
||||
|
||||
final Method testMethod = getTestMethod();
|
||||
|
||||
if (isDisabledInThisEnvironment(testMethod)) {
|
||||
recordDisabled();
|
||||
this.logger.info("**** " + getClass().getName() + "." + getName() + " is disabled in this environment: "
|
||||
+ "Total disabled tests=" + getDisabledTestCount());
|
||||
return;
|
||||
}
|
||||
|
||||
TransactionDefinition explicitTransactionDefinition =
|
||||
this.transactionAttributeSource.getTransactionAttribute(testMethod, getClass());
|
||||
TransactionDefinition explicitTransactionDefinition = this.transactionAttributeSource.getTransactionAttribute(
|
||||
testMethod, getClass());
|
||||
if (explicitTransactionDefinition != null) {
|
||||
this.logger.info("Custom transaction definition [" + explicitTransactionDefinition + "] for test method ["
|
||||
+ getName() + "].");
|
||||
setTransactionDefinition(explicitTransactionDefinition);
|
||||
}
|
||||
else if (testMethod.isAnnotationPresent(NotTransactional.class)) {
|
||||
// Don't have any transaction...
|
||||
|
||||
if (this.transactionDefinition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
|
||||
preventTransaction();
|
||||
}
|
||||
|
||||
// Let JUnit handle execution. We're just changing the state of the test class first.
|
||||
runTestTimed(new TestExecutionCallback() {
|
||||
@Override
|
||||
public void run() throws Throwable {
|
||||
try {
|
||||
AbstractAnnotationAwareTransactionalTests.super.runBare();
|
||||
}
|
||||
finally {
|
||||
// Mark the context to be blown away if the test was
|
||||
// annotated to result in setDirty being invoked
|
||||
// automatically.
|
||||
if (testMethod.isAnnotationPresent(DirtiesContext.class)) {
|
||||
AbstractAnnotationAwareTransactionalTests.this.setDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
}, testMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the test for the supplied {@code testMethod} should
|
||||
* run in the current environment.
|
||||
* <p>The default implementation is based on
|
||||
* {@link IfProfileValue @IfProfileValue} semantics.
|
||||
* @param testMethod the test method
|
||||
* @return {@code true} if the test is <em>disabled</em> in the current environment
|
||||
* @see ProfileValueUtils#isTestEnabledInThisEnvironment
|
||||
*/
|
||||
protected boolean isDisabledInThisEnvironment(Method testMethod) {
|
||||
return !ProfileValueUtils.isTestEnabledInThisEnvironment(this.profileValueSource, testMethod, getClass());
|
||||
super.runBare();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,89 +118,17 @@ public abstract class AbstractAnnotationAwareTransactionalTests extends
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether or not to rollback transactions for the current test
|
||||
* by taking into consideration the
|
||||
* {@link #isDefaultRollback() default rollback} flag and a possible
|
||||
* method-level override via the {@link Rollback @Rollback} annotation.
|
||||
* Determine whether or not to roll back transactions for the current test.
|
||||
* <p>The default implementation simply delegates to {@link #isDefaultRollback()}.
|
||||
* @return the <em>rollback</em> flag for the current test
|
||||
*/
|
||||
@Override
|
||||
protected boolean isRollback() {
|
||||
boolean rollback = isDefaultRollback();
|
||||
Rollback rollbackAnnotation = getTestMethod().getAnnotation(Rollback.class);
|
||||
if (rollbackAnnotation != null) {
|
||||
boolean rollbackOverride = rollbackAnnotation.value();
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("Method-level @Rollback(" + rollbackOverride + ") overrides default rollback ["
|
||||
+ rollback + "] for test [" + getName() + "].");
|
||||
}
|
||||
rollback = rollbackOverride;
|
||||
}
|
||||
else {
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("No method-level @Rollback override: using default rollback [" + rollback
|
||||
+ "] for test [" + getName() + "].");
|
||||
}
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug("Using default rollback [" + rollback + "] for test [" + getName() + "].");
|
||||
}
|
||||
return rollback;
|
||||
}
|
||||
|
||||
private void runTestTimed(TestExecutionCallback tec, Method testMethod) throws Throwable {
|
||||
Timed timed = testMethod.getAnnotation(Timed.class);
|
||||
if (timed == null) {
|
||||
runTest(tec, testMethod);
|
||||
}
|
||||
else {
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
runTest(tec, testMethod);
|
||||
}
|
||||
finally {
|
||||
long elapsed = System.currentTimeMillis() - startTime;
|
||||
if (elapsed > timed.millis()) {
|
||||
fail("Took " + elapsed + " ms; limit was " + timed.millis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void runTest(TestExecutionCallback tec, Method testMethod) throws Throwable {
|
||||
ExpectedException expectedExceptionAnnotation = testMethod.getAnnotation(ExpectedException.class);
|
||||
boolean exceptionIsExpected = (expectedExceptionAnnotation != null && expectedExceptionAnnotation.value() != null);
|
||||
Class<? extends Throwable> expectedException = (exceptionIsExpected ? expectedExceptionAnnotation.value() : null);
|
||||
|
||||
Repeat repeat = testMethod.getAnnotation(Repeat.class);
|
||||
int runs = ((repeat != null) && (repeat.value() > 1)) ? repeat.value() : 1;
|
||||
|
||||
for (int i = 0; i < runs; i++) {
|
||||
try {
|
||||
if (runs > 1 && this.logger != null && this.logger.isInfoEnabled()) {
|
||||
this.logger.info("Repetition " + (i + 1) + " of test " + testMethod.getName());
|
||||
}
|
||||
tec.run();
|
||||
if (exceptionIsExpected) {
|
||||
fail("Expected exception: " + expectedException.getName());
|
||||
}
|
||||
}
|
||||
catch (Throwable t) {
|
||||
if (!exceptionIsExpected) {
|
||||
throw t;
|
||||
}
|
||||
if (!expectedException.isAssignableFrom(t.getClass())) {
|
||||
// Wrap the unexpected throwable with an explicit message.
|
||||
AssertionFailedError assertionError = new AssertionFailedError("Unexpected exception, expected<" +
|
||||
expectedException.getName() + "> but was<" + t.getClass().getName() + ">");
|
||||
assertionError.initCause(t);
|
||||
throw assertionError;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static interface TestExecutionCallback {
|
||||
|
||||
void run() throws Throwable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test annotation to indicate that a test method <em>dirties</em> the context
|
||||
* for the current test.
|
||||
* </p>
|
||||
* <p>
|
||||
* Using this annotation in conjunction with
|
||||
* {@link AbstractAnnotationAwareTransactionalTests} is less error-prone than
|
||||
* calling
|
||||
* {@link org.springframework.test.AbstractSingleSpringContextTests#setDirty() setDirty()}
|
||||
* explicitly because the call to {@code setDirty()} is guaranteed to
|
||||
* occur, even if the test failed. If only a particular code path in the test
|
||||
* dirties the context, prefer calling {@code setDirty()} explicitly --
|
||||
* and take care!
|
||||
* </p>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests
|
||||
*/
|
||||
@Target( { ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DirtiesContext {
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Test annotation to indicate that a test method is required to throw the
|
||||
* specified exception.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target( { ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ExpectedException {
|
||||
|
||||
Class<? extends Throwable> value();
|
||||
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test annotation to indicate that a test is enabled for a specific testing
|
||||
* profile or environment. If the configured {@link ProfileValueSource} returns
|
||||
* a matching {@link #value() value} for the provided {@link #name() name}, the
|
||||
* test will be enabled.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note: {@link IfProfileValue @IfProfileValue} can be applied at either the
|
||||
* class or method level.
|
||||
* </p>
|
||||
* <p>
|
||||
* Examples: when using {@link SystemProfileValueSource} as the
|
||||
* {@link ProfileValueSource} implementation, you can configure a test method to
|
||||
* run only on Java VMs from Sun Microsystems as follows:
|
||||
* </p>
|
||||
*
|
||||
* <pre class="code">
|
||||
* {@link IfProfileValue @IfProfileValue}(name="java.vendor", value="Sun Microsystems Inc.")
|
||||
* testSomething() {
|
||||
* // ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* You can alternatively configure {@link IfProfileValue @IfProfileValue} with
|
||||
* <em>OR</em> semantics for multiple {@link #values() values} as follows
|
||||
* (assuming a {@link ProfileValueSource} has been appropriately configured for
|
||||
* the "test-groups" name):
|
||||
* </p>
|
||||
*
|
||||
* <pre class="code">
|
||||
* {@link IfProfileValue @IfProfileValue}(name="test-groups", values={"unit-tests", "integration-tests"})
|
||||
* public void testWhichRunsForUnitOrIntegrationTestGroups() {
|
||||
* // ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
* @see ProfileValueSource
|
||||
* @see ProfileValueSourceConfiguration
|
||||
* @see ProfileValueUtils
|
||||
* @see AbstractAnnotationAwareTransactionalTests
|
||||
* @see org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests
|
||||
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
|
||||
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
|
||||
*/
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
@Documented
|
||||
public @interface IfProfileValue {
|
||||
|
||||
/**
|
||||
* The {@code name} of the <em>profile value</em> against which to test.
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* A single, permissible {@code value} of the <em>profile value</em>
|
||||
* for the given {@link #name() name}.
|
||||
* <p>Note: Assigning values to both {@link #value()} and {@link #values()}
|
||||
* will lead to a configuration conflict.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* A list of all permissible {@code values} of the
|
||||
* <em>profile value</em> for the given {@link #name() name}.
|
||||
* <p>Note: Assigning values to both {@link #value()} and {@link #values()}
|
||||
* will lead to a configuration conflict.
|
||||
*/
|
||||
String[] values() default {};
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Test annotation to indicate that a method is not transactional.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target( { ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface NotTransactional {
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Strategy interface for retrieving <em>profile values</em> for a given
|
||||
* testing environment.
|
||||
* </p>
|
||||
* <p>
|
||||
* Concrete implementations must provide a {@code public} no-args
|
||||
* constructor.
|
||||
* </p>
|
||||
* <p>
|
||||
* Spring provides the following out-of-the-box implementations:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li>{@link SystemProfileValueSource}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
* @see ProfileValueSourceConfiguration
|
||||
* @see IfProfileValue
|
||||
* @see ProfileValueUtils
|
||||
*/
|
||||
public interface ProfileValueSource {
|
||||
|
||||
/**
|
||||
* Get the <em>profile value</em> indicated by the specified key.
|
||||
* @param key the name of the <em>profile value</em>
|
||||
* @return the String value of the <em>profile value</em>, or {@code null}
|
||||
* if there is no <em>profile value</em> with that key
|
||||
*/
|
||||
String get(String key);
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* ProfileValueSourceConfiguration is a class-level annotation which is used to
|
||||
* specify what type of {@link ProfileValueSource} to use when retrieving
|
||||
* <em>profile values</em> configured via the
|
||||
* {@link IfProfileValue @IfProfileValue} annotation.
|
||||
* </p>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see ProfileValueSource
|
||||
* @see IfProfileValue
|
||||
* @see ProfileValueUtils
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
@Documented
|
||||
public @interface ProfileValueSourceConfiguration {
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The type of {@link ProfileValueSource} to use when retrieving
|
||||
* <em>profile values</em>.
|
||||
* </p>
|
||||
*
|
||||
* @see SystemProfileValueSource
|
||||
*/
|
||||
Class<? extends ProfileValueSource> value() default SystemProfileValueSource.class;
|
||||
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* General utility methods for working with <em>profile values</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
* @see ProfileValueSource
|
||||
* @see ProfileValueSourceConfiguration
|
||||
* @see IfProfileValue
|
||||
*/
|
||||
public abstract class ProfileValueUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ProfileValueUtils.class);
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the {@link ProfileValueSource} type for the specified
|
||||
* {@link Class test class} as configured via the
|
||||
* {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration}
|
||||
* annotation and instantiates a new instance of that type.
|
||||
* <p>
|
||||
* If
|
||||
* {@link ProfileValueSourceConfiguration @ProfileValueSourceConfiguration}
|
||||
* is not present on the specified class or if a custom
|
||||
* {@link ProfileValueSource} is not declared, the default
|
||||
* {@link SystemProfileValueSource} will be returned instead.
|
||||
*
|
||||
* @param testClass The test class for which the ProfileValueSource should
|
||||
* be retrieved
|
||||
* @return the configured (or default) ProfileValueSource for the specified
|
||||
* class
|
||||
* @see SystemProfileValueSource
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static ProfileValueSource retrieveProfileValueSource(Class<?> testClass) {
|
||||
Assert.notNull(testClass, "testClass must not be null");
|
||||
|
||||
Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class;
|
||||
ProfileValueSourceConfiguration config = testClass.getAnnotation(annotationType);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class ["
|
||||
+ testClass.getName() + "]");
|
||||
}
|
||||
|
||||
Class<? extends ProfileValueSource> profileValueSourceType;
|
||||
if (config != null) {
|
||||
profileValueSourceType = config.value();
|
||||
}
|
||||
else {
|
||||
profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils.getDefaultValue(annotationType);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class ["
|
||||
+ testClass.getName() + "]");
|
||||
}
|
||||
|
||||
ProfileValueSource profileValueSource;
|
||||
if (SystemProfileValueSource.class.equals(profileValueSourceType)) {
|
||||
profileValueSource = SystemProfileValueSource.getInstance();
|
||||
}
|
||||
else {
|
||||
try {
|
||||
profileValueSource = profileValueSourceType.newInstance();
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType
|
||||
+ "] for class [" + testClass.getName() + "]: using default.", e);
|
||||
}
|
||||
profileValueSource = SystemProfileValueSource.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
return profileValueSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@code testClass} is <em>enabled</em>
|
||||
* in the current environment, as specified by the
|
||||
* {@link IfProfileValue @IfProfileValue} annotation at the class level.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no
|
||||
* {@link IfProfileValue @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the test is <em>enabled</em> in the
|
||||
* current environment
|
||||
*/
|
||||
public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) {
|
||||
IfProfileValue ifProfileValue = testClass.getAnnotation(IfProfileValue.class);
|
||||
if (ifProfileValue == null) {
|
||||
return true;
|
||||
}
|
||||
ProfileValueSource profileValueSource = retrieveProfileValueSource(testClass);
|
||||
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@code testMethod} is <em>enabled</em>
|
||||
* in the current environment, as specified by the
|
||||
* {@link IfProfileValue @IfProfileValue} annotation, which may be declared
|
||||
* on the test method itself or at the class level.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no
|
||||
* {@link IfProfileValue @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param testMethod the test method
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the test is <em>enabled</em> in the
|
||||
* current environment
|
||||
*/
|
||||
public static boolean isTestEnabledInThisEnvironment(Method testMethod, Class<?> testClass) {
|
||||
IfProfileValue ifProfileValue = testMethod.getAnnotation(IfProfileValue.class);
|
||||
if (ifProfileValue == null) {
|
||||
ifProfileValue = testClass.getAnnotation(IfProfileValue.class);
|
||||
if (ifProfileValue == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
ProfileValueSource profileValueSource = retrieveProfileValueSource(testClass);
|
||||
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the supplied {@code testMethod} is <em>enabled</em>
|
||||
* in the current environment, as specified by the
|
||||
* {@link IfProfileValue @IfProfileValue} annotation, which may be declared
|
||||
* on the test method itself or at the class level.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no
|
||||
* {@link IfProfileValue @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param profileValueSource the ProfileValueSource to use to determine if
|
||||
* the test is enabled
|
||||
* @param testMethod the test method
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the test is <em>enabled</em> in the
|
||||
* current environment
|
||||
*/
|
||||
public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod,
|
||||
Class<?> testClass) {
|
||||
|
||||
IfProfileValue ifProfileValue = testMethod.getAnnotation(IfProfileValue.class);
|
||||
if (ifProfileValue == null) {
|
||||
ifProfileValue = testClass.getAnnotation(IfProfileValue.class);
|
||||
if (ifProfileValue == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the {@code value} (or one of the {@code values})
|
||||
* in the supplied {@link IfProfileValue @IfProfileValue} annotation is
|
||||
* <em>enabled</em> in the current environment.
|
||||
*
|
||||
* @param profileValueSource the ProfileValueSource to use to determine if
|
||||
* the test is enabled
|
||||
* @param ifProfileValue the annotation to introspect
|
||||
* @return {@code true} if the test is <em>enabled</em> in the
|
||||
* current environment
|
||||
*/
|
||||
private static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource,
|
||||
IfProfileValue ifProfileValue) {
|
||||
|
||||
String environmentValue = profileValueSource.get(ifProfileValue.name());
|
||||
String[] annotatedValues = ifProfileValue.values();
|
||||
if (StringUtils.hasLength(ifProfileValue.value())) {
|
||||
if (annotatedValues.length > 0) {
|
||||
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes "
|
||||
+ "of @IfProfileValue is not allowed: choose one or the other.");
|
||||
}
|
||||
annotatedValues = new String[] { ifProfileValue.value() };
|
||||
}
|
||||
|
||||
for (String value : annotatedValues) {
|
||||
if (ObjectUtils.nullSafeEquals(value, environmentValue)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Test annotation to indicate that a test method should be invoked repeatedly.
|
||||
* <p />
|
||||
* Note that the scope of execution to be repeated includes execution of the
|
||||
* test method itself as well as any <em>set up</em> or <em>tear down</em>
|
||||
* of the test fixture.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
@Target( { ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Repeat {
|
||||
|
||||
int value() default 1;
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Test annotation to indicate whether or not the transaction for the annotated
|
||||
* test method should be <em>rolled back</em> after the test method has
|
||||
* completed. If {@code true}, the transaction will be rolled back;
|
||||
* otherwise, the transaction will be committed.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
@Target( { ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Rollback {
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Whether or not the transaction for the annotated method should be rolled
|
||||
* back after the method has completed.
|
||||
* </p>
|
||||
*/
|
||||
boolean value() default true;
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of {@link ProfileValueSource} which uses system properties as
|
||||
* the underlying source.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SystemProfileValueSource implements ProfileValueSource {
|
||||
|
||||
private static final SystemProfileValueSource INSTANCE = new SystemProfileValueSource();
|
||||
|
||||
|
||||
/**
|
||||
* Obtain the canonical instance of this ProfileValueSource.
|
||||
*/
|
||||
public static final SystemProfileValueSource getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Private constructor, enforcing the singleton pattern.
|
||||
*/
|
||||
private SystemProfileValueSource() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the <em>profile value</em> indicated by the specified key from the
|
||||
* system properties.
|
||||
* @see System#getProperty(String)
|
||||
*/
|
||||
@Override
|
||||
public String get(String key) {
|
||||
Assert.hasText(key, "'key' must not be empty");
|
||||
return System.getProperty(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Test-specific annotation to indicate that a test method has to finish
|
||||
* execution in a {@link #millis() specified time period}.
|
||||
* </p>
|
||||
* <p>
|
||||
* If the text execution takes longer than the specified time period, then the
|
||||
* test is to be considered failed.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note that the time period includes execution of the test method itself, any
|
||||
* {@link Repeat repetitions} of the test, and any <em>set up</em> or
|
||||
* <em>tear down</em> of the test fixture.
|
||||
* </p>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
* @see Repeat
|
||||
* @see AbstractAnnotationAwareTransactionalTests
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface Timed {
|
||||
|
||||
/**
|
||||
* The maximum amount of time (in milliseconds) that a test execution can
|
||||
* take without being marked as failed due to taking too long.
|
||||
*/
|
||||
long millis();
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -50,7 +50,9 @@ import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalT
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Convenient support class for JPA-related tests. Offers the same contract as
|
||||
* This class is only used within tests in the spring-orm module.
|
||||
*
|
||||
* <p>Convenient support class for JPA-related tests. Offers the same contract as
|
||||
* AbstractTransactionalDataSourceSpringContextTests and equally good performance,
|
||||
* even when performing the instrumentation required by the JPA specification.
|
||||
*
|
||||
@@ -171,15 +173,6 @@ public abstract class AbstractJpaTests extends AbstractAnnotationAwareTransactio
|
||||
return;
|
||||
}
|
||||
|
||||
final Method testMethod = getTestMethod();
|
||||
|
||||
if (isDisabledInThisEnvironment(testMethod)) {
|
||||
recordDisabled();
|
||||
this.logger.info("**** " + getClass().getName() + "." + getName() + " is disabled in this environment: "
|
||||
+ "Total disabled tests=" + getDisabledTestCount());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!shouldUseShadowLoader()) {
|
||||
super.runBare();
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user