Skip to content

Commit

Permalink
Improve condition assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jun 29, 2023
1 parent 0a20c8a commit e902f95
Show file tree
Hide file tree
Showing 21 changed files with 204 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ protected void doTestTony(PropertyValues pvs) {
assertThat(pvs.contains("forname")).as("Contains forname").isTrue();
assertThat(pvs.contains("surname")).as("Contains surname").isTrue();
assertThat(pvs.contains("age")).as("Contains age").isTrue();
boolean condition1 = !pvs.contains("tory");
assertThat(condition1).as("Doesn't contain tory").isTrue();
assertThat(!pvs.contains("tory")).as("Doesn't contain tory").isTrue();

PropertyValue[] ps = pvs.getPropertyValues();
Map<String, String> m = new HashMap<>();
Expand All @@ -46,8 +45,7 @@ protected void doTestTony(PropertyValues pvs) {
for (PropertyValue element : ps) {
Object val = m.get(element.getName());
assertThat(val).as("Can't have unexpected value").isNotNull();
boolean condition = val instanceof String;
assertThat(condition).as("Val i string").isTrue();
assertThat(val instanceof String).as("Val i string").isTrue();
assertThat(val.equals(element.getValue())).as("val matches expected").isTrue();
m.remove(element.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public void testFindsBeansOfTypeWithStaticFactory() {
assertThat(beans.get("t1")).isEqualTo(t1);
assertThat(beans.get("t2")).isEqualTo(t2);
assertThat(beans.get("t3")).isEqualTo(t3.getObject());
boolean condition = beans.get("t4") instanceof TestBean;
assertThat(condition).isTrue();
assertThat(beans.get("t4") instanceof TestBean).isTrue();

beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
assertThat(beans).hasSize(2);
Expand Down Expand Up @@ -192,8 +191,7 @@ public void testFindsBeansOfTypeWithDefaultFactory() {
assertThat(beans.get("t1")).isEqualTo(t1);
assertThat(beans.get("t2")).isEqualTo(t2);
assertThat(beans.get("t3")).isEqualTo(t3.getObject());
boolean condition2 = beans.get("t4") instanceof TestBean;
assertThat(condition2).isTrue();
assertThat(beans.get("t4") instanceof TestBean).isTrue();
// t3 and t4 are found here as of Spring 2.0, since they are pre-registered
// singleton instances, while testFactory1 and testFactory are *not* found
// because they are FactoryBean definitions that haven't been initialized yet.
Expand All @@ -212,13 +210,11 @@ public void testFindsBeansOfTypeWithDefaultFactory() {
assertThat(beans.get("test3")).isEqualTo(test3);
assertThat(beans.get("test")).isEqualTo(test);
assertThat(beans.get("testFactory1")).isEqualTo(testFactory1);
boolean condition1 = beans.get("testFactory2") instanceof TestBean;
assertThat(condition1).isTrue();
assertThat(beans.get("testFactory2") instanceof TestBean).isTrue();
assertThat(beans.get("t1")).isEqualTo(t1);
assertThat(beans.get("t2")).isEqualTo(t2);
assertThat(beans.get("t3")).isEqualTo(t3.getObject());
boolean condition = beans.get("t4") instanceof TestBean;
assertThat(condition).isTrue();
assertThat(beans.get("t4") instanceof TestBean).isTrue();

beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
assertThat(beans).hasSize(4);
Expand Down Expand Up @@ -261,8 +257,7 @@ public void testHierarchicalResolutionWithOverride() {
assertThat(beans.get("test3")).isEqualTo(test3);
assertThat(beans.get("test")).isEqualTo(test);
assertThat(beans.get("testFactory1")).isEqualTo(testFactory1);
boolean condition = beans.get("testFactory2") instanceof TestBean;
assertThat(condition).isTrue();
assertThat(beans.get("testFactory2") instanceof TestBean).isTrue();

beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
assertThat(beans).hasSize(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void testIncompleteBeanDefinition() {
bf.getBean("testBean");
}
catch (BeanCreationException ex) {
boolean condition = ex.getRootCause() instanceof IllegalStateException;
assertThat(condition).isTrue();
assertThat(ex.getRootCause() instanceof IllegalStateException).isTrue();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void testSunnyDayWithBonaFideScopeClass() {
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
boolean condition = factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope;
assertThat(condition).isTrue();
assertThat(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope).isTrue();
}

@Test
Expand All @@ -77,8 +76,7 @@ public void testSunnyDayWithBonaFideScopeClassName() {
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
boolean condition = factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope;
assertThat(condition).isTrue();
assertThat(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void testPropertyPathFactoryBeanWithSingletonResult() {
assertThat(xbf.getType("otb.spouse")).isEqualTo(ITestBean.class);
Object result1 = xbf.getBean("otb.spouse");
Object result2 = xbf.getBean("otb.spouse");
boolean condition = result1 instanceof TestBean;
assertThat(condition).isTrue();
assertThat(result1 instanceof TestBean).isTrue();
assertThat(result1).isSameAs(result2);
assertThat(((TestBean) result1).getAge()).isEqualTo(99);
}
Expand All @@ -64,12 +63,9 @@ public void testPropertyPathFactoryBeanWithPrototypeResult() {
Object result1 = xbf.getBean("tb.spouse");
Object result2 = xbf.getBean("propertyPath3");
Object result3 = xbf.getBean("propertyPath3");
boolean condition2 = result1 instanceof TestBean;
assertThat(condition2).isTrue();
boolean condition1 = result2 instanceof TestBean;
assertThat(condition1).isTrue();
boolean condition = result3 instanceof TestBean;
assertThat(condition).isTrue();
assertThat(result1 instanceof TestBean).isTrue();
assertThat(result2 instanceof TestBean).isTrue();
assertThat(result3 instanceof TestBean).isTrue();
assertThat(((TestBean) result1).getAge()).isEqualTo(11);
assertThat(((TestBean) result2).getAge()).isEqualTo(11);
assertThat(((TestBean) result3).getAge()).isEqualTo(11);
Expand All @@ -93,8 +89,7 @@ public void testPropertyPathFactoryBeanAsInnerBean() {
TestBean spouse = (TestBean) xbf.getBean("otb.spouse");
TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner");
assertThat(tbWithInner.getSpouse()).isSameAs(spouse);
boolean condition = !tbWithInner.getFriends().isEmpty();
assertThat(condition).isTrue();
assertThat(!tbWithInner.getFriends().isEmpty()).isTrue();
assertThat(tbWithInner.getFriends().iterator().next()).isSameAs(spouse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,10 @@ public void testServiceMappings() {
assertThat(testBean3).isNotSameAs(testBean2);
assertThat(testBean4).isNotSameAs(testBean2);
assertThat(testBean4).isNotSameAs(testBean3);
boolean condition3 = testBean1 instanceof ExtendedTestService;
assertThat(condition3).isFalse();
boolean condition2 = testBean2 instanceof ExtendedTestService;
assertThat(condition2).isFalse();
boolean condition1 = testBean3 instanceof ExtendedTestService;
assertThat(condition1).isFalse();
boolean condition = testBean4 instanceof ExtendedTestService;
assertThat(condition).isTrue();
assertThat(testBean1 instanceof ExtendedTestService).isFalse();
assertThat(testBean2 instanceof ExtendedTestService).isFalse();
assertThat(testBean3 instanceof ExtendedTestService).isFalse();
assertThat(testBean4 instanceof ExtendedTestService).isTrue();
}

@Test
Expand Down Expand Up @@ -264,17 +260,14 @@ public void testRequiresListableBeanFactoryAndChokesOnAnythingElse() throws Exce


public static class TestService {

}


public static class ExtendedTestService extends TestService {

}


public static class TestService2 {

}


Expand Down Expand Up @@ -345,7 +338,6 @@ public CustomServiceLocatorException3(String message) {

@SuppressWarnings("serial")
public static class ExceptionClassWithOnlyZeroArgCtor extends Exception {

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -99,8 +99,7 @@ public void testMapWithPeriodsInKey() {
assertThat(map).hasSize(1);
assertThat(map.containsKey("foo")).isTrue();
Object object = map.get("foo");
boolean condition = object instanceof LinkedHashMap;
assertThat(condition).isTrue();
assertThat(object instanceof LinkedHashMap).isTrue();
@SuppressWarnings("unchecked")
Map<String, Object> sub = (Map<String, Object>) object;
assertThat(sub.containsKey("key1.key2")).isTrue();
Expand All @@ -115,8 +114,7 @@ public void testMapWithIntegerValue() {
assertThat(map).hasSize(1);
assertThat(map.containsKey("foo")).isTrue();
Object object = map.get("foo");
boolean condition = object instanceof LinkedHashMap;
assertThat(condition).isTrue();
assertThat(object instanceof LinkedHashMap).isTrue();
@SuppressWarnings("unchecked")
Map<String, Object> sub = (Map<String, Object>) object;
assertThat(sub).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -48,8 +48,7 @@ void testServiceLoaderFactoryBean() {
bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName());
bf.registerBeanDefinition("service", bd);
ServiceLoader<?> serviceLoader = (ServiceLoader<?>) bf.getBean("service");
boolean condition = serviceLoader.iterator().next() instanceof DocumentBuilderFactory;
assertThat(condition).isTrue();
assertThat(serviceLoader.iterator().next() instanceof DocumentBuilderFactory).isTrue();
}

@Test
Expand All @@ -58,8 +57,7 @@ void testServiceFactoryBean() {
RootBeanDefinition bd = new RootBeanDefinition(ServiceFactoryBean.class);
bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName());
bf.registerBeanDefinition("service", bd);
boolean condition = bf.getBean("service") instanceof DocumentBuilderFactory;
assertThat(condition).isTrue();
assertThat(bf.getBean("service") instanceof DocumentBuilderFactory).isTrue();
}

@Test
Expand All @@ -69,8 +67,7 @@ void testServiceListFactoryBean() {
bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName());
bf.registerBeanDefinition("service", bd);
List<?> serviceList = (List<?>) bf.getBean("service");
boolean condition = serviceList.get(0) instanceof DocumentBuilderFactory;
assertThat(condition).isTrue();
assertThat(serviceList.get(0) instanceof DocumentBuilderFactory).isTrue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public void beanDefinitionEquality() {
bd.setLazyInit(true);
bd.setScope("request");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
otherBd.setScope("request");
Expand All @@ -54,15 +52,11 @@ public void beanDefinitionEqualityWithPropertyValues() {
bd.getPropertyValues().add("age", "99");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getPropertyValues().add("name", "myName");
boolean condition3 = !bd.equals(otherBd);
assertThat(condition3).isTrue();
boolean condition2 = !otherBd.equals(bd);
assertThat(condition2).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getPropertyValues().add("age", "11");
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getPropertyValues().add("age", "99");
assertThat(bd.equals(otherBd)).isTrue();
assertThat(otherBd.equals(bd)).isTrue();
Expand All @@ -76,15 +70,11 @@ public void beanDefinitionEqualityWithConstructorArguments() {
bd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5);
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getConstructorArgumentValues().addGenericArgumentValue("test");
boolean condition3 = !bd.equals(otherBd);
assertThat(condition3).isTrue();
boolean condition2 = !otherBd.equals(bd);
assertThat(condition2).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 9);
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5);
assertThat(bd.equals(otherBd)).isTrue();
assertThat(otherBd.equals(bd)).isTrue();
Expand All @@ -99,15 +89,11 @@ public void beanDefinitionEqualityWithTypedConstructorArguments() {
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getConstructorArgumentValues().addGenericArgumentValue("test", "int");
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5);
boolean condition3 = !bd.equals(otherBd);
assertThat(condition3).isTrue();
boolean condition2 = !otherBd.equals(bd);
assertThat(condition2).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5, "int");
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, 5, "long");
assertThat(bd.equals(otherBd)).isTrue();
assertThat(otherBd.equals(bd)).isTrue();
Expand All @@ -125,10 +111,8 @@ public void genericBeanDefinitionEquality() {
otherBd.setScope("request");
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.setParentName("parent");
assertThat(bd.equals(otherBd)).isTrue();
assertThat(otherBd.equals(bd)).isTrue();
Expand All @@ -153,10 +137,8 @@ public void beanDefinitionHolderEquality() {
bd.setScope("request");
BeanDefinitionHolder holder = new BeanDefinitionHolder(bd, "bd");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
assertThat(!bd.equals(otherBd)).isTrue();
assertThat(!otherBd.equals(bd)).isTrue();
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
otherBd.setScope("request");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,8 @@ void testGenericMapWithCollectionValueConstructor() {
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

boolean condition1 = gb.getCollectionMap().get(1) instanceof HashSet;
assertThat(condition1).isTrue();
boolean condition = gb.getCollectionMap().get(2) instanceof ArrayList;
assertThat(condition).isTrue();
assertThat(gb.getCollectionMap().get(1) instanceof HashSet).isTrue();
assertThat(gb.getCollectionMap().get(2) instanceof ArrayList).isTrue();
}


Expand Down Expand Up @@ -577,10 +575,8 @@ void testGenericMapWithCollectionValueFactoryMethod() {
bf.registerBeanDefinition("genericBean", rbd);
GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

boolean condition1 = gb.getCollectionMap().get(1) instanceof HashSet;
assertThat(condition1).isTrue();
boolean condition = gb.getCollectionMap().get(2) instanceof ArrayList;
assertThat(condition).isTrue();
assertThat(gb.getCollectionMap().get(1) instanceof HashSet).isTrue();
assertThat(gb.getCollectionMap().get(2) instanceof ArrayList).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,14 @@ public void testFactoryMethodWithDifferentReturnType() {
// Check that listInstance is not considered a bean of type FactoryMethods.
assertThat(List.class.isAssignableFrom(xbf.getType("listInstance"))).isTrue();
String[] names = xbf.getBeanNamesForType(FactoryMethods.class);
boolean condition1 = !Arrays.asList(names).contains("listInstance");
assertThat(condition1).isTrue();
assertThat(Arrays.asList(names).contains("listInstance")).isFalse();
names = xbf.getBeanNamesForType(List.class);
assertThat(Arrays.asList(names).contains("listInstance")).isTrue();

xbf.preInstantiateSingletons();
assertThat(List.class.isAssignableFrom(xbf.getType("listInstance"))).isTrue();
names = xbf.getBeanNamesForType(FactoryMethods.class);
boolean condition = !Arrays.asList(names).contains("listInstance");
assertThat(condition).isTrue();
assertThat(Arrays.asList(names).contains("listInstance")).isFalse();
names = xbf.getBeanNamesForType(List.class);
assertThat(Arrays.asList(names).contains("listInstance")).isTrue();
List<?> list = (List<?>) xbf.getBean("listInstance");
Expand Down
Loading

0 comments on commit e902f95

Please sign in to comment.