From 267283e3cd8bce10e28d5521274c1cc3d05937ad Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Wed, 15 Jul 2020 19:17:31 +0200 Subject: [PATCH] Handle null items as advertized in the Javadoc See gh-22344 --- .../condition/ConditionMessage.java | 6 +++++- .../condition/ConditionMessageTests.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java index 68b6fafb81..2fca3a250b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java @@ -381,7 +381,8 @@ public final class ConditionMessage { Assert.notNull(style, "Style must not be null"); StringBuilder message = new StringBuilder(this.reason); items = style.applyTo(items); - if ((this.condition == null || items.size() <= 1) && StringUtils.hasLength(this.singular)) { + if ((this.condition == null || items == null || items.size() <= 1) + && StringUtils.hasLength(this.singular)) { message.append(" ").append(this.singular); } else if (StringUtils.hasLength(this.plural)) { @@ -420,6 +421,9 @@ public final class ConditionMessage { }; public Collection applyTo(Collection items) { + if (items == null) { + return null; + } List result = new ArrayList<>(items.size()); for (Object item : items) { result.add(applyToItem(item)); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionMessageTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionMessageTests.java index 40d1cfce88..d8c47ecccc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionMessageTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionMessageTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.condition; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.junit.jupiter.api.Test; @@ -188,4 +189,19 @@ class ConditionMessageTests { assertThat(message.toString()).isEqualTo("@Test JMX is available"); } + @Test + void itemsTolerateNullInput() { + Collection items = null; + ConditionMessage message = ConditionMessage.forCondition(Test.class).didNotFind("item").items(items); + assertThat(message.toString()).isEqualTo("@Test did not find item"); + } + + @Test + void quotedItemsTolerateNullInput() { + Collection items = null; + ConditionMessage message = ConditionMessage.forCondition(Test.class).didNotFind("item").items(Style.QUOTE, + items); + assertThat(message.toString()).isEqualTo("@Test did not find item"); + } + }