|
| 1 | +package com.baeldung.iostreams; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.SequenceInputStream; |
| 9 | +import java.net.URISyntaxException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Vector; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | + |
| 18 | +public class InputSequenceUnitTest { |
| 19 | + |
| 20 | + private static final String FILE1 = "iostreams/File1.txt"; |
| 21 | + private static final String FILE2 = "iostreams/File2.txt"; |
| 22 | + private static final String FILE3 = "iostreams/File3.txt"; |
| 23 | + |
| 24 | + private static final String FILE1_AND_FILE2_CONTENT = "InputSequenceUnitTest"; |
| 25 | + private static final String ALL_FILES_CONTENT = "InputSequenceUnitTest is Success"; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void givenTwoFiles_readAsString() throws URISyntaxException, IOException { |
| 29 | + String file1 = Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString(); |
| 30 | + String file2 = Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString(); |
| 31 | + InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(file1, file2); |
| 32 | + String stringValue = inputSequenceHandler.readAsString(); |
| 33 | + inputSequenceHandler.close(); |
| 34 | + assertEquals(stringValue, FILE1_AND_FILE2_CONTENT); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenFileList_readAsString() throws URISyntaxException, IOException { |
| 39 | + List<String> filesList = new ArrayList<>(); |
| 40 | + filesList.add(Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString()); |
| 41 | + filesList.add(Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString()); |
| 42 | + filesList.add(Paths.get(ClassLoader.getSystemResource(FILE3).toURI()).toString()); |
| 43 | + InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(filesList); |
| 44 | + String stringValue = inputSequenceHandler.readAsString(); |
| 45 | + inputSequenceHandler.close(); |
| 46 | + assertEquals(stringValue, ALL_FILES_CONTENT); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenVectorOfInputStreams_readAsString() throws IOException { |
| 51 | + String[] strings = {"Testing", "Leads", "to", "failure", |
| 52 | + "and", "failure", "leads", "to", "understanding"}; |
| 53 | + Vector<InputStream> inputStreamVector = new Vector<>(); |
| 54 | + StringBuilder stringBuilder = new StringBuilder(); |
| 55 | + for (String string: strings) { |
| 56 | + inputStreamVector.add(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))); |
| 57 | + stringBuilder.append(string); |
| 58 | + } |
| 59 | + InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(inputStreamVector); |
| 60 | + String combinedString = inputSequenceHandler.readAsString(); |
| 61 | + inputSequenceHandler.close(); |
| 62 | + assertEquals(stringBuilder.toString(), combinedString); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void givenTwoStrings_readCombinedValue() throws IOException { |
| 67 | + InputStream first = new ByteArrayInputStream("One".getBytes()); |
| 68 | + InputStream second = new ByteArrayInputStream("Magic".getBytes()); |
| 69 | + SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second); |
| 70 | + StringBuilder stringBuilder = new StringBuilder(); |
| 71 | + int byteValue; |
| 72 | + while ((byteValue = sequenceInputStream.read()) != -1) { |
| 73 | + stringBuilder.append((char) byteValue); |
| 74 | + } |
| 75 | + assertEquals("OneMagic", stringBuilder.toString()); |
| 76 | + } |
| 77 | +} |
0 commit comments