From e0b146fdf05e9d66b81d5190bbb984307cd5dc7e Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 11:15:08 -0500 Subject: [PATCH 01/14] inspections --- .../java/org/apache/maven/archiver/ManifestConfiguration.java | 2 +- src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java b/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java index 6b839b3..817fd87 100644 --- a/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java +++ b/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java @@ -246,7 +246,7 @@ public void setPackageName(String packageName) { public String getClasspathPrefix() { String cpp = classpathPrefix.replaceAll("\\\\", "/"); - if (cpp.length() != 0 && !cpp.endsWith("/")) { + if (!cpp.isEmpty() && !cpp.endsWith("/")) { cpp += "/"; } diff --git a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java index de9722c..391ed77 100644 --- a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java @@ -71,7 +71,7 @@ private void createPropertiesFile(Properties properties, Path outputFile, boolea return; } - try (PrintWriter pw = new PrintWriter(outputFile.toFile(), StandardCharsets.ISO_8859_1.name()); + try (PrintWriter pw = new PrintWriter(outputFile.toFile(), StandardCharsets.ISO_8859_1); StringWriter sw = new StringWriter()) { properties.store(sw, null); From c14ba55f6c1c890f6c52c91a30c9fba82290f554 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 11:21:16 -0500 Subject: [PATCH 02/14] linefeed --- .../org/apache/maven/archiver/PomPropertiesUtil.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java index 391ed77..81e8b6e 100644 --- a/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java @@ -21,9 +21,9 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -71,8 +71,8 @@ private void createPropertiesFile(Properties properties, Path outputFile, boolea return; } - try (PrintWriter pw = new PrintWriter(outputFile.toFile(), StandardCharsets.ISO_8859_1); - StringWriter sw = new StringWriter()) { + try ( Writer writer = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1); + StringWriter sw = new StringWriter()) { properties.store(sw, null); @@ -87,8 +87,9 @@ private void createPropertiesFile(Properties properties, Path outputFile, boolea } Collections.sort(lines); - for (String l : lines) { - pw.println(l); + for (String line : lines) { + writer.write(line); + writer.write( '\n' ); } } } From 5b93bbb4abf97b44101adf45231223bba68fe788 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 12:57:34 -0500 Subject: [PATCH 03/14] properties --- .../shared/archiver/PomPropertiesUtil.java | 31 ++----------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 48795a6..f60babd 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -18,18 +18,11 @@ */ package org.apache.maven.shared.archiver; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; import java.util.Properties; import org.apache.maven.api.Project; @@ -71,26 +64,8 @@ private void createPropertiesFile(Properties properties, Path outputFile, boolea return; } - try ( Writer writer = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1); - StringWriter sw = new StringWriter()) { - - properties.store(sw, null); - - List lines = new ArrayList<>(); - try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) { - String line; - while ((line = r.readLine()) != null) { - if (!line.startsWith("#")) { - lines.add(line); - } - } - } - - Collections.sort(lines); - for (String line : lines) { - writer.write(line); - writer.write( '\n' ); - } + try (OutputStream out = Files.newOutputStream(outputFile)) { + properties.store(out, null); } } From 03a9f2cdd4450e10b4af1012611e7d52ac68d21f Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 14:32:25 -0500 Subject: [PATCH 04/14] wip --- .../shared/archiver/PomPropertiesUtil.java | 1 + .../archiver/PomPropertiesUtilTest.java | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index f60babd..32a1f1d 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -105,6 +105,7 @@ public void createPomProperties( // CHECKSTYLE_OFF: ParameterNumber public void createPomProperties( + // TODO the session isn't needed here Session session, String groupId, String artifactId, diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java new file mode 100644 index 0000000..3ec4bb7 --- /dev/null +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.shared.archiver; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +class PomPropertiesUtilTest { + + + @Test + void testCreatePomProperties() { + PomPropertiesUtil util = new PomPropertiesUtil(); + util.createPomProperties( + null, + "org.foo", + "bar", + "2.1.5", + archiver, + customPomPropertiesFile, + pomPropertiesFile, + true); + } +} From f66503b4fab55d8ec8cbdb8c257c108e5a482d93 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 16:12:21 -0500 Subject: [PATCH 05/14] Add test --- .../shared/archiver/PomPropertiesUtil.java | 30 +++++++++++++-- .../archiver/PomPropertiesUtilTest.java | 38 +++++++++++++++---- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 32a1f1d..a262b0d 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -21,9 +21,19 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.Set; +import java.util.TreeMap; import org.apache.maven.api.Project; import org.apache.maven.api.Session; @@ -54,18 +64,30 @@ private boolean sameContents(Properties props, Path file) throws IOException { return fileProps.equals(props); } - private void createPropertiesFile(Properties properties, Path outputFile, boolean forceCreation) + private void createPropertiesFile(Properties unsortedProperties, Path outputFile, boolean forceCreation) throws IOException { Path outputDir = outputFile.getParent(); if (outputDir != null && !Files.isDirectory(outputDir)) { Files.createDirectories(outputDir); } - if (!forceCreation && sameContents(properties, outputFile)) { + if (!forceCreation && sameContents(unsortedProperties, outputFile)) { return; } - try (OutputStream out = Files.newOutputStream(outputFile)) { - properties.store(out, null); + // For reproducible builds, sort the properties and drop comments. + // The java.util.Properties class doesn't guarantee order so we have + // to write the file using a Writer. + Set propertyNames = unsortedProperties.stringPropertyNames(); + List sortedPropertyNames = new ArrayList<>(propertyNames); + Collections.sort( sortedPropertyNames ); + + try ( Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { + for (String key : sortedPropertyNames) { + out.write( key ); + out.write( ": " ); + out.write( unsortedProperties.getProperty( key ) ); + out.write( '\n' ); + } } } diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index 3ec4bb7..2a337da 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -18,27 +18,51 @@ */ package org.apache.maven.shared.archiver; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; import java.util.Map; +import java.util.Properties; +import org.apache.maven.api.Session; +import org.codehaus.plexus.archiver.jar.JarArchiver; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.CleanupMode; +import org.junit.jupiter.api.io.TempDir; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; +import static org.junit.jupiter.api.Assertions.assertEquals; class PomPropertiesUtilTest { - @Test - void testCreatePomProperties() { + void testCreatePomProperties( @TempDir(cleanup = CleanupMode.ALWAYS) Path tempDirectory) + throws IOException + { PomPropertiesUtil util = new PomPropertiesUtil(); - util.createPomProperties( - null, + Path pomPropertiesFile = tempDirectory.resolve( "bar.properties" ); + util.createPomProperties( (Session) null, "org.foo", "bar", "2.1.5", - archiver, - customPomPropertiesFile, + new JarArchiver(), + null, pomPropertiesFile, true); + + assertThat( pomPropertiesFile ).exists(); + Properties actual = new Properties(); + actual.load( Files.newInputStream(pomPropertiesFile)); + assertEquals("org.foo", actual.getProperty( "groupId" )); + assertEquals("bar", actual.getProperty( "artifactId" )); + assertEquals("2.1.5", actual.getProperty( "version" )); + + // Now read the raw file to check for alphabetical order + List contents = Files.readAllLines( pomPropertiesFile, StandardCharsets.ISO_8859_1 ); + assertEquals("artifactId: bar", contents.get( 0 )); + assertEquals("groupId: org.foo", contents.get( 1 )); + assertEquals("version: 2.1.5", contents.get( 2 )); } } From f925ac4d4a06988e56a69f0b3c2e8fb1d89637c1 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 16:16:04 -0500 Subject: [PATCH 06/14] check size --- .../org/apache/maven/shared/archiver/PomPropertiesUtilTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index 2a337da..1b3f3e2 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -64,5 +64,6 @@ void testCreatePomProperties( @TempDir(cleanup = CleanupMode.ALWAYS) Path tempDi assertEquals("artifactId: bar", contents.get( 0 )); assertEquals("groupId: org.foo", contents.get( 1 )); assertEquals("version: 2.1.5", contents.get( 2 )); + assertEquals( 3, contents.size() ); } } From 953ac668cc01ffb2dc89d30cc79bf82bba7142b2 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 16:16:46 -0500 Subject: [PATCH 07/14] comment --- .../org/apache/maven/shared/archiver/PomPropertiesUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index 1b3f3e2..4f9d645 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -59,7 +59,7 @@ void testCreatePomProperties( @TempDir(cleanup = CleanupMode.ALWAYS) Path tempDi assertEquals("bar", actual.getProperty( "artifactId" )); assertEquals("2.1.5", actual.getProperty( "version" )); - // Now read the raw file to check for alphabetical order + // Now read the file directly to check for alphabetical order List contents = Files.readAllLines( pomPropertiesFile, StandardCharsets.ISO_8859_1 ); assertEquals("artifactId: bar", contents.get( 0 )); assertEquals("groupId: org.foo", contents.get( 1 )); From 24111c598ebc0e3b6b92780d1288c3f0303ce699 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 16:17:03 -0500 Subject: [PATCH 08/14] spotless --- .../shared/archiver/PomPropertiesUtil.java | 17 +++------ .../archiver/PomPropertiesUtilTest.java | 37 +++++++------------ 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index a262b0d..266d9ce 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -20,20 +20,15 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Properties; import java.util.Set; -import java.util.TreeMap; import org.apache.maven.api.Project; import org.apache.maven.api.Session; @@ -79,14 +74,14 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile // to write the file using a Writer. Set propertyNames = unsortedProperties.stringPropertyNames(); List sortedPropertyNames = new ArrayList<>(propertyNames); - Collections.sort( sortedPropertyNames ); + Collections.sort(sortedPropertyNames); - try ( Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { + try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { for (String key : sortedPropertyNames) { - out.write( key ); - out.write( ": " ); - out.write( unsortedProperties.getProperty( key ) ); - out.write( '\n' ); + out.write(key); + out.write(": "); + out.write(unsortedProperties.getProperty(key)); + out.write('\n'); } } } diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index 4f9d645..ed30457 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -23,7 +23,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.Map; import java.util.Properties; import org.apache.maven.api.Session; @@ -38,32 +37,24 @@ class PomPropertiesUtilTest { @Test - void testCreatePomProperties( @TempDir(cleanup = CleanupMode.ALWAYS) Path tempDirectory) - throws IOException - { + void testCreatePomProperties(@TempDir(cleanup = CleanupMode.ALWAYS) Path tempDirectory) throws IOException { PomPropertiesUtil util = new PomPropertiesUtil(); - Path pomPropertiesFile = tempDirectory.resolve( "bar.properties" ); - util.createPomProperties( (Session) null, - "org.foo", - "bar", - "2.1.5", - new JarArchiver(), - null, - pomPropertiesFile, - true); + Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); + util.createPomProperties( + (Session) null, "org.foo", "bar", "2.1.5", new JarArchiver(), null, pomPropertiesFile, true); - assertThat( pomPropertiesFile ).exists(); + assertThat(pomPropertiesFile).exists(); Properties actual = new Properties(); - actual.load( Files.newInputStream(pomPropertiesFile)); - assertEquals("org.foo", actual.getProperty( "groupId" )); - assertEquals("bar", actual.getProperty( "artifactId" )); - assertEquals("2.1.5", actual.getProperty( "version" )); + actual.load(Files.newInputStream(pomPropertiesFile)); + assertEquals("org.foo", actual.getProperty("groupId")); + assertEquals("bar", actual.getProperty("artifactId")); + assertEquals("2.1.5", actual.getProperty("version")); // Now read the file directly to check for alphabetical order - List contents = Files.readAllLines( pomPropertiesFile, StandardCharsets.ISO_8859_1 ); - assertEquals("artifactId: bar", contents.get( 0 )); - assertEquals("groupId: org.foo", contents.get( 1 )); - assertEquals("version: 2.1.5", contents.get( 2 )); - assertEquals( 3, contents.size() ); + List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); + assertEquals("artifactId: bar", contents.get(0)); + assertEquals("groupId: org.foo", contents.get(1)); + assertEquals("version: 2.1.5", contents.get(2)); + assertEquals(3, contents.size()); } } From 6504b37c293281537d0c1c296b65482bef8be7ae Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 17:55:49 -0500 Subject: [PATCH 09/14] revert ManifestConfiguration.java --- .../apache/maven/shared/archiver/ManifestConfiguration.java | 2 +- .../org/apache/maven/shared/archiver/PomPropertiesUtil.java | 2 +- .../apache/maven/shared/archiver/PomPropertiesUtilTest.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/ManifestConfiguration.java b/src/main/java/org/apache/maven/shared/archiver/ManifestConfiguration.java index 22c3ae4..e1cd9b3 100644 --- a/src/main/java/org/apache/maven/shared/archiver/ManifestConfiguration.java +++ b/src/main/java/org/apache/maven/shared/archiver/ManifestConfiguration.java @@ -246,7 +246,7 @@ public void setPackageName(String packageName) { public String getClasspathPrefix() { String cpp = classpathPrefix.replaceAll("\\\\", "/"); - if (!cpp.isEmpty() && !cpp.endsWith("/")) { + if (cpp.length() != 0 && !cpp.endsWith("/")) { cpp += "/"; } diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 266d9ce..5103433 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -79,7 +79,7 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { for (String key : sortedPropertyNames) { out.write(key); - out.write(": "); + out.write("="); out.write(unsortedProperties.getProperty(key)); out.write('\n'); } diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index ed30457..de2648b 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -52,9 +52,9 @@ void testCreatePomProperties(@TempDir(cleanup = CleanupMode.ALWAYS) Path tempDir // Now read the file directly to check for alphabetical order List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); - assertEquals("artifactId: bar", contents.get(0)); - assertEquals("groupId: org.foo", contents.get(1)); - assertEquals("version: 2.1.5", contents.get(2)); + assertEquals("artifactId=bar", contents.get(0)); + assertEquals("groupId=org.foo", contents.get(1)); + assertEquals("version=2.1.5", contents.get(2)); assertEquals(3, contents.size()); } } From 2076c712ad87df8ab99cc5ffbcecd1f0d4ac66b9 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 26 Nov 2024 18:25:48 -0500 Subject: [PATCH 10/14] Unicode escape --- pom.xml | 6 ++++ .../shared/archiver/PomPropertiesUtil.java | 10 +++++-- .../archiver/PomPropertiesUtilTest.java | 30 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index ddd2eef..abcc2e3 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,12 @@ ${slf4jVersion} + + org.apache.commons + commons-text + 1.12.0 + + diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 5103433..f0e4983 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -30,6 +30,7 @@ import java.util.Properties; import java.util.Set; +import org.apache.commons.text.StringEscapeUtils; import org.apache.maven.api.Project; import org.apache.maven.api.Session; import org.codehaus.plexus.archiver.Archiver; @@ -78,14 +79,19 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { for (String key : sortedPropertyNames) { - out.write(key); + out.write(escape(key)); out.write("="); - out.write(unsortedProperties.getProperty(key)); + out.write(escape(unsortedProperties.getProperty(key))); out.write('\n'); } } } + private static String escape(String s) { + String escaped = StringEscapeUtils.escapeJava(s); + return escaped; + } + /** * Creates the pom.properties file. * diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index de2648b..a2838c7 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -28,7 +28,6 @@ import org.apache.maven.api.Session; import org.codehaus.plexus.archiver.jar.JarArchiver; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; import static org.assertj.core.api.Assertions.assertThat; @@ -36,9 +35,13 @@ class PomPropertiesUtilTest { + private PomPropertiesUtil util = new PomPropertiesUtil(); + + @TempDir + Path tempDirectory; + @Test - void testCreatePomProperties(@TempDir(cleanup = CleanupMode.ALWAYS) Path tempDirectory) throws IOException { - PomPropertiesUtil util = new PomPropertiesUtil(); + void testCreatePomProperties() throws IOException { Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); util.createPomProperties( (Session) null, "org.foo", "bar", "2.1.5", new JarArchiver(), null, pomPropertiesFile, true); @@ -57,4 +60,25 @@ void testCreatePomProperties(@TempDir(cleanup = CleanupMode.ALWAYS) Path tempDir assertEquals("version=2.1.5", contents.get(2)); assertEquals(3, contents.size()); } + + @Test + void testUnicodeEscape() throws IOException { + Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); + util.createPomProperties( + (Session) null, "org.foo", "こんにちは", "2.1.5", new JarArchiver(), null, pomPropertiesFile, true); + + assertThat(pomPropertiesFile).exists(); + Properties actual = new Properties(); + actual.load(Files.newInputStream(pomPropertiesFile)); + assertEquals("org.foo", actual.getProperty("groupId")); + assertEquals("こんにちは", actual.getProperty("artifactId")); + assertEquals("2.1.5", actual.getProperty("version")); + + // Now read the file directly to check for alphabetical order and encoding + List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); + assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(0)); + assertEquals("groupId=org.foo", contents.get(1)); + assertEquals("version=2.1.5", contents.get(2)); + assertEquals(3, contents.size()); + } } From b4ed34c92fafa0320e4ad4c5e3c96bab818157ab Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 27 Nov 2024 04:42:54 -0500 Subject: [PATCH 11/14] escape whitespace --- .../shared/archiver/PomPropertiesUtil.java | 21 ++++++++++-- .../archiver/PomPropertiesUtilTest.java | 33 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index f0e4983..774cd4d 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -27,10 +27,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Properties; import java.util.Set; -import org.apache.commons.text.StringEscapeUtils; import org.apache.maven.api.Project; import org.apache.maven.api.Session; import org.codehaus.plexus.archiver.Archiver; @@ -88,8 +88,23 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile } private static String escape(String s) { - String escaped = StringEscapeUtils.escapeJava(s); - return escaped; + StringBuilder sb = new StringBuilder(s.length()); + for (char c : s.toCharArray()) { + if (Character.isWhitespace(c) || c == '#' || c == '!' || c == '=' || c == ':') { // backslash escape + sb.append('\\'); + sb.append(c); + } else if (c < 256) { // 8859-1 + sb.append(c); + } else { + sb.append("\\u"); + String hexString = Integer.toHexString(c).toUpperCase(Locale.ENGLISH); + while (hexString.length() < 4) { + hexString = '0' + hexString; + } + sb.append(hexString); + } + } + return sb.toString(); } /** diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index a2838c7..946c91b 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -19,6 +19,7 @@ package org.apache.maven.shared.archiver; import java.io.IOException; +import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -81,4 +82,36 @@ void testUnicodeEscape() throws IOException { assertEquals("version=2.1.5", contents.get(2)); assertEquals(3, contents.size()); } + + @Test + void testWhitespaceEscape() throws IOException { + Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); + Path customPomPropertiesFile = tempDirectory.resolve("custom.properties"); + try (Writer out = Files.newBufferedWriter(customPomPropertiesFile, StandardCharsets.ISO_8859_1)) { + out.write("a\\u0020key\\u0020with\\u0009whitespace=value\\u0020with\\u0009whitespace\n"); + } + + util.createPomProperties( + (Session) null, + "org.foo", + "こんにちは", + "2.1.5", + new JarArchiver(), + customPomPropertiesFile, + pomPropertiesFile, + true); + assertThat(pomPropertiesFile).exists(); + + Properties actual = new Properties(); + actual.load(Files.newInputStream(pomPropertiesFile)); + assertEquals("value with\twhitespace", actual.getProperty("a key with\twhitespace")); + + // Now read the file directly to check for alphabetical order and encoding + List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); + assertEquals(4, contents.size()); + assertEquals("a\\ key\\ with\\\twhitespace=value\\ with\\\twhitespace", contents.get(0)); + assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(1)); + assertEquals("groupId=org.foo", contents.get(2)); + assertEquals("version=2.1.5", contents.get(3)); + } } From f04cb5634e4a37b55c59f92ca497efae2db55120 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 27 Nov 2024 04:43:51 -0500 Subject: [PATCH 12/14] remove dependency on commons-text --- pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pom.xml b/pom.xml index abcc2e3..ddd2eef 100644 --- a/pom.xml +++ b/pom.xml @@ -86,12 +86,6 @@ ${slf4jVersion} - - org.apache.commons - commons-text - 1.12.0 - - From 6bed28b47b5a628a8031bf2ec2c6e0de108f02a8 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 27 Nov 2024 06:36:05 -0500 Subject: [PATCH 13/14] escapeValue --- .../shared/archiver/PomPropertiesUtil.java | 50 +++++++++++++++---- .../archiver/PomPropertiesUtilTest.java | 10 +++- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 774cd4d..439771a 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -79,29 +79,59 @@ private void createPropertiesFile(Properties unsortedProperties, Path outputFile try (Writer out = Files.newBufferedWriter(outputFile, StandardCharsets.ISO_8859_1)) { for (String key : sortedPropertyNames) { - out.write(escape(key)); + out.write(escapeKey(key)); out.write("="); - out.write(escape(unsortedProperties.getProperty(key))); + out.write(escapeValue(unsortedProperties.getProperty(key))); out.write('\n'); } } } - private static String escape(String s) { + private static String escapeKey(String s) { StringBuilder sb = new StringBuilder(s.length()); for (char c : s.toCharArray()) { - if (Character.isWhitespace(c) || c == '#' || c == '!' || c == '=' || c == ':') { // backslash escape + if (Character.isWhitespace(c) + || c == '#' + || c == '!' + || c == '=' + || c == ':' + || c == '\\') { // backslash escape sb.append('\\'); sb.append(c); } else if (c < 256) { // 8859-1 sb.append(c); } else { - sb.append("\\u"); - String hexString = Integer.toHexString(c).toUpperCase(Locale.ENGLISH); - while (hexString.length() < 4) { - hexString = '0' + hexString; - } - sb.append(hexString); + sb.append(hexEncode(c)); + } + } + return sb.toString(); + } + + private static String hexEncode(char c) { + String hexString = "\\u" + Integer.toHexString(c).toUpperCase(Locale.ENGLISH); + while (hexString.length() < 4) { + hexString = '0' + hexString; + } + return hexString; + } + + private static String escapeValue(String s) { + StringBuilder sb = new StringBuilder(s.length()); + boolean atBeginning = true; + for (char c : s.toCharArray()) { + if (Character.isWhitespace(c) && atBeginning) { + sb.append('\\'); + sb.append(c); + } else if (c == '#' || c == '!' || c == '=' || c == ':' || c == '\\') { // backslash escape + sb.append('\\'); + sb.append(c); + atBeginning = false; + } else if (c < 256) { // 8859-1 + sb.append(c); + atBeginning = false; + } else { + sb.append(hexEncode(c)); + atBeginning = false; } } return sb.toString(); diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index 946c91b..d8191c6 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -89,6 +89,8 @@ void testWhitespaceEscape() throws IOException { Path customPomPropertiesFile = tempDirectory.resolve("custom.properties"); try (Writer out = Files.newBufferedWriter(customPomPropertiesFile, StandardCharsets.ISO_8859_1)) { out.write("a\\u0020key\\u0020with\\u0009whitespace=value\\u0020with\\u0009whitespace\n"); + out.write("zkey=value with \\\\ not at end of line\n"); + out.write("ykey=\\tvalue with whitespace at beginning\n"); } util.createPomProperties( @@ -105,13 +107,17 @@ void testWhitespaceEscape() throws IOException { Properties actual = new Properties(); actual.load(Files.newInputStream(pomPropertiesFile)); assertEquals("value with\twhitespace", actual.getProperty("a key with\twhitespace")); + assertEquals("value with \\ not at end of line", actual.getProperty("zkey")); + assertEquals("\tvalue with whitespace at beginning", actual.getProperty("ykey")); // Now read the file directly to check for alphabetical order and encoding List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); - assertEquals(4, contents.size()); - assertEquals("a\\ key\\ with\\\twhitespace=value\\ with\\\twhitespace", contents.get(0)); + assertEquals(6, contents.size()); + assertEquals("a\\ key\\ with\\\twhitespace=value with\twhitespace", contents.get(0)); assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(1)); assertEquals("groupId=org.foo", contents.get(2)); assertEquals("version=2.1.5", contents.get(3)); + assertEquals("ykey=\\\tvalue with whitespace at beginning", contents.get(4)); + assertEquals("zkey=value with \\\\ not at end of line", contents.get(5)); } } From 8024b6faabb79a500d0e1f2dbe1f7260e7539fb6 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 27 Nov 2024 07:13:31 -0500 Subject: [PATCH 14/14] hex encoding --- .../shared/archiver/PomPropertiesUtil.java | 20 +++++++++---------- .../archiver/PomPropertiesUtilTest.java | 9 ++++++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java index 439771a..131ea14 100644 --- a/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java +++ b/src/main/java/org/apache/maven/shared/archiver/PomPropertiesUtil.java @@ -98,7 +98,7 @@ private static String escapeKey(String s) { || c == '\\') { // backslash escape sb.append('\\'); sb.append(c); - } else if (c < 256) { // 8859-1 + } else if (c < 128) { // ASCII sb.append(c); } else { sb.append(hexEncode(c)); @@ -107,14 +107,6 @@ private static String escapeKey(String s) { return sb.toString(); } - private static String hexEncode(char c) { - String hexString = "\\u" + Integer.toHexString(c).toUpperCase(Locale.ENGLISH); - while (hexString.length() < 4) { - hexString = '0' + hexString; - } - return hexString; - } - private static String escapeValue(String s) { StringBuilder sb = new StringBuilder(s.length()); boolean atBeginning = true; @@ -126,7 +118,7 @@ private static String escapeValue(String s) { sb.append('\\'); sb.append(c); atBeginning = false; - } else if (c < 256) { // 8859-1 + } else if (c < 128) { // ASCII sb.append(c); atBeginning = false; } else { @@ -137,6 +129,14 @@ private static String escapeValue(String s) { return sb.toString(); } + private static String hexEncode(char c) { + String hexString = Integer.toHexString(c).toUpperCase(Locale.ENGLISH); + while (hexString.length() < 4) { + hexString = '0' + hexString; + } + return "\\u" + hexString; + } + /** * Creates the pom.properties file. * diff --git a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java index d8191c6..c1dc57e 100644 --- a/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java +++ b/src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java @@ -91,6 +91,7 @@ void testWhitespaceEscape() throws IOException { out.write("a\\u0020key\\u0020with\\u0009whitespace=value\\u0020with\\u0009whitespace\n"); out.write("zkey=value with \\\\ not at end of line\n"); out.write("ykey=\\tvalue with whitespace at beginning\n"); + out.write("xkey=\\u00E9\\u00FC\\u00E5\n"); } util.createPomProperties( @@ -109,15 +110,17 @@ void testWhitespaceEscape() throws IOException { assertEquals("value with\twhitespace", actual.getProperty("a key with\twhitespace")); assertEquals("value with \\ not at end of line", actual.getProperty("zkey")); assertEquals("\tvalue with whitespace at beginning", actual.getProperty("ykey")); + assertEquals("éüå", actual.getProperty("xkey")); // Now read the file directly to check for alphabetical order and encoding List contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); - assertEquals(6, contents.size()); + assertEquals(7, contents.size()); assertEquals("a\\ key\\ with\\\twhitespace=value with\twhitespace", contents.get(0)); assertEquals("artifactId=\\u3053\\u3093\\u306B\\u3061\\u306F", contents.get(1)); assertEquals("groupId=org.foo", contents.get(2)); assertEquals("version=2.1.5", contents.get(3)); - assertEquals("ykey=\\\tvalue with whitespace at beginning", contents.get(4)); - assertEquals("zkey=value with \\\\ not at end of line", contents.get(5)); + assertEquals("xkey=\\u00E9\\u00FC\\u00E5", contents.get(4)); + assertEquals("ykey=\\\tvalue with whitespace at beginning", contents.get(5)); + assertEquals("zkey=value with \\\\ not at end of line", contents.get(6)); } }