java-commons/src/test/java/de/nclazz/commons/io/StreamUtilsTests.java

54 lines
1.7 KiB
Java

package de.nclazz.commons.io;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StreamUtilsTests {
byte[] alphabetBytes = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', };
@Test
void testInputStreamToByteArray() throws IOException {
InputStream stream = StreamUtilsTests.class.getResourceAsStream("/test-files/alphabet-utf8.txt");
byte[] bytes = StreamUtils.inputStreamToByteArray(stream);
assertTrue(Arrays.equals(alphabetBytes, bytes));
}
@Test
void testStreamPiping() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream stream = StreamUtilsTests.class.getResourceAsStream("/test-files/alphabet-utf8.txt");
StreamUtils.pipeStream(stream, bos);
byte[] bytes = bos.toByteArray();
assertTrue(Arrays.equals(alphabetBytes, bytes));
}
@Test
void stringToInputStreamRoundTrip() throws IOException {
String testString = "Hello World!";
InputStream stream = StreamUtils.toInputStream(testString);
String backString = StreamUtils.toString(stream);
stream.close();
assertEquals(testString, backString);
}
}