|
| 1 | +package com.baeldung.readlastlines; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Scanner; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import org.apache.commons.io.FileUtils; |
| 18 | +import org.apache.commons.io.input.ReversedLinesFileReader; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +public class ReadLastLinesUnitTest { |
| 22 | + |
| 23 | + private static final String FILE_PATH = "src/test/resources/data.txt"; |
| 24 | + private static final int TOTAL_LINES = 10; |
| 25 | + private static final int LAST_LINES_TO_READ = 3; |
| 26 | + private static final String OUTPUT_TO_VERIFY = "line 8\nline 9\nline 10"; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenFile_whenUsingBufferedReader_thenExtractedLastLinesCorrect() throws IOException { |
| 30 | + try (BufferedReader br = Files.newBufferedReader(Paths.get(FILE_PATH))) { |
| 31 | + for (int i = 0; i < (TOTAL_LINES - LAST_LINES_TO_READ); i++) { |
| 32 | + br.readLine(); |
| 33 | + } |
| 34 | + StringBuilder stringBuilder = new StringBuilder(); |
| 35 | + for (int i = (TOTAL_LINES - LAST_LINES_TO_READ); i < TOTAL_LINES; i++) { |
| 36 | + stringBuilder.append(br.readLine()).append("\n"); |
| 37 | + } |
| 38 | + |
| 39 | + assertEquals(stringBuilder.toString().trim(), OUTPUT_TO_VERIFY); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenFile_whenUsingScanner_thenExtractedLastLinesCorrect() throws IOException { |
| 45 | + try (Scanner scanner = new Scanner(new File(FILE_PATH))) { |
| 46 | + for (int i = 0; i < (TOTAL_LINES - LAST_LINES_TO_READ); i++) { |
| 47 | + scanner.nextLine(); |
| 48 | + } |
| 49 | + StringBuilder stringBuilder = new StringBuilder(); |
| 50 | + for (int i = (TOTAL_LINES - LAST_LINES_TO_READ); i < TOTAL_LINES; i++) { |
| 51 | + stringBuilder.append(scanner.nextLine()).append("\n"); |
| 52 | + } |
| 53 | + |
| 54 | + assertEquals(stringBuilder.toString().trim(), OUTPUT_TO_VERIFY); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenLargeFile_whenUsingFilesAPI_thenExtractedLastLinesCorrect() throws IOException{ |
| 60 | + try (Stream<String> lines = Files.lines(Paths.get(FILE_PATH))) { |
| 61 | + Stream<String> remainingLines = lines.skip(TOTAL_LINES - LAST_LINES_TO_READ); |
| 62 | + |
| 63 | + assertEquals(OUTPUT_TO_VERIFY, remainingLines.collect(Collectors.joining("\n"))); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenFile_whenUsingFileUtils_thenExtractedLastLinesCorrect() throws IOException{ |
| 69 | + File file = new File(FILE_PATH); |
| 70 | + List<String> lines = FileUtils.readLines(file, "UTF-8"); |
| 71 | + StringBuilder stringBuilder = new StringBuilder(); |
| 72 | + for (int i = (TOTAL_LINES - LAST_LINES_TO_READ); i < TOTAL_LINES; i++) { |
| 73 | + stringBuilder.append(lines.get(i)).append("\n"); |
| 74 | + } |
| 75 | + |
| 76 | + assertEquals(OUTPUT_TO_VERIFY, stringBuilder.toString().trim()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenFile_whenUsingReverseFileReader_thenExtractedLastLinesCorrect() throws IOException{ |
| 81 | + File file = new File(FILE_PATH); |
| 82 | + try (ReversedLinesFileReader rlfReader = new ReversedLinesFileReader(file, StandardCharsets.UTF_8)) { |
| 83 | + List<String> lastLines = rlfReader.readLines(LAST_LINES_TO_READ); |
| 84 | + StringBuilder stringBuilder = new StringBuilder(); |
| 85 | + Collections.reverse(lastLines); |
| 86 | + lastLines.forEach( |
| 87 | + line -> stringBuilder.append(line).append("\n") |
| 88 | + ); |
| 89 | + |
| 90 | + assertEquals(OUTPUT_TO_VERIFY, stringBuilder.toString().trim()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments