Browse Source

Add nullability annotations to tests in module/spring-boot-cache

See gh-47263
pull/47387/head
Moritz Halbritter 3 months ago
parent
commit
f8ce17c751
  1. 10
      module/spring-boot-cache/build.gradle
  2. 18
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/actuate/endpoint/CachesEndpointTests.java
  3. 1
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/actuate/endpoint/CachesEndpointWebIntegrationTests.java
  4. 3
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/AbstractCacheAutoConfigurationTests.java
  5. 32
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/CacheAutoConfigurationTests.java
  6. 6
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/EhCache3CacheAutoConfigurationTests.java
  7. 1
      module/spring-boot-cache/src/test/java/org/springframework/boot/cache/metrics/JCacheCacheMeterBinderProviderTests.java

10
module/spring-boot-cache/build.gradle vendored

@ -66,5 +66,15 @@ dependencies { @@ -66,5 +66,15 @@ dependencies {
testImplementation(testFixtures(project(":module:spring-boot-webmvc")))
testImplementation(testFixtures(project(":module:spring-boot-webflux")))
testCompileOnly("com.google.code.findbugs:jsr305")
testRuntimeOnly("ch.qos.logback:logback-classic")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}
tasks.named("compileDockerTestJava") {
options.nullability.checking = "tests"
}

18
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/actuate/endpoint/CachesEndpointTests.java vendored

@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap; @@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.Test;
import org.springframework.boot.cache.actuate.endpoint.CachesEndpoint.CacheDescriptor;
import org.springframework.boot.cache.actuate.endpoint.CachesEndpoint.CacheEntryDescriptor;
import org.springframework.boot.cache.actuate.endpoint.CachesEndpoint.CacheManagerDescriptor;
import org.springframework.cache.Cache;
@ -52,9 +53,14 @@ class CachesEndpointTests { @@ -52,9 +53,14 @@ class CachesEndpointTests {
Map<String, CacheManagerDescriptor> allDescriptors = endpoint.caches().getCacheManagers();
assertThat(allDescriptors).containsOnlyKeys("test");
CacheManagerDescriptor descriptors = allDescriptors.get("test");
assertThat(descriptors).isNotNull();
assertThat(descriptors.getCaches()).containsOnlyKeys("a", "b");
assertThat(descriptors.getCaches().get("a").getTarget()).isEqualTo(ConcurrentHashMap.class.getName());
assertThat(descriptors.getCaches().get("b").getTarget()).isEqualTo(ConcurrentHashMap.class.getName());
CacheDescriptor a = descriptors.getCaches().get("a");
assertThat(a).isNotNull();
assertThat(a.getTarget()).isEqualTo(ConcurrentHashMap.class.getName());
CacheDescriptor b = descriptors.getCaches().get("b");
assertThat(b).isNotNull();
assertThat(b.getTarget()).isEqualTo(ConcurrentHashMap.class.getName());
}
@Test
@ -65,8 +71,12 @@ class CachesEndpointTests { @@ -65,8 +71,12 @@ class CachesEndpointTests {
CachesEndpoint endpoint = new CachesEndpoint(cacheManagers);
Map<String, CacheManagerDescriptor> allDescriptors = endpoint.caches().getCacheManagers();
assertThat(allDescriptors).containsOnlyKeys("test", "another");
assertThat(allDescriptors.get("test").getCaches()).containsOnlyKeys("a", "b");
assertThat(allDescriptors.get("another").getCaches()).containsOnlyKeys("a", "c");
CacheManagerDescriptor test = allDescriptors.get("test");
assertThat(test).isNotNull();
assertThat(test.getCaches()).containsOnlyKeys("a", "b");
CacheManagerDescriptor another = allDescriptors.get("another");
assertThat(another).isNotNull();
assertThat(another.getCaches()).containsOnlyKeys("a", "c");
}
@Test

1
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/actuate/endpoint/CachesEndpointWebIntegrationTests.java vendored

@ -85,6 +85,7 @@ class CachesEndpointWebIntegrationTests { @@ -85,6 +85,7 @@ class CachesEndpointWebIntegrationTests {
@WebEndpointTest
void clearNamedCache(WebTestClient client, ApplicationContext context) {
Cache b = context.getBean("one", CacheManager.class).getCache("b");
assertThat(b).isNotNull();
b.put("test", "value");
client.delete().uri("/actuator/caches/b").exchange().expectStatus().isNoContent();
assertThat(b.get("test")).isNull();

3
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/AbstractCacheAutoConfigurationTests.java vendored

@ -24,6 +24,7 @@ import java.util.Map; @@ -24,6 +24,7 @@ import java.util.Map;
import com.hazelcast.spring.cache.HazelcastCacheManager;
import org.cache2k.extra.spring.SpringCache2kCacheManager;
import org.infinispan.spring.embedded.provider.SpringEmbeddedCacheManager;
import org.jspecify.annotations.Nullable;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
@ -146,7 +147,7 @@ abstract class AbstractCacheAutoConfigurationTests { @@ -146,7 +147,7 @@ abstract class AbstractCacheAutoConfigurationTests {
abstract static class CacheManagerTestCustomizer<T extends CacheManager> implements CacheManagerCustomizer<T> {
T cacheManager;
@Nullable T cacheManager;
@Override
public void customize(T cacheManager) {

32
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/CacheAutoConfigurationTests.java vendored

@ -402,6 +402,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -402,6 +402,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
assertThat(cacheManager.getCacheNames()).containsOnly("one", "two");
CompleteConfiguration<?, ?> defaultCacheConfiguration = context.getBean(CompleteConfiguration.class);
MockCacheManager mockCacheManager = (MockCacheManager) cacheManager.getCacheManager();
assertThat(mockCacheManager).isNotNull();
assertThat(mockCacheManager.getConfigurations()).containsEntry("one", defaultCacheConfiguration)
.containsEntry("two", defaultCacheConfiguration);
});
@ -438,7 +439,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -438,7 +439,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
.run((context) -> {
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
assertThat(cacheManager.getCacheManager().getURI()).isEqualTo(configResource.getURI());
javax.cache.CacheManager jCache = cacheManager.getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getURI()).isEqualTo(configResource.getURI());
});
}
@ -462,7 +465,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -462,7 +465,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn)
.run((context) -> {
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
assertThat(cacheManager.getCacheManager().getClassLoader()).isEqualTo(context.getClassLoader());
javax.cache.CacheManager jCache = cacheManager.getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getClassLoader()).isEqualTo(context.getClassLoader());
});
}
@ -479,7 +484,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -479,7 +484,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
.withBean(JCachePropertiesCustomizer.class, () -> customizer)
.run((context) -> {
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
assertThat(cacheManager.getCacheManager().getProperties()).containsEntry("customized", "true");
javax.cache.CacheManager jCache = cacheManager.getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getProperties()).containsEntry("customized", "true");
});
}
@ -584,7 +591,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -584,7 +591,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
.run((context) -> {
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
assertThat(cacheManager.getCacheManager().getURI()).isEqualTo(configResource.getURI());
javax.cache.CacheManager jCache = cacheManager.getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getURI()).isEqualTo(configResource.getURI());
assertThat(Hazelcast.getAllHazelcastInstances()).hasSize(1);
});
}
@ -672,8 +681,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -672,8 +681,9 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
"spring.cache.jcache.config=" + configLocation)
.run((context) -> {
Resource configResource = new ClassPathResource(configLocation);
assertThat(getCacheManager(context, JCacheCacheManager.class).getCacheManager().getURI())
.isEqualTo(configResource.getURI());
javax.cache.CacheManager jCache = getCacheManager(context, JCacheCacheManager.class).getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getURI()).isEqualTo(configResource.getURI());
});
}
@ -767,6 +777,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -767,6 +777,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
CaffeineCacheManager manager = getCacheManager(context, CaffeineCacheManager.class);
assertThat(manager.getCacheNames()).containsOnly("foo");
Cache foo = manager.getCache("foo");
assertThat(foo).isNotNull();
foo.get("1");
// See next tests: no spec given so stats should be disabled
assertThat(((CaffeineCache) foo).getNativeCache().stats().missCount()).isZero();
@ -823,16 +834,21 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests { @@ -823,16 +834,21 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
CaffeineCacheManager manager = getCacheManager(context, CaffeineCacheManager.class);
assertThat(manager.getCacheNames()).containsOnly("foo", "bar");
Cache foo = manager.getCache("foo");
assertThat(foo).isNotNull();
foo.get("1");
assertThat(((CaffeineCache) foo).getNativeCache().stats().missCount()).isOne();
}
private CouchbaseCacheConfiguration getDefaultCouchbaseCacheConfiguration(CouchbaseCacheManager cacheManager) {
return (CouchbaseCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
Object field = ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
assertThat(field).isNotNull();
return (CouchbaseCacheConfiguration) field;
}
private RedisCacheConfiguration getDefaultRedisCacheConfiguration(RedisCacheManager cacheManager) {
return (RedisCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfiguration");
Object field = ReflectionTestUtils.getField(cacheManager, "defaultCacheConfiguration");
assertThat(field).isNotNull();
return (RedisCacheConfiguration) field;
}
@Configuration(proxyBeanMethods = false)

6
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/autoconfigure/EhCache3CacheAutoConfigurationTests.java vendored

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.boot.cache.autoconfigure;
import javax.cache.CacheManager;
import org.ehcache.jsr107.EhcacheCachingProvider;
import org.junit.jupiter.api.Test;
@ -88,7 +90,9 @@ class EhCache3CacheAutoConfigurationTests extends AbstractCacheAutoConfiguration @@ -88,7 +90,9 @@ class EhCache3CacheAutoConfigurationTests extends AbstractCacheAutoConfiguration
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
Resource configResource = new ClassPathResource(configLocation);
assertThat(cacheManager.getCacheManager().getURI()).isEqualTo(configResource.getURI());
CacheManager jCache = cacheManager.getCacheManager();
assertThat(jCache).isNotNull();
assertThat(jCache.getURI()).isEqualTo(configResource.getURI());
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
});
}

1
module/spring-boot-cache/src/test/java/org/springframework/boot/cache/metrics/JCacheCacheMeterBinderProviderTests.java vendored

@ -42,6 +42,7 @@ import static org.mockito.Mockito.mock; @@ -42,6 +42,7 @@ import static org.mockito.Mockito.mock;
class JCacheCacheMeterBinderProviderTests {
@Mock
@SuppressWarnings("NullAway.Init")
private javax.cache.Cache<Object, Object> nativeCache;
@Test

Loading…
Cancel
Save