This commit is contained in:
Stéphane Nicoll
2024-01-15 11:17:11 +01:00
parent e1236a8672
commit 0c42965fc3
71 changed files with 543 additions and 828 deletions

View File

@@ -21,10 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
@@ -49,23 +47,21 @@ class AnnotationCacheOperationSourceTests {
@Test
void singularAnnotation() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "singular", 1);
assertThat(ops).element(0).isInstanceOf(CacheableOperation.class);
assertThat(ops).singleElement().satisfies(cacheOperation(CacheableOperation.class, "test"));
}
@Test
void multipleAnnotation() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "multiple", 2);
Iterator<CacheOperation> it = ops.iterator();
assertThat(it.next()).isInstanceOf(CacheableOperation.class);
assertThat(it.next()).isInstanceOf(CacheEvictOperation.class);
assertThat(ops).satisfiesExactly(cacheOperation(CacheableOperation.class),
cacheOperation(CacheEvictOperation.class));
}
@Test
void caching() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "caching", 2);
Iterator<CacheOperation> it = ops.iterator();
assertThat(it.next()).isInstanceOf(CacheableOperation.class);
assertThat(it.next()).isInstanceOf(CacheEvictOperation.class);
assertThat(ops).satisfiesExactly(cacheOperation(CacheableOperation.class),
cacheOperation(CacheEvictOperation.class));
}
@Test
@@ -76,76 +72,69 @@ class AnnotationCacheOperationSourceTests {
@Test
void singularStereotype() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "singleStereotype", 1);
assertThat(ops).element(0).isInstanceOf(CacheEvictOperation.class);
assertThat(ops).satisfiesExactly(cacheOperation(CacheEvictOperation.class));
}
@Test
void multipleStereotypes() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "multipleStereotype", 3);
Iterator<CacheOperation> it = ops.iterator();
assertThat(it.next()).isInstanceOf(CacheableOperation.class);
CacheOperation next = it.next();
assertThat(next).isInstanceOf(CacheEvictOperation.class);
assertThat(next.getCacheNames()).contains("foo");
next = it.next();
assertThat(next).isInstanceOf(CacheEvictOperation.class);
assertThat(next.getCacheNames()).contains("bar");
assertThat(ops).satisfiesExactly(cacheOperation(CacheableOperation.class),
cacheOperation(CacheEvictOperation.class, "foo"),
cacheOperation(CacheEvictOperation.class, "bar")
);
}
@Test
void singleComposedAnnotation() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "singleComposed", 2);
Iterator<CacheOperation> it = ops.iterator();
CacheOperation cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("directly declared"));
assertThat(cacheOperation.getKey()).isEmpty();
cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("composedCache"));
assertThat(cacheOperation.getKey()).isEqualTo("composedKey");
assertThat(ops).satisfiesExactly(
zero -> {
assertThat(zero).satisfies(cacheOperation(CacheOperation.class, "directly declared"));
assertThat(zero.getKey()).isEmpty();
},
first -> {
assertThat(first).satisfies(cacheOperation(CacheOperation.class, "composedCache"));
assertThat(first.getKey()).isEqualTo("composedKey");
}
);
}
@Test
void multipleComposedAnnotations() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "multipleComposed", 4);
Iterator<CacheOperation> it = ops.iterator();
CacheOperation cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("directly declared"));
assertThat(cacheOperation.getKey()).isEmpty();
cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("composedCache"));
assertThat(cacheOperation.getKey()).isEqualTo("composedKey");
cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("foo"));
assertThat(cacheOperation.getKey()).isEmpty();
cacheOperation = it.next();
assertThat(cacheOperation).isInstanceOf(CacheEvictOperation.class);
assertThat(cacheOperation.getCacheNames()).isEqualTo(Collections.singleton("composedCacheEvict"));
assertThat(cacheOperation.getKey()).isEqualTo("composedEvictionKey");
assertThat(ops).satisfiesExactly(
zero -> {
assertThat(zero).satisfies(cacheOperation(CacheOperation.class, "directly declared"));
assertThat(zero.getKey()).isEmpty();
},
first -> {
assertThat(first).satisfies(cacheOperation(CacheOperation.class, "composedCache"));
assertThat(first.getKey()).isEqualTo("composedKey");
},
two -> {
assertThat(two).satisfies(cacheOperation(CacheOperation.class, "foo"));
assertThat(two.getKey()).isEmpty();
},
three -> {
assertThat(three).satisfies(cacheOperation(CacheEvictOperation.class, "composedCacheEvict"));
assertThat(three.getKey()).isEqualTo("composedEvictionKey");
}
);
}
@Test
void customKeyGenerator() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customKeyGenerator", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getKeyGenerator()).as("Custom key generator not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getKeyGenerator()).isEqualTo("custom"));
}
@Test
void customKeyGeneratorInherited() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customKeyGeneratorInherited", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getKeyGenerator()).as("Custom key generator not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getKeyGenerator()).isEqualTo("custom"));
}
@Test
@@ -157,29 +146,29 @@ class AnnotationCacheOperationSourceTests {
@Test
void customCacheManager() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheManager", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getCacheManager()).as("Custom cache manager not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getCacheManager()).isEqualTo("custom"));
}
@Test
void customCacheManagerInherited() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheManagerInherited", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getCacheManager()).as("Custom cache manager not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getCacheManager()).isEqualTo("custom"));
}
@Test
void customCacheResolver() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheResolver", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getCacheResolver()).as("Custom cache resolver not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getCacheResolver()).isEqualTo("custom"));
}
@Test
void customCacheResolverInherited() {
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheResolverInherited", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getCacheResolver()).as("Custom cache resolver not set").isEqualTo("custom");
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getCacheResolver()).isEqualTo("custom"));
}
@Test
@@ -191,90 +180,96 @@ class AnnotationCacheOperationSourceTests {
@Test
void fullClassLevelWithCustomCacheName() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithFullDefault.class, "methodLevelCacheName", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "", "classCacheResolver", "custom");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "", "classCacheResolver", "custom"));
}
@Test
void fullClassLevelWithCustomKeyManager() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithFullDefault.class, "methodLevelKeyGenerator", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "custom", "", "classCacheResolver" , "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"custom", "", "classCacheResolver" , "classCacheName"));
}
@Test
void fullClassLevelWithCustomCacheManager() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithFullDefault.class, "methodLevelCacheManager", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "custom", "", "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "custom", "", "classCacheName"));
}
@Test
void fullClassLevelWithCustomCacheResolver() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithFullDefault.class, "methodLevelCacheResolver", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "", "custom" , "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "", "custom" , "classCacheName"));
}
@Test
void validateNoCacheIsValid() {
// Valid as a CacheResolver might return the cache names to use with other info
Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "noCacheNameSpecified");
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation.getCacheNames()).as("cache names set must not be null").isNotNull();
assertThat(cacheOperation.getCacheNames()).as("no cache names specified").isEmpty();
assertThat(ops).singleElement().satisfies(cacheOperation ->
assertThat(cacheOperation.getCacheNames()).isEmpty());
}
@Test
void customClassLevelWithCustomCacheName() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithCustomDefault.class, "methodLevelCacheName", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "", "classCacheResolver", "custom");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "", "classCacheResolver", "custom"));
}
@Test
void severalCacheConfigUseClosest() {
Collection<CacheOperation> ops = getOps(MultipleCacheConfig.class, "multipleCacheConfig");
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "", "", "", "myCache");
assertThat(ops).singleElement().satisfies(hasSharedConfig("", "", "", "myCache"));
}
@Test
void cacheConfigFromInterface() {
Collection<CacheOperation> ops = getOps(InterfaceCacheConfig.class, "interfaceCacheConfig");
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "", "", "", "myCache");
assertThat(ops).singleElement().satisfies(hasSharedConfig("", "", "", "myCache"));
}
@Test
void cacheAnnotationOverride() {
Collection<CacheOperation> ops = getOps(InterfaceCacheConfig.class, "interfaceCacheableOverride");
assertThat(ops.size()).isSameAs(1);
CacheOperation cacheOperation = ops.iterator().next();
assertThat(cacheOperation).isInstanceOf(CacheableOperation.class);
assertThat(ops).singleElement().satisfies(cacheOperation(CacheableOperation.class));
}
@Test
void partialClassLevelWithCustomCacheManager() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithSomeDefault.class, "methodLevelCacheManager", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "custom", "", "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "custom", "", "classCacheName"));
}
@Test
void partialClassLevelWithCustomCacheResolver() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithSomeDefault.class, "methodLevelCacheResolver", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "", "custom", "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "", "custom", "classCacheName"));
}
@Test
void partialClassLevelWithNoCustomization() {
Collection<CacheOperation> ops = getOps(AnnotatedClassWithSomeDefault.class, "noCustomization", 1);
CacheOperation cacheOperation = ops.iterator().next();
assertSharedConfig(cacheOperation, "classKeyGenerator", "classCacheManager", "", "classCacheName");
assertThat(ops).singleElement().satisfies(hasSharedConfig(
"classKeyGenerator", "classCacheManager", "", "classCacheName"));
}
private Consumer<CacheOperation> cacheOperation(Class<? extends CacheOperation> type, String... cacheNames) {
return candidate -> {
assertThat(candidate).isInstanceOf(type);
assertThat(candidate.getCacheNames()).containsExactly(cacheNames);
};
}
private Consumer<CacheOperation> cacheOperation(Class<? extends CacheOperation> type) {
return candidate -> assertThat(candidate).isInstanceOf(type);
}
private Collection<CacheOperation> getOps(Class<?> target, String name, int expectedNumberOfOperations) {
Collection<CacheOperation> result = getOps(target, name);
@@ -292,14 +287,15 @@ class AnnotationCacheOperationSourceTests {
}
}
private void assertSharedConfig(CacheOperation actual, String keyGenerator, String cacheManager,
private Consumer<CacheOperation> hasSharedConfig(String keyGenerator, String cacheManager,
String cacheResolver, String... cacheNames) {
assertThat(actual.getKeyGenerator()).as("Wrong key manager").isEqualTo(keyGenerator);
assertThat(actual.getCacheManager()).as("Wrong cache manager").isEqualTo(cacheManager);
assertThat(actual.getCacheResolver()).as("Wrong cache resolver").isEqualTo(cacheResolver);
assertThat(actual.getCacheNames()).as("Wrong number of cache names").hasSameSizeAs(cacheNames);
Arrays.stream(cacheNames).forEach(cacheName -> assertThat(actual.getCacheNames()).as("Cache '" + cacheName + "' not found in " + actual.getCacheNames()).contains(cacheName));
return actual -> {
assertThat(actual.getKeyGenerator()).isEqualTo(keyGenerator);
assertThat(actual.getCacheManager()).isEqualTo(cacheManager);
assertThat(actual.getCacheResolver()).isEqualTo(cacheResolver);
assertThat(actual.getCacheNames()).hasSameSizeAs(cacheNames);
assertThat(actual.getCacheNames()).containsExactly(cacheNames);
};
}

View File

@@ -490,16 +490,13 @@ class ClassPathBeanDefinitionScannerTests {
assertThat(fooService.foo(123)).isEqualTo("bar");
assertThat(fooService.lookupFoo(123)).isEqualTo("bar");
assertThat(fooService.beanFactory).isSameAs(context.getDefaultListableBeanFactory());
assertThat(fooService.listableBeanFactory).hasSize(2);
assertThat(fooService.listableBeanFactory).element(0).isSameAs(context.getDefaultListableBeanFactory());
assertThat(fooService.listableBeanFactory).element(1).isSameAs(myBf);
assertThat(fooService.listableBeanFactory).containsExactly(context.getDefaultListableBeanFactory(), myBf);
assertThat(fooService.resourceLoader).isSameAs(context);
assertThat(fooService.resourcePatternResolver).isSameAs(context);
assertThat(fooService.eventPublisher).isSameAs(context);
assertThat(fooService.messageSource).isSameAs(ms);
assertThat(fooService.context).isSameAs(context);
assertThat(fooService.configurableContext).hasSize(1);
assertThat(fooService.configurableContext[0]).isSameAs(context);
assertThat(fooService.configurableContext).containsExactly(context);
assertThat(fooService.genericContext).isSameAs(context);
}

View File

@@ -66,6 +66,7 @@ import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -1048,9 +1049,7 @@ class ConfigurationClassPostProcessorTests {
void testCollectionArgumentOnBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionArgumentConfiguration.class, TestBean.class);
CollectionArgumentConfiguration bean = ctx.getBean(CollectionArgumentConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).hasSize(1);
assertThat(bean.testBeans).element(0).isSameAs(ctx.getBean(TestBean.class));
assertThat(bean.testBeans).containsExactly(ctx.getBean(TestBean.class));
ctx.close();
}
@@ -1058,7 +1057,6 @@ class ConfigurationClassPostProcessorTests {
void testEmptyCollectionArgumentOnBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionArgumentConfiguration.class);
CollectionArgumentConfiguration bean = ctx.getBean(CollectionArgumentConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).isEmpty();
ctx.close();
}
@@ -1067,9 +1065,7 @@ class ConfigurationClassPostProcessorTests {
void testMapArgumentOnBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapArgumentConfiguration.class, DummyRunnable.class);
MapArgumentConfiguration bean = ctx.getBean(MapArgumentConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).hasSize(1);
assertThat(bean.testBeans.values()).element(0).isSameAs(ctx.getBean(Runnable.class));
assertThat(bean.testBeans).hasSize(1).containsValue(ctx.getBean(Runnable.class));
ctx.close();
}
@@ -1077,7 +1073,6 @@ class ConfigurationClassPostProcessorTests {
void testEmptyMapArgumentOnBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapArgumentConfiguration.class);
MapArgumentConfiguration bean = ctx.getBean(MapArgumentConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).isEmpty();
ctx.close();
}
@@ -1086,9 +1081,7 @@ class ConfigurationClassPostProcessorTests {
void testCollectionInjectionFromSameConfigurationClass() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionInjectionConfiguration.class);
CollectionInjectionConfiguration bean = ctx.getBean(CollectionInjectionConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).hasSize(1);
assertThat(bean.testBeans).element(0).isSameAs(ctx.getBean(TestBean.class));
assertThat(bean.testBeans).containsExactly(ctx.getBean(TestBean.class));
ctx.close();
}
@@ -1096,18 +1089,14 @@ class ConfigurationClassPostProcessorTests {
void testMapInjectionFromSameConfigurationClass() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MapInjectionConfiguration.class);
MapInjectionConfiguration bean = ctx.getBean(MapInjectionConfiguration.class);
assertThat(bean.testBeans).isNotNull();
assertThat(bean.testBeans).hasSize(1);
assertThat(bean.testBeans.get("testBean")).isSameAs(ctx.getBean(Runnable.class));
assertThat(bean.testBeans).containsOnly(entry("testBean", ctx.getBean(Runnable.class)));
ctx.close();
}
@Test
void testBeanLookupFromSameConfigurationClass() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanLookupConfiguration.class);
BeanLookupConfiguration bean = ctx.getBean(BeanLookupConfiguration.class);
assertThat(bean.getTestBean()).isNotNull();
assertThat(bean.getTestBean()).isSameAs(ctx.getBean(TestBean.class));
assertThat(ctx.getBean(BeanLookupConfiguration.class).getTestBean()).isSameAs(ctx.getBean(TestBean.class));
ctx.close();
}

View File

@@ -126,8 +126,7 @@ public class ImportSelectorTests {
ordered.verify(beanFactory).registerBeanDefinition(eq("c"), any());
ordered.verify(beanFactory).registerBeanDefinition(eq("d"), any());
assertThat(TestImportGroup.instancesCount.get()).isEqualTo(1);
assertThat(TestImportGroup.imports).hasSize(1);
assertThat(TestImportGroup.imports.values()).element(0).asInstanceOf(LIST).hasSize(2);
assertThat(TestImportGroup.imports.values()).singleElement().asInstanceOf(LIST).hasSize(2);
}
@Test

View File

@@ -317,17 +317,13 @@ class ClassPathXmlApplicationContextTests {
assertThat(beanNamesForType[0]).isEqualTo("myMessageSource");
Map<?, StaticMessageSource> beansOfType = ctx.getBeansOfType(StaticMessageSource.class);
assertThat(beansOfType).hasSize(1);
assertThat(beansOfType.values()).element(0).isSameAs(myMessageSource);
assertThat(beansOfType.values()).singleElement().isSameAs(myMessageSource);
beansOfType = ctx.getBeansOfType(StaticMessageSource.class, true, true);
assertThat(beansOfType).hasSize(1);
assertThat(beansOfType.values()).element(0).isSameAs(myMessageSource);
assertThat(beansOfType.values()).singleElement().isSameAs(myMessageSource);
beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, StaticMessageSource.class);
assertThat(beansOfType).hasSize(1);
assertThat(beansOfType.values()).element(0).isSameAs(myMessageSource);
assertThat(beansOfType.values()).singleElement().isSameAs(myMessageSource);
beansOfType = BeanFactoryUtils.beansOfTypeIncludingAncestors(ctx, StaticMessageSource.class, true, true);
assertThat(beansOfType).hasSize(1);
assertThat(beansOfType.values()).element(0).isSameAs(myMessageSource);
assertThat(beansOfType.values()).singleElement().isSameAs(myMessageSource);
}
@Test

View File

@@ -17,6 +17,7 @@
package org.springframework.context.support;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
@@ -194,12 +195,8 @@ class DefaultLifecycleProcessorTests {
assertThat(bean3.isRunning()).isTrue();
assertThat(beanMax.isRunning()).isTrue();
context.stop();
assertThat(startedBeans).hasSize(5);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(Integer.MIN_VALUE);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(1);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(2);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(3);
assertThat(getPhase(startedBeans.get(4))).isEqualTo(Integer.MAX_VALUE);
assertThat(startedBeans).satisfiesExactly(hasPhase(Integer.MIN_VALUE),hasPhase(1),
hasPhase(2), hasPhase(3), hasPhase(Integer.MAX_VALUE));
context.close();
}
@@ -224,17 +221,13 @@ class DefaultLifecycleProcessorTests {
assertThat(smartBean2.isRunning()).isTrue();
assertThat(simpleBean1.isRunning()).isFalse();
assertThat(simpleBean2.isRunning()).isFalse();
assertThat(startedBeans).hasSize(2);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(-3);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(5);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5));
context.start();
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isTrue();
assertThat(simpleBean1.isRunning()).isTrue();
assertThat(simpleBean2.isRunning()).isTrue();
assertThat(startedBeans).hasSize(4);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(0);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5), hasPhase(0), hasPhase(0));
context.close();
}
@@ -259,9 +252,7 @@ class DefaultLifecycleProcessorTests {
assertThat(smartBean2.isRunning()).isTrue();
assertThat(simpleBean1.isRunning()).isFalse();
assertThat(simpleBean2.isRunning()).isFalse();
assertThat(startedBeans).hasSize(2);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(-3);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(5);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5));
context.stop();
assertThat(simpleBean1.isRunning()).isFalse();
assertThat(simpleBean2.isRunning()).isFalse();
@@ -272,11 +263,8 @@ class DefaultLifecycleProcessorTests {
assertThat(smartBean2.isRunning()).isTrue();
assertThat(simpleBean1.isRunning()).isTrue();
assertThat(simpleBean2.isRunning()).isTrue();
assertThat(startedBeans).hasSize(6);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(-3);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(4))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(5))).isEqualTo(5);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5),
hasPhase(-3), hasPhase(0), hasPhase(0), hasPhase(5));
context.close();
}
@@ -305,10 +293,7 @@ class DefaultLifecycleProcessorTests {
assertThat(simpleBean2.isRunning()).isFalse();
smartBean2.stop();
simpleBean1.start();
assertThat(startedBeans).hasSize(3);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(-3);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(5);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(0);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5), hasPhase(0));
lifecycleProcessor.stopForRestart();
assertThat(simpleBean1.isRunning()).isFalse();
assertThat(simpleBean2.isRunning()).isFalse();
@@ -319,17 +304,15 @@ class DefaultLifecycleProcessorTests {
assertThat(smartBean2.isRunning()).isFalse();
assertThat(simpleBean1.isRunning()).isTrue();
assertThat(simpleBean2.isRunning()).isFalse();
assertThat(startedBeans).hasSize(5);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(4))).isEqualTo(5);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5),
hasPhase(0), hasPhase(0), hasPhase(5));
context.start();
assertThat(smartBean1.isRunning()).isTrue();
assertThat(smartBean2.isRunning()).isTrue();
assertThat(simpleBean1.isRunning()).isTrue();
assertThat(simpleBean2.isRunning()).isTrue();
assertThat(startedBeans).hasSize(7);
assertThat(getPhase(startedBeans.get(5))).isEqualTo(-3);
assertThat(getPhase(startedBeans.get(6))).isEqualTo(0);
assertThat(startedBeans).satisfiesExactly(hasPhase(-3), hasPhase(5),
hasPhase(0), hasPhase(0), hasPhase(5), hasPhase(-3), hasPhase(0));
context.close();
}
@@ -354,13 +337,8 @@ class DefaultLifecycleProcessorTests {
context.getBeanFactory().registerSingleton("bean7", bean7);
context.refresh();
context.stop();
assertThat(getPhase(stoppedBeans.get(0))).isEqualTo(Integer.MAX_VALUE);
assertThat(getPhase(stoppedBeans.get(1))).isEqualTo(3);
assertThat(getPhase(stoppedBeans.get(2))).isEqualTo(3);
assertThat(getPhase(stoppedBeans.get(3))).isEqualTo(2);
assertThat(getPhase(stoppedBeans.get(4))).isEqualTo(2);
assertThat(getPhase(stoppedBeans.get(5))).isEqualTo(1);
assertThat(getPhase(stoppedBeans.get(6))).isEqualTo(1);
assertThat(stoppedBeans).satisfiesExactly(hasPhase(Integer.MAX_VALUE), hasPhase(3),
hasPhase(3), hasPhase(2), hasPhase(2), hasPhase(1), hasPhase(1));
context.close();
}
@@ -390,9 +368,8 @@ class DefaultLifecycleProcessorTests {
bean.start();
assertThat(bean.isRunning()).isTrue();
context.stop();
assertThat(stoppedBeans).hasSize(1);
assertThat(bean.isRunning()).isFalse();
assertThat(stoppedBeans).element(0).isEqualTo(bean);
assertThat(stoppedBeans).singleElement().isEqualTo(bean);
context.close();
}
@@ -434,14 +411,8 @@ class DefaultLifecycleProcessorTests {
assertThat(bean5.isRunning()).isFalse();
assertThat(bean6.isRunning()).isFalse();
assertThat(bean7.isRunning()).isFalse();
assertThat(stoppedBeans).hasSize(7);
assertThat(getPhase(stoppedBeans.get(0))).isEqualTo(Integer.MAX_VALUE);
assertThat(getPhase(stoppedBeans.get(1))).isEqualTo(500);
assertThat(getPhase(stoppedBeans.get(2))).isEqualTo(1);
assertThat(getPhase(stoppedBeans.get(3))).isEqualTo(0);
assertThat(getPhase(stoppedBeans.get(4))).isEqualTo(0);
assertThat(getPhase(stoppedBeans.get(5))).isEqualTo(-1);
assertThat(getPhase(stoppedBeans.get(6))).isEqualTo(Integer.MIN_VALUE);
assertThat(stoppedBeans).satisfiesExactly(hasPhase(Integer.MAX_VALUE), hasPhase(500),
hasPhase(1), hasPhase(0), hasPhase(0), hasPhase(-1), hasPhase(Integer.MIN_VALUE));
context.close();
}
@@ -463,13 +434,11 @@ class DefaultLifecycleProcessorTests {
assertThat(bean2.isRunning()).isTrue();
assertThat(bean99.isRunning()).isTrue();
assertThat(beanMax.isRunning()).isTrue();
assertThat(startedBeans).hasSize(4);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(Integer.MIN_VALUE);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(99);
assertThat(startedBeans).element(1).isEqualTo(bean99);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(2);
assertThat(startedBeans).element(2).isEqualTo(bean2);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(Integer.MAX_VALUE);
assertThat(startedBeans).satisfiesExactly(
hasPhase(Integer.MIN_VALUE),
one -> assertThat(one).isEqualTo(bean99).satisfies(hasPhase(99)),
two -> assertThat(two).isEqualTo(bean2).satisfies(hasPhase(2)),
hasPhase(Integer.MAX_VALUE));
context.stop();
context.close();
}
@@ -507,14 +476,11 @@ class DefaultLifecycleProcessorTests {
assertThat(bean99.isRunning()).isFalse();
assertThat(beanMax.isRunning()).isFalse();
assertThat(stoppedBeans).hasSize(6);
assertThat(getPhase(stoppedBeans.get(0))).isEqualTo(Integer.MAX_VALUE);
assertThat(getPhase(stoppedBeans.get(1))).isEqualTo(2);
assertThat(stoppedBeans).element(1).isEqualTo(bean2);
assertThat(getPhase(stoppedBeans.get(2))).isEqualTo(99);
assertThat(stoppedBeans).element(2).isEqualTo(bean99);
assertThat(getPhase(stoppedBeans.get(3))).isEqualTo(7);
assertThat(getPhase(stoppedBeans.get(4))).isEqualTo(1);
assertThat(getPhase(stoppedBeans.get(5))).isEqualTo(Integer.MIN_VALUE);
assertThat(stoppedBeans).satisfiesExactly(
hasPhase(Integer.MAX_VALUE),
one -> assertThat(one).isEqualTo(bean2).satisfies(hasPhase(2)),
two -> assertThat(two).isEqualTo(bean99).satisfies(hasPhase(99)),
hasPhase(7), hasPhase(1), hasPhase(Integer.MIN_VALUE));
context.close();
}
@@ -540,11 +506,7 @@ class DefaultLifecycleProcessorTests {
assertThat(bean99.isRunning()).isTrue();
assertThat(bean7.isRunning()).isTrue();
assertThat(simpleBean.isRunning()).isTrue();
assertThat(startedBeans).hasSize(4);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(-99);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(7);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(3))).isEqualTo(99);
assertThat(startedBeans).satisfiesExactly(hasPhase(-99), hasPhase(7), hasPhase(0), hasPhase(99));
context.stop();
context.close();
}
@@ -582,13 +544,8 @@ class DefaultLifecycleProcessorTests {
assertThat(bean2.isRunning()).isFalse();
assertThat(bean7.isRunning()).isFalse();
assertThat(simpleBean.isRunning()).isFalse();
assertThat(stoppedBeans).hasSize(6);
assertThat(getPhase(stoppedBeans.get(0))).isEqualTo(7);
assertThat(getPhase(stoppedBeans.get(1))).isEqualTo(2);
assertThat(getPhase(stoppedBeans.get(2))).isEqualTo(1);
assertThat(getPhase(stoppedBeans.get(3))).isEqualTo(-99);
assertThat(getPhase(stoppedBeans.get(4))).isEqualTo(0);
assertThat(getPhase(stoppedBeans.get(5))).isEqualTo(Integer.MIN_VALUE);
assertThat(stoppedBeans).satisfiesExactly(hasPhase(7), hasPhase(2),
hasPhase(1), hasPhase(-99), hasPhase(0), hasPhase(Integer.MIN_VALUE));
context.close();
}
@@ -607,10 +564,7 @@ class DefaultLifecycleProcessorTests {
assertThat(beanMin.isRunning()).isTrue();
assertThat(bean7.isRunning()).isTrue();
assertThat(simpleBean.isRunning()).isTrue();
assertThat(startedBeans).hasSize(3);
assertThat(getPhase(startedBeans.get(0))).isEqualTo(0);
assertThat(getPhase(startedBeans.get(1))).isEqualTo(Integer.MIN_VALUE);
assertThat(getPhase(startedBeans.get(2))).isEqualTo(7);
assertThat(startedBeans).satisfiesExactly(hasPhase(0), hasPhase(Integer.MIN_VALUE), hasPhase(7));
context.stop();
context.close();
}
@@ -645,21 +599,18 @@ class DefaultLifecycleProcessorTests {
assertThat(bean2.isRunning()).isFalse();
assertThat(bean7.isRunning()).isFalse();
assertThat(simpleBean.isRunning()).isFalse();
assertThat(stoppedBeans).hasSize(5);
assertThat(getPhase(stoppedBeans.get(0))).isEqualTo(7);
assertThat(getPhase(stoppedBeans.get(1))).isEqualTo(0);
assertThat(getPhase(stoppedBeans.get(2))).isEqualTo(2);
assertThat(getPhase(stoppedBeans.get(3))).isEqualTo(1);
assertThat(getPhase(stoppedBeans.get(4))).isEqualTo(Integer.MIN_VALUE);
assertThat(stoppedBeans).satisfiesExactly(hasPhase(7), hasPhase(0),
hasPhase(2), hasPhase(1), hasPhase(Integer.MIN_VALUE));
context.close();
}
private static int getPhase(Lifecycle lifecycle) {
return (lifecycle instanceof SmartLifecycle smartLifecycle ? smartLifecycle.getPhase() : 0);
private Consumer<? super Lifecycle> hasPhase(int phase) {
return lifecycle -> {
int actual = lifecycle instanceof SmartLifecycle smartLifecycle ? smartLifecycle.getPhase() : 0;
assertThat(actual).isEqualTo(phase);
};
}
private static class TestLifecycleBean implements Lifecycle {
private final CopyOnWriteArrayList<Lifecycle> startedBeans;

View File

@@ -112,7 +112,7 @@ class PropertySourcesPlaceholderConfigurerTests {
ppc.setPropertySources(propertySources);
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("foo");
assertThat(propertySources).element(0).isEqualTo(ppc.getAppliedPropertySources().iterator().next());
assertThat(propertySources).containsExactlyElementsOf(ppc.getAppliedPropertySources());
}
@Test
@@ -132,7 +132,7 @@ class PropertySourcesPlaceholderConfigurerTests {
ppc.setIgnoreUnresolvablePlaceholders(true);
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName()).isEqualTo("${my.name}");
assertThat(propertySources).element(0).isEqualTo(ppc.getAppliedPropertySources().iterator().next());
assertThat(propertySources).containsExactlyElementsOf(ppc.getAppliedPropertySources());
}
@Test

View File

@@ -44,7 +44,7 @@ class SerializableBeanFactoryMemoryLeakTests {
*/
@BeforeAll
@AfterAll
public static void zeroOutFactoryCount() throws Exception {
static void zeroOutFactoryCount() throws Exception {
getSerializableFactoryMap().clear();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -32,9 +32,8 @@ class Spr7283Tests {
void listWithInconsistentElementTypes() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spr7283.xml", getClass());
List<?> list = ctx.getBean("list", List.class);
assertThat(list).hasSize(2);
assertThat(list).element(0).isInstanceOf(A.class);
assertThat(list).element(1).isInstanceOf(B.class);
assertThat(list).satisfiesExactly(zero -> assertThat(zero).isInstanceOf(A.class),
one -> assertThat(one).isInstanceOf(B.class));
ctx.close();
}

View File

@@ -697,10 +697,8 @@ class MBeanExporterTests extends AbstractMBeanServerTests {
private static void assertListener(MockMBeanExporterListener listener) throws MalformedObjectNameException {
ObjectName desired = ObjectNameManager.getInstance(OBJECT_NAME);
assertThat(listener.getRegistered()).as("Incorrect number of registrations").hasSize(1);
assertThat(listener.getUnregistered()).as("Incorrect number of unregistrations").hasSize(1);
assertThat(listener.getRegistered()).element(0).as("Incorrect ObjectName in register").isEqualTo(desired);
assertThat(listener.getUnregistered()).element(0).as("Incorrect ObjectName in unregister").isEqualTo(desired);
assertThat(listener.getRegistered()).singleElement().isEqualTo(desired);
assertThat(listener.getUnregistered()).singleElement().isEqualTo(desired);
}

View File

@@ -69,7 +69,7 @@ class AsyncResultTests {
values.add(ex);
}
});
assertThat(values).element(0).isSameAs(ex);
assertThat(values).singleElement().isSameAs(ex);
assertThatExceptionOfType(ExecutionException.class)
.isThrownBy(future::get)
.withCause(ex);
@@ -98,7 +98,7 @@ class AsyncResultTests {
final Set<Throwable> values = new HashSet<>(1);
org.springframework.util.concurrent.ListenableFuture<String> future = AsyncResult.forExecutionException(ex);
future.addCallback(result -> new AssertionError("Success callback not expected: " + result), values::add);
assertThat(values).element(0).isSameAs(ex);
assertThat(values).singleElement().isSameAs(ex);
assertThatExceptionOfType(ExecutionException.class)
.isThrownBy(future::get)
.withCause(ex);

View File

@@ -36,6 +36,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import org.assertj.core.api.IntegerAssert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanWrapper;
@@ -2041,7 +2042,7 @@ class DataBinderTests {
binder.bind(pvs);
assertThat(tb.getIntegerList()).hasSize(257);
assertThat(tb.getIntegerList()).element(256).isEqualTo(1);
assertThat(tb.getIntegerList(), IntegerAssert.class).element(256).isEqualTo(1);
assertThat(binder.getBindingResult().getFieldValue("integerList[256]")).isEqualTo(1);
}