|
12 | 12 |
|
13 | 13 | public class Java8EncodeDecodeTest { |
14 | 14 |
|
| 15 | + // tests |
| 16 | + |
15 | 17 | @Test |
16 | 18 | public void whenStringIsEncoded() throws UnsupportedEncodingException { |
17 | | - String originalInput = "test input"; |
18 | | - String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); |
| 19 | + final String originalInput = "test input"; |
| 20 | + final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); |
19 | 21 | assertNotNull(encodedString); |
20 | 22 | assertNotEquals(originalInput, encodedString); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | @Test |
24 | 26 | public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException { |
25 | | - String originalInput = "test input"; |
26 | | - String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); |
| 27 | + final String originalInput = "test input"; |
| 28 | + final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); |
27 | 29 |
|
28 | | - byte[] decodedBytes = Base64.getDecoder().decode(encodedString); |
29 | | - String decodedString = new String(decodedBytes); |
| 30 | + final byte[] decodedBytes = Base64.getDecoder().decode(encodedString); |
| 31 | + final String decodedString = new String(decodedBytes); |
30 | 32 |
|
31 | 33 | assertNotNull(decodedString); |
32 | 34 | assertEquals(originalInput, decodedString); |
33 | 35 |
|
34 | 36 | } |
35 | | - |
| 37 | + |
36 | 38 | @Test |
37 | 39 | public void whenStringIsEncodedWithoutPadding() throws UnsupportedEncodingException { |
38 | | - String originalInput = "test input"; |
39 | | - String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); |
| 40 | + final String originalInput = "test input"; |
| 41 | + final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); |
40 | 42 | assertNotNull(encodedString); |
41 | 43 | assertNotEquals(originalInput, encodedString); |
42 | 44 | } |
43 | | - |
| 45 | + |
44 | 46 | @Test |
45 | 47 | public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException { |
46 | | - String originalInput = "test input"; |
47 | | - String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); |
| 48 | + final String originalInput = "test input"; |
| 49 | + final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); |
48 | 50 |
|
49 | | - byte[] decodedBytes = Base64.getDecoder().decode(encodedString); |
50 | | - String decodedString = new String(decodedBytes); |
| 51 | + final byte[] decodedBytes = Base64.getDecoder().decode(encodedString); |
| 52 | + final String decodedString = new String(decodedBytes); |
51 | 53 |
|
52 | 54 | assertNotNull(decodedString); |
53 | 55 | assertEquals(originalInput, decodedString); |
54 | 56 |
|
55 | 57 | } |
56 | 58 |
|
57 | | - |
58 | 59 | @Test |
59 | 60 | public void whenURLIsEncoded() throws UnsupportedEncodingException { |
60 | | - String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; |
61 | | - String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); |
| 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()); |
62 | 63 | assertNotNull(encodedURL); |
63 | 64 | assertNotEquals(originalURL, encodedURL); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | @Test |
67 | 68 | public void whenURLIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException { |
68 | | - String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; |
69 | | - String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); |
70 | | - byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes()); |
71 | | - String decodedURL = new String(decodedBytes); |
| 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 | + final byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes()); |
| 72 | + final String decodedURL = new String(decodedBytes); |
72 | 73 | assertNotNull(decodedURL); |
73 | 74 | assertEquals(originalURL, decodedURL); |
74 | 75 | } |
75 | 76 |
|
76 | 77 | @Test |
77 | 78 | public void whenMIMEIsEncoded() throws UnsupportedEncodingException { |
78 | | - StringBuilder buffer = getMimeBuffer(); |
| 79 | + final StringBuilder buffer = getMimeBuffer(); |
79 | 80 |
|
80 | | - byte[] forEncode = buffer.toString().getBytes(); |
81 | | - String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); |
| 81 | + final byte[] forEncode = buffer.toString().getBytes(); |
| 82 | + final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); |
82 | 83 |
|
83 | 84 | assertNotNull(encodedMime); |
84 | 85 | } |
85 | 86 |
|
86 | 87 | @Test |
87 | 88 | public void whenMIMEIsEncoded_thenMIMECanBeDecoded() throws UnsupportedEncodingException { |
88 | | - StringBuilder buffer = getMimeBuffer(); |
| 89 | + final StringBuilder buffer = getMimeBuffer(); |
89 | 90 |
|
90 | | - byte[] forEncode = buffer.toString().getBytes(); |
91 | | - String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); |
| 91 | + final byte[] forEncode = buffer.toString().getBytes(); |
| 92 | + final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); |
92 | 93 |
|
93 | | - byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime); |
94 | | - String decodedMime = new String(decodedBytes); |
| 94 | + final byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime); |
| 95 | + final String decodedMime = new String(decodedBytes); |
95 | 96 | assertNotNull(decodedMime); |
96 | 97 | } |
97 | 98 |
|
| 99 | + // |
| 100 | + |
98 | 101 | private static StringBuilder getMimeBuffer() { |
99 | | - StringBuilder buffer = new StringBuilder(); |
| 102 | + final StringBuilder buffer = new StringBuilder(); |
100 | 103 | for (int count = 0; count < 10; ++count) { |
101 | 104 | buffer.append(UUID.randomUUID().toString()); |
102 | 105 | } |
|
0 commit comments