Skip to content

Commit 66ba1cd

Browse files
committed
Tidy up tests in spring-boot-autoconfigure-all
1 parent 3543539 commit 66ba1cd

File tree

17 files changed

+56
-63
lines changed

17 files changed

+56
-63
lines changed

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/META-INF/services/javax.cache.spi.CachingProvider

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/jndi.properties

Lines changed: 0 additions & 5 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/another.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/data.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/encoding-data.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/encoding-schema.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/lexical-schema-aaa.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/lexical-schema-bbb.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/org/springframework/boot/autoconfigure/jdbc/schema.sql

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure-all/src/test/resources/simple-jndi

Whitespace-only changes.
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import org.springframework.boot.autoconfigure.AutoConfigurations;
3434
import org.springframework.boot.builder.SpringApplicationBuilder;
3535
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
36-
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration;
37-
import org.springframework.boot.web.server.servlet.context.ServletWebServerApplicationContext;
38-
import org.springframework.boot.webmvc.autoconfigure.DispatcherServletAutoConfiguration;
3936
import org.springframework.context.ConfigurableApplicationContext;
4037
import org.springframework.context.annotation.Bean;
4138
import org.springframework.context.annotation.Configuration;
@@ -108,21 +105,6 @@ void registerWithCustomJmxNameWhenThereAreMultipleMBeanExporters() {
108105
});
109106
}
110107

111-
@Test
112-
void registerWithSimpleWebApp() throws Exception {
113-
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
114-
.sources(TomcatServletWebServerAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
115-
MultipleMBeanExportersConfiguration.class, SpringApplicationAdminJmxAutoConfiguration.class)
116-
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0")) {
117-
assertThat(context).isInstanceOf(ServletWebServerApplicationContext.class);
118-
assertThat(this.server.getAttribute(createDefaultObjectName(), "EmbeddedWebApplication"))
119-
.isEqualTo(Boolean.TRUE);
120-
int expected = ((ServletWebServerApplicationContext) context).getWebServer().getPort();
121-
String actual = getProperty(createDefaultObjectName(), "local.server.port");
122-
assertThat(actual).isEqualTo(String.valueOf(expected));
123-
}
124-
}
125-
126108
@Test
127109
void onlyRegisteredOnceWhenThereIsAChildContext() {
128110
SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder().web(WebApplicationType.NONE)
@@ -151,11 +133,6 @@ private ObjectName createObjectName(String jmxName) {
151133
}
152134
}
153135

154-
private String getProperty(ObjectName objectName, String key) throws Exception {
155-
return (String) this.server.invoke(objectName, "getProperty", new Object[] { key },
156-
new String[] { String.class.getName() });
157-
}
158-
159136
@Configuration(proxyBeanMethods = false)
160137
static class MultipleMBeanExportersConfiguration {
161138

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.autoconfigure.web.servlet;
17+
package org.springframework.boot.autoconfigure.condition;
1818

1919
import java.io.IOException;
2020
import java.util.function.Consumer;
@@ -26,7 +26,6 @@
2626
import jakarta.servlet.ServletResponse;
2727
import org.junit.jupiter.api.Test;
2828

29-
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingFilterBean;
3029
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3130
import org.springframework.boot.web.servlet.FilterRegistrationBean;
3231
import org.springframework.context.ConfigurableApplicationContext;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.template;
1818

19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
1923
import java.util.Collection;
2024
import java.util.Collections;
2125

@@ -25,7 +29,9 @@
2529
import org.mockito.Mock;
2630
import org.mockito.junit.jupiter.MockitoExtension;
2731

32+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2833
import org.springframework.context.ApplicationContext;
34+
import org.springframework.core.env.Environment;
2935
import org.springframework.core.io.ResourceLoader;
3036
import org.springframework.mock.env.MockEnvironment;
3137

@@ -71,11 +77,14 @@ void createWhenApplicationContextIsNullShouldThrowException() {
7177
}
7278

7379
@Test
80+
@SuppressWarnings("rawtypes")
81+
@WithTestTemplateAvailabilityProvider
7482
void createWhenUsingApplicationContextShouldLoadProviders() {
7583
ApplicationContext applicationContext = mock(ApplicationContext.class);
76-
given(applicationContext.getClassLoader()).willReturn(this.classLoader);
84+
given(applicationContext.getClassLoader()).willReturn(Thread.currentThread().getContextClassLoader());
7785
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(applicationContext);
78-
assertThat(providers.getProviders()).isNotEmpty();
86+
assertThat(providers.getProviders()).extracting((provider) -> (Class) provider.getClass())
87+
.containsExactly(TestTemplateAvailabilityProvider.class);
7988
then(applicationContext).should().getClassLoader();
8089
}
8190

@@ -86,8 +95,10 @@ void createWhenClassLoaderIsNullShouldThrowException() {
8695
}
8796

8897
@Test
98+
@WithTestTemplateAvailabilityProvider
8999
void createWhenUsingClassLoaderShouldLoadProviders() {
90-
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(this.classLoader);
100+
TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(
101+
Thread.currentThread().getContextClassLoader());
91102
assertThat(providers.getProviders()).isNotEmpty();
92103
}
93104

@@ -187,4 +198,23 @@ void getProviderWhenCacheDisabledShouldNotUseCache() {
187198
.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
188199
}
189200

201+
@Target(ElementType.METHOD)
202+
@Retention(RetentionPolicy.RUNTIME)
203+
@WithResource(name = "META-INF/spring.factories",
204+
content = "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider="
205+
+ "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvidersTests$TestTemplateAvailabilityProvider")
206+
@interface WithTestTemplateAvailabilityProvider {
207+
208+
}
209+
210+
static class TestTemplateAvailabilityProvider implements TemplateAvailabilityProvider {
211+
212+
@Override
213+
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
214+
ResourceLoader resourceLoader) {
215+
return false;
216+
}
217+
218+
}
219+
190220
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
2626
import org.springframework.beans.factory.aot.AotServices;
2727
import org.springframework.boot.test.context.FilteredClassLoader;
28+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2829
import org.springframework.core.io.ClassPathResource;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,8 +47,9 @@ void templateRuntimeHintsIsRegistered() {
4647
}
4748

4849
@Test
50+
@WithResource(name = "templates/test.html")
4951
void contributeWhenTemplateLocationExists() {
50-
RuntimeHints runtimeHints = contribute(getClass().getClassLoader());
52+
RuntimeHints runtimeHints = contribute(Thread.currentThread().getContextClassLoader());
5153
assertThat(TEST_PREDICATE.test(runtimeHints)).isTrue();
5254
}
5355

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/admin/SpringApplicationAdminMXBeanRegistrarTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.admin;
1818

1919
import java.lang.management.ManagementFactory;
20+
import java.util.Map;
2021

2122
import javax.management.InstanceNotFoundException;
2223
import javax.management.MBeanServer;
@@ -34,6 +35,7 @@
3435
import org.springframework.context.annotation.Bean;
3536
import org.springframework.context.annotation.Configuration;
3637
import org.springframework.context.event.ContextRefreshedEvent;
38+
import org.springframework.core.env.MapPropertySource;
3739
import org.springframework.test.util.ReflectionTestUtils;
3840

3941
import static org.assertj.core.api.Assertions.assertThat;
@@ -111,6 +113,19 @@ void environmentIsExposed() {
111113
assertThat(getProperty(objectName, "does.not.exist.test")).isNull();
112114
}
113115

116+
@Test
117+
void whenEnvironmentContainsServerPortsPropertySourceEmbeddedWebApplicationIsDetected() {
118+
final ObjectName objectName = createObjectName(OBJECT_NAME);
119+
SpringApplication application = new SpringApplication(Config.class);
120+
application.setWebApplicationType(WebApplicationType.NONE);
121+
application.addInitializers((context) -> context.getEnvironment()
122+
.getPropertySources()
123+
.addLast(new MapPropertySource("server.ports", Map.of("local.server.port", "8910"))));
124+
this.context = application.run("--foo.bar=blam");
125+
assertThat(isApplicationReady(objectName)).isTrue();
126+
assertThat(isApplicationEmbeddedWebApplication(objectName)).isTrue();
127+
}
128+
114129
@Test
115130
void shutdownApp() {
116131
final ObjectName objectName = createObjectName(OBJECT_NAME);

0 commit comments

Comments
 (0)