From 770d6ac46703699a415bda2b7657dd3cb38bf21f Mon Sep 17 00:00:00 2001 From: jameswartell <33641420+jameswartell@users.noreply.github.com> Date: Mon, 27 Jan 2025 11:06:21 -0600 Subject: [PATCH 1/5] Various changes due to issues building/running on Windows. Using the file class to build paths results in the file separator changes, but then we compare the paths using regex's which assume the Unix style file separator. There was also some encoding issues presumably due to UTF-8 not always being the default encoding on Windows. --- .../xwiki/tool/xar/AbstractVerifyMojo.java | 70 +++++++++---------- .../org/xwiki/tool/xar/FormatMojoTest.java | 51 ++++++++++---- 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java index 6d45d429d1..0d8a32bca3 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java @@ -19,6 +19,13 @@ */ package org.xwiki.tool.xar; +import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; +import static org.twdata.maven.mojoexecutor.MojoExecutor.element; +import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; +import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; +import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; +import static org.twdata.maven.mojoexecutor.MojoExecutor.name; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -44,13 +51,6 @@ import org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection; import org.codehaus.plexus.components.io.resources.PlexusIoResource; -import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; -import static org.twdata.maven.mojoexecutor.MojoExecutor.element; -import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; -import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; -import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; -import static org.twdata.maven.mojoexecutor.MojoExecutor.name; - /** * Common code for the Verify and Format Mojos. * @@ -73,6 +73,8 @@ public abstract class AbstractVerifyMojo extends AbstractXARMojo private static final Pattern TRANSLATION_PATTERN = Pattern.compile("(.*)\\..*\\.xml"); + private static final String UNIX_STYLE_FILE_SEPARATOR = "/"; + /** * If true then don't check if the packaging is XAR before running mojos. */ @@ -150,8 +152,8 @@ public abstract class AbstractVerifyMojo extends AbstractXARMojo /** * Explicitly define a list of pages (it's a regex) that are technical pages but that should be visible (not - * hidden). For example, home pages of applications. These pages must have their default langue set to empty so - * that a search in a given language doesn't return them as they're not content. + * hidden). For example, home pages of applications. These pages must have their default langue set to empty so that + * a search in a given language doesn't return them as they're not content. * * @since 12.3RC1 */ @@ -243,15 +245,16 @@ protected Collection getXARXMLFiles() throws MojoFailureException /** * Guess the {@code <defaultLanguage>} value to use for the passed file using the following algorithm: * + * * @param file the XML file for which to guess the default language that it should have * @param xwikiXmlFiles the list of all XML files that is used to check for translations of the passed XML file * @return the default language as a string (e.g. "en" for English or "" for an empty default language) @@ -306,9 +309,14 @@ protected boolean isVisibleTechnicalPage(String filePath) private boolean isMatchingPage(String filePath, List patterns) { + String tranformedFilePath = filePath; + // we use the unix file.seperator even if we're currently running on windows. + if (!File.separator.equals(UNIX_STYLE_FILE_SEPARATOR)) { + tranformedFilePath = filePath.replace(Pattern.quote(File.separator), UNIX_STYLE_FILE_SEPARATOR); + } if (patterns != null) { for (Pattern pattern : patterns) { - if (pattern.matcher(filePath).matches()) { + if (pattern.matcher(tranformedFilePath).matches()) { return true; } } @@ -347,25 +355,11 @@ protected void executeLicenseGoal(String goal) throws MojoExecutionException throw new MojoExecutionException("License plugin could not be found in "); } - executeMojo( - licensePlugin, - goal(goal), - configuration( - element(name("licenseSets"), - element(name("licenseSet"), - element(name("header"), "license.txt"), - element(name("headerDefinitions"), - element(name("headerDefinition"), "license-xml-definition.xml")), - element(name("includes"), - element(name("include"), "src/main/resources/**/*.xml")) - ) - ) - ), - executionEnvironment( - this.project, - this.mavenSession, - this.pluginManager - ) - ); + executeMojo(licensePlugin, goal(goal), + configuration(element(name("licenseSets"), + element(name("licenseSet"), element(name("header"), "license.txt"), + element(name("headerDefinitions"), element(name("headerDefinition"), "license-xml-definition.xml")), + element(name("includes"), element(name("include"), "src/main/resources/**/*.xml"))))), + executionEnvironment(this.project, this.mavenSession, this.pluginManager)); } } diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java index 553701840d..cad84ecb49 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java @@ -19,13 +19,19 @@ */ package org.xwiki.tool.xar; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xwiki.tool.xar.internal.XMLUtils.getSAXReader; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.dom4j.Document; @@ -33,9 +39,6 @@ import org.dom4j.io.SAXReader; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.xwiki.tool.xar.internal.XMLUtils.getSAXReader; - /** * Unit tests for {@link FormatMojo}. * @@ -51,9 +54,7 @@ void defaultLanguageForDefaultDocumentWhenTranslation() mojo.defaultLanguage = "en"; File file = new File("Some/Space/Document.xml"); - List files = Arrays.asList( - new File("Some/Space/Document.xml"), - new File("Some/Space/Document.fr.xml")); + List files = Arrays.asList(new File("Some/Space/Document.xml"), new File("Some/Space/Document.fr.xml")); assertEquals(Locale.ENGLISH, mojo.guessDefaultLocale(file, files)); } @@ -76,10 +77,10 @@ void defaultLanguageForMatchingContentPage() { FormatMojo mojo = new FormatMojo(); mojo.defaultLanguage = "en"; - mojo.contentPages = Arrays.asList(".*/Document\\.xml"); + mojo.contentPages = Arrays.asList(".*" + Pattern.quote(File.separator) + "Document\\.xml"); mojo.initializePatterns(); - File file = new File("Some/Space/Document.xml"); + File file = new File("Some" + File.separator + "Space" + File.separator + "Document.xml"); assertEquals(Locale.ENGLISH, mojo.guessDefaultLocale(file, Collections.emptyList())); } @@ -118,9 +119,7 @@ void defaultLanguageForDocumentWhenNoTranslationButFileWithSameNameInOtherSpace( File file = new File("Space1/Document.xml"); // Simulate a page with the same name and with a translation but in a different space. - List files = Arrays.asList( - new File("Space2/Document.xml"), - new File("Space2/Document.fr.xml")); + List files = Arrays.asList(new File("Space2/Document.xml"), new File("Space2/Document.fr.xml")); assertEquals(Locale.ROOT, mojo.guessDefaultLocale(file, files)); } @@ -132,8 +131,13 @@ void defaultLanguageForDocumentWhenNoTranslationButFileWithSameNameInOtherSpace( void formatSpecialContentFailingWithXercesFromJDK8() throws Exception { SAXReader reader = getSAXReader(); + reader.setEncoding("UTF-8"); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("XWikiSyntaxLinks.it.xml"); String expectedContent = IOUtils.toString(is, "UTF-8"); + // github clients may automatically convert the file to use windows line returns. + if (!System.lineSeparator().equals("\n")) { + expectedContent = expectedContent.replaceAll(Pattern.quote(System.lineSeparator()), "\n"); + } is = Thread.currentThread().getContextClassLoader().getResourceAsStream("XWikiSyntaxLinks.it.xml"); Document domdoc = reader.read(is); @@ -146,7 +150,28 @@ void formatSpecialContentFailingWithXercesFromJDK8() throws Exception writer.setVersion("1.1"); writer.write(domdoc); writer.close(); - - assertEquals(expectedContent, baos.toString()); + String actual = baos.toString(Charset.forName("UTF-8")); + + int offset = -1; + if (!expectedContent.equals(actual)) { + for (int i = 0; i < Math.min(((String) expectedContent).length(), ((String) actual).length()); i++) { + char c1 = expectedContent.charAt(i); + char c2 = actual.charAt(i); + if (c1 != c2) { + offset = i; + break; + } + } + String result = ""; + if (offset != -1) { + result += "Offset of first difference: " + offset + " expected char: " + expectedContent.charAt(offset) + + " (" + ((int) expectedContent.charAt(offset)) + ")" + " actual char: " + actual.charAt(offset) + + " (" + ((int) actual.charAt(offset)) + ")" + System.lineSeparator(); + } + result += "Expected:" + System.lineSeparator() + expectedContent + System.lineSeparator() + + System.lineSeparator() + " Does not match actual: " + System.lineSeparator() + actual + + System.lineSeparator() + System.lineSeparator(); + assertTrue(false, result); + } } } From 44d8e48762c75a127ea250aca54e94412e1596d2 Mon Sep 17 00:00:00 2001 From: jameswartell <33641420+jameswartell@users.noreply.github.com> Date: Mon, 27 Jan 2025 11:26:32 -0600 Subject: [PATCH 2/5] Whoops. This should have been a regex syntax replaceAll(). Also in the test I actually assume the patterns will use the unix line returns, so I shouldn't have put that in the pattern. --- .../src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java | 2 +- .../src/test/java/org/xwiki/tool/xar/FormatMojoTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java index 0d8a32bca3..f4d8026a95 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java @@ -312,7 +312,7 @@ private boolean isMatchingPage(String filePath, List patterns) String tranformedFilePath = filePath; // we use the unix file.seperator even if we're currently running on windows. if (!File.separator.equals(UNIX_STYLE_FILE_SEPARATOR)) { - tranformedFilePath = filePath.replace(Pattern.quote(File.separator), UNIX_STYLE_FILE_SEPARATOR); + tranformedFilePath = filePath.replaceAll(Pattern.quote(File.separator), UNIX_STYLE_FILE_SEPARATOR); } if (patterns != null) { for (Pattern pattern : patterns) { diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java index cad84ecb49..2f8d8a4288 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java @@ -77,7 +77,7 @@ void defaultLanguageForMatchingContentPage() { FormatMojo mojo = new FormatMojo(); mojo.defaultLanguage = "en"; - mojo.contentPages = Arrays.asList(".*" + Pattern.quote(File.separator) + "Document\\.xml"); + mojo.contentPages = Arrays.asList(".*/Document\\.xml"); mojo.initializePatterns(); File file = new File("Some" + File.separator + "Space" + File.separator + "Document.xml"); From 55328cbf31f845a9be84fb1298eaebdf426cd0e4 Mon Sep 17 00:00:00 2001 From: jameswartell <33641420+jameswartell@users.noreply.github.com> Date: Mon, 27 Jan 2025 12:58:47 -0600 Subject: [PATCH 3/5] fix static imports. Eclipse was moving these automatically. --- .../org/xwiki/tool/xar/AbstractVerifyMojo.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java index f4d8026a95..d6a843d22c 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java @@ -19,13 +19,6 @@ */ package org.xwiki.tool.xar; -import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; -import static org.twdata.maven.mojoexecutor.MojoExecutor.element; -import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; -import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; -import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; -import static org.twdata.maven.mojoexecutor.MojoExecutor.name; - import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -51,6 +44,13 @@ import org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection; import org.codehaus.plexus.components.io.resources.PlexusIoResource; +import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; +import static org.twdata.maven.mojoexecutor.MojoExecutor.element; +import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; +import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; +import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; +import static org.twdata.maven.mojoexecutor.MojoExecutor.name; + /** * Common code for the Verify and Format Mojos. * From 4dab4277bbab93e9b1372760db7584daa5ed301f Mon Sep 17 00:00:00 2001 From: jameswartell <33641420+jameswartell@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:05:57 -0600 Subject: [PATCH 4/5] Confirmed this builds without this change. --- .../test/java/org/xwiki/tool/xar/FormatMojoTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java index 2f8d8a4288..ad52580f19 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java @@ -19,10 +19,6 @@ */ package org.xwiki.tool.xar; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.xwiki.tool.xar.internal.XMLUtils.getSAXReader; - import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; @@ -39,6 +35,10 @@ import org.dom4j.io.SAXReader; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.xwiki.tool.xar.internal.XMLUtils.getSAXReader; + /** * Unit tests for {@link FormatMojo}. * @@ -80,7 +80,7 @@ void defaultLanguageForMatchingContentPage() mojo.contentPages = Arrays.asList(".*/Document\\.xml"); mojo.initializePatterns(); - File file = new File("Some" + File.separator + "Space" + File.separator + "Document.xml"); + File file = new File("Some/Space/Document.xml"); assertEquals(Locale.ENGLISH, mojo.guessDefaultLocale(file, Collections.emptyList())); } From 2e9ae6e340f9af5dd0f41dd3205dc3636d46ba59 Mon Sep 17 00:00:00 2001 From: jameswartell <33641420+jameswartell@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:10:23 -0600 Subject: [PATCH 5/5] err fix charset lookup. no need to set charset on reader. Change .gitAttributes to force "/n" to be the line separator for xml on check-in and locally. Small optimization so we don't replace file.seperator when we don't even end up comparing it to a pattern. --- .gitattributes | 2 +- .../java/org/xwiki/tool/xar/AbstractVerifyMojo.java | 13 +++++++------ .../java/org/xwiki/tool/xar/FormatMojoTest.java | 9 +-------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/.gitattributes b/.gitattributes index 523a688c25..72fbe9dac4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,6 @@ * text=auto *.java text ident diff=java -*.xml text ident diff=html +*.xml eol=lf ident diff=html *.vm text ident *.js text ident *.css text ident diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java index d6a843d22c..06024cab0f 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/main/java/org/xwiki/tool/xar/AbstractVerifyMojo.java @@ -309,12 +309,13 @@ protected boolean isVisibleTechnicalPage(String filePath) private boolean isMatchingPage(String filePath, List patterns) { - String tranformedFilePath = filePath; - // we use the unix file.seperator even if we're currently running on windows. - if (!File.separator.equals(UNIX_STYLE_FILE_SEPARATOR)) { - tranformedFilePath = filePath.replaceAll(Pattern.quote(File.separator), UNIX_STYLE_FILE_SEPARATOR); - } - if (patterns != null) { + + if (patterns != null && patterns.size() > 0) { + String tranformedFilePath = filePath; + // we use the unix file.seperator even if we're currently running on windows. + if (!File.separator.equals(UNIX_STYLE_FILE_SEPARATOR)) { + tranformedFilePath = filePath.replaceAll(Pattern.quote(File.separator), UNIX_STYLE_FILE_SEPARATOR); + } for (Pattern pattern : patterns) { if (pattern.matcher(tranformedFilePath).matches()) { return true; diff --git a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java index ad52580f19..59e3209ed2 100644 --- a/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java +++ b/xwiki-commons-tools/xwiki-commons-tool-xar/xwiki-commons-tool-xar-plugin/src/test/java/org/xwiki/tool/xar/FormatMojoTest.java @@ -22,12 +22,10 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; -import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; import org.dom4j.Document; @@ -131,13 +129,8 @@ void defaultLanguageForDocumentWhenNoTranslationButFileWithSameNameInOtherSpace( void formatSpecialContentFailingWithXercesFromJDK8() throws Exception { SAXReader reader = getSAXReader(); - reader.setEncoding("UTF-8"); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("XWikiSyntaxLinks.it.xml"); String expectedContent = IOUtils.toString(is, "UTF-8"); - // github clients may automatically convert the file to use windows line returns. - if (!System.lineSeparator().equals("\n")) { - expectedContent = expectedContent.replaceAll(Pattern.quote(System.lineSeparator()), "\n"); - } is = Thread.currentThread().getContextClassLoader().getResourceAsStream("XWikiSyntaxLinks.it.xml"); Document domdoc = reader.read(is); @@ -150,7 +143,7 @@ void formatSpecialContentFailingWithXercesFromJDK8() throws Exception writer.setVersion("1.1"); writer.write(domdoc); writer.close(); - String actual = baos.toString(Charset.forName("UTF-8")); + String actual = baos.toString(java.nio.charset.StandardCharsets.UTF_8); int offset = -1; if (!expectedContent.equals(actual)) {