Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 24, 2018
1 parent 069704f commit 974e7b8
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 194 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -253,7 +253,7 @@ public ResolvableType getResolvableType() {
/**
* 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
*/
Expand Down Expand Up @@ -308,7 +308,6 @@ public Class<?> getDependencyType() {
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
type = args[args.length - 1];
}
// TODO: Object.class if unresolvable
}
if (type instanceof Class) {
return (Class<?>) type;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -76,7 +76,7 @@ public final int getOrder() {
* 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 + "].");
}
Expand All @@ -91,7 +91,7 @@ public void prepareTestInstance(final TestContext testContext) throws Exception
* 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 + "].");
Expand All @@ -112,7 +112,7 @@ public void beforeTestMethod(final TestContext testContext) throws Exception {
* @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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -43,13 +43,6 @@
@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
Expand All @@ -63,60 +56,68 @@ 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);
assertEquals("Dogbert", this.pet.getName());
}

@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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -124,66 +152,41 @@ public final void verifyAnnotationAutowiredFields() {

@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());
}

@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);
}

Expand Down
Loading

0 comments on commit 974e7b8

Please sign in to comment.