Move bean definition counting only used in tests into test code

Closes gh-22105
pull/22113/head
Andy Wilkinson 4 years ago
parent fe78be240a
commit 16263e342c

@ -120,51 +120,43 @@ class BeanDefinitionLoader {
/**
* Load the sources into the reader.
* @return the number of loaded beans
*/
int load() {
int count = 0;
void load() {
for (Object source : this.sources) {
count += load(source);
load(source);
}
return count;
}
private int load(Object source) {
private void load(Object source) {
Assert.notNull(source, "Source must not be null");
if (source instanceof Class<?>) {
return load((Class<?>) source);
load((Class<?>) source);
return;
}
if (source instanceof Resource) {
return load((Resource) source);
load((Resource) source);
return;
}
if (source instanceof Package) {
return load((Package) source);
load((Package) source);
return;
}
if (source instanceof CharSequence) {
return load((CharSequence) source);
load((CharSequence) source);
return;
}
throw new IllegalArgumentException("Invalid source type " + source.getClass());
}
private int load(Class<?> source) {
private void load(Class<?> source) {
if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
// Any GroovyLoaders added in beans{} DSL can contribute beans here
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader);
((GroovyBeanDefinitionReader) this.groovyReader).beans(loader.getBeans());
}
if (isEligible(source)) {
this.annotatedReader.register(source);
return 1;
}
return 0;
}
private int load(GroovyBeanDefinitionSource source) {
int before = this.xmlReader.getRegistry().getBeanDefinitionCount();
((GroovyBeanDefinitionReader) this.groovyReader).beans(source.getBeans());
int after = this.xmlReader.getRegistry().getBeanDefinitionCount();
return after - before;
}
private int load(Resource source) {
@ -181,36 +173,41 @@ class BeanDefinitionLoader {
return this.scanner.scan(source.getName());
}
private int load(CharSequence source) {
private void load(CharSequence source) {
String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(source.toString());
// Attempt as a Class
try {
return load(ClassUtils.forName(resolvedSource, null));
load(ClassUtils.forName(resolvedSource, null));
return;
}
catch (IllegalArgumentException | ClassNotFoundException ex) {
// swallow exception and continue
}
// Attempt as resources
Resource[] resources = findResources(resolvedSource);
int loadCount = 0;
boolean atLeastOneResourceExists = false;
for (Resource resource : resources) {
if (isLoadCandidate(resource)) {
atLeastOneResourceExists = true;
loadCount += load(resource);
}
}
if (atLeastOneResourceExists) {
return loadCount;
// Attempt as Resources
if (loadAsResources(resolvedSource)) {
return;
}
// Attempt as package
Package packageResource = findPackage(resolvedSource);
if (packageResource != null) {
return load(packageResource);
load(packageResource);
return;
}
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
}
private boolean loadAsResources(String resolvedSource) {
boolean foundCandidate = false;
Resource[] resources = findResources(resolvedSource);
for (Resource resource : resources) {
if (isLoadCandidate(resource)) {
foundCandidate = true;
load(resource);
}
}
return foundCandidate;
}
private boolean isGroovyPresent() {
return ClassUtils.isPresent("groovy.lang.MetaClass", null);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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,7 +51,7 @@ class BeanDefinitionLoaderTests {
@Test
void loadClass() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class);
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myComponent")).isTrue();
}
@ -61,13 +61,13 @@ class BeanDefinitionLoaderTests {
};
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
assertThat(loader.load()).isEqualTo(0);
assertThat(load(loader)).isEqualTo(0);
}
@Test
void loadJsr330Class() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}
@ -75,7 +75,7 @@ class BeanDefinitionLoaderTests {
void loadXmlResource() {
ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
}
@ -84,7 +84,7 @@ class BeanDefinitionLoaderTests {
void loadGroovyResource() {
ClassPathResource resource = new ClassPathResource("sample-beans.groovy", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
}
@ -93,7 +93,7 @@ class BeanDefinitionLoaderTests {
void loadGroovyResourceWithNamespace() {
ClassPathResource resource = new ClassPathResource("sample-namespace.groovy", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
}
@ -101,7 +101,7 @@ class BeanDefinitionLoaderTests {
@Test
void loadPackage() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage());
assertThat(loader.load()).isEqualTo(2);
assertThat(load(loader)).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}
@ -109,7 +109,7 @@ class BeanDefinitionLoaderTests {
@Test
void loadClassName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getName());
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myComponent")).isTrue();
}
@ -117,7 +117,7 @@ class BeanDefinitionLoaderTests {
void loadResourceName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
"classpath:org/springframework/boot/sample-beans.xml");
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
}
@ -125,14 +125,14 @@ class BeanDefinitionLoaderTests {
void loadGroovyName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
"classpath:org/springframework/boot/sample-beans.groovy");
assertThat(loader.load()).isEqualTo(1);
assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
}
@Test
void loadPackageName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName());
assertThat(loader.load()).isEqualTo(2);
assertThat(load(loader)).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}
@ -142,7 +142,7 @@ class BeanDefinitionLoaderTests {
// See gh-6126
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
MyComponentInPackageWithoutDot.class.getPackage().getName());
int loaded = loader.load();
int loaded = load(loader);
assertThat(loaded).isEqualTo(1);
assertThat(this.registry.containsBean("myComponentInPackageWithoutDot")).isTrue();
}
@ -151,9 +151,15 @@ class BeanDefinitionLoaderTests {
void loadPackageAndClassDoesNotDoubleAdd() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
MyComponent.class);
assertThat(loader.load()).isEqualTo(2);
assertThat(load(loader)).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
}
private int load(BeanDefinitionLoader loader) {
int beans = this.registry.getBeanDefinitionCount();
loader.load();
return this.registry.getBeanDefinitionCount() - beans;
}
}

Loading…
Cancel
Save