JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) (#14361)
* JAVA-22516 Split or move core-java-io-apis-2 module (moved-1) * JAVA-22516 Adding back the file after fixing the conflicts --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class BufferedReaderUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReadingAFile_thenReadsLineByLine() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
final Path filePath = new File("src/test/resources/sampleText1.txt").toPath();
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
|
||||
while((line = br.readLine()) != null) {
|
||||
result.append(line);
|
||||
result.append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("first line\nsecond line\nthird line\n", result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.bufferedreadervsfilereader;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileReaderUnitTest {
|
||||
|
||||
@Test
|
||||
void whenReadingAFile_thenReadsCharByChar() {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {
|
||||
int i = fr.read();
|
||||
|
||||
while(i != -1) {
|
||||
result.append((char)i);
|
||||
|
||||
i = fr.read();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assertEquals("qwerty", result.toString());
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.multinput;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.InputMismatchException;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class TestMultipleInputsUnitTest {
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingSpaceDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "10 20\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingSpaceDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingREDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "30, 40\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingREDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultipleInputs_whenUsingCustomDelimiter_thenExpectPrintingOutputs() {
|
||||
String input = "50; 60\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
mi.UsingCustomDelimiter();
|
||||
// You can add assertions here to verify the behavior of the method
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidInput_whenUsingSpaceDelimiter_thenExpectInputMismatchException() {
|
||||
String input = "abc\n";
|
||||
InputStream in = new ByteArrayInputStream(input.getBytes());
|
||||
System.setIn(in);
|
||||
MultiInputs mi = new MultiInputs();
|
||||
Assertions.assertThrows(InputMismatchException.class, mi::UsingSpaceDelimiter);
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.outputtofile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
class DualPrintStream extends PrintStream {
|
||||
private final PrintStream second;
|
||||
|
||||
public DualPrintStream(OutputStream main, PrintStream second) {
|
||||
super(main);
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
super.close();
|
||||
second.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
super.flush();
|
||||
second.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] buf, int off, int len) {
|
||||
super.write(buf, off, len);
|
||||
second.write(buf, off, len);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
super.write(b);
|
||||
second.write(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(byte[] b) throws IOException {
|
||||
super.write(b);
|
||||
second.write(b);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsoleOutputToFileUnitTest {
|
||||
|
||||
// @formatter:off
|
||||
private final static List<String> OUTPUT_LINES = Lists.newArrayList(
|
||||
"I came",
|
||||
"I saw",
|
||||
"I conquered");
|
||||
// @formatter:on
|
||||
|
||||
@Test
|
||||
void whenReplacingSystemOutPrintStreamWithFileOutputStream_thenOutputsGoToFile(@TempDir Path tempDir) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
Path outputFilePath = tempDir.resolve("file-output.txt");
|
||||
PrintStream out = new PrintStream(Files.newOutputStream(outputFilePath), true);
|
||||
System.setOut(out);
|
||||
|
||||
OUTPUT_LINES.forEach(line -> System.out.println(line));
|
||||
assertTrue(outputFilePath.toFile()
|
||||
.exists(), "The file exists");
|
||||
assertLinesMatch(OUTPUT_LINES, Files.readAllLines(outputFilePath));
|
||||
System.setOut(originalOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingDualPrintStream_thenOutputsGoToConsoleAndFile(@TempDir Path tempDir) throws IOException {
|
||||
PrintStream originalOut = System.out;
|
||||
Path outputFilePath = tempDir.resolve("dual-output.txt");
|
||||
DualPrintStream dualOut = new DualPrintStream(Files.newOutputStream(outputFilePath), System.out);
|
||||
System.setOut(dualOut);
|
||||
|
||||
OUTPUT_LINES.forEach(line -> System.out.println(line));
|
||||
assertTrue(outputFilePath.toFile()
|
||||
.exists(), "The file exists");
|
||||
assertLinesMatch(OUTPUT_LINES, Files.readAllLines(outputFilePath));
|
||||
System.setOut(originalOut);
|
||||
|
||||
}
|
||||
}
|
||||
-200
@@ -1,200 +0,0 @@
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaScannerUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadFileWithScanner_thenCorrect() throws IOException {
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read.in"));
|
||||
|
||||
assertTrue(scanner.hasNext());
|
||||
assertEquals("Hello", scanner.next());
|
||||
assertEquals("world", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertInputStreamToString_thenConverted() throws IOException {
|
||||
final String expectedValue = "Hello world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.useDelimiter("\\A");
|
||||
|
||||
final String result = scanner.next();
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadUsingBufferedReader_thenCorrect() throws IOException {
|
||||
final String firstLine = "Hello world";
|
||||
final String secondLine = "Hi, John";
|
||||
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read_multiple.in"));
|
||||
|
||||
String result = reader.readLine();
|
||||
assertEquals(firstLine, result);
|
||||
|
||||
result = reader.readLine();
|
||||
assertEquals(secondLine, result);
|
||||
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadUsingScanner_thenCorrect() throws IOException {
|
||||
final String firstLine = "Hello world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read_multiple.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
final String result = scanner.nextLine();
|
||||
assertEquals(firstLine, result);
|
||||
|
||||
scanner.useDelimiter(", ");
|
||||
assertEquals("Hi", scanner.next());
|
||||
assertEquals("John", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingInputFromConsole_thenCorrect() {
|
||||
final String input = "Hello";
|
||||
final InputStream stdin = System.in;
|
||||
System.setIn(new ByteArrayInputStream(input.getBytes()));
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
final String result = scanner.next();
|
||||
assertEquals(input, result);
|
||||
|
||||
System.setIn(stdin);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidateInputUsingScanner_thenValidated() throws IOException {
|
||||
final String input = "2000";
|
||||
final InputStream stdin = System.in;
|
||||
System.setIn(new ByteArrayInputStream(input.getBytes()));
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
final boolean isIntInput = scanner.hasNextInt();
|
||||
assertTrue(isIntInput);
|
||||
|
||||
System.setIn(stdin);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScanString_thenCorrect() throws IOException {
|
||||
final String input = "Hello 1 F 3.5";
|
||||
final Scanner scanner = new Scanner(input);
|
||||
scanner.useLocale(Locale.US);
|
||||
|
||||
assertEquals("Hello", scanner.next());
|
||||
assertEquals(1, scanner.nextInt());
|
||||
assertEquals(15, scanner.nextInt(16));
|
||||
assertEquals(3.5, scanner.nextDouble(), 0.00000001);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindPatternUsingScanner_thenFound() throws IOException {
|
||||
final String expectedValue = "world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
final String result = scanner.findInLine("wo..d");
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindPatternInHorizon_thenFound() throws IOException {
|
||||
final String expectedValue = "world";
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
String result = scanner.findWithinHorizon("wo..d", 5);
|
||||
assertNull(result);
|
||||
|
||||
result = scanner.findWithinHorizon("wo..d", 100);
|
||||
assertEquals(expectedValue, result);
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkipPatternUsingScanner_thenSkiped() throws IOException {
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
|
||||
scanner.skip(".e.lo");
|
||||
|
||||
assertEquals("world", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangeScannerDelimiter_thenChanged() throws IOException {
|
||||
final String expectedValue = "Hello world";
|
||||
final String[] splited = expectedValue.split("o");
|
||||
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.useDelimiter("o");
|
||||
|
||||
assertEquals(splited[0], scanner.next());
|
||||
assertEquals(splited[1], scanner.next());
|
||||
assertEquals(splited[2], scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithScannerTwoDelimiters_thenCorrect() throws IOException {
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read_d.in"));
|
||||
scanner.useDelimiter(",|-");
|
||||
|
||||
assertEquals("John", scanner.next());
|
||||
assertEquals("Adam", scanner.next());
|
||||
assertEquals("Tom", scanner.next());
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void givenClosingScanner_whenReading_thenThrowException() throws IOException {
|
||||
final FileInputStream inputStream = new FileInputStream("src/test/resources/test_read.in");
|
||||
|
||||
final Scanner scanner = new Scanner(inputStream);
|
||||
scanner.next();
|
||||
scanner.close();
|
||||
|
||||
final Scanner scanner2 = new Scanner(inputStream);
|
||||
scanner2.next();
|
||||
scanner2.close();
|
||||
}
|
||||
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.scannernextline;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ScannerNextLineUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReadingLines_thenCorrect() {
|
||||
String input = "Scanner\nTest\n";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
assertEquals("Scanner", scanner.nextLine());
|
||||
assertEquals("Test", scanner.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadingPartialLines_thenCorrect() {
|
||||
String input = "Scanner\n";
|
||||
try (Scanner scanner = new Scanner(input)) {
|
||||
scanner.useDelimiter("");
|
||||
scanner.next();
|
||||
assertEquals("canner", scanner.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void givenNoNewLine_whenReadingNextLine_thenThrowNoSuchElementException() {
|
||||
try (Scanner scanner = new Scanner("")) {
|
||||
String result = scanner.nextLine();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenScannerIsClosed_whenReadingNextLine_thenThrowIllegalStateException() {
|
||||
Scanner scanner = new Scanner("");
|
||||
scanner.close();
|
||||
String result = scanner.nextLine();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
Hello world
|
||||
@@ -1 +0,0 @@
|
||||
John,Adam-Tom
|
||||
@@ -1,2 +0,0 @@
|
||||
Hello world
|
||||
Hi, John
|
||||
Reference in New Issue
Block a user