|
| 1 | +package com.baeldung.streamutils; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | + |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.Test; |
| 15 | +import org.springframework.util.StreamUtils; |
| 16 | + |
| 17 | +import static com.baeldung.streamutils.CopyStream.getStringFromInputStream; |
| 18 | + |
| 19 | +public class CopyStreamTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException { |
| 23 | + String inputFileName = "src/test/resources/input.txt"; |
| 24 | + String outputFileName = "src/test/resources/output.txt"; |
| 25 | + File outputFile = new File(outputFileName); |
| 26 | + InputStream in = new FileInputStream(inputFileName); |
| 27 | + OutputStream out = new FileOutputStream(outputFileName); |
| 28 | + |
| 29 | + StreamUtils.copy(in, out); |
| 30 | + |
| 31 | + assertTrue(outputFile.exists()); |
| 32 | + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); |
| 33 | + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); |
| 34 | + Assert.assertEquals(inputFileContent, outputFileContent); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException { |
| 39 | + String inputFileName = "src/test/resources/input.txt"; |
| 40 | + String outputFileName = "src/test/resources/output.txt"; |
| 41 | + File outputFile = new File(outputFileName); |
| 42 | + InputStream in = new FileInputStream(inputFileName); |
| 43 | + OutputStream out = new FileOutputStream(outputFileName); |
| 44 | + |
| 45 | + StreamUtils.copyRange(in, out, 1, 10); |
| 46 | + |
| 47 | + assertTrue(outputFile.exists()); |
| 48 | + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); |
| 49 | + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); |
| 50 | + Assert.assertEquals(inputFileContent.substring(1, 11), outputFileContent); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenCopyStringToOutputStream_thenCorrect() throws IOException { |
| 55 | + String string = "Should be copied to OutputStream."; |
| 56 | + String outputFileName = "src/test/resources/output.txt"; |
| 57 | + File outputFile = new File(outputFileName); |
| 58 | + OutputStream out = new FileOutputStream("src/test/resources/output.txt"); |
| 59 | + |
| 60 | + StreamUtils.copy(string, StandardCharsets.UTF_8, out); |
| 61 | + |
| 62 | + assertTrue(outputFile.exists()); |
| 63 | + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); |
| 64 | + Assert.assertEquals(outputFileContent, string); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void whenCopyInputStreamToString_thenCorrect() throws IOException { |
| 69 | + String inputFileName = "src/test/resources/input.txt"; |
| 70 | + InputStream is = new FileInputStream(inputFileName); |
| 71 | + String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); |
| 72 | + |
| 73 | + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); |
| 74 | + Assert.assertEquals(inputFileContent, content); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void whenCopyByteArrayToOutputStream_thenCorrect() throws IOException { |
| 79 | + String outputFileName = "src/test/resources/output.txt"; |
| 80 | + String string = "Should be copied to OutputStream."; |
| 81 | + byte[] byteArray = string.getBytes(); |
| 82 | + OutputStream out = new FileOutputStream("src/test/resources/output.txt"); |
| 83 | + |
| 84 | + StreamUtils.copy(byteArray, out); |
| 85 | + String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); |
| 86 | + Assert.assertEquals(outputFileContent, string); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException { |
| 91 | + String inputFileName = "src/test/resources/input.txt"; |
| 92 | + InputStream in = new FileInputStream(inputFileName); |
| 93 | + byte[] out = StreamUtils.copyToByteArray(in); |
| 94 | + |
| 95 | + String content = new String(out); |
| 96 | + String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); |
| 97 | + Assert.assertEquals(inputFileContent, content); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments