Skip to content

Commit

Permalink
maven added dependency and IO work
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenp committed Jan 6, 2014
1 parent 52a6046 commit 458db43
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 130 deletions.
6 changes: 6 additions & 0 deletions core-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<version>2.4</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2</version>
</dependency>

<!-- web -->

<!-- marshalling -->
Expand Down
55 changes: 52 additions & 3 deletions core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package org.baeldung.java;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import org.apache.commons.io.FileUtils;
Expand All @@ -11,12 +20,15 @@
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;

public class CoreJavaIoUnitTest {
protected final Logger logger = LoggerFactory.getLogger(getClass());

// tests -
// tests - iterate lines in a file

@Test
public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException {
Expand Down Expand Up @@ -70,7 +82,45 @@ public final void whenStreamingThroughAFile_thenCorrect() throws IOException {
logMemory();
}

//
// tests - InputStream to String

@Test
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
final String originalString = randomAlphabetic(8);
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8);

// When
String text = null;
try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
text = scanner.useDelimiter("\\A").next();
}

assertThat(text, equalTo(originalString));
}

@Test
public final void givenUsingGuavaNoEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
final String originalString = randomAlphabetic(8);
final InputSupplier<StringReader> readerSupplier = CharStreams.newReaderSupplier(originalString);

// When
final String text = CharStreams.toString(readerSupplier);

assertThat(text, equalTo(originalString));
}

@Test
public final void givenUsingGuavaWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
final String originalString = randomAlphabetic(8);
final InputSupplier<Reader> readerSupplier = ByteSource.wrap(originalString.getBytes()).asCharSource(Charsets.UTF_8);

// When
final String text = CharStreams.toString(readerSupplier);

assertThat(text, equalTo(originalString));
}

// utils

private final void logMemory() {
logger.info("Max Memory: {} Mb", Runtime.getRuntime().maxMemory() / 1048576);
Expand All @@ -79,4 +129,3 @@ private final void logMemory() {
}

}
//
259 changes: 132 additions & 127 deletions guava/pom.xml
Original file line number Diff line number Diff line change
@@ -1,129 +1,134 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<groupId>org.baeldung</groupId>
<artifactId>spring-rest</artifactId>
<version>0.1-SNAPSHOT</version>

<name>spring-rest</name>

<dependencies>

<!-- utils -->

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>

<!-- web -->

<!-- test scoped -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>spring-rest</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>

</plugins>

</build>

<properties>
<!-- Spring -->
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>

<!-- persistence -->
<hibernate.version>4.3.0.Final</hibernate.version>
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>

<!-- logging -->
<org.slf4j.version>1.7.5</org.slf4j.version>
<logback.version>1.0.11</logback.version>

<!-- various -->
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>

<!-- util -->
<guava.version>15.0</guava.version>
<commons-lang3.version>3.1</commons-lang3.version>

<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>

<httpcore.version>4.3</httpcore.version>
<httpclient.version>4.3.1</httpclient.version>

<rest-assured.version>2.1.0</rest-assured.version>

<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>

</properties>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>
<groupId>org.baeldung</groupId>
<artifactId>spring-rest</artifactId>
<version>0.1-SNAPSHOT</version>

<name>spring-rest</name>

<dependencies>

<!-- utils -->

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2</version>
</dependency>

<!-- web -->

<!-- test scoped -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>spring-rest</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>

</plugins>

</build>

<properties>
<!-- Spring -->
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>

<!-- persistence -->
<hibernate.version>4.3.0.Final</hibernate.version>
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>

<!-- logging -->
<org.slf4j.version>1.7.5</org.slf4j.version>
<logback.version>1.0.11</logback.version>

<!-- various -->
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>

<!-- util -->
<guava.version>15.0</guava.version>
<commons-lang3.version>3.1</commons-lang3.version>

<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>

<httpcore.version>4.3</httpcore.version>
<httpclient.version>4.3.1</httpclient.version>

<rest-assured.version>2.1.0</rest-assured.version>

<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>

</properties>

</project>

0 comments on commit 458db43

Please sign in to comment.