forked from phoedos/pmd
Merge branch 'pr-2171'
[core] Preserve newlines in DocumentFile
This commit is contained in:
@@ -44,6 +44,7 @@ the implementation based on your feedback.
|
||||
|
||||
* core
|
||||
* [#2006](https://github.com/pmd/pmd/issues/2006): \[core] PMD should warn about multiple instances of the same rule in a ruleset
|
||||
* [#2170](https://github.com/pmd/pmd/issues/2170): \[core] DocumentFile doesn't preserve newlines
|
||||
* java-bestpractices
|
||||
* [#2149](https://github.com/pmd/pmd/issues/2149): \[java] JUnitAssertionsShouldIncludeMessage - False positive with assertEquals and JUnit5
|
||||
* java-codestyle
|
||||
|
@@ -21,6 +21,8 @@ import java.util.Scanner;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
/**
|
||||
* Implementation that handles a Document as a file in the filesystem and receives operations in a sorted manner
|
||||
* (i.e. the regions are sorted). This improves the efficiency of reading the file by only scanning it once while
|
||||
@@ -157,11 +159,7 @@ public class DocumentFile implements Document, Closeable {
|
||||
}
|
||||
|
||||
private void writeUntilEOF() throws IOException {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
writer.write(line);
|
||||
}
|
||||
IOUtils.copy(reader, writer);
|
||||
}
|
||||
|
||||
/* package-private */ List<Integer> getLineToOffset() {
|
||||
|
@@ -16,6 +16,7 @@ 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;
|
||||
@@ -48,6 +49,22 @@ public class DocumentFileTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPreserveNewlines() throws IOException {
|
||||
final String testFileContent = IOUtils.toString(
|
||||
DocumentFileTest.class.getResource("ShouldPreserveNewlines.java"), StandardCharsets.UTF_8);
|
||||
writeContentToTemporaryFile(testFileContent);
|
||||
|
||||
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
|
||||
documentFile.insert(0, 0, "public ");
|
||||
}
|
||||
|
||||
try (FileInputStream stream = new FileInputStream(temporaryFile)) {
|
||||
final String actualContent = new String(readAllBytes(stream));
|
||||
assertEquals("public " + testFileContent, actualContent);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] readAllBytes(final FileInputStream stream) throws IOException {
|
||||
final int defaultBufferSize = 8192;
|
||||
final int maxBufferSize = Integer.MAX_VALUE - 8;
|
||||
|
@@ -0,0 +1,8 @@
|
||||
class ShouldPreserveNewlines {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Test");
|
||||
}
|
||||
}
|
||||
// note: multiple empty lines at the end
|
||||
|
||||
|
Reference in New Issue
Block a user