BAEL-1033 Introduction to StreamUtils (#2341)
* Evaluation article: Different Types of Bean Injection in Spring * added tests & changed configuration to Java-based config * removed xml config files * rename unit tests * BAEL-972 - Apache Commons Text * remove code from evaluation article * remove code from evaluation article * BAEL-972 - Apache Commons Text - added another example * BAEL-972 - Apache Commons Text - just indentation * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java - fix problems * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * fix formatting * BAEL-1033 minor refactor
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
585597f11d
commit
4b53c00bca
+134
-128
@@ -1,141 +1,147 @@
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>spring-core</name>
|
||||
<name>spring-core</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<version>${javax.inject.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
<version>${mockito.spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<version>${javax.inject.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
<version>${mockito.spring.boot.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons.io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
|
||||
<spring.version>4.3.4.RELEASE</spring.version>
|
||||
<javax.inject.version>1</javax.inject.version>
|
||||
<guava.version>20.0</guava.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
</properties>
|
||||
<properties>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
|
||||
<spring.version>4.3.4.RELEASE</spring.version>
|
||||
<javax.inject.version>1</javax.inject.version>
|
||||
<guava.version>20.0</guava.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<commons.io.version>2.5</commons.io.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>java.net</id>
|
||||
<url>https://maven.java.net/content/repositories/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>java.net</id>
|
||||
<url>https://maven.java.net/content/repositories/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.streamutils;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
public class CopyStream {
|
||||
public static String getStringFromInputStream(InputStream input) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
IOUtils.copy(input, writer, "UTF-8");
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
public InputStream getNonClosingInputStream() throws IOException {
|
||||
InputStream in = new FileInputStream("src/test/resources/input.txt");
|
||||
return StreamUtils.nonClosing(in);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.streamutils;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
public class DrainStream {
|
||||
public InputStream getInputStream() {
|
||||
return StreamUtils.emptyInput();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.baeldung.streamutils;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static com.baeldung.streamutils.CopyStream.getStringFromInputStream;
|
||||
|
||||
public class CopyStreamTest {
|
||||
|
||||
@Test
|
||||
public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||
String inputFileName = "src/test/resources/input.txt";
|
||||
String outputFileName = "src/test/resources/output.txt";
|
||||
File outputFile = new File(outputFileName);
|
||||
InputStream in = new FileInputStream(inputFileName);
|
||||
OutputStream out = new FileOutputStream(outputFileName);
|
||||
|
||||
StreamUtils.copy(in, out);
|
||||
|
||||
assertTrue(outputFile.exists());
|
||||
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
|
||||
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
|
||||
assertEquals(inputFileContent, outputFileContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||
String inputFileName = "src/test/resources/input.txt";
|
||||
String outputFileName = "src/test/resources/output.txt";
|
||||
File outputFile = new File(outputFileName);
|
||||
InputStream in = new FileInputStream(inputFileName);
|
||||
OutputStream out = new FileOutputStream(outputFileName);
|
||||
|
||||
StreamUtils.copyRange(in, out, 1, 10);
|
||||
|
||||
assertTrue(outputFile.exists());
|
||||
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
|
||||
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
|
||||
assertEquals(inputFileContent.substring(1, 11), outputFileContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyStringToOutputStream_thenCorrect() throws IOException {
|
||||
String string = "Should be copied to OutputStream.";
|
||||
String outputFileName = "src/test/resources/output.txt";
|
||||
File outputFile = new File(outputFileName);
|
||||
OutputStream out = new FileOutputStream("src/test/resources/output.txt");
|
||||
|
||||
StreamUtils.copy(string, StandardCharsets.UTF_8, out);
|
||||
|
||||
assertTrue(outputFile.exists());
|
||||
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
|
||||
assertEquals(outputFileContent, string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyInputStreamToString_thenCorrect() throws IOException {
|
||||
String inputFileName = "src/test/resources/input.txt";
|
||||
InputStream is = new FileInputStream(inputFileName);
|
||||
String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8);
|
||||
|
||||
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
|
||||
assertEquals(inputFileContent, content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyByteArrayToOutputStream_thenCorrect() throws IOException {
|
||||
String outputFileName = "src/test/resources/output.txt";
|
||||
String string = "Should be copied to OutputStream.";
|
||||
byte[] byteArray = string.getBytes();
|
||||
OutputStream out = new FileOutputStream("src/test/resources/output.txt");
|
||||
|
||||
StreamUtils.copy(byteArray, out);
|
||||
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
|
||||
assertEquals(outputFileContent, string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException {
|
||||
String inputFileName = "src/test/resources/input.txt";
|
||||
InputStream in = new FileInputStream(inputFileName);
|
||||
byte[] out = StreamUtils.copyToByteArray(in);
|
||||
|
||||
String content = new String(out);
|
||||
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
|
||||
assertEquals(inputFileContent, content);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This file is merely for testing.
|
||||
@@ -0,0 +1 @@
|
||||
Should be copied to OutputStream.
|
||||
Reference in New Issue
Block a user