|
| 1 | +package org.baeldung.java.io; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.Scanner; |
| 15 | + |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class JavaScannerTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void whenReadFileWithScanner_thenCorrect() throws IOException { |
| 22 | + final Scanner scanner = new Scanner(new File("src/test/resources/test_read.in")); |
| 23 | + |
| 24 | + assertTrue(scanner.hasNext()); |
| 25 | + assertEquals("Hello", scanner.next()); |
| 26 | + assertEquals("world", scanner.next()); |
| 27 | + |
| 28 | + scanner.close(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void whenConvertInputStreamToString_thenConverted() throws IOException { |
| 33 | + final String expectedValue = "Hello world"; |
| 34 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in"); |
| 35 | + final Scanner scanner = new Scanner(inputStream); |
| 36 | + scanner.useDelimiter("\\A"); |
| 37 | + |
| 38 | + final String result = scanner.next(); |
| 39 | + assertEquals(expectedValue, result); |
| 40 | + |
| 41 | + scanner.close(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenReadUsingBufferedReader_thenCorrect() throws IOException { |
| 46 | + final String firstLine = "Hello world"; |
| 47 | + final String secondLine = "Hi, John"; |
| 48 | + final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read_multiple.in")); |
| 49 | + |
| 50 | + String result = reader.readLine(); |
| 51 | + assertEquals(firstLine, result); |
| 52 | + |
| 53 | + result = reader.readLine(); |
| 54 | + assertEquals(secondLine, result); |
| 55 | + |
| 56 | + reader.close(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenReadUsingScanner_thenCorrect() throws IOException { |
| 61 | + final String firstLine = "Hello world"; |
| 62 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read_multiple.in"); |
| 63 | + final Scanner scanner = new Scanner(inputStream); |
| 64 | + |
| 65 | + final String result = scanner.nextLine(); |
| 66 | + assertEquals(firstLine, result); |
| 67 | + |
| 68 | + scanner.useDelimiter(", "); |
| 69 | + assertEquals("Hi", scanner.next()); |
| 70 | + assertEquals("John", scanner.next()); |
| 71 | + |
| 72 | + scanner.close(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenReadInputFromConsole_thenCorrect() { |
| 77 | + final String input = "Hello"; |
| 78 | + final InputStream stdin = System.in; |
| 79 | + System.setIn(new ByteArrayInputStream(input.getBytes())); |
| 80 | + |
| 81 | + final Scanner scanner = new Scanner(System.in); |
| 82 | + final String result = scanner.next(); |
| 83 | + assertEquals(input, result); |
| 84 | + |
| 85 | + System.setIn(stdin); |
| 86 | + scanner.close(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void whenValidateInputUsingScanner_thenValidated() throws IOException { |
| 91 | + final String input = "2000"; |
| 92 | + final InputStream stdin = System.in; |
| 93 | + System.setIn(new ByteArrayInputStream(input.getBytes())); |
| 94 | + |
| 95 | + final Scanner scanner = new Scanner(System.in); |
| 96 | + |
| 97 | + final boolean isIntInput = scanner.hasNextInt(); |
| 98 | + assertTrue(isIntInput); |
| 99 | + |
| 100 | + System.setIn(stdin); |
| 101 | + scanner.close(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void whenScanString_thenCorrect() throws IOException { |
| 106 | + final String input = "Hello 1 F 3.5"; |
| 107 | + final Scanner scanner = new Scanner(input); |
| 108 | + |
| 109 | + assertEquals("Hello", scanner.next()); |
| 110 | + assertEquals(1, scanner.nextInt()); |
| 111 | + assertEquals(15, scanner.nextInt(16)); |
| 112 | + assertEquals(3.5, scanner.nextDouble(), 0.00000001); |
| 113 | + |
| 114 | + scanner.close(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void whenFindPatternUsingScanner_thenFound() throws IOException { |
| 119 | + final String expectedValue = "world"; |
| 120 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in"); |
| 121 | + final Scanner scanner = new Scanner(inputStream); |
| 122 | + |
| 123 | + final String result = scanner.findInLine("wo..d"); |
| 124 | + assertEquals(expectedValue, result); |
| 125 | + |
| 126 | + scanner.close(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void whenFindPatternInHorizon_thenFound() throws IOException { |
| 131 | + final String expectedValue = "world"; |
| 132 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in"); |
| 133 | + final Scanner scanner = new Scanner(inputStream); |
| 134 | + |
| 135 | + String result = scanner.findWithinHorizon("wo..d", 5); |
| 136 | + assertNull(result); |
| 137 | + |
| 138 | + result = scanner.findWithinHorizon("wo..d", 100); |
| 139 | + assertEquals(expectedValue, result); |
| 140 | + |
| 141 | + scanner.close(); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void whenSkipPatternUsingScanner_thenSkiped() throws IOException { |
| 146 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in"); |
| 147 | + final Scanner scanner = new Scanner(inputStream); |
| 148 | + |
| 149 | + scanner.skip(".e.lo"); |
| 150 | + |
| 151 | + assertEquals("world", scanner.next()); |
| 152 | + |
| 153 | + scanner.close(); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void whenChangeScannerDelimiter_thenChanged() throws IOException { |
| 158 | + final String expectedValue = "Hello world"; |
| 159 | + final String[] splited = expectedValue.split("o"); |
| 160 | + |
| 161 | + final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in"); |
| 162 | + final Scanner scanner = new Scanner(inputStream); |
| 163 | + scanner.useDelimiter("o"); |
| 164 | + |
| 165 | + assertEquals(splited[0], scanner.next()); |
| 166 | + assertEquals(splited[1], scanner.next()); |
| 167 | + assertEquals(splited[2], scanner.next()); |
| 168 | + |
| 169 | + scanner.close(); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void whenReadWithScannerTwoDelimiters_thenCorrect() throws IOException { |
| 174 | + final Scanner scanner = new Scanner(new File("src/test/resources/test_read_d.in")); |
| 175 | + scanner.useDelimiter(",|-"); |
| 176 | + |
| 177 | + assertEquals("John", scanner.next()); |
| 178 | + assertEquals("Adam", scanner.next()); |
| 179 | + assertEquals("Tom", scanner.next()); |
| 180 | + |
| 181 | + scanner.close(); |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments