-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unused parameters, use CachingWriter, simplify a bit #79
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d859ec8
Remove unused parameters, use CachingWriter, simplify a bit
elharo e5f765e
Make sure we support encoding correctly
gnodet af39927
Add comment
gnodet 1811567
Use String.lines()
gnodet 79ecd96
The call to Files.isDirectory() is not needed
gnodet b5cd991
Use UTF-8, system independent new line, add comments
gnodet 3787873
Fixes
gnodet 8cee0f5
Add test for whitespaces
gnodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/org/apache/maven/shared/archiver/PomPropertiesUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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.io.IOException; | ||
import java.io.Writer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.codehaus.plexus.archiver.jar.JarArchiver; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class PomPropertiesUtilTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an additional test in the latest draft of my PR you can adopt. |
||
|
||
private PomPropertiesUtil util = new PomPropertiesUtil(); | ||
|
||
@TempDir | ||
Path tempDirectory; | ||
|
||
@Test | ||
void testCreatePomProperties() throws IOException { | ||
Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); | ||
util.createPomProperties("org.foo", "bar", "2.1.5", new JarArchiver(), null, pomPropertiesFile); | ||
|
||
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 file directly to check for alphabetical order | ||
List<String> 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()); | ||
} | ||
|
||
@Test | ||
void testUnicodeEscape() throws IOException { | ||
Path pomPropertiesFile = tempDirectory.resolve("bar.properties"); | ||
util.createPomProperties("org.foo", "こんにちは", "2.1.5", new JarArchiver(), null, pomPropertiesFile); | ||
|
||
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<String> 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()); | ||
} | ||
|
||
@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"); | ||
out.write("zkey=value with \\\\ not at end of line\n"); | ||
out.write("ykey=\\tvalue with tab at beginning\n"); | ||
out.write("xkey=\\ value with whitespace at beginning\n"); | ||
out.write("wkey=\\u00E9\\u00FC\\u00E5\n"); | ||
} | ||
|
||
util.createPomProperties( | ||
"org.foo", "こんにちは", "2.1.5", new JarArchiver(), customPomPropertiesFile, pomPropertiesFile); | ||
assertThat(pomPropertiesFile).exists(); | ||
|
||
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 tab at beginning", actual.getProperty("ykey")); | ||
assertEquals(" value with whitespace at beginning", actual.getProperty("xkey")); | ||
assertEquals("éüå", actual.getProperty("wkey")); | ||
|
||
// Now read the file directly to check for alphabetical order and encoding | ||
List<String> contents = Files.readAllLines(pomPropertiesFile, StandardCharsets.ISO_8859_1); | ||
assertEquals(8, 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("wkey=\\u00E9\\u00FC\\u00E5", contents.get(4)); | ||
assertEquals("xkey=\\ value with whitespace at beginning", contents.get(5)); | ||
assertEquals("ykey=\\tvalue with tab at beginning", contents.get(6)); | ||
assertEquals("zkey=value with \\\\ not at end of line", contents.get(7)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're leaning pretty heavily on Hyrum's Law here. I'm not convinced we can rely on the description of current encoding in the Javadoc to apply to future encoding. In particular, I would not be at all surprised to see properties.store write in UTF-8 in a future JDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been this way since JDK 1.2, so 26 years. And for the encoding, the Properties class has been opened to other encodings in JDK 1.5. The default encoding for reading properties has already been changed in JDK 9. The encoding used here is kinda irrelevant (whether ISO-8859-1 or UTF-8) because all non-ascii chars have been transformed into
\uxxxx
sequences, thereby removing everything that is encoding specific.