Add test for text files
This commit is contained in:
3 files changed
+115
-2
No files matched your search
@@ -212,7 +212,24 @@ public interface TextFile extends Closeable {
|
||||
*
|
||||
* @throws NullPointerException If any parameter is null
|
||||
*/
|
||||
static TextFileBuilder forReader(Reader reader, String pathId, LanguageVersion languageVersion) {
|
||||
static TextFile forReader(Reader reader, String pathId, LanguageVersion languageVersion) {
|
||||
return builderForReader(reader, pathId, languageVersion).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a read-only builder reading from a reader.
|
||||
* The reader is first read when {@link TextFile#readContents()} is first
|
||||
* called, and is closed when that method exits. Note that this may
|
||||
* only be called once, afterwards, {@link TextFile#readContents()} will
|
||||
* throw an {@link IOException}.
|
||||
*
|
||||
* @param reader Text of the file
|
||||
* @param pathId File name to use as path id
|
||||
* @param languageVersion Language version
|
||||
*
|
||||
* @throws NullPointerException If any parameter is null
|
||||
*/
|
||||
static TextFileBuilder builderForReader(Reader reader, String pathId, LanguageVersion languageVersion) {
|
||||
return new ForReader(languageVersion, reader, pathId);
|
||||
}
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ public class TreeExportCli {
|
||||
throw bail("One of --file or --read-stdin must be mentioned");
|
||||
} else if (readStdin) {
|
||||
io.stderr.println("Reading from stdin...");
|
||||
textFile = TextFile.forReader(readFromSystemIn(), "stdin", langVersion).build();
|
||||
textFile = TextFile.forReader(readFromSystemIn(), "stdin", langVersion);
|
||||
} else {
|
||||
textFile = TextFile.forPath(Paths.get(file), Charset.forName(encoding), langVersion);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.document;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import net.sourceforge.pmd.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
|
||||
public class TextFilesTest {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempDir = TemporaryFolder.builder().build();
|
||||
|
||||
private LanguageVersion dummyVersion = DummyLanguageModule.getInstance().getDefaultVersion();
|
||||
|
||||
@Test
|
||||
public void testNioFile() throws IOException {
|
||||
Path file = makeTmpFile(StandardCharsets.UTF_8, "some content");
|
||||
try (TextFile tf = TextFile.forPath(file, StandardCharsets.UTF_8, dummyVersion)) {
|
||||
Assert.assertEquals(file.toAbsolutePath().toString(), tf.getPathId());
|
||||
Assert.assertEquals(file.toString(), tf.getDisplayName());
|
||||
Assert.assertEquals(dummyVersion, tf.getLanguageVersion());
|
||||
Assert.assertEquals(Chars.wrap("some content"), tf.readContents().getNormalizedText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNioFileBuilder() throws IOException {
|
||||
Path file = makeTmpFile(StandardCharsets.UTF_8, "some content");
|
||||
try (TextFile tf = TextFile.builderForPath(file, StandardCharsets.UTF_8, dummyVersion)
|
||||
.withDisplayName("aname")
|
||||
.build()) {
|
||||
Assert.assertEquals(file.toAbsolutePath().toString(), tf.getPathId());
|
||||
Assert.assertEquals("aname", tf.getDisplayName());
|
||||
Assert.assertEquals(dummyVersion, tf.getLanguageVersion());
|
||||
Assert.assertEquals(Chars.wrap("some content"), tf.readContents().getNormalizedText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNioFileEscape() throws IOException {
|
||||
Path file = makeTmpFile(StandardCharsets.UTF_8, "some\r\ncontent");
|
||||
try (TextFile tf = TextFile.forPath(file, StandardCharsets.UTF_8, dummyVersion)) {
|
||||
Assert.assertEquals(Chars.wrap("some\ncontent"), tf.readContents().getNormalizedText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReaderFileEscape() throws IOException {
|
||||
Path file = makeTmpFile(StandardCharsets.UTF_8, "some\r\ncontent");
|
||||
try (TextFile tf = TextFile.forReader(Files.newBufferedReader(file, StandardCharsets.UTF_8), "filename", dummyVersion)) {
|
||||
Assert.assertEquals("filename", tf.getPathId());
|
||||
Assert.assertEquals("filename", tf.getDisplayName());
|
||||
Assert.assertEquals(dummyVersion, tf.getLanguageVersion());
|
||||
Assert.assertEquals(Chars.wrap("some\ncontent"), tf.readContents().getNormalizedText());
|
||||
Assert.assertThrows(ReadOnlyFileException.class, () -> tf.writeContents(
|
||||
TextFileContent.fromCharSeq("new content")
|
||||
));
|
||||
}
|
||||
}@Test
|
||||
public void testStringFileEscape() throws IOException {
|
||||
try (TextFile tf = TextFile.forCharSeq("cont\r\nents", "filename", dummyVersion)) {
|
||||
Assert.assertEquals("filename", tf.getPathId());
|
||||
Assert.assertEquals("filename", tf.getDisplayName());
|
||||
Assert.assertEquals(dummyVersion, tf.getLanguageVersion());
|
||||
Assert.assertEquals(Chars.wrap("cont\r\nent"), tf.readContents().getNormalizedText());
|
||||
Assert.assertThrows(ReadOnlyFileException.class, () -> tf.writeContents(
|
||||
TextFileContent.fromCharSeq("new content")
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
private @NonNull Path makeTmpFile(Charset charset, String content) throws IOException {
|
||||
Path file = tempDir.newFile().toPath();
|
||||
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
|
||||
IOUtils.write(content, writer);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user