Browse Source

Polishing

pull/1946/head
Juergen Hoeller 7 years ago
parent
commit
b5270a9cff
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java
  2. 10
      spring-test/src/main/java/org/springframework/test/context/support/DependencyInjectionTestExecutionListener.java
  3. 45
      spring-test/src/test/java/org/springframework/test/context/groovy/GroovySpringContextTests.java
  4. 105
      spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java
  5. 83
      spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests.java
  6. 138
      spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java
  7. 4
      spring-web/src/main/java/org/springframework/web/context/support/GroovyWebApplicationContext.java
  8. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

2
spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java

@ -296,7 +296,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable @@ -296,7 +296,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable
/**
* Return whether a fallback match is allowed.
* <p>This is {@code false} by default but may be overridden to return {@code true} in order
* to suggest to a {@link org.springframework.beans.factory.support.AutowireCandidateResolver}
* to suggest to an {@link org.springframework.beans.factory.support.AutowireCandidateResolver}
* that a fallback match is acceptable as well.
* @since 4.0
*/

10
spring-test/src/main/java/org/springframework/test/context/support/DependencyInjectionTestExecutionListener.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -49,7 +49,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut @@ -49,7 +49,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
*/
public static final String REINJECT_DEPENDENCIES_ATTRIBUTE = Conventions.getQualifiedAttributeName(
DependencyInjectionTestExecutionListener.class, "reinjectDependencies");
DependencyInjectionTestExecutionListener.class, "reinjectDependencies");
private static final Log logger = LogFactory.getLog(DependencyInjectionTestExecutionListener.class);
@ -76,7 +76,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut @@ -76,7 +76,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
* from the test context, regardless of its value.
*/
@Override
public void prepareTestInstance(final TestContext testContext) throws Exception {
public void prepareTestInstance(TestContext testContext) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Performing dependency injection for test context [" + testContext + "].");
}
@ -91,7 +91,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut @@ -91,7 +91,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
* otherwise, this method will have no effect.
*/
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
public void beforeTestMethod(TestContext testContext) throws Exception {
if (Boolean.TRUE.equals(testContext.getAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE))) {
if (logger.isDebugEnabled()) {
logger.debug("Reinjecting dependencies for test context [" + testContext + "].");
@ -112,7 +112,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut @@ -112,7 +112,7 @@ public class DependencyInjectionTestExecutionListener extends AbstractTestExecut
* @see #prepareTestInstance(TestContext)
* @see #beforeTestMethod(TestContext)
*/
protected void injectDependencies(final TestContext testContext) throws Exception {
protected void injectDependencies(TestContext testContext) throws Exception {
Object bean = testContext.getTestInstance();
AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);

45
spring-test/src/test/java/org/springframework/test/context/groovy/GroovySpringContextTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
@ -43,13 +43,6 @@ import static org.junit.Assert.*; @@ -43,13 +43,6 @@ import static org.junit.Assert.*;
@ContextConfiguration("context.groovy")
public class GroovySpringContextTests implements BeanNameAware, InitializingBean {
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
@Autowired
private ApplicationContext applicationContext;
private Employee employee;
@Autowired
@ -63,41 +56,49 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean @@ -63,41 +56,49 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean
protected String bar;
@Autowired
private ApplicationContext applicationContext;
private String beanName;
private boolean beanInitialized = false;
@Autowired
protected final void setEmployee(final Employee employee) {
protected void setEmployee(Employee employee) {
this.employee = employee;
}
@Resource
protected final void setBar(final String bar) {
protected void setBar(String bar) {
this.bar = bar;
}
@Override
public final void setBeanName(final String beanName) {
public void setBeanName(String beanName) {
this.beanName = beanName;
}
@Override
public final void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
this.beanInitialized = true;
}
@Test
public final void verifyBeanInitialized() {
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
public void verifyBeanNameSet() {
assertTrue("The bean name of this test instance should have been set to the fully qualified class name " +
"due to BeanNameAware semantics.", this.beanName.startsWith(getClass().getName()));
}
@Test
public final void verifyBeanNameSet() {
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
public void verifyBeanInitialized() {
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
}
@Test
public final void verifyAnnotationAutowiredFields() {
public void verifyAnnotationAutowiredFields() {
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
assertNotNull("The application context should have been autowired.", this.applicationContext);
assertNotNull("The pet field should have been autowired.", this.pet);
@ -105,18 +106,18 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean @@ -105,18 +106,18 @@ public class GroovySpringContextTests implements BeanNameAware, InitializingBean
}
@Test
public final void verifyAnnotationAutowiredMethods() {
public void verifyAnnotationAutowiredMethods() {
assertNotNull("The employee setter method should have been autowired.", this.employee);
assertEquals("Dilbert", this.employee.getName());
}
@Test
public final void verifyResourceAnnotationWiredFields() {
public void verifyResourceAnnotationWiredFields() {
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
}
@Test
public final void verifyResourceAnnotationWiredMethods() {
public void verifyResourceAnnotationWiredMethods() {
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
}

105
spring-test/src/test/java/org/springframework/test/context/junit4/ConcreteTransactionalJUnit4SpringContextTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@ -51,10 +51,6 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans @@ -51,10 +51,6 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
private static final String SUE = "sue";
private static final String YODA = "yoda";
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
private Employee employee;
@Autowired
@ -68,54 +64,86 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans @@ -68,54 +64,86 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
private String bar;
private String beanName;
private boolean beanInitialized = false;
@Autowired
private final void setEmployee(final Employee employee) {
private void setEmployee(Employee employee) {
this.employee = employee;
}
@Resource
private final void setBar(final String bar) {
private void setBar(String bar) {
this.bar = bar;
}
@Override
public final void setBeanName(final String beanName) {
public void setBeanName(String beanName) {
this.beanName = beanName;
}
@Override
public final void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
this.beanInitialized = true;
}
@Before
public void setUp() {
assertEquals("Verifying the number of rows in the person table before a test method.",
(inTransaction() ? 2 : 1), countRowsInPersonTable());
}
@After
public void tearDown() {
assertEquals("Verifying the number of rows in the person table after a test method.",
(inTransaction() ? 4 : 1), countRowsInPersonTable());
}
@BeforeTransaction
public void beforeTransaction() {
assertEquals("Verifying the number of rows in the person table before a transactional test method.",
1, countRowsInPersonTable());
assertEquals("Adding yoda", 1, addPerson(YODA));
}
@AfterTransaction
public void afterTransaction() {
assertEquals("Deleting yoda", 1, deletePerson(YODA));
assertEquals("Verifying the number of rows in the person table after a transactional test method.",
1, countRowsInPersonTable());
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyApplicationContext() {
public void verifyBeanNameSet() {
assertInTransaction(false);
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
super.applicationContext);
assertTrue("The bean name of this test instance should have been set to the fully qualified class name " +
"due to BeanNameAware semantics.", this.beanName.startsWith(getClass().getName()));
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyBeanInitialized() {
public void verifyApplicationContext() {
assertInTransaction(false);
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
super.applicationContext);
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyBeanNameSet() {
public void verifyBeanInitialized() {
assertInTransaction(false);
assertEquals("The bean name of this test instance should have been set to the fully qualified class name "
+ "due to BeanNameAware semantics.", getClass().getName(), this.beanName);
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyAnnotationAutowiredFields() {
public void verifyAnnotationAutowiredFields() {
assertInTransaction(false);
assertNull("The nonrequiredLong property should NOT have been autowired.", this.nonrequiredLong);
assertNotNull("The pet field should have been autowired.", this.pet);
@ -124,7 +152,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans @@ -124,7 +152,7 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyAnnotationAutowiredMethods() {
public void verifyAnnotationAutowiredMethods() {
assertInTransaction(false);
assertNotNull("The employee setter method should have been autowired.", this.employee);
assertEquals("John Smith", this.employee.getName());
@ -132,58 +160,33 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans @@ -132,58 +160,33 @@ public class ConcreteTransactionalJUnit4SpringContextTests extends AbstractTrans
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyResourceAnnotationWiredFields() {
public void verifyResourceAnnotationWiredFields() {
assertInTransaction(false);
assertEquals("The foo field should have been wired via @Resource.", "Foo", this.foo);
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public final void verifyResourceAnnotationWiredMethods() {
public void verifyResourceAnnotationWiredMethods() {
assertInTransaction(false);
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
}
@BeforeTransaction
public void beforeTransaction() {
assertEquals("Verifying the number of rows in the person table before a transactional test method.", 1,
countRowsInPersonTable());
assertEquals("Adding yoda", 1, addPerson(YODA));
}
@Before
public void setUp() throws Exception {
assertEquals("Verifying the number of rows in the person table before a test method.",
(inTransaction() ? 2 : 1), countRowsInPersonTable());
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(JANE));
assertEquals("Adding sue", 1, addPerson(SUE));
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().", 4,
countRowsInPersonTable());
}
@After
public void tearDown() throws Exception {
assertEquals("Verifying the number of rows in the person table after a test method.",
(inTransaction() ? 4 : 1), countRowsInPersonTable());
assertEquals("Verifying the number of rows in the person table in modifyTestDataWithinTransaction().",
4, countRowsInPersonTable());
}
@AfterTransaction
public void afterTransaction() {
assertEquals("Deleting yoda", 1, deletePerson(YODA));
assertEquals("Verifying the number of rows in the person table after a transactional test method.", 1,
countRowsInPersonTable());
}
private int addPerson(final String name) {
private int addPerson(String name) {
return super.jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private int deletePerson(final String name) {
private int deletePerson(String name) {
return super.jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}

83
spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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,6 @@ import javax.inject.Named; @@ -23,7 +23,6 @@ import javax.inject.Named;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@ -82,13 +81,9 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -82,13 +81,9 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
* Default resource path for the application context configuration for
* {@link SpringJUnit4ClassRunnerAppCtxTests}: {@value #DEFAULT_CONTEXT_RESOURCE_PATH}
*/
public static final String DEFAULT_CONTEXT_RESOURCE_PATH = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
public static final String DEFAULT_CONTEXT_RESOURCE_PATH =
"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
private ApplicationContext applicationContext;
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
private Employee employee;
@ -124,31 +119,20 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -124,31 +119,20 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
@Named("quux")
protected String namedQuux;
private String beanName;
// ------------------------------------------------------------------------|
@Override
public final void afterPropertiesSet() throws Exception {
this.beanInitialized = true;
}
private ApplicationContext applicationContext;
@Override
public final void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private boolean beanInitialized = false;
@Override
public final void setBeanName(final String beanName) {
this.beanName = beanName;
}
@Autowired
protected final void setEmployee(final Employee employee) {
protected void setEmployee(Employee employee) {
this.employee = employee;
}
@Resource
protected final void setBar(final String bar) {
protected void setBar(String bar) {
this.bar = bar;
}
@ -162,33 +146,46 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -162,33 +146,46 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
this.spelParameterValue = spelParameterValue;
}
// ------------------------------------------------------------------------|
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() {
this.beanInitialized = true;
}
@Test
public final void verifyApplicationContextSet() {
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
this.applicationContext);
public void verifyBeanNameSet() {
assertTrue("The bean name of this test instance should have been set due to BeanNameAware semantics.",
this.beanName.startsWith(getClass().getName()));
}
@Test
public final void verifyBeanInitialized() {
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
public void verifyApplicationContextSet() {
assertNotNull("The application context should have been set due to ApplicationContextAware semantics.",
this.applicationContext);
}
@Test
public final void verifyBeanNameSet() {
assertEquals("The bean name of this test instance should have been set due to BeanNameAware semantics.",
getClass().getName(), this.beanName);
public void verifyBeanInitialized() {
assertTrue("This test bean should have been initialized due to InitializingBean semantics.",
this.beanInitialized);
}
@Test
public final void verifyAnnotationAutowiredAndInjectedFields() {
public void verifyAnnotationAutowiredAndInjectedFields() {
assertNull("The nonrequiredLong field should NOT have been autowired.", this.nonrequiredLong);
assertEquals("The quux field should have been autowired via @Autowired and @Qualifier.", "Quux", this.quux);
assertEquals("The namedFoo field should have been injected via @Inject and @Named.", "Quux", this.namedQuux);
assertSame("@Autowired/@Qualifier and @Inject/@Named quux should be the same object.", this.quux,
this.namedQuux);
assertSame("@Autowired/@Qualifier and @Inject/@Named quux should be the same object.", this.quux, this.namedQuux);
assertNotNull("The pet field should have been autowired.", this.autowiredPet);
assertNotNull("The pet field should have been injected.", this.injectedPet);
@ -198,13 +195,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -198,13 +195,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public final void verifyAnnotationAutowiredMethods() {
public void verifyAnnotationAutowiredMethods() {
assertNotNull("The employee setter method should have been autowired.", this.employee);
assertEquals("John Smith", this.employee.getName());
}
@Test
public final void verifyAutowiredAtValueFields() {
public void verifyAutowiredAtValueFields() {
assertNotNull("Literal @Value field should have been autowired", this.literalFieldValue);
assertNotNull("SpEL @Value field should have been autowired.", this.spelFieldValue);
assertEquals("enigma", this.literalFieldValue);
@ -212,7 +209,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -212,7 +209,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public final void verifyAutowiredAtValueMethods() {
public void verifyAutowiredAtValueMethods() {
assertNotNull("Literal @Value method parameter should have been autowired.", this.literalParameterValue);
assertNotNull("SpEL @Value method parameter should have been autowired.", this.spelParameterValue);
assertEquals("enigma", this.literalParameterValue);
@ -220,13 +217,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa @@ -220,13 +217,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public final void verifyResourceAnnotationInjectedFields() {
public void verifyResourceAnnotationInjectedFields() {
assertEquals("The foo field should have been injected via @Resource.", "Foo", this.foo);
}
@Test
public final void verifyResourceAnnotationInjectedMethods() {
public void verifyResourceAnnotationInjectedMethods() {
assertEquals("The bar method should have been wired via @Resource.", "Bar", this.bar);
}
}
}

138
spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@ -61,9 +61,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -61,9 +61,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
private static int numTearDownCalls = 0;
private static int numTearDownCallsInTransaction = 0;
private boolean beanInitialized = false;
private String beanName = "replace me with [" + getClass().getName() + "]";
private Employee employee;
@ -71,51 +68,39 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -71,51 +68,39 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
private Pet pet;
@Autowired(required = false)
protected Long nonrequiredLong;
private Long nonrequiredLong;
@Resource()
protected String foo;
protected String bar;
@Resource
private String foo;
private String bar;
private int createPerson(String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private String beanName;
private int deletePerson(String name) {
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
@Override
public void afterPropertiesSet() throws Exception {
this.beanInitialized = true;
}
private boolean beanInitialized = false;
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
@Autowired
protected void setEmployee(Employee employee) {
private void setEmployee(Employee employee) {
this.employee = employee;
}
@Resource
protected void setBar(String bar) {
private void setBar(String bar) {
this.bar = bar;
}
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
assertEquals(countRowsInTable("person"), expectedNumRows, "the number of rows in the person table ("
+ testState + ").");
@Override
public void setBeanName(String beanName) {
this.beanName = beanName;
}
private void assertAddPerson(final String name) {
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
@Override
public void afterPropertiesSet() {
this.beanInitialized = true;
}
@BeforeClass
void beforeClass() {
numSetUpCalls = 0;
@ -132,12 +117,51 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -132,12 +117,51 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
assertEquals(numTearDownCallsInTransaction, NUM_TX_TESTS, "number of calls to tearDown() within a transaction.");
}
@BeforeMethod
void setUp() {
numSetUpCalls++;
if (inTransaction()) {
numSetUpCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
}
@AfterMethod
void tearDown() {
numTearDownCalls++;
if (inTransaction()) {
numTearDownCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
}
@BeforeTransaction
void beforeTransaction() {
assertNumRowsInPersonTable(1, "before a transactional test method");
assertAddPerson(YODA);
}
@AfterTransaction
void afterTransaction() {
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
assertNumRowsInPersonTable(1, "after a transactional test method");
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
void verifyBeanNameSet() {
assertInTransaction(false);
assertTrue(this.beanName.startsWith(getClass().getName()), "The bean name of this test instance " +
"should have been set to the fully qualified class name due to BeanNameAware semantics.");
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
void verifyApplicationContextSet() {
assertInTransaction(false);
assertNotNull(super.applicationContext,
"The application context should have been set due to ApplicationContextAware semantics.");
"The application context should have been set due to ApplicationContextAware semantics.");
Employee employeeBean = (Employee) super.applicationContext.getBean("employee");
assertEquals(employeeBean.getName(), "John Smith", "employee's name.");
}
@ -147,15 +171,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -147,15 +171,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
void verifyBeanInitialized() {
assertInTransaction(false);
assertTrue(beanInitialized,
"This test instance should have been initialized due to InitializingBean semantics.");
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
void verifyBeanNameSet() {
assertInTransaction(false);
assertEquals(beanName, getClass().getName(),
"The bean name of this test instance should have been set due to BeanNameAware semantics.");
"This test instance should have been initialized due to InitializingBean semantics.");
}
@Test
@ -189,21 +205,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -189,21 +205,6 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
assertEquals(bar, "Bar", "The setBar() method should have been injected via @Resource.");
}
@BeforeTransaction
void beforeTransaction() {
assertNumRowsInPersonTable(1, "before a transactional test method");
assertAddPerson(YODA);
}
@BeforeMethod
void setUp() throws Exception {
numSetUpCalls++;
if (inTransaction()) {
numSetUpCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 2 : 1), "before a test method");
}
@Test
void modifyTestDataWithinTransaction() {
assertInTransaction(true);
@ -212,19 +213,22 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans @@ -212,19 +213,22 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans
assertNumRowsInPersonTable(4, "in modifyTestDataWithinTransaction()");
}
@AfterMethod
void tearDown() throws Exception {
numTearDownCalls++;
if (inTransaction()) {
numTearDownCallsInTransaction++;
}
assertNumRowsInPersonTable((inTransaction() ? 4 : 1), "after a test method");
private int createPerson(String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
@AfterTransaction
void afterTransaction() {
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
assertNumRowsInPersonTable(1, "after a transactional test method");
private int deletePerson(String name) {
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
private void assertNumRowsInPersonTable(int expectedNumRows, String testState) {
assertEquals(countRowsInTable("person"), expectedNumRows,
"the number of rows in the person table (" + testState + ").");
}
private void assertAddPerson(String name) {
assertEquals(createPerson(name), 1, "Adding '" + name + "'");
}
}

4
spring-web/src/main/java/org/springframework/web/context/support/GroovyWebApplicationContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@ -33,7 +33,7 @@ import org.springframework.lang.Nullable; @@ -33,7 +33,7 @@ import org.springframework.lang.Nullable;
/**
* {@link org.springframework.web.context.WebApplicationContext} implementation which takes
* its configuration from Groovy bean definition scripts and/or XML files, as understood by
* an {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader}.
* a {@link org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader}.
* This is essentially the equivalent of
* {@link org.springframework.context.support.GenericGroovyApplicationContext}
* for a web environment.

2
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

@ -157,7 +157,7 @@ import org.springframework.web.util.UrlPathHelper; @@ -157,7 +157,7 @@ import org.springframework.web.util.UrlPathHelper;
* <ul>
* <li>a {@link ContentNegotiationManager}
* <li>a {@link DefaultFormattingConversionService}
* <li>a {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}
* <li>an {@link org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean}
* if a JSR-303 implementation is available on the classpath
* <li>a range of {@link HttpMessageConverter HttpMessageConverters} depending on the third-party
* libraries available on the classpath.

Loading…
Cancel
Save