Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.assertj.core.api.Assertions;
import org.editorconfig.plugin.maven.model.EndOfLine;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* TODO add integration tests with real files
*/
class BufferedInputStreamTest {

@Test
void test_readLine_LF() throws IOException {
void getEndOfLine_3LinesLfLfLf_Lf() throws IOException {
// given
String firstRow = "first row\n";
String secondRow = "second row\n";
Expand All @@ -30,17 +31,18 @@ void test_readLine_LF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.LINE_FEED, firstEndOfLine);
assertEquals(EndOfLine.LINE_FEED, secondEndOfLine);
assertEquals(EndOfLine.LINE_FEED, thirdEndOfLine);
}

@Test
void test_readLine_LF_lastLineIsEOFEnded() throws IOException {
void getEndOfLine_3LinesLfLfEof_Eof() throws IOException {
// given
String firstRow = "first row\n";
String secondRow = "second row\n";
Expand All @@ -52,17 +54,18 @@ void test_readLine_LF_lastLineIsEOFEnded() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.LINE_FEED, firstEndOfLine);
assertEquals(EndOfLine.LINE_FEED, secondEndOfLine);
assertEquals(EndOfLine.EOF, thirdEndOfLine);
}

@Test
void test_readLine_CRLF() throws IOException {
void getEndOfLine_3LinesCrlfCrlfCrlf_Crlf() throws IOException {
// given
String firstRow = "first row\r\n";
String secondRow = "second row\r\n";
Expand All @@ -74,17 +77,18 @@ void test_readLine_CRLF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.CARRIAGE_RERUN_LINE_FEED, firstEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN_LINE_FEED, secondEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN_LINE_FEED, thirdEndOfLine);
}

@Test
void test_readLine_CRLF_lastRowEndWithEOF() throws IOException {
void getEndOfLine_3LinesCrlfCrlfEof_Eof() throws IOException {
// given
String firstRow = "first row\r\n";
String secondRow = "second row\r\n";
Expand All @@ -96,17 +100,18 @@ void test_readLine_CRLF_lastRowEndWithEOF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.CARRIAGE_RERUN_LINE_FEED, firstEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN_LINE_FEED, secondEndOfLine);
assertEquals(EndOfLine.EOF, thirdEndOfLine);
}

@Test
void test_readLine_CR() throws IOException {
void getEndOfLine_3LinesCrCrCr_Cr() throws IOException {
// given
String firstRow = "first row\r";
String secondRow = "second row\r";
Expand All @@ -118,17 +123,18 @@ void test_readLine_CR() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.CARRIAGE_RERUN, firstEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN, secondEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN, thirdEndOfLine);
}

@Test
void test_readLine_CR_lastRowEndWithEOF() throws IOException {
void getEndOfLine_3LinesCrCrEof_Eof() throws IOException {
// given
String firstRow = "first row\r";
String secondRow = "second row\r";
Expand All @@ -140,13 +146,14 @@ void test_readLine_CR_lastRowEndWithEOF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
assertEquals(EndOfLine.CARRIAGE_RERUN, firstEndOfLine);
assertEquals(EndOfLine.CARRIAGE_RERUN, secondEndOfLine);
assertEquals(EndOfLine.EOF, thirdEndOfLine);
}

private static ByteArrayInputStream testFromAsciiString(String asciiString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.editorconfig.plugin.maven.common.ByteArrayLineTestUtils.fromUtf16BEString;
import static org.editorconfig.plugin.maven.common.ByteArrayLineTestUtils.fromUtf8String;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit tests for {@link ByteArrayLine}
Expand All @@ -26,25 +27,28 @@
class ByteArrayLineTest {

@Test
void test_endOfLine() {

// when.
void isTheLastLine_2LinesLfEof_FalseTrue() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second =
new ByteArrayLine(new byte[] {'H', 'e', 'l', 'l', 'o'}, 5, EndOfLine.EOF);

// then.
assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
assertThat(first.isTheLastLine()).isFalse();
assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.EOF);
assertThat(second.isTheLastLine()).isTrue();
// when
EndOfLine firstEndOfLine = first.getEndOfLine();
EndOfLine secondEndOfLine = second.getEndOfLine();

// then
assertEquals(EndOfLine.LINE_FEED, firstEndOfLine);
assertEquals(EndOfLine.EOF, secondEndOfLine);

assertFalse(first.isTheLastLine());
assertTrue(second.isTheLastLine());
}

@Test
void test_getContent() {

// when.
void getContentWithEol_3LinesLfCrlfEof_Ok() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second = new ByteArrayLine(
Expand All @@ -54,20 +58,20 @@ void test_getContent() {
ByteArrayLine third = new ByteArrayLine(
new byte[] {'B', 'B', 'B', '\0', '\0', '\0', '\0'}, 3, EndOfLine.EOF);

// then.
assertThat(first.getContentWithEol())
.hasSize(6)
.containsExactly('H', 'e', 'l', 'l', 'o', '\n');
assertThat(second.getContentWithEol())
.hasSize(6)
.containsExactly('A', 'l', 'e', 'x', '\r', '\n');
assertThat(third.getContentWithEol()).hasSize(4).containsExactly('B', 'B', 'B', '\0');
// when
byte[] firstContent = first.getContentWithEol();
byte[] secondContent = second.getContentWithEol();
byte[] thirdContent = third.getContentWithEol();

// then
assertThat(firstContent).hasSize(6).containsExactly('H', 'e', 'l', 'l', 'o', '\n');
assertThat(secondContent).hasSize(6).containsExactly('A', 'l', 'e', 'x', '\r', '\n');
assertThat(thirdContent).hasSize(4).containsExactly('B', 'B', 'B', '\0');
}

@Test
void test_lengthWithEol() {

// when.
void lengthWithEol_3LinesLfCrlfEof_Ok() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second = new ByteArrayLine(
Expand All @@ -77,33 +81,36 @@ void test_lengthWithEol() {
ByteArrayLine third = new ByteArrayLine(
new byte[] {'B', 'B', 'B', '\0', '\0', '\0', '\0'}, 3, EndOfLine.EOF);

// then.
assertThat(first.lengthWithEoL()).isEqualTo(6);
assertThat(second.lengthWithEoL()).isEqualTo(6);
assertThat(third.lengthWithEoL()).isEqualTo(4);
// then
assertEquals(6, first.lengthWithEoL());
assertEquals(6, second.lengthWithEoL());
assertEquals(4, third.lengthWithEoL());
}

@ParameterizedTest
@MethodSource("test_isEmpty")
void test_isEmpty(String string, EndOfLine endOfLine, boolean expectedIsEmpty) {

// when.
boolean actualIsEmpty = new ByteArrayLine(
string.getBytes(US_ASCII),
string.length() - endOfLine.getLengthInBytes(),
endOfLine)
.isEmpty();

// then.
assertThat(actualIsEmpty).isEqualTo(expectedIsEmpty);
void isEmpty_ListOfStrings_Ok(String string, EndOfLine endOfLine, boolean expected) {
// given
ByteArrayLine line = new ByteArrayLine(
string.getBytes(US_ASCII),
string.length() - endOfLine.getLengthInBytes(),
endOfLine);

// when
boolean actual = line.isEmpty();

// then
assertEquals(expected, actual);
}

@ParameterizedTest
@MethodSource("test_getIndent")
void test_getIndent(ByteArrayLine line, Charset charset, int expectedIndent) {
int indent = line.getIndent(2, charset);
void getIndent_ListOfStrings_Ok(ByteArrayLine line, Charset charset, int expected) {
// when
int actual = line.getIndent(2, charset);

assertThat(indent).isEqualTo(expectedIndent);
// then
assertEquals(expected, actual);
}

static Stream<Arguments> test_getIndent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class CachingInputStreamTest {

@Test
void test_readAllBytes_twice() throws IOException {
void reset_readAllBytesTwice_areEqual() throws IOException {
CachingInputStream cachingInputStream = fromFile("caching_input_stream/SomeClass.java");

byte[] first = cachingInputStream.readAllBytes();
Expand All @@ -27,7 +27,7 @@ void test_readAllBytes_twice() throws IOException {
}

@Test
void test_BufferedInputStream() throws IOException {
void reset_writeBytes_areEqual() throws IOException {
CachingInputStream cachingInputStream = fromFile("caching_input_stream/SimpleRecord.java");

BufferedInputStream bufferedInputStream = new BufferedInputStream(cachingInputStream);
Expand Down