|
| 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