Polish contribution

See gh-47229
This commit is contained in:
Stéphane Nicoll
2025-09-17 15:04:35 +02:00
parent 2314d15430
commit aebecb3007
3 changed files with 18 additions and 8 deletions
@@ -697,7 +697,7 @@ public class TomcatServerProperties {
/**
* Maximum size of the static resource cache.
*/
private @Nullable DataSize cacheMaxSize;
private DataSize cacheMaxSize = DataSize.ofMegabytes(10);
/**
* Time-to-live of the static resource cache.
@@ -712,11 +712,11 @@ public class TomcatServerProperties {
this.allowCaching = allowCaching;
}
public @Nullable DataSize getCacheMaxSize() {
public DataSize getCacheMaxSize() {
return this.cacheMaxSize;
}
public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) {
public void setCacheMaxSize(DataSize cacheMaxSize) {
this.cacheMaxSize = cacheMaxSize;
}
@@ -380,14 +380,12 @@ public class TomcatWebServerFactoryCustomizer
factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> {
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
context.getResources().setCachingAllowed(resource.isAllowCaching());
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
if (resource.getCacheTtl() != null) {
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
if (resource.getCacheMaxSize() != null) {
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
}
}
}));
}
@@ -322,6 +322,18 @@ class TomcatWebServerFactoryCustomizerTests {
assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2");
}
@Test
void resourceCacheMatchesDefault() {
TomcatServerProperties properties = new TomcatServerProperties();
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed());
assertThat(properties.getResource().getCacheMaxSize())
.isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize()));
});
}
@Test
void customStaticResourceAllowCaching() {
bind("server.tomcat.resource.allow-caching=false");
@@ -334,7 +346,7 @@ class TomcatWebServerFactoryCustomizerTests {
@Test
void customStaticResourceCacheMaxSize() {
bind("server.tomcat.resource.cache-max-size=4096KB");
bind("server.tomcat.resource.cache-max-size=4MB");
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];