Merge pull request #235 from vrajeshjayswal/master

Java encode and decode test program
This commit is contained in:
Eugen 2015-08-12 23:50:54 -07:00
commit 10cceaa58e
2 changed files with 140 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}