Polishing

This commit is contained in:
Juergen Hoeller
2017-02-22 15:32:19 +01:00
parent 7fb0ad37da
commit f4de1ea147
8 changed files with 102 additions and 128 deletions

View File

@@ -66,18 +66,17 @@ import static org.junit.Assert.*;
*/
public class ConfigurationClassPostProcessorTests {
private DefaultListableBeanFactory beanFactory;
private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@Before
public void setUp() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
public void setup() {
QualifierAnnotationAutowireCandidateResolver acr = new QualifierAnnotationAutowireCandidateResolver();
acr.setBeanFactory(bf);
bf.setAutowireCandidateResolver(acr);
this.beanFactory = bf;
acr.setBeanFactory(this.beanFactory);
this.beanFactory.setAutowireCandidateResolver(acr);
}
/**
* Enhanced {@link Configuration} classes are only necessary for respecting
* certain bean semantics, like singleton-scoping, scoped proxies, etc.
@@ -950,7 +949,6 @@ public class ConfigurationClassPostProcessorTests {
}
}
@Configuration
public static class RawRepositoryConfiguration {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -22,11 +22,11 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
@@ -38,24 +38,15 @@ import static org.junit.Assert.*;
*/
public class Spr11202Tests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test // Fails
@Test
public void testWithImporter() {
context = new AnnotationConfigApplicationContext(Wrapper.class);
ApplicationContext context = new AnnotationConfigApplicationContext(Wrapper.class);
assertEquals("foo", context.getBean("value"));
}
@Test // Passes
@Test
public void testWithoutImporter() {
context = new AnnotationConfigApplicationContext(Config.class);
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
assertEquals("foo", context.getBean("value"));
}
@@ -65,6 +56,7 @@ public class Spr11202Tests {
protected static class Wrapper {
}
protected static class Selector implements ImportSelector {
@Override
@@ -73,6 +65,7 @@ public class Spr11202Tests {
}
}
@Configuration
protected static class Config {
@@ -95,6 +88,7 @@ public class Spr11202Tests {
}
}
protected static class NoBarCondition implements Condition {
@Override
@@ -106,12 +100,14 @@ public class Spr11202Tests {
}
}
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
protected static @interface Bar {
protected @interface Bar {
}
protected static class FooFactoryBean implements FactoryBean<Foo>, InitializingBean {
private Foo foo = new Foo();
@@ -137,6 +133,7 @@ public class Spr11202Tests {
}
}
protected static class Foo {
private String name;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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.
@@ -32,6 +32,7 @@ import static org.junit.Assert.*;
* @author Chris Beams
*/
public class Spr6602Tests {
@Test
public void testXmlBehavior() throws Exception {
doAssertions(new ClassPathXmlApplicationContext("Spr6602Tests-context.xml", Spr6602Tests.class));
@@ -59,8 +60,10 @@ public class Spr6602Tests {
assertThat(bar3, is(not(bar4)));
}
@Configuration
public static class FooConfig {
@Bean
public Foo foo() throws Exception {
return new Foo(barFactory().getObject());
@@ -72,7 +75,9 @@ public class Spr6602Tests {
}
}
public static class Foo {
final Bar bar;
public Foo(Bar bar) {
@@ -80,9 +85,11 @@ public class Spr6602Tests {
}
}
public static class Bar {
}
public static class BarFactory implements FactoryBean<Bar> {
@Override
@@ -102,4 +109,4 @@ public class Spr6602Tests {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -63,8 +63,8 @@ import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
* Miscellaneous system tests covering {@link Bean} naming, aliases, scoping and error
* handling within {@link Configuration} class definitions.
* Miscellaneous system tests covering {@link Bean} naming, aliases, scoping and
* error handling within {@link Configuration} class definitions.
*
* @author Chris Beams
* @author Juergen Hoeller
@@ -72,30 +72,6 @@ import static org.junit.Assert.*;
*/
public class ConfigurationClassProcessingTests {
/**
* Creates a new {@link BeanFactory}, populates it with a {@link BeanDefinition} for
* each of the given {@link Configuration} <var>configClasses</var>, and then
* post-processes the factory using JavaConfig's {@link ConfigurationClassPostProcessor}.
* When complete, the factory is ready to service requests for any {@link Bean} methods
* declared by <var>configClasses</var>.
*/
private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
for (Class<?> configClass : configClasses) {
String configBeanName = configClass.getName();
factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
}
ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
ccpp.postProcessBeanDefinitionRegistry(factory);
ccpp.postProcessBeanFactory(factory);
RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor();
rapp.setBeanFactory(factory);
factory.addBeanPostProcessor(rapp);
factory.freezeConfiguration();
return factory;
}
@Rule
public final ExpectedException exception = ExpectedException.none();
@@ -267,6 +243,30 @@ public class ConfigurationClassProcessingTests {
}
/**
* Creates a new {@link BeanFactory}, populates it with a {@link BeanDefinition}
* for each of the given {@link Configuration} {@code configClasses}, and then
* post-processes the factory using JavaConfig's {@link ConfigurationClassPostProcessor}.
* When complete, the factory is ready to service requests for any {@link Bean} methods
* declared by {@code configClasses}.
*/
private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
for (Class<?> configClass : configClasses) {
String configBeanName = configClass.getName();
factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
}
ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
ccpp.postProcessBeanDefinitionRegistry(factory);
ccpp.postProcessBeanFactory(factory);
RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor();
rapp.setBeanFactory(factory);
factory.addBeanPostProcessor(rapp);
factory.freezeConfiguration();
return factory;
}
@Configuration
static class ConfigWithBeanWithCustomName {