Skip to content

Commit cf560fe

Browse files
committed
Preformatted ordered values to simplify comparison
1 parent 79db82f commit cf560fe

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

src/main/java/digital/pragmatech/testing/ContextCacheEntry.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package digital.pragmatech.testing;
22

3+
import org.springframework.test.context.MergedContextConfiguration;
4+
35
import java.time.Instant;
4-
import java.util.Arrays;
5-
import java.util.Collections;
6-
import java.util.LinkedHashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Optional;
10-
import java.util.Set;
6+
import java.util.*;
117
import java.util.concurrent.ConcurrentHashMap;
128
import java.util.concurrent.CopyOnWriteArrayList;
139
import java.util.concurrent.atomic.AtomicInteger;
1410
import java.util.stream.Collectors;
1511

16-
import org.springframework.test.context.MergedContextConfiguration;
17-
1812
/**
1913
* Entry representing a cached context configuration.
2014
*/
@@ -181,8 +175,10 @@ public Map<String, Object> getConfigurationSummary() {
181175

182176
summary.put("properties", configuration.getPropertySourceProperties().length + " properties");
183177
summary.put("parentContext", configuration.getParent());
184-
summary.put("contextCustomizers", configuration.getContextCustomizers());
185-
summary.put("locations", String.join(",", configuration.getLocations()));
178+
// contextCustomizers equality is based on Set. Convert to ordered String representation to simplify comparison
179+
summary.put("contextCustomizers", prettyPrintCollection(toStringSortedSet(configuration.getContextCustomizers())));
180+
// locations equality is based on String[], no ordering
181+
summary.put("locations", String.join(",", prettyPrintCollection(Arrays.asList(configuration.getLocations()))));
186182

187183
summary.put("contextInitializers", configuration.getContextInitializerClasses().stream()
188184
.map(Class::getSimpleName)
@@ -193,4 +189,36 @@ public Map<String, Object> getConfigurationSummary() {
193189

194190
return summary;
195191
}
192+
193+
/**
194+
* Convert collection of objects to SortedSet of String. This can be helpful for visually comparable representations.
195+
*
196+
* TODO move to string utility class
197+
*
198+
* @param elements
199+
* @return
200+
*/
201+
private static SortedSet<String> toStringSortedSet(Collection<?> elements) {
202+
var set = new TreeSet<String>();
203+
elements.forEach(e -> set.add(Objects.toString(e)));
204+
return set;
205+
}
206+
207+
/**
208+
* Pretty format collection elements (new line for each element).
209+
*
210+
* TODO move to string utility class
211+
*
212+
* @param elements
213+
* @return
214+
*/
215+
private static String prettyPrintCollection(Collection<String> elements) {
216+
if (elements == null) {
217+
return "";
218+
}
219+
if (elements.isEmpty()) {
220+
return "[]";
221+
}
222+
return "[\n" + String.join(",\n", elements) + "\n]";
223+
}
196224
}

src/main/resources/templates/fragments/configurations.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ <h2>Spring Context Configurations</h2>
3030
<div class="config-details">
3131
<div th:each="entry : ${configInfo.configuration.entrySet()}" class="config-detail-item">
3232
<div class="config-detail-key" th:text="${entry.key + ':'}">Key:</div>
33-
<div class="config-detail-value" th:text="${entry.value}">Value</div>
33+
<div class="config-detail-value">
34+
<pre th:text="${entry.value}"></pre>
35+
</div>
3436
</div>
3537
</div>
3638

0 commit comments

Comments
 (0)