Tidy up tests in spring-boot-autoconfigure-all

This commit is contained in:
Andy Wilkinson 2025-05-08 10:36:48 +01:00
parent 3543539fbe
commit 66ba1cd044
17 changed files with 56 additions and 63 deletions

View File

@ -1,4 +0,0 @@
#
# Test JSR 107 provider for testing purposes only.
#
org.springframework.boot.autoconfigure.cache.support.MockCachingProvider

View File

@ -1,5 +0,0 @@
java.naming.factory.initial = org.osjava.sj.SimpleJndiContextFactory
org.osjava.sj.delimiter = /
org.osjava.sj.jndi.shared = true
org.osjava.sj.root = src/test/resources/simple-jndi
org.osjava.sj.jndi.ignoreClose = true

View File

@ -1,4 +0,0 @@
CREATE TABLE SPAM (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(30)
);

View File

@ -1,2 +0,0 @@
INSERT INTO BAR(id, name) VALUES (1, 'bar');
INSERT INTO BAR(id, name) VALUES (2, 'ばー');

View File

@ -1,4 +0,0 @@
CREATE TABLE BAR (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
);

View File

@ -1,4 +0,0 @@
CREATE TABLE FOO (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
todrop VARCHAR(30)
);

View File

@ -1,2 +0,0 @@
ALTER TABLE FOO DROP COLUMN todrop;
ALTER TABLE FOO ADD COLUMN name VARCHAR(30);

View File

@ -1,4 +0,0 @@
CREATE TABLE FOO (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(30)
);

View File

@ -33,9 +33,6 @@ import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
import org.springframework.boot.web.server.servlet.context.ServletWebServerApplicationContext;
import org.springframework.boot.webmvc.autoconfigure.DispatcherServletAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -108,21 +105,6 @@ class SpringApplicationAdminJmxAutoConfigurationTests {
});
}
@Test
void registerWithSimpleWebApp() throws Exception {
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(TomcatServletWebServerAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
MultipleMBeanExportersConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class)
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0")) {
assertThat(context).isInstanceOf(ServletWebServerApplicationContext.class);
assertThat(this.server.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication"))
.isEqualTo(Boolean.TRUE);
int expected = ((ServletWebServerApplicationContext) context).getWebServer().getPort();
String actual = getProperty(createDefaultObjectName(), "local.server.port");
assertThat(actual).isEqualTo(String.valueOf(expected));
}
}
@Test
void onlyRegisteredOnceWhenThereIsAChildContext() {
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder().web(WebApplicationType.NONE)
@ -151,11 +133,6 @@ class SpringApplicationAdminJmxAutoConfigurationTests {
}
}
private String getProperty(ObjectName objectName, String key) throws Exception {
return (String) this.server.invoke(objectName, "getProperty", new Object[] { key },
new String[] { String.class.getName() });
}
@Configuration(proxyBeanMethods = false)
static class MultipleMBeanExportersConfiguration {

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.web.servlet;
package org.springframework.boot.autoconfigure.condition;
import java.io.IOException;
import java.util.function.Consumer;
@ -26,7 +26,6 @@ import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingFilterBean;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.

View File

@ -16,6 +16,10 @@
package org.springframework.boot.autoconfigure.template;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Collections;
@ -25,7 +29,9 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.testsupport.classpath.resources.WithResource;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mock.env.MockEnvironment;
@ -71,11 +77,14 @@ class TemplateAvailabilityProvidersTests {
}
@Test
@SuppressWarnings("rawtypes")
@WithTestTemplateAvailabilityProvider
void createWhenUsingApplicationContextShouldLoadProviders() {
ApplicationContext applicationContext = mock(ApplicationContext.class);
given(applicationContext.getClassLoader()).willReturn(this.classLoader);
given(applicationContext.getClassLoader()).willReturn(Thread.currentThread().getContextClassLoader());
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(applicationContext);
assertThat(providers.getProviders()).isNotEmpty();
assertThat(providers.getProviders()).extracting((provider) -> (Class) provider.getClass())
.containsExactly(TestTemplateAvailabilityProvider.class);
then(applicationContext).should().getClassLoader();
}
@ -86,8 +95,10 @@ class TemplateAvailabilityProvidersTests {
}
@Test
@WithTestTemplateAvailabilityProvider
void createWhenUsingClassLoaderShouldLoadProviders() {
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(this.classLoader);
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(
Thread.currentThread().getContextClassLoader());
assertThat(providers.getProviders()).isNotEmpty();
}
@ -187,4 +198,23 @@ class TemplateAvailabilityProvidersTests {
.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@WithResource(name = "META-INF/spring.factories",
content = "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider="
+ "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvidersTests$TestTemplateAvailabilityProvider")
@interface WithTestTemplateAvailabilityProvider {
}
static class TestTemplateAvailabilityProvider implements TemplateAvailabilityProvider {
@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
ResourceLoader resourceLoader) {
return false;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@ -25,6 +25,7 @@ import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.beans.factory.aot.AotServices;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.testsupport.classpath.resources.WithResource;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
@ -46,8 +47,9 @@ class TemplateRuntimeHintsTests {
}
@Test
@WithResource(name = "templates/test.html")
void contributeWhenTemplateLocationExists() {
RuntimeHints runtimeHints = contribute(getClass().getClassLoader());
RuntimeHints runtimeHints = contribute(Thread.currentThread().getContextClassLoader());
assertThat(TEST_PREDICATE.test(runtimeHints)).isTrue();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@ -17,6 +17,7 @@
package org.springframework.boot.admin;
import java.lang.management.ManagementFactory;
import java.util.Map;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
@ -34,6 +35,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -111,6 +113,19 @@ class SpringApplicationAdminMXBeanRegistrarTests {
assertThat(getProperty(objectName, "does.not.exist.test")).isNull();
}
@Test
void whenEnvironmentContainsServerPortsPropertySourceEmbeddedWebApplicationIsDetected() {
final ObjectName objectName = createObjectName(OBJECT_NAME);
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.addInitializers((context) -> context.getEnvironment()
.getPropertySources()
.addLast(new MapPropertySource("server.ports", Map.of("local.server.port", "8910"))));
this.context = application.run("--foo.bar=blam");
assertThat(isApplicationReady(objectName)).isTrue();
assertThat(isApplicationEmbeddedWebApplication(objectName)).isTrue();
}
@Test
void shutdownApp() {
final ObjectName objectName = createObjectName(OBJECT_NAME);