[core] Replace IOUtils with IOUtil

This commit is contained in:
Andreas Dangel 2022-05-13 09:52:13 +02:00
parent 753cb49e40
commit 2887fe3ae0
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
35 changed files with 97 additions and 100 deletions

View File

@ -16,9 +16,8 @@ import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.util.IOUtil;
/**
* Provides a simple filter mechanism to avoid failing to parse an old ruleset,
@ -116,7 +115,7 @@ public class RuleSetFactoryCompatibility {
* @throws IOException if the stream couldn't be read
*/
public Reader filterRuleSetFile(InputStream stream) throws IOException {
byte[] bytes = IOUtils.toByteArray(stream);
byte[] bytes = IOUtil.toByteArray(stream);
String encoding = determineEncoding(bytes);
String ruleset = new String(bytes, encoding);

View File

@ -22,7 +22,6 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.CDATASection;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
@ -37,6 +36,7 @@ import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyDescriptorField;
import net.sourceforge.pmd.properties.PropertyTypeId;
import net.sourceforge.pmd.util.IOUtil;
/**
* This class represents a way to serialize a RuleSet to an XML configuration
@ -62,7 +62,7 @@ public class RuleSetWriter {
}
public void close() {
IOUtils.closeQuietly(outputStream);
IOUtil.closeQuietly(outputStream);
}
public void write(RuleSet ruleSet) {

View File

@ -18,7 +18,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
@ -30,6 +29,7 @@ import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.renderers.RendererFactory;
import net.sourceforge.pmd.util.IOUtil;
public class Formatter {
@ -187,8 +187,8 @@ public class Formatter {
isOnError = false;
} finally {
if (isOnError) {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(writer);
IOUtil.closeQuietly(output);
IOUtil.closeQuietly(writer);
}
}
return writer;

View File

@ -23,8 +23,6 @@ import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FilenameUtils;
import net.sourceforge.pmd.PMDVersion;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSets;
@ -35,6 +33,7 @@ import net.sourceforge.pmd.benchmark.TimedOperation;
import net.sourceforge.pmd.benchmark.TimedOperationCategory;
import net.sourceforge.pmd.cache.internal.ClasspathFingerprinter;
import net.sourceforge.pmd.stat.Metric;
import net.sourceforge.pmd.util.IOUtil;
/**
* Abstract implementation of the analysis cache. Handles all operations, except for persistence.
@ -189,7 +188,7 @@ public abstract class AbstractAnalysisCache implements AnalysisCache {
@Override
public FileVisitResult visitFile(final Path file,
final BasicFileAttributes attrs) throws IOException {
String extension = FilenameUtils.getExtension(file.toString());
String extension = IOUtil.getFilenameExtension(file.toString());
if ("jar".equalsIgnoreCase(extension)) {
fileVisitor.visitFile(file, attrs);
}

View File

@ -13,10 +13,9 @@ import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.util.IOUtil;
/**
* The result of a single file analysis.
@ -45,8 +44,7 @@ public class AnalysisResult {
new BufferedInputStream(Files.newInputStream(sourceFile.toPath())), new Adler32());
) {
// Just read it, the CheckedInputStream will update the checksum on it's own
IOUtils.skipFully(stream, sourceFile.length());
IOUtil.skipFully(stream, sourceFile.length());
return stream.getChecksum().getValue();
} catch (final IOException ignored) {
// We don't really care, if it's unreadable

View File

@ -14,7 +14,7 @@ import java.util.logging.Logger;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.util.IOUtil;
/**
* Base fingerprinter for raw files.
@ -40,7 +40,7 @@ public class RawFileFingerprinter implements ClasspathEntryFingerprinter {
public void fingerprint(URL entry, Checksum checksum) throws IOException {
try (CheckedInputStream inputStream = new CheckedInputStream(entry.openStream(), checksum)) {
// Just read it, the CheckedInputStream will update the checksum on it's own
while (IOUtils.skip(inputStream, Long.MAX_VALUE) == Long.MAX_VALUE) {
while (IOUtil.skipFully(inputStream, Long.MAX_VALUE) == Long.MAX_VALUE) {
// just loop
}
} catch (final FileNotFoundException ignored) {

View File

@ -17,11 +17,10 @@ import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FilenameUtils;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
import net.sourceforge.pmd.util.FileFinder;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.database.DBMSMetadata;
import net.sourceforge.pmd.util.database.DBURI;
import net.sourceforge.pmd.util.database.SourceObject;
@ -93,8 +92,7 @@ public class CPD {
current.add(signature);
}
if (!FilenameUtils.equalsNormalizedOnSystem(file.getAbsoluteFile().getCanonicalPath(),
file.getAbsolutePath())) {
if (!IOUtil.equalsNormalizedPaths(file.getAbsoluteFile().getCanonicalPath(), file.getAbsolutePath())) {
System.err.println("Skipping " + file + " since it appears to be a symlink");
return;
}

View File

@ -14,8 +14,7 @@ import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import net.sourceforge.pmd.util.IOUtil;
public class SourceCode {
@ -112,11 +111,10 @@ public class SourceCode {
@Override
public Reader getReader() throws Exception {
BOMInputStream inputStream = new BOMInputStream(Files.newInputStream(file.toPath()), ByteOrderMark.UTF_8,
ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE);
IOUtil.BomAwareInputStream inputStream = new IOUtil.BomAwareInputStream(Files.newInputStream(file.toPath()));
if (inputStream.hasBOM()) {
encoding = inputStream.getBOMCharsetName();
if (inputStream.hasBom()) {
encoding = inputStream.getBomCharsetName();
}
return new InputStreamReader(inputStream, encoding);
}

View File

@ -21,7 +21,7 @@ import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.util.IOUtil;
/**
* Implementation that handles a Document as a file in the filesystem and receives operations in a sorted manner
@ -159,7 +159,7 @@ public class DocumentFile implements Document, Closeable {
}
private void writeUntilEOF() throws IOException {
IOUtils.copy(reader, writer);
IOUtil.copy(reader, writer);
}
/* package-private */ List<Integer> getLineToOffset() {

View File

@ -15,13 +15,12 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.document.FileCollector;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.util.FileUtil;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.database.DBMSMetadata;
import net.sourceforge.pmd.util.database.DBURI;
import net.sourceforge.pmd.util.database.SourceObject;
@ -157,7 +156,7 @@ public final class FileCollectionUtil {
collector.getReporter().trace("Adding database source object {0}", falseFilePath);
try (Reader sourceCode = dbmsMetadata.getSourceCode(sourceObject)) {
String source = IOUtils.toString(sourceCode);
String source = IOUtil.readToString(sourceCode);
collector.addSourceFile(source, falseFilePath);
} catch (SQLException ex) {
collector.getReporter().warnEx("Cannot get SourceCode for {0} - skipping ...",

View File

@ -12,11 +12,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.datasource.DataSource;
import net.sourceforge.pmd.util.datasource.FileDataSource;
@ -68,7 +67,7 @@ class NioTextFile implements TextFile {
}
try (BufferedReader br = Files.newBufferedReader(path, charset)) {
return IOUtils.toString(br);
return IOUtil.readToString(br);
}
}

View File

@ -11,7 +11,6 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import net.sourceforge.pmd.PMDConfiguration;
@ -27,6 +26,7 @@ import net.sourceforge.pmd.benchmark.TimeTracker;
import net.sourceforge.pmd.benchmark.TimedOperation;
import net.sourceforge.pmd.benchmark.TimedOperationCategory;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.datasource.DataSource;
/**
@ -151,7 +151,7 @@ public abstract class AbstractPMDProcessor {
// in case we analyzed files within Zip Files/Jars, we need to close them after
// the analysis is finished
for (DataSource dataSource : files) {
IOUtils.closeQuietly(dataSource);
IOUtil.closeQuietly(dataSource);
}
}
}

View File

@ -9,8 +9,6 @@ import java.io.Writer;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.cli.PMDParameters;
@ -108,7 +106,7 @@ public abstract class AbstractRenderer extends AbstractPropertySource implements
} catch (IOException e) {
throw new IllegalStateException(e);
} finally {
IOUtils.closeQuietly(writer);
IOUtil.closeQuietly(writer);
}
}

View File

@ -21,13 +21,13 @@ import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.commons.io.output.WriterOutputStream;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.PMDVersion;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.properties.StringProperty;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.StringUtil;
/**
@ -256,7 +256,7 @@ public class XMLRenderer extends AbstractIncrementingRenderer {
public void setWriter(final Writer writer) {
String encoding = getProperty(ENCODING);
// for backwards compatibility, create a OutputStream that writes to the writer.
this.stream = new WriterOutputStream(writer, encoding);
this.stream = IOUtil.fromWriter(writer, encoding);
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
try {

View File

@ -8,10 +8,9 @@ import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.io.comparator.PathFileComparator;
import net.sourceforge.pmd.annotation.InternalApi;
/**
@ -49,7 +48,12 @@ public class FileFinder {
return;
}
Arrays.sort(candidates, PathFileComparator.PATH_INSENSITIVE_COMPARATOR);
Arrays.sort(candidates, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getPath().compareToIgnoreCase(o2.getPath());
}
});
for (File tmp : candidates) {
if (tmp.isDirectory()) {

View File

@ -20,7 +20,6 @@ import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.annotation.InternalApi;
@ -181,7 +180,7 @@ public final class FileUtil {
* @throws IOException if the file couldn't be read
*/
public static String readFilelist(File filelist) throws IOException {
String filePaths = FileUtils.readFileToString(filelist);
String filePaths = IOUtil.readFileToString(filelist);
filePaths = StringUtils.trimToEmpty(filePaths);
filePaths = filePaths.replaceAll("\\r?\\n", ",");
filePaths = filePaths.replaceAll(",+", ",");

View File

@ -8,8 +8,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import org.apache.commons.io.input.ReaderInputStream;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.datasource.internal.AbstractDataSource;
/**
@ -50,7 +49,7 @@ public class ReaderDataSource extends AbstractDataSource {
*/
@Override
public InputStream getInputStream() throws IOException {
return new ReaderInputStream(reader);
return IOUtil.fromReader(reader);
}
/**

View File

@ -9,10 +9,10 @@ import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.ResourceLoader;
public class RuleSetFactoryCompatibilityTest {
@ -48,7 +48,7 @@ public class RuleSetFactoryCompatibilityTest {
InputStream stream = new ByteArrayInputStream(ruleset.getBytes(StandardCharsets.ISO_8859_1));
Reader filtered = rsfc.filterRuleSetFile(stream);
String out = IOUtils.toString(filtered);
String out = IOUtil.readToString(filtered);
Assert.assertFalse(out.contains("notexisting.xml"));
Assert.assertFalse(out.contains("OldDummyBasicMockRule"));
@ -90,7 +90,7 @@ public class RuleSetFactoryCompatibilityTest {
InputStream stream = new ByteArrayInputStream(ruleset.getBytes(StandardCharsets.ISO_8859_1));
Reader filtered = rsfc.filterRuleSetFile(stream);
String out = IOUtils.toString(filtered);
String out = IOUtil.readToString(filtered);
Assert.assertTrue(out.contains("OldDummyBasicMockRule"));
}
@ -112,7 +112,7 @@ public class RuleSetFactoryCompatibilityTest {
+ " <rule ref=\"rulesets/dummy/basic.xml/OldNameOfBasicMockRule\" />\n" + "</ruleset>\n";
InputStream stream = new ByteArrayInputStream(in.getBytes(StandardCharsets.ISO_8859_1));
Reader filtered = rsfc.filterRuleSetFile(stream);
String out = IOUtils.toString(filtered);
String out = IOUtil.readToString(filtered);
Assert.assertFalse(out.contains("notexisting.xml"));
Assert.assertTrue(out.contains("<rule ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\" />"));
@ -136,7 +136,7 @@ public class RuleSetFactoryCompatibilityTest {
+ " <exclude name=\"AnotherOldNameOfBasicMockRule\"/>\n" + " </rule>\n" + "</ruleset>\n";
InputStream stream = new ByteArrayInputStream(in.getBytes(StandardCharsets.ISO_8859_1));
Reader filtered = rsfc.filterRuleSetFile(stream);
String out = IOUtils.toString(filtered);
String out = IOUtil.readToString(filtered);
Assert.assertFalse(out.contains("OldNameOfBasicMockRule"));
Assert.assertTrue(out.contains("<exclude name=\"NewNameOfBasicMockRule\" />"));

View File

@ -22,9 +22,9 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import net.sourceforge.pmd.util.IOUtil;
import net.sourceforge.pmd.util.ResourceLoader;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
@ -123,7 +123,7 @@ public class RuleSetReferenceIdTest {
assertRuleSetReferenceId(true, rulesetUrl, true, null, rulesetUrl, ruleSetReferenceId);
try (InputStream inputStream = ruleSetReferenceId.getInputStream(new ResourceLoader())) {
String loaded = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
String loaded = IOUtil.readToString(inputStream, StandardCharsets.UTF_8);
assertEquals("xyz", loaded);
}
@ -139,8 +139,8 @@ public class RuleSetReferenceIdTest {
String path = "/profiles/export?format=pmd&language=java&name=Sonar%2520way";
String completePath = path + "/DummyBasicMockRule";
String hostpart = "http://localhost:" + wireMockRule.port();
String basicRuleSet = IOUtils
.toString(RuleSetReferenceId.class.getResourceAsStream("/rulesets/dummy/basic.xml"), StandardCharsets.UTF_8);
String basicRuleSet = IOUtil
.readToString(RuleSetReferenceId.class.getResourceAsStream("/rulesets/dummy/basic.xml"), StandardCharsets.UTF_8);
stubFor(head(urlEqualTo(completePath)).willReturn(aResponse().withStatus(404)));
stubFor(head(urlEqualTo(path)).willReturn(aResponse().withStatus(200).withHeader("Content-type", "text/xml")));
@ -152,7 +152,7 @@ public class RuleSetReferenceIdTest {
ruleSetReferenceId);
try (InputStream inputStream = ruleSetReferenceId.getInputStream(new ResourceLoader())) {
String loaded = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
String loaded = IOUtil.readToString(inputStream, StandardCharsets.UTF_8);
assertEquals(basicRuleSet, loaded);
}

View File

@ -22,7 +22,6 @@ import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import net.sourceforge.pmd.Report.ProcessingError;
@ -35,6 +34,7 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.MockRule;
import net.sourceforge.pmd.lang.rule.RuleReference;
import net.sourceforge.pmd.util.CollectionUtil;
import net.sourceforge.pmd.util.IOUtil;
public class RuleSetTest {
@ -582,7 +582,7 @@ public class RuleSetTest {
assertTrue("Should be a RuntimeException", processingError.getError() instanceof RuntimeException);
assertEquals("Wrong filename in processing error",
"net.sourceforge.pmd.RuleSetTest/ruleExceptionShouldBeReported.java",
FilenameUtils.normalize(processingError.getFile(), true));
IOUtil.normalizePath(processingError.getFile()));
assertEquals("There should be a violation", 1, context.getReport().size());
}

View File

@ -11,7 +11,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.Assert;
@ -19,6 +18,8 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import net.sourceforge.pmd.util.IOUtil;
public class PMDTaskTest {
@Rule
@ -74,7 +75,7 @@ public class PMDTaskTest {
buildRule.executeTarget("testWithShortFilenames");
try (InputStream in = new FileInputStream("target/pmd-ant-test.txt")) {
String actual = IOUtils.toString(in, StandardCharsets.UTF_8);
String actual = IOUtil.readToString(in, StandardCharsets.UTF_8);
// remove any trailing newline
actual = actual.replaceAll("\n|\r", "");
Assert.assertEquals("sample.dummy:0:\tSampleXPathRule:\tTest Rule 2", actual);
@ -87,11 +88,11 @@ public class PMDTaskTest {
try (InputStream in = new FileInputStream("target/pmd-ant-xml.xml");
InputStream expectedStream = PMDTaskTest.class.getResourceAsStream("xml/expected-pmd-ant-xml.xml")) {
String actual = IOUtils.toString(in, StandardCharsets.UTF_8);
String actual = IOUtil.readToString(in, StandardCharsets.UTF_8);
actual = actual.replaceFirst("timestamp=\"[^\"]+\"", "timestamp=\"\"");
actual = actual.replaceFirst("\\.xsd\" version=\"[^\"]+\"", ".xsd\" version=\"\"");
String expected = IOUtils.toString(expectedStream, StandardCharsets.UTF_8);
String expected = IOUtil.readToString(expectedStream, StandardCharsets.UTF_8);
expected = expected.replaceFirst("timestamp=\"[^\"]+\"", "timestamp=\"\"");
expected = expected.replaceFirst("\\.xsd\" version=\"[^\"]+\"", ".xsd\" version=\"\"");

View File

@ -20,7 +20,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
@ -33,6 +32,7 @@ import org.junit.rules.TemporaryFolder;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMD.StatusCode;
import net.sourceforge.pmd.junit.JavaUtilLoggingRule;
import net.sourceforge.pmd.util.IOUtil;
/**
*
@ -131,7 +131,7 @@ public class CoreCliTest {
runPmdSuccessfully("--no-cache", "--dir", srcDir, "--rulesets", DUMMY_RULESET, "--report-file", reportFile, "--debug");
assertTrue("Report file should have been created", Files.exists(reportFile));
String reportText = IOUtils.toString(Files.newBufferedReader(reportFile, StandardCharsets.UTF_8));
String reportText = IOUtil.readToString(Files.newBufferedReader(reportFile, StandardCharsets.UTF_8));
assertThat(reportText, not(containsStringIgnoringCase("error")));
}

View File

@ -7,11 +7,11 @@ package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
public class CPDFilelistTest {
@ -28,7 +28,7 @@ public class CPDFilelistTest {
assertEquals(2, paths.size());
Set<String> simpleNames = new HashSet<>();
for (String path : paths) {
simpleNames.add(FilenameUtils.getName(path));
simpleNames.add(Paths.get(path).getFileName().toString());
}
assertTrue(simpleNames.contains("anotherfile.dummy"));
assertTrue(simpleNames.contains("somefile.dummy"));
@ -46,7 +46,7 @@ public class CPDFilelistTest {
assertEquals(2, paths.size());
Set<String> simpleNames = new HashSet<>();
for (String path : paths) {
simpleNames.add(FilenameUtils.getName(path));
simpleNames.add(Paths.get(path).getFileName().toString());
}
assertTrue(simpleNames.contains("anotherfile.dummy"));
assertTrue(simpleNames.contains("somefile.dummy"));

View File

@ -17,10 +17,11 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.util.IOUtil;
public class CpdXsltTest {
/* Sample ant build.xml file. Run with "ant cpdxsl".
@ -49,7 +50,7 @@ public class CpdXsltTest {
transformer.setErrorListener(errorListener);
transformer.transform(cpdReport, result);
String expected = IOUtils.toString(CpdXsltTest.class.getResourceAsStream("ExpectedCpdHtmlReport.html"), StandardCharsets.UTF_8);
String expected = IOUtil.readToString(CpdXsltTest.class.getResourceAsStream("ExpectedCpdHtmlReport.html"), StandardCharsets.UTF_8);
Assert.assertEquals(expected, result.getWriter().toString());
Assert.assertTrue("XSLT errors occured: " + errorListener, errorListener.hasNoErrors());
}

View File

@ -16,12 +16,13 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import net.sourceforge.pmd.util.IOUtil;
public class DocumentFileTest {
private static final String FILE_PATH = "psvm.java";
@ -51,8 +52,8 @@ public class DocumentFileTest {
@Test
public void shouldPreserveNewlines() throws IOException {
final String testFileContent = IOUtils.toString(
DocumentFileTest.class.getResource("ShouldPreserveNewlines.java"), StandardCharsets.UTF_8);
final String testFileContent = IOUtil.readToString(
DocumentFileTest.class.getResourceAsStream("ShouldPreserveNewlines.java"), StandardCharsets.UTF_8);
writeContentToTemporaryFile(testFileContent);
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {

View File

@ -4,11 +4,12 @@
package net.sourceforge.pmd.junit;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.rules.ExternalResource;
/**
@ -63,7 +64,11 @@ public class JavaUtilLoggingRule extends ExternalResource {
*/
public String getLog() {
customLogHandler.flush();
return stream.toString(StandardCharsets.UTF_8);
try {
return stream.toString(StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**

View File

@ -12,7 +12,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.jaxen.Navigator;
import net.sourceforge.pmd.Rule;
@ -29,6 +28,7 @@ import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleChainVisitor;
import net.sourceforge.pmd.util.IOUtil;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.sxpath.IndependentContext;
@ -130,7 +130,7 @@ public class DummyLanguageModule extends BaseLanguageModule {
@Override
public Node parse(String fileName, Reader source) throws ParseException {
try {
String text = IOUtils.toString(source);
String text = IOUtil.readToString(source);
DummyRootNode rootNode = readLispNode(text);
AbstractParser.setFileName(fileName, rootNode);
return rootNode;

View File

@ -10,7 +10,6 @@ import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -26,6 +25,7 @@ import net.sourceforge.pmd.RuleWithProperties;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.util.IOUtil;
public abstract class AbstractRendererTest {
@ -204,6 +204,6 @@ public abstract class AbstractRendererTest {
renderer.renderFileReport(report);
renderer.end();
renderer.flush();
return FileUtils.readFileToString(file, expectedEncoding);
return IOUtil.readFileToString(file, expectedEncoding);
}
}

View File

@ -11,7 +11,6 @@ import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
@ -19,6 +18,7 @@ import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.ConfigurationError;
import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.util.IOUtil;
public class JsonRendererTest extends AbstractRendererTest {
@ -69,7 +69,7 @@ public class JsonRendererTest extends AbstractRendererTest {
private String readFile(String name) {
try (InputStream in = JsonRendererTest.class.getResourceAsStream("json/" + name)) {
return IOUtils.toString(in, StandardCharsets.UTF_8);
return IOUtil.readToString(in, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -10,7 +10,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
@ -18,7 +17,7 @@ import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.util.IOUtil;
public class SarifRendererTest extends AbstractRendererTest {
@ -112,7 +111,7 @@ public class SarifRendererTest extends AbstractRendererTest {
private String readFile(String name) {
try (InputStream in = SarifRendererTest.class.getResourceAsStream("sarif/" + name)) {
String asd = IOUtils.toString(in, StandardCharsets.UTF_8);
String asd = IOUtil.readToString(in, StandardCharsets.UTF_8);
return asd;
} catch (IOException e) {
throw new RuntimeException(e);

Some files were not shown because too many files have changed in this diff Show More