Skip to content

Commit bddddbd

Browse files
committed
Conflicts: spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java
2 parents d4bf988 + a5375cb commit bddddbd

114 files changed

Lines changed: 1152 additions & 414 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2+
The "REST with Spring" Classes
3+
==============================
4+
This is what I'm working on: <br/>
5+
**[>> THE REST WITH SPRING CLASSES](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=50off)**
6+
7+
18
Spring Tutorials
29
================
310

‎core-java-8/pom.xml‎

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

31+
<dependency>
32+
<groupId>commons-codec</groupId>
33+
<artifactId>commons-codec</artifactId>
34+
<version>1.10</version>
35+
</dependency>
36+
3137
<dependency>
3238
<groupId>org.apache.commons</groupId>
3339
<artifactId>commons-lang3</artifactId>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.baeldung.java8.base64;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
import static org.junit.Assert.assertNotNull;
6+
7+
import java.io.UnsupportedEncodingException;
8+
9+
import org.apache.commons.codec.binary.Base64;
10+
import org.junit.Test;
11+
12+
public class ApacheCommonsEncodeDecodeTest {
13+
14+
// tests
15+
16+
@Test
17+
public void whenStringIsEncoded() throws UnsupportedEncodingException {
18+
final String originalInput = "test input";
19+
final Base64 base64 = new Base64();
20+
final String encodedString = new String(base64.encode(originalInput.getBytes()));
21+
22+
assertNotNull(encodedString);
23+
assertNotEquals(originalInput, encodedString);
24+
}
25+
26+
@Test
27+
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
28+
final String originalInput = "test input";
29+
final Base64 base64 = new Base64();
30+
final String encodedString = new String(base64.encode(originalInput.getBytes()));
31+
32+
final String decodedString = new String(base64.decode(encodedString.getBytes()));
33+
34+
assertNotNull(decodedString);
35+
assertEquals(originalInput, decodedString);
36+
}
37+
38+
@Test
39+
public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException {
40+
final String originalInput = "test input";
41+
final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
42+
43+
assertNotNull(encodedString);
44+
assertNotEquals(originalInput, encodedString);
45+
}
46+
47+
@Test
48+
public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException {
49+
final String originalInput = "test input";
50+
final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
51+
52+
final String decodedString = new String(Base64.decodeBase64(encodedString.getBytes()));
53+
54+
assertNotNull(decodedString);
55+
assertEquals(originalInput, decodedString);
56+
}
57+
58+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.baeldung.java8.base64;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
import static org.junit.Assert.assertNotNull;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.util.Base64;
9+
import java.util.UUID;
10+
11+
import org.junit.Test;
12+
13+
public class Java8EncodeDecodeTest {
14+
15+
// tests
16+
17+
@Test
18+
public void whenStringIsEncoded_thenOk() throws UnsupportedEncodingException {
19+
final String originalInput = "test input";
20+
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
21+
22+
assertNotNull(encodedString);
23+
assertNotEquals(originalInput, encodedString);
24+
}
25+
26+
@Test
27+
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
28+
final String originalInput = "test input";
29+
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
30+
31+
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
32+
final String decodedString = new String(decodedBytes);
33+
34+
assertNotNull(decodedString);
35+
assertEquals(originalInput, decodedString);
36+
}
37+
38+
@Test
39+
public void whenStringIsEncodedWithoutPadding_thenOk() throws UnsupportedEncodingException {
40+
final String originalInput = "test input";
41+
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
42+
43+
assertNotNull(encodedString);
44+
assertNotEquals(originalInput, encodedString);
45+
}
46+
47+
@Test
48+
public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException {
49+
final String originalInput = "test input";
50+
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
51+
52+
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
53+
final String decodedString = new String(decodedBytes);
54+
55+
assertNotNull(decodedString);
56+
assertEquals(originalInput, decodedString);
57+
}
58+
59+
@Test
60+
public void whenUrlIsEncoded_thenOk() throws UnsupportedEncodingException {
61+
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
62+
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
63+
assertNotNull(encodedUrl);
64+
assertNotEquals(originalUrl, encodedUrl);
65+
}
66+
67+
@Test
68+
public void whenUrlIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException {
69+
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
70+
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
71+
72+
final byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl.getBytes());
73+
final String decodedUrl = new String(decodedBytes);
74+
75+
assertNotNull(decodedUrl);
76+
assertEquals(originalUrl, decodedUrl);
77+
}
78+
79+
@Test
80+
public void whenMimeIsEncoded_thenOk() throws UnsupportedEncodingException {
81+
final StringBuilder buffer = getMimeBuffer();
82+
83+
final byte[] forEncode = buffer.toString().getBytes();
84+
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
85+
86+
assertNotNull(encodedMime);
87+
}
88+
89+
@Test
90+
public void whenMimeIsEncoded_thenItCanBeDecoded() throws UnsupportedEncodingException {
91+
final StringBuilder buffer = getMimeBuffer();
92+
93+
final byte[] forEncode = buffer.toString().getBytes();
94+
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
95+
96+
final byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime);
97+
final String decodedMime = new String(decodedBytes);
98+
assertNotNull(decodedMime);
99+
}
100+
101+
//
102+
103+
private static StringBuilder getMimeBuffer() {
104+
final StringBuilder buffer = new StringBuilder();
105+
for (int count = 0; count < 10; ++count) {
106+
buffer.append(UUID.randomUUID().toString());
107+
}
108+
return buffer;
109+
}
110+
111+
}

‎core-java/src/test/java/org/baeldung/java/io/JavaInputStreamToXUnitTest.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.BufferedReader;
99
import java.io.ByteArrayInputStream;
10+
import java.io.ByteArrayOutputStream;
1011
import java.io.File;
1112
import java.io.FileInputStream;
1213
import java.io.FileOutputStream;
@@ -128,12 +129,27 @@ public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAStri
128129
// tests - InputStream to byte[]
129130

130131
@Test
131-
public final void givenUsingPlainJava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
132+
public final void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
132133
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
133134
final byte[] targetArray = new byte[initialStream.available()];
134135
initialStream.read(targetArray);
135136
}
136137

138+
@Test
139+
public final void givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
140+
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
141+
142+
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
143+
int nRead;
144+
final byte[] data = new byte[1024];
145+
while ((nRead = is.read(data, 0, data.length)) != -1) {
146+
buffer.write(data, 0, nRead);
147+
}
148+
149+
buffer.flush();
150+
final byte[] byteArray = buffer.toByteArray();
151+
}
152+
137153
@Test
138154
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
139155
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream();

0 commit comments

Comments
 (0)