forked from ivanp81/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#235 from vrajeshjayswal/master
Java encode and decode test program
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
core-java-8/src/main/java/com/demo/encoding/ApacheCommonsEncodeDecode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.demo.encoding; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.apache.commons.codec.binary.Base64; | ||
import org.junit.Test; | ||
|
||
public class ApacheCommonsEncodeDecode { | ||
|
||
@Test | ||
public void whenStringIsEncoded() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
Base64 base64 = new Base64(); | ||
String encodedString = new String(base64.encode(originalInput.getBytes())); | ||
|
||
assertNotNull(encodedString); | ||
assertNotEquals(originalInput, encodedString); | ||
} | ||
|
||
@Test | ||
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
Base64 base64 = new Base64(); | ||
String encodedString = new String(base64.encode(originalInput.getBytes())); | ||
|
||
String decodedString = new String(base64.decode(encodedString.getBytes())); | ||
|
||
assertNotNull(decodedString); | ||
assertEquals(originalInput, decodedString); | ||
} | ||
|
||
@Test | ||
public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes())); | ||
|
||
assertNotNull(encodedString); | ||
assertNotEquals(originalInput, encodedString); | ||
} | ||
|
||
@Test | ||
public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes())); | ||
|
||
String decodedString = new String(Base64.decodeBase64(encodedString.getBytes())); | ||
|
||
assertNotNull(decodedString); | ||
assertEquals(originalInput, decodedString); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
core-java-8/src/main/java/com/demo/encoding/Java8EncodeDecode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.demo.encoding; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Base64; | ||
import java.util.UUID; | ||
|
||
import org.junit.Test; | ||
|
||
public class Java8EncodeDecode { | ||
|
||
@Test | ||
public void whenStringIsEncoded() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); | ||
assertNotNull(encodedString); | ||
assertNotEquals(originalInput, encodedString); | ||
} | ||
|
||
@Test | ||
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException { | ||
String originalInput = "test input"; | ||
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); | ||
|
||
byte[] decodedBytes = Base64.getDecoder().decode(encodedString); | ||
String decodedString = new String(decodedBytes); | ||
|
||
assertNotNull(decodedString); | ||
assertEquals(originalInput, decodedString); | ||
|
||
} | ||
|
||
@Test | ||
public void whenURLIsEncoded() throws UnsupportedEncodingException { | ||
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; | ||
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); | ||
assertNotNull(encodedURL); | ||
assertNotEquals(originalURL, encodedURL); | ||
} | ||
|
||
@Test | ||
public void whenURLIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException { | ||
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; | ||
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); | ||
byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes()); | ||
String decodedURL = new String(decodedBytes); | ||
assertNotNull(decodedURL); | ||
assertEquals(originalURL, decodedURL); | ||
} | ||
|
||
@Test | ||
public void whenMIMEIsEncoded() throws UnsupportedEncodingException { | ||
StringBuilder buffer = getMimeBuffer(); | ||
|
||
byte[] forEncode = buffer.toString().getBytes(); | ||
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); | ||
|
||
assertNotNull(encodedMime); | ||
} | ||
|
||
@Test | ||
public void whenMIMEIsEncoded_thenMIMECanBeDecoded() throws UnsupportedEncodingException { | ||
StringBuilder buffer = getMimeBuffer(); | ||
|
||
byte[] forEncode = buffer.toString().getBytes(); | ||
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); | ||
|
||
byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime); | ||
String decodedMime = new String(decodedBytes); | ||
assertNotNull(decodedMime); | ||
} | ||
|
||
private static StringBuilder getMimeBuffer() { | ||
StringBuilder buffer = new StringBuilder(); | ||
for (int count = 0; count < 10; ++count) { | ||
buffer.append(UUID.randomUUID().toString()); | ||
} | ||
return buffer; | ||
} | ||
|
||
} |