From b19b7e027085fb215f8f510d7bef8033ecbd4d48 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 18 Oct 2024 12:49:22 +0000 Subject: [PATCH] [MDEP-930] Make test robust against platform default character sets (#448) --- .../plugins/dependency/tree/TestTreeMojo.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java index 003f57c66..b1c756786 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java @@ -23,14 +23,14 @@ import javax.json.JsonObject; import javax.json.JsonReader; -import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.StringReader; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -57,6 +57,7 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { /* * @see org.apache.maven.plugin.testing.AbstractMojoTestCase#setUp() */ + @Override protected void setUp() throws Exception { // required for mojo lookups to work super.setUp("tree", false); @@ -225,10 +226,11 @@ public void testTreeJsonParsing() throws Exception { */ private List runTreeMojo(String outputFile, String format) throws Exception { File testPom = new File(getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml"); - String outputFileName = testDir.getAbsolutePath() + outputFile; + Path outputFilePath = Paths.get(testDir.getAbsolutePath() + outputFile); TreeMojo mojo = (TreeMojo) lookupMojo("tree", testPom); + setVariableValueToObject(mojo, "outputEncoding", "UTF-8"); setVariableValueToObject(mojo, "outputType", format); - setVariableValueToObject(mojo, "outputFile", new File(outputFileName)); + setVariableValueToObject(mojo, "outputFile", outputFilePath.toFile()); assertNotNull(mojo); assertNotNull(mojo.getProject()); @@ -244,14 +246,7 @@ private List runTreeMojo(String outputFile, String format) throws Except mojo.execute(); - BufferedReader fp1 = new BufferedReader(new FileReader(outputFileName)); - List contents = new ArrayList<>(); - - String line; - while ((line = fp1.readLine()) != null) { - contents.add(line); - } - fp1.close(); + List contents = Files.readAllLines(outputFilePath); return contents; }