From ef3b0e93c5c1d7149a027abf0cb71d487abe9822 Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Sun, 15 May 2022 19:11:09 +0530 Subject: [PATCH] Deprecate redundant methods in Files --- .../main/java/org/testng/JarFileUtils.java | 4 +-- .../main/java/org/testng/reporters/Files.java | 26 ++++++++++++------- .../java/org/testng/reporters/jq/Main.java | 5 ++-- .../testng/reporters/jq/ResultsByClass.java | 13 +++------- .../commandline/CommandLineOverridesXml.java | 5 ++-- .../test/dataprovider/InterceptorTest.java | 5 ++-- .../java/test/groups/issue1834/IssueTest.java | 5 ++-- .../test/java/test/override/OverrideTest.java | 5 ++-- .../src/test/java/test/yaml/YamlTest.java | 4 +-- 9 files changed, 38 insertions(+), 34 deletions(-) diff --git a/testng-core/src/main/java/org/testng/JarFileUtils.java b/testng-core/src/main/java/org/testng/JarFileUtils.java index 5452dfae72..86ad2fbbbe 100644 --- a/testng-core/src/main/java/org/testng/JarFileUtils.java +++ b/testng-core/src/main/java/org/testng/JarFileUtils.java @@ -4,6 +4,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.Collection; import java.util.Enumeration; import java.util.List; @@ -12,7 +13,6 @@ import java.util.jar.JarFile; import org.testng.collections.Lists; import org.testng.internal.Utils; -import org.testng.reporters.Files; import org.testng.util.Strings; import org.testng.xml.IPostProcessor; import org.testng.xml.XmlSuite; @@ -76,7 +76,7 @@ private boolean testngXmlExistsInJar(File jarFile, List classes) throws if (Parser.canParse(jeName.toLowerCase())) { InputStream inputStream = jf.getInputStream(je); File copyFile = new File(file, jeName); - Files.copyFile(inputStream, copyFile); + Files.copy(inputStream, copyFile.toPath()); if (matchesXmlPathInJar(je)) { suitePath = copyFile.toString(); } diff --git a/testng-core/src/main/java/org/testng/reporters/Files.java b/testng-core/src/main/java/org/testng/reporters/Files.java index a4f778e6df..8589b76fbb 100644 --- a/testng-core/src/main/java/org/testng/reporters/Files.java +++ b/testng-core/src/main/java/org/testng/reporters/Files.java @@ -14,9 +14,16 @@ import java.io.StringWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; -public class Files { +public final class Files { + private Files() { + // defeat instantiation + } + + /** @deprecated - This method stands deprecated as of TestNG 7.6.0 */ + @Deprecated public static String readFile(File f) throws IOException { try (InputStream is = new FileInputStream(f)) { return readFile(is); @@ -24,16 +31,13 @@ public static String readFile(File f) throws IOException { } public static String readFile(InputStream is) throws IOException { - StringBuilder result = new StringBuilder(); - BufferedReader br = new BufferedReader(new InputStreamReader(is)); - String line = br.readLine(); - while (line != null) { - result.append(line).append("\n"); - line = br.readLine(); - } - return result.toString(); + return new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining("\n")); } + /** @deprecated - This method stands deprecated as of TestNG 7.6.0 */ + @Deprecated public static void writeFile(String string, File f) throws IOException { f.getParentFile().mkdirs(); try (FileWriter fw = new FileWriter(f); @@ -42,6 +46,8 @@ public static void writeFile(String string, File f) throws IOException { } } + /** @deprecated - This method stands deprecated as of TestNG 7.6.0 */ + @Deprecated public static void copyFile(InputStream from, File to) throws IOException { if (!to.getParentFile().exists()) { to.getParentFile().mkdirs(); @@ -57,6 +63,8 @@ public static void copyFile(InputStream from, File to) throws IOException { } } + /** @deprecated - This method stands deprecated as of TestNG 7.6.0 */ + @Deprecated public static String streamToString(InputStream is) throws IOException { if (is != null) { Writer writer = new StringWriter(); diff --git a/testng-core/src/main/java/org/testng/reporters/jq/Main.java b/testng-core/src/main/java/org/testng/reporters/jq/Main.java index 7af3b0f54a..a020810b97 100644 --- a/testng-core/src/main/java/org/testng/reporters/jq/Main.java +++ b/testng-core/src/main/java/org/testng/reporters/jq/Main.java @@ -90,7 +90,7 @@ public void generateReport( if (is == null) { throw new AssertionError("Couldn't find resource: " + fileName); } - Files.copyFile(is, new File(outputDirectory, fileName)); + java.nio.file.Files.copy(is, new File(outputDirectory, fileName).toPath()); } } all = Files.readFile(header); @@ -102,8 +102,7 @@ public void generateReport( } private InputStream load(String fileName) { - String path; - path = Main.TESTNG_RESOURCE_PREFIX + fileName; + String path = Main.TESTNG_RESOURCE_PREFIX + fileName; return getClass().getResourceAsStream(path); } } diff --git a/testng-core/src/main/java/org/testng/reporters/jq/ResultsByClass.java b/testng-core/src/main/java/org/testng/reporters/jq/ResultsByClass.java index 81ca29710b..342a1d42bd 100644 --- a/testng-core/src/main/java/org/testng/reporters/jq/ResultsByClass.java +++ b/testng-core/src/main/java/org/testng/reporters/jq/ResultsByClass.java @@ -1,6 +1,5 @@ package org.testng.reporters.jq; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Set; @@ -10,15 +9,9 @@ public class ResultsByClass { public static final Comparator METHOD_NAME_COMPARATOR = - new Comparator() { + Comparator.comparing(arg0 -> arg0.getMethod().getMethodName()); - @Override - public int compare(ITestResult arg0, ITestResult arg1) { - return arg0.getMethod().getMethodName().compareTo(arg1.getMethod().getMethodName()); - } - }; - - private ListMultiMap, ITestResult> m_results = Maps.newListMultiMap(); + private final ListMultiMap, ITestResult> m_results = Maps.newListMultiMap(); public void addResult(Class c, ITestResult tr) { m_results.put(c, tr); @@ -26,7 +19,7 @@ public void addResult(Class c, ITestResult tr) { public List getResults(Class c) { List result = m_results.get(c); - Collections.sort(result, METHOD_NAME_COMPARATOR); + result.sort(METHOD_NAME_COMPARATOR); return result; } diff --git a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java index c81cd57b97..b647dd01e3 100644 --- a/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java +++ b/testng-core/src/test/java/test/commandline/CommandLineOverridesXml.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -89,8 +90,8 @@ public void ensureNoNullPointerExceptionIsThrown() throws IOException { private static String createTemporarySuiteAndGetItsPath() throws IOException { Path file = Files.createTempFile("testng", ".xml"); - org.testng.reporters.Files.writeFile( - buildSuiteContentThatRefersToInvalidTestClass(), file.toFile()); + Files.write( + file, buildSuiteContentThatRefersToInvalidTestClass().getBytes(StandardCharsets.UTF_8)); return file.toFile().getAbsolutePath(); } diff --git a/testng-core/src/test/java/test/dataprovider/InterceptorTest.java b/testng-core/src/test/java/test/dataprovider/InterceptorTest.java index 7cbb0ad929..8217066281 100644 --- a/testng-core/src/test/java/test/dataprovider/InterceptorTest.java +++ b/testng-core/src/test/java/test/dataprovider/InterceptorTest.java @@ -4,12 +4,13 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Collections; import org.testng.IDataProviderInterceptor; import org.testng.TestNG; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import org.testng.reporters.Files; import test.SimpleBaseTest; import test.TestHelper; import test.dataprovider.issue2111.CountingListener; @@ -61,7 +62,7 @@ public void ensureInterceptorIsInvokedViaListenersTag(Class testClass) throws + ""; xml = String.format(xml, testClass.getName()); File suiteFile = File.createTempFile("testng", ".xml"); - Files.writeFile(xml, suiteFile); + Files.write(suiteFile.toPath(), xml.getBytes(StandardCharsets.UTF_8)); TestNG testng = create(); testng.setTestSuites(Collections.singletonList(suiteFile.getAbsolutePath())); CountingListener counter = new CountingListener(); diff --git a/testng-core/src/test/java/test/groups/issue1834/IssueTest.java b/testng-core/src/test/java/test/groups/issue1834/IssueTest.java index 09cb839075..d510cac6d5 100644 --- a/testng-core/src/test/java/test/groups/issue1834/IssueTest.java +++ b/testng-core/src/test/java/test/groups/issue1834/IssueTest.java @@ -4,10 +4,11 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Collections; import org.testng.TestNG; import org.testng.annotations.Test; -import org.testng.reporters.Files; import test.SimpleBaseTest; import test.TestHelper; @@ -16,7 +17,7 @@ public class IssueTest extends SimpleBaseTest { @Test(description = "GITHUB-1834") public void ensureDependenciesDefinedInSuiteAreHonored() throws IOException { File file = File.createTempFile("1834", ".xml"); - Files.writeFile(asSuite(), file); + Files.write(file.toPath(), asSuite().getBytes(StandardCharsets.UTF_8)); TestNG testng = create(); testng.setTestSuites(Collections.singletonList(file.getAbsolutePath())); OutputGatheringListener listener = new OutputGatheringListener(); diff --git a/testng-core/src/test/java/test/override/OverrideTest.java b/testng-core/src/test/java/test/override/OverrideTest.java index 3d00f31d64..8b6b3670e6 100644 --- a/testng-core/src/test/java/test/override/OverrideTest.java +++ b/testng-core/src/test/java/test/override/OverrideTest.java @@ -2,13 +2,14 @@ import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Collections; import org.testng.Assert; import org.testng.TestListenerAdapter; import org.testng.TestNG; import org.testng.TestNGException; import org.testng.annotations.Test; -import org.testng.reporters.Files; import test.SimpleBaseTest; import test.TestHelper; @@ -26,7 +27,7 @@ private static File createTempFile(String content) { // Delete temp file when program exits. result.deleteOnExit(); - Files.writeFile(content, result); + Files.write(result.toPath(), content.getBytes(StandardCharsets.UTF_8)); return result; } catch (IOException e) { diff --git a/testng-core/src/test/java/test/yaml/YamlTest.java b/testng-core/src/test/java/test/yaml/YamlTest.java index 85d639f373..3d5f6fc33f 100644 --- a/testng-core/src/test/java/test/yaml/YamlTest.java +++ b/testng-core/src/test/java/test/yaml/YamlTest.java @@ -6,6 +6,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.Collection; import java.util.regex.Matcher; @@ -15,7 +16,6 @@ import org.testng.annotations.Test; import org.testng.internal.Yaml; import org.testng.internal.YamlParser; -import org.testng.reporters.Files; import org.testng.xml.SuiteXmlParser; import org.testng.xml.XmlSuite; import org.testng.xml.internal.Parser; @@ -55,7 +55,7 @@ public void testParameterInclusion() throws IOException { assertThat(count).isEqualTo(5); File newSuite = File.createTempFile("suite", ".xml"); newSuite.deleteOnExit(); - Files.writeFile(yaml.toString(), newSuite); + Files.write(newSuite.toPath(), yaml.toString().getBytes(StandardCharsets.UTF_8)); assertThat(parser.parse(newSuite.getAbsolutePath(), new FileInputStream(file), false)) .isEqualTo(xmlSuite); }