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. * Load the sources into the reader.
* @return the number of loaded beans
*/ */
int load() { void load() {
int count = 0;
for (Object source : this.sources) { 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"); Assert.notNull(source, "Source must not be null");
if (source instanceof Class<?>) { if (source instanceof Class<?>) {
return load((Class<?>) source); load((Class<?>) source);
return;
} }
if (source instanceof Resource) { if (source instanceof Resource) {
return load((Resource) source); load((Resource) source);
return;
} }
if (source instanceof Package) { if (source instanceof Package) {
return load((Package) source); load((Package) source);
return;
} }
if (source instanceof CharSequence) { if (source instanceof CharSequence) {
return load((CharSequence) source); load((CharSequence) source);
return;
} }
throw new IllegalArgumentException("Invalid source type " + source.getClass()); throw new IllegalArgumentException("Invalid source type " + source.getClass());
} }
private int load(Class<?> source) { private void load(Class<?> source) {
if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) { if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
// Any GroovyLoaders added in beans{} DSL can contribute beans here // Any GroovyLoaders added in beans{} DSL can contribute beans here
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class); GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader); ((GroovyBeanDefinitionReader) this.groovyReader).beans(loader.getBeans());
} }
if (isEligible(source)) { if (isEligible(source)) {
this.annotatedReader.register(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) { private int load(Resource source) {
@ -181,36 +173,41 @@ class BeanDefinitionLoader {
return this.scanner.scan(source.getName()); return this.scanner.scan(source.getName());
} }
private int load(CharSequence source) { private void load(CharSequence source) {
String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(source.toString()); String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(source.toString());
// Attempt as a Class // Attempt as a Class
try { try {
return load(ClassUtils.forName(resolvedSource, null)); load(ClassUtils.forName(resolvedSource, null));
return;
} }
catch (IllegalArgumentException | ClassNotFoundException ex) { catch (IllegalArgumentException | ClassNotFoundException ex) {
// swallow exception and continue // swallow exception and continue
} }
// Attempt as resources // Attempt as Resources
Resource[] resources = findResources(resolvedSource); if (loadAsResources(resolvedSource)) {
int loadCount = 0; return;
boolean atLeastOneResourceExists = false;
for (Resource resource : resources) {
if (isLoadCandidate(resource)) {
atLeastOneResourceExists = true;
loadCount += load(resource);
}
}
if (atLeastOneResourceExists) {
return loadCount;
} }
// Attempt as package // Attempt as package
Package packageResource = findPackage(resolvedSource); Package packageResource = findPackage(resolvedSource);
if (packageResource != null) { if (packageResource != null) {
return load(packageResource); load(packageResource);
return;
} }
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'"); 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() { private boolean isGroovyPresent() {
return ClassUtils.isPresent("groovy.lang.MetaClass", null); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -51,7 +51,7 @@ class BeanDefinitionLoaderTests {
@Test @Test
void loadClass() { void loadClass() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class);
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myComponent")).isTrue(); assertThat(this.registry.containsBean("myComponent")).isTrue();
} }
@ -61,13 +61,13 @@ class BeanDefinitionLoaderTests {
}; };
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass()); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
assertThat(loader.load()).isEqualTo(0); assertThat(load(loader)).isEqualTo(0);
} }
@Test @Test
void loadJsr330Class() { void loadJsr330Class() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myNamedComponent")).isTrue(); assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
} }
@ -75,7 +75,7 @@ class BeanDefinitionLoaderTests {
void loadXmlResource() { void loadXmlResource() {
ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass()); ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myXmlComponent")).isTrue(); assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
} }
@ -84,7 +84,7 @@ class BeanDefinitionLoaderTests {
void loadGroovyResource() { void loadGroovyResource() {
ClassPathResource resource = new ClassPathResource("sample-beans.groovy", getClass()); ClassPathResource resource = new ClassPathResource("sample-beans.groovy", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue(); assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
} }
@ -93,7 +93,7 @@ class BeanDefinitionLoaderTests {
void loadGroovyResourceWithNamespace() { void loadGroovyResourceWithNamespace() {
ClassPathResource resource = new ClassPathResource("sample-namespace.groovy", getClass()); ClassPathResource resource = new ClassPathResource("sample-namespace.groovy", getClass());
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue(); assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
} }
@ -101,7 +101,7 @@ class BeanDefinitionLoaderTests {
@Test @Test
void loadPackage() { void loadPackage() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage()); 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("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue(); assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
} }
@ -109,7 +109,7 @@ class BeanDefinitionLoaderTests {
@Test @Test
void loadClassName() { void loadClassName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getName()); 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(); assertThat(this.registry.containsBean("myComponent")).isTrue();
} }
@ -117,7 +117,7 @@ class BeanDefinitionLoaderTests {
void loadResourceName() { void loadResourceName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
"classpath:org/springframework/boot/sample-beans.xml"); "classpath:org/springframework/boot/sample-beans.xml");
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myXmlComponent")).isTrue(); assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
} }
@ -125,14 +125,14 @@ class BeanDefinitionLoaderTests {
void loadGroovyName() { void loadGroovyName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
"classpath:org/springframework/boot/sample-beans.groovy"); "classpath:org/springframework/boot/sample-beans.groovy");
assertThat(loader.load()).isEqualTo(1); assertThat(load(loader)).isEqualTo(1);
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue(); assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
} }
@Test @Test
void loadPackageName() { void loadPackageName() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName()); 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("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).isTrue(); assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
} }
@ -142,7 +142,7 @@ class BeanDefinitionLoaderTests {
// See gh-6126 // See gh-6126
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
MyComponentInPackageWithoutDot.class.getPackage().getName()); MyComponentInPackageWithoutDot.class.getPackage().getName());
int loaded = loader.load(); int loaded = load(loader);
assertThat(loaded).isEqualTo(1); assertThat(loaded).isEqualTo(1);
assertThat(this.registry.containsBean("myComponentInPackageWithoutDot")).isTrue(); assertThat(this.registry.containsBean("myComponentInPackageWithoutDot")).isTrue();
} }
@ -151,9 +151,15 @@ class BeanDefinitionLoaderTests {
void loadPackageAndClassDoesNotDoubleAdd() { void loadPackageAndClassDoesNotDoubleAdd() {
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(), BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
MyComponent.class); MyComponent.class);
assertThat(loader.load()).isEqualTo(2); assertThat(load(loader)).isEqualTo(2);
assertThat(this.registry.containsBean("myComponent")).isTrue(); assertThat(this.registry.containsBean("myComponent")).isTrue();
assertThat(this.registry.containsBean("myNamedComponent")).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