Skip to content

Commit

Permalink
Merge pull request eugenp#235 from vrajeshjayswal/master
Browse files Browse the repository at this point in the history
Java encode and decode test program
  • Loading branch information
Eugen committed Aug 13, 2015
2 parents f2d083f + f76247a commit 10cceaa
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
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 core-java-8/src/main/java/com/demo/encoding/Java8EncodeDecode.java
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;
}

}

0 comments on commit 10cceaa

Please sign in to comment.