|
| 1 | +package com.baeldung.linenumber; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.apache.commons.io.IOUtils; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Scanner; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | + |
| 20 | +public class LineAtGivenNumberUnitTest { |
| 21 | + |
| 22 | + private static final String FILE_PATH = "src/test/resources/linesInput.txt"; |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenFile_whenUsingBufferedReader_thenExtractedLineIsCorrect() throws IOException { |
| 26 | + try (BufferedReader br = Files.newBufferedReader(Paths.get(FILE_PATH))) { |
| 27 | + for (int i = 0; i < 3; i++) { |
| 28 | + br.readLine(); |
| 29 | + } |
| 30 | + |
| 31 | + String extractedLine = br.readLine(); |
| 32 | + assertEquals("Line 4", extractedLine); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenFile_whenUsingScanner_thenExtractedLineIsCorrect() throws IOException { |
| 38 | + try (Scanner scanner = new Scanner(new File(FILE_PATH))) { |
| 39 | + for (int i = 0; i < 3; i++) { |
| 40 | + scanner.nextLine(); |
| 41 | + } |
| 42 | + |
| 43 | + String extractedLine = scanner.nextLine(); |
| 44 | + assertEquals("Line 4", extractedLine); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenSmallFile_whenUsingFilesAPI_thenExtractedLineIsCorrect() throws IOException { |
| 50 | + String extractedLine = Files.readAllLines(Paths.get(FILE_PATH)).get(4); |
| 51 | + |
| 52 | + assertEquals("Line 5", extractedLine); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenLargeFile_whenUsingFilesAPI_thenExtractedLineIsCorrect() throws IOException { |
| 57 | + try (Stream<String> lines = Files.lines(Paths.get(FILE_PATH))) { |
| 58 | + String extractedLine = lines.skip(4).findFirst().get(); |
| 59 | + |
| 60 | + assertEquals("Line 5", extractedLine); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenFile_whenUsingFileUtils_thenExtractedLineIsCorrect() throws IOException { |
| 66 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 67 | + File file = new File(classLoader.getResource("linesInput.txt").getFile()); |
| 68 | + |
| 69 | + List<String> lines = FileUtils.readLines(file, "UTF-8"); |
| 70 | + |
| 71 | + String extractedLine = lines.get(0); |
| 72 | + assertEquals("Line 1", extractedLine); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void givenFile_whenUsingIOUtils_thenExtractedLineIsCorrect() throws IOException { |
| 77 | + String fileContent = IOUtils.toString(new FileInputStream(FILE_PATH), StandardCharsets.UTF_8); |
| 78 | + |
| 79 | + String extractedLine = fileContent.split(System.lineSeparator())[0]; |
| 80 | + assertEquals("Line 1", extractedLine); |
| 81 | + } |
| 82 | +} |
0 commit comments