Merge branch '2.6.x' into 2.7.x

Closes gh-31879
This commit is contained in:
Phillip Webb
2022-07-26 18:16:13 +01:00
2 changed files with 38 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
@@ -20,6 +20,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
@@ -31,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for {@link NamedContributorsMapAdapter}.
*
* @author Phillip Webb
* @author Guirong Hu
*/
class NamedContributorsMapAdapterTests {
@@ -92,6 +94,21 @@ class NamedContributorsMapAdapterTests {
assertThat(adapter.getContributor("two")).isEqualTo("owt");
}
@Test
void getContributorCallsAdaptersOnlyOnce() {
Map<String, String> map = new LinkedHashMap<>();
map.put("one", "one");
map.put("two", "two");
int callCount = map.size();
AtomicInteger counter = new AtomicInteger(0);
TestNamedContributorsMapAdapter<String> adapter = new TestNamedContributorsMapAdapter<>(map,
(name) -> count(name, counter));
assertThat(adapter.getContributor("one")).isEqualTo("eno");
assertThat(counter.get()).isEqualTo(callCount);
assertThat(adapter.getContributor("two")).isEqualTo("owt");
assertThat(counter.get()).isEqualTo(callCount);
}
@Test
void getContributorWhenNotInMapReturnsNull() {
TestNamedContributorsMapAdapter<String> adapter = createAdapter();
@@ -106,6 +123,11 @@ class NamedContributorsMapAdapterTests {
return adapter;
}
private String count(CharSequence charSequence, AtomicInteger counter) {
counter.incrementAndGet();
return reverse(charSequence);
}
private String reverse(CharSequence charSequence) {
return new StringBuilder(charSequence).reverse().toString();
}