Skip to content

Commit 458db43

Browse files
author
eugenp
committed
maven added dependency and IO work
1 parent 52a6046 commit 458db43

3 files changed

Lines changed: 190 additions & 130 deletions

File tree

core-java/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<version>2.4</version>
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>org.apache.commons</groupId>
33+
<artifactId>commons-lang3</artifactId>
34+
<version>3.2</version>
35+
</dependency>
36+
3137
<!-- web -->
3238

3339
<!-- marshalling -->

core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package org.baeldung.java;
22

3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.ByteArrayInputStream;
38
import java.io.File;
49
import java.io.FileInputStream;
510
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.Reader;
13+
import java.io.StringReader;
14+
import java.nio.charset.StandardCharsets;
615
import java.util.Scanner;
716

817
import org.apache.commons.io.FileUtils;
@@ -11,12 +20,15 @@
1120
import org.slf4j.LoggerFactory;
1221

1322
import com.google.common.base.Charsets;
23+
import com.google.common.io.ByteSource;
24+
import com.google.common.io.CharStreams;
1425
import com.google.common.io.Files;
26+
import com.google.common.io.InputSupplier;
1527

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

19-
// tests -
31+
// tests - iterate lines in a file
2032

2133
@Test
2234
public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException {
@@ -70,7 +82,45 @@ public final void whenStreamingThroughAFile_thenCorrect() throws IOException {
7082
logMemory();
7183
}
7284

73-
//
85+
// tests - InputStream to String
86+
87+
@Test
88+
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
89+
final String originalString = randomAlphabetic(8);
90+
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8);
91+
92+
// When
93+
String text = null;
94+
try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
95+
text = scanner.useDelimiter("\\A").next();
96+
}
97+
98+
assertThat(text, equalTo(originalString));
99+
}
100+
101+
@Test
102+
public final void givenUsingGuavaNoEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
103+
final String originalString = randomAlphabetic(8);
104+
final InputSupplier<StringReader> readerSupplier = CharStreams.newReaderSupplier(originalString);
105+
106+
// When
107+
final String text = CharStreams.toString(readerSupplier);
108+
109+
assertThat(text, equalTo(originalString));
110+
}
111+
112+
@Test
113+
public final void givenUsingGuavaWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
114+
final String originalString = randomAlphabetic(8);
115+
final InputSupplier<Reader> readerSupplier = ByteSource.wrap(originalString.getBytes()).asCharSource(Charsets.UTF_8);
116+
117+
// When
118+
final String text = CharStreams.toString(readerSupplier);
119+
120+
assertThat(text, equalTo(originalString));
121+
}
122+
123+
// utils
74124

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

81131
}
82-
//

guava/pom.xml

Lines changed: 132 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,134 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>org.baeldung</groupId>
5-
<artifactId>spring-rest</artifactId>
6-
<version>0.1-SNAPSHOT</version>
7-
8-
<name>spring-rest</name>
9-
10-
<dependencies>
11-
12-
<!-- utils -->
13-
14-
<dependency>
15-
<groupId>com.google.guava</groupId>
16-
<artifactId>guava</artifactId>
17-
<version>15.0</version>
18-
</dependency>
19-
20-
<dependency>
21-
<groupId>org.apache.commons</groupId>
22-
<artifactId>commons-collections4</artifactId>
23-
<version>4.0</version>
24-
</dependency>
25-
26-
<!-- web -->
27-
28-
<!-- test scoped -->
29-
30-
<dependency>
31-
<groupId>junit</groupId>
32-
<artifactId>junit-dep</artifactId>
33-
<version>${junit.version}</version>
34-
<scope>test</scope>
35-
</dependency>
36-
37-
<dependency>
38-
<groupId>org.hamcrest</groupId>
39-
<artifactId>hamcrest-core</artifactId>
40-
<version>${org.hamcrest.version}</version>
41-
<scope>test</scope>
42-
</dependency>
43-
<dependency>
44-
<groupId>org.hamcrest</groupId>
45-
<artifactId>hamcrest-library</artifactId>
46-
<version>${org.hamcrest.version}</version>
47-
<scope>test</scope>
48-
</dependency>
49-
50-
<dependency>
51-
<groupId>org.mockito</groupId>
52-
<artifactId>mockito-core</artifactId>
53-
<version>${mockito.version}</version>
54-
<scope>test</scope>
55-
</dependency>
56-
57-
</dependencies>
58-
59-
<build>
60-
<finalName>spring-rest</finalName>
61-
<resources>
62-
<resource>
63-
<directory>src/main/resources</directory>
64-
<filtering>true</filtering>
65-
</resource>
66-
</resources>
67-
68-
<plugins>
69-
70-
<plugin>
71-
<groupId>org.apache.maven.plugins</groupId>
72-
<artifactId>maven-compiler-plugin</artifactId>
73-
<version>${maven-compiler-plugin.version}</version>
74-
<configuration>
75-
<source>1.7</source>
76-
<target>1.7</target>
77-
</configuration>
78-
</plugin>
79-
80-
<plugin>
81-
<groupId>org.apache.maven.plugins</groupId>
82-
<artifactId>maven-surefire-plugin</artifactId>
83-
<version>${maven-surefire-plugin.version}</version>
84-
</plugin>
85-
86-
</plugins>
87-
88-
</build>
89-
90-
<properties>
91-
<!-- Spring -->
92-
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
93-
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
94-
95-
<!-- persistence -->
96-
<hibernate.version>4.3.0.Final</hibernate.version>
97-
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
98-
99-
<!-- logging -->
100-
<org.slf4j.version>1.7.5</org.slf4j.version>
101-
<logback.version>1.0.11</logback.version>
102-
103-
<!-- various -->
104-
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
105-
106-
<!-- util -->
107-
<guava.version>15.0</guava.version>
108-
<commons-lang3.version>3.1</commons-lang3.version>
109-
110-
<!-- testing -->
111-
<org.hamcrest.version>1.3</org.hamcrest.version>
112-
<junit.version>4.11</junit.version>
113-
<mockito.version>1.9.5</mockito.version>
114-
115-
<httpcore.version>4.3</httpcore.version>
116-
<httpclient.version>4.3.1</httpclient.version>
117-
118-
<rest-assured.version>2.1.0</rest-assured.version>
119-
120-
<!-- maven plugins -->
121-
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
122-
<maven-war-plugin.version>2.4</maven-war-plugin.version>
123-
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
124-
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
125-
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>
126-
127-
</properties>
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.baeldung</groupId>
4+
<artifactId>spring-rest</artifactId>
5+
<version>0.1-SNAPSHOT</version>
6+
7+
<name>spring-rest</name>
8+
9+
<dependencies>
10+
11+
<!-- utils -->
12+
13+
<dependency>
14+
<groupId>com.google.guava</groupId>
15+
<artifactId>guava</artifactId>
16+
<version>15.0</version>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>org.apache.commons</groupId>
21+
<artifactId>commons-collections4</artifactId>
22+
<version>4.0</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.apache.commons</groupId>
27+
<artifactId>commons-lang3</artifactId>
28+
<version>3.2</version>
29+
</dependency>
30+
31+
<!-- web -->
32+
33+
<!-- test scoped -->
34+
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit-dep</artifactId>
38+
<version>${junit.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.hamcrest</groupId>
44+
<artifactId>hamcrest-core</artifactId>
45+
<version>${org.hamcrest.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.hamcrest</groupId>
50+
<artifactId>hamcrest-library</artifactId>
51+
<version>${org.hamcrest.version}</version>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.mockito</groupId>
57+
<artifactId>mockito-core</artifactId>
58+
<version>${mockito.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
</dependencies>
63+
64+
<build>
65+
<finalName>spring-rest</finalName>
66+
<resources>
67+
<resource>
68+
<directory>src/main/resources</directory>
69+
<filtering>true</filtering>
70+
</resource>
71+
</resources>
72+
73+
<plugins>
74+
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-compiler-plugin</artifactId>
78+
<version>${maven-compiler-plugin.version}</version>
79+
<configuration>
80+
<source>1.7</source>
81+
<target>1.7</target>
82+
</configuration>
83+
</plugin>
84+
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-surefire-plugin</artifactId>
88+
<version>${maven-surefire-plugin.version}</version>
89+
</plugin>
90+
91+
</plugins>
92+
93+
</build>
94+
95+
<properties>
96+
<!-- Spring -->
97+
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
98+
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
99+
100+
<!-- persistence -->
101+
<hibernate.version>4.3.0.Final</hibernate.version>
102+
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
103+
104+
<!-- logging -->
105+
<org.slf4j.version>1.7.5</org.slf4j.version>
106+
<logback.version>1.0.11</logback.version>
107+
108+
<!-- various -->
109+
<hibernate-validator.version>5.0.1.Final</hibernate-validator.version>
110+
111+
<!-- util -->
112+
<guava.version>15.0</guava.version>
113+
<commons-lang3.version>3.1</commons-lang3.version>
114+
115+
<!-- testing -->
116+
<org.hamcrest.version>1.3</org.hamcrest.version>
117+
<junit.version>4.11</junit.version>
118+
<mockito.version>1.9.5</mockito.version>
119+
120+
<httpcore.version>4.3</httpcore.version>
121+
<httpclient.version>4.3.1</httpclient.version>
122+
123+
<rest-assured.version>2.1.0</rest-assured.version>
124+
125+
<!-- maven plugins -->
126+
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
127+
<maven-war-plugin.version>2.4</maven-war-plugin.version>
128+
<maven-surefire-plugin.version>2.16</maven-surefire-plugin.version>
129+
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
130+
<cargo-maven2-plugin.version>1.4.5</cargo-maven2-plugin.version>
131+
132+
</properties>
128133

129134
</project>

0 commit comments

Comments
 (0)