Skip to content

Commit a14638c

Browse files
author
godfreyduke
committed
Add coverage for Java 8's Base64 utility class.
1 parent 88d47c9 commit a14638c

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

koans/app/config/PathToEnlightenment.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@
5252
<suite class="AboutLambdas"/>
5353
<suite class="AboutStreams"/>
5454
<suite class="AboutMultipleInheritance"/>
55+
<suite class="AboutBase64"/>
5556
</package>
5657
</packages>

koans/src/java8/AboutBase64.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package java8;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import java.util.Base64;
6+
import java.io.UnsupportedEncodingException;
7+
8+
import static com.sandwich.koan.constant.KoanConstants.__;
9+
import static com.sandwich.util.Assert.assertEquals;
10+
11+
public class AboutBase64 {
12+
13+
private final String plainText = "lorem ipsum";
14+
private final String encodedText = "bG9yZW0gaXBzdW0=";
15+
16+
@Koan
17+
public void base64Encoding() {
18+
try {
19+
// Encode the plainText
20+
// This uses the basic Base64 encoding scheme but there are corresponding
21+
// getMimeEncoder and getUrlEncoder methods available if you require a
22+
// different format/Base64 Alphabet
23+
assertEquals(encodedText, Base64.getEncoder().encodeToString(__.getBytes("utf-8")));
24+
} catch (UnsupportedEncodingException ex) {}
25+
}
26+
27+
@Koan
28+
public void base64Decoding() {
29+
// Decode the Base64 encodedText
30+
// This uses the basic Base64 decoding scheme but there are corresponding
31+
// getMimeDecoder and getUrlDecoder methods available if you require a
32+
// different format/Base64 Alphabet
33+
byte[] decodedBytes = Base64.getDecoder().decode(__);
34+
try {
35+
String decodedText = new String(decodedBytes, "utf-8");
36+
assertEquals(plainText, decodedText);
37+
} catch (UnsupportedEncodingException ex) {}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)