Skip to content

Commit 10cceaa

Browse files
author
Eugen
committed
Merge pull request eugenp#235 from vrajeshjayswal/master
Java encode and decode test program
2 parents f2d083f + f76247a commit 10cceaa

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.demo.encoding;
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 ApacheCommonsEncodeDecode {
13+
14+
@Test
15+
public void whenStringIsEncoded() throws UnsupportedEncodingException {
16+
String originalInput = "test input";
17+
Base64 base64 = new Base64();
18+
String encodedString = new String(base64.encode(originalInput.getBytes()));
19+
20+
assertNotNull(encodedString);
21+
assertNotEquals(originalInput, encodedString);
22+
}
23+
24+
@Test
25+
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
26+
String originalInput = "test input";
27+
Base64 base64 = new Base64();
28+
String encodedString = new String(base64.encode(originalInput.getBytes()));
29+
30+
String decodedString = new String(base64.decode(encodedString.getBytes()));
31+
32+
assertNotNull(decodedString);
33+
assertEquals(originalInput, decodedString);
34+
}
35+
36+
@Test
37+
public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException {
38+
String originalInput = "test input";
39+
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
40+
41+
assertNotNull(encodedString);
42+
assertNotEquals(originalInput, encodedString);
43+
}
44+
45+
@Test
46+
public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException {
47+
String originalInput = "test input";
48+
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
49+
50+
String decodedString = new String(Base64.decodeBase64(encodedString.getBytes()));
51+
52+
assertNotNull(decodedString);
53+
assertEquals(originalInput, decodedString);
54+
}
55+
56+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.demo.encoding;
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 Java8EncodeDecode {
14+
15+
@Test
16+
public void whenStringIsEncoded() throws UnsupportedEncodingException {
17+
String originalInput = "test input";
18+
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
19+
assertNotNull(encodedString);
20+
assertNotEquals(originalInput, encodedString);
21+
}
22+
23+
@Test
24+
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
25+
String originalInput = "test input";
26+
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
27+
28+
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
29+
String decodedString = new String(decodedBytes);
30+
31+
assertNotNull(decodedString);
32+
assertEquals(originalInput, decodedString);
33+
34+
}
35+
36+
@Test
37+
public void whenURLIsEncoded() throws UnsupportedEncodingException {
38+
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
39+
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
40+
assertNotNull(encodedURL);
41+
assertNotEquals(originalURL, encodedURL);
42+
}
43+
44+
@Test
45+
public void whenURLIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException {
46+
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
47+
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
48+
byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes());
49+
String decodedURL = new String(decodedBytes);
50+
assertNotNull(decodedURL);
51+
assertEquals(originalURL, decodedURL);
52+
}
53+
54+
@Test
55+
public void whenMIMEIsEncoded() throws UnsupportedEncodingException {
56+
StringBuilder buffer = getMimeBuffer();
57+
58+
byte[] forEncode = buffer.toString().getBytes();
59+
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
60+
61+
assertNotNull(encodedMime);
62+
}
63+
64+
@Test
65+
public void whenMIMEIsEncoded_thenMIMECanBeDecoded() throws UnsupportedEncodingException {
66+
StringBuilder buffer = getMimeBuffer();
67+
68+
byte[] forEncode = buffer.toString().getBytes();
69+
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
70+
71+
byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime);
72+
String decodedMime = new String(decodedBytes);
73+
assertNotNull(decodedMime);
74+
}
75+
76+
private static StringBuilder getMimeBuffer() {
77+
StringBuilder buffer = new StringBuilder();
78+
for (int count = 0; count < 10; ++count) {
79+
buffer.append(UUID.randomUUID().toString());
80+
}
81+
return buffer;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)