-
Notifications
You must be signed in to change notification settings - Fork 53.5k
Expand file tree
/
Copy pathJava8EncodeDecodeUnitTest.java
More file actions
109 lines (81 loc) · 3.94 KB
/
Copy pathJava8EncodeDecodeUnitTest.java
File metadata and controls
109 lines (81 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.baeldung.base64encodinganddecoding;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.UUID;
import static org.junit.Assert.*;
public class Java8EncodeDecodeUnitTest {
// tests
@Test
public void whenStringIsEncoded_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
final String decodedString = new String(decodedBytes);
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
@Test
public void whenStringIsEncodedWithoutPadding_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
final String decodedString = new String(decodedBytes);
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
@Test
public void whenUrlIsEncoded_thenOk() throws UnsupportedEncodingException {
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
assertNotNull(encodedUrl);
assertNotEquals(originalUrl, encodedUrl);
}
@Test
public void whenUrlIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException {
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
final byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl.getBytes());
final String decodedUrl = new String(decodedBytes);
assertNotNull(decodedUrl);
assertEquals(originalUrl, decodedUrl);
}
@Test
public void whenMimeIsEncoded_thenOk() throws UnsupportedEncodingException {
final StringBuilder buffer = getMimeBuffer();
final byte[] forEncode = buffer.toString().getBytes();
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
assertNotNull(encodedMime);
}
@Test
public void whenMimeIsEncoded_thenItCanBeDecoded() throws UnsupportedEncodingException {
final StringBuilder buffer = getMimeBuffer();
final byte[] forEncode = buffer.toString().getBytes();
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
final byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime);
final String decodedMime = new String(decodedBytes);
assertNotNull(decodedMime);
}
//
private static StringBuilder getMimeBuffer() {
final StringBuilder buffer = new StringBuilder();
for (int count = 0; count < 10; ++count) {
buffer.append(UUID.randomUUID().toString());
}
return buffer;
}
}