Skip to content

Commit

Permalink
Deprecate redundant methods in Files
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed May 15, 2022
1 parent 0033b9f commit 3e5588a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 34 deletions.
4 changes: 2 additions & 2 deletions testng-core/src/main/java/org/testng/JarFileUtils.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -76,7 +76,7 @@ private boolean testngXmlExistsInJar(File jarFile, List<String> 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();
}
Expand Down
26 changes: 17 additions & 9 deletions testng-core/src/main/java/org/testng/reporters/Files.java
Expand Up @@ -14,26 +14,30 @@
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 <code>7.6.0</code> */
@Deprecated
public static String readFile(File f) throws IOException {
try (InputStream is = new FileInputStream(f)) {
return readFile(is);
}
}

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 <code>7.6.0</code> */
@Deprecated
public static void writeFile(String string, File f) throws IOException {
f.getParentFile().mkdirs();
try (FileWriter fw = new FileWriter(f);
Expand All @@ -42,6 +46,8 @@ public static void writeFile(String string, File f) throws IOException {
}
}

/** @deprecated - This method stands deprecated as of TestNG <code>7.6.0</code> */
@Deprecated
public static void copyFile(InputStream from, File to) throws IOException {
if (!to.getParentFile().exists()) {
to.getParentFile().mkdirs();
Expand All @@ -57,6 +63,8 @@ public static void copyFile(InputStream from, File to) throws IOException {
}
}

/** @deprecated - This method stands deprecated as of TestNG <code>7.6.0</code> */
@Deprecated
public static String streamToString(InputStream is) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
Expand Down
5 changes: 2 additions & 3 deletions testng-core/src/main/java/org/testng/reporters/jq/Main.java
Expand Up @@ -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);
Expand All @@ -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);
}
}
@@ -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;
Expand All @@ -10,23 +9,17 @@

public class ResultsByClass {
public static final Comparator<ITestResult> METHOD_NAME_COMPARATOR =
new Comparator<ITestResult>() {
Comparator.comparing(arg0 -> arg0.getMethod().getMethodName());

@Override
public int compare(ITestResult arg0, ITestResult arg1) {
return arg0.getMethod().getMethodName().compareTo(arg1.getMethod().getMethodName());
}
};

private ListMultiMap<Class<?>, ITestResult> m_results = Maps.newListMultiMap();
private final ListMultiMap<Class<?>, ITestResult> m_results = Maps.newListMultiMap();

public void addResult(Class<?> c, ITestResult tr) {
m_results.put(c, tr);
}

public List<ITestResult> getResults(Class<?> c) {
List<ITestResult> result = m_results.get(c);
Collections.sort(result, METHOD_NAME_COMPARATOR);
result.sort(METHOD_NAME_COMPARATOR);
return result;
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void ensureInterceptorIsInvokedViaListenersTag(Class<?> testClass) throws
+ "</suite>";
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();
Expand Down
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions testng-core/src/test/java/test/override/OverrideTest.java
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions testng-core/src/test/java/test/yaml/YamlTest.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 3e5588a

Please sign in to comment.