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.
1515 */
1616package com .diffplug .gradle ;
1717
18-
1918import java .util .Arrays ;
19+ import java .util .regex .Matcher ;
20+ import java .util .regex .Pattern ;
2021import java .util .stream .Collectors ;
2122import 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