Commit 0cd83007 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '2.3.x'

Closes gh-22357
parents 85d1f539 70282316
...@@ -381,7 +381,8 @@ public final class ConditionMessage { ...@@ -381,7 +381,8 @@ public final class ConditionMessage {
Assert.notNull(style, "Style must not be null"); Assert.notNull(style, "Style must not be null");
StringBuilder message = new StringBuilder(this.reason); StringBuilder message = new StringBuilder(this.reason);
items = style.applyTo(items); 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); message.append(" ").append(this.singular);
} }
else if (StringUtils.hasLength(this.plural)) { else if (StringUtils.hasLength(this.plural)) {
...@@ -420,6 +421,9 @@ public final class ConditionMessage { ...@@ -420,6 +421,9 @@ public final class ConditionMessage {
}; };
public Collection<?> applyTo(Collection<?> items) { public Collection<?> applyTo(Collection<?> items) {
if (items == null) {
return null;
}
List<Object> result = new ArrayList<>(items.size()); List<Object> result = new ArrayList<>(items.size());
for (Object item : items) { for (Object item : items) {
result.add(applyToItem(item)); result.add(applyToItem(item));
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.condition; package org.springframework.boot.autoconfigure.condition;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -188,4 +189,19 @@ class ConditionMessageTests { ...@@ -188,4 +189,19 @@ class ConditionMessageTests {
assertThat(message.toString()).isEqualTo("@Test JMX is available"); 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");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment