Skip to content

Commit 459828f

Browse files
test: normalize XML before comparison to avoid attribute-order failures
spotless apply
1 parent 39da644 commit 459828f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/test/java/com/diffplug/gradle/CleanedAssert.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 DiffPlug
2+
* Copyright (C) 2021-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,9 @@
1515
*/
1616
package com.diffplug.gradle;
1717

18-
1918
import java.util.Arrays;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
2021
import java.util.stream.Collectors;
2122
import org.junit.Assert;
2223

@@ -26,7 +27,28 @@ public static void assumeJre8() {
2627
}
2728

2829
public static void xml(String expected, String actual) {
29-
Assert.assertEquals(trim(expected), trim(actual));
30+
Assert.assertEquals(normalizeXml(trim(expected)), normalizeXml(trim(actual)));
31+
}
32+
33+
private static String normalizeXml(String xml) {
34+
Pattern tagPattern = Pattern.compile("<([^\\s/>]+)([^>]*)>");
35+
Matcher matcher = tagPattern.matcher(xml);
36+
StringBuffer sb = new StringBuffer();
37+
while (matcher.find()) {
38+
String tagName = matcher.group(1);
39+
String remainder = matcher.group(2).trim();
40+
boolean selfClosing = remainder.endsWith("/");
41+
if (selfClosing) {
42+
remainder = remainder.substring(0, remainder.length() - 1).trim();
43+
}
44+
String[] attrArray = remainder.isEmpty() ? new String[0] : remainder.split("\\s+");
45+
Arrays.sort(attrArray);
46+
String attrs = (attrArray.length > 0 ? " " + String.join(" ", attrArray) : "");
47+
String replacement = "<" + tagName + attrs + (selfClosing ? "/>" : ">");
48+
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
49+
}
50+
matcher.appendTail(sb);
51+
return sb.toString();
3052
}
3153

3254
private static String trim(String input) {

0 commit comments

Comments
 (0)