test cleanup

This commit is contained in:
eugenp 2015-08-20 11:37:01 +03:00
parent 6b10bee0a0
commit ed2b7e4e2f
2 changed files with 47 additions and 42 deletions

View File

@ -11,11 +11,13 @@ import org.junit.Test;
public class ApacheCommonsEncodeDecodeTest { public class ApacheCommonsEncodeDecodeTest {
// tests
@Test @Test
public void whenStringIsEncoded() throws UnsupportedEncodingException { public void whenStringIsEncoded() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
Base64 base64 = new Base64(); final Base64 base64 = new Base64();
String encodedString = new String(base64.encode(originalInput.getBytes())); final String encodedString = new String(base64.encode(originalInput.getBytes()));
assertNotNull(encodedString); assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString); assertNotEquals(originalInput, encodedString);
@ -23,11 +25,11 @@ public class ApacheCommonsEncodeDecodeTest {
@Test @Test
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException { public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
Base64 base64 = new Base64(); final Base64 base64 = new Base64();
String encodedString = new String(base64.encode(originalInput.getBytes())); final String encodedString = new String(base64.encode(originalInput.getBytes()));
String decodedString = new String(base64.decode(encodedString.getBytes())); final String decodedString = new String(base64.decode(encodedString.getBytes()));
assertNotNull(decodedString); assertNotNull(decodedString);
assertEquals(originalInput, decodedString); assertEquals(originalInput, decodedString);
@ -35,8 +37,8 @@ public class ApacheCommonsEncodeDecodeTest {
@Test @Test
public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException { public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes())); final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
assertNotNull(encodedString); assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString); assertNotEquals(originalInput, encodedString);
@ -44,10 +46,10 @@ public class ApacheCommonsEncodeDecodeTest {
@Test @Test
public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException { public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes())); final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
String decodedString = new String(Base64.decodeBase64(encodedString.getBytes())); final String decodedString = new String(Base64.decodeBase64(encodedString.getBytes()));
assertNotNull(decodedString); assertNotNull(decodedString);
assertEquals(originalInput, decodedString); assertEquals(originalInput, decodedString);

View File

@ -12,91 +12,94 @@ import org.junit.Test;
public class Java8EncodeDecodeTest { public class Java8EncodeDecodeTest {
// tests
@Test @Test
public void whenStringIsEncoded() throws UnsupportedEncodingException { public void whenStringIsEncoded() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
assertNotNull(encodedString); assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString); assertNotEquals(originalInput, encodedString);
} }
@Test @Test
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException { public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes()); final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
byte[] decodedBytes = Base64.getDecoder().decode(encodedString); final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes); final String decodedString = new String(decodedBytes);
assertNotNull(decodedString); assertNotNull(decodedString);
assertEquals(originalInput, decodedString); assertEquals(originalInput, decodedString);
} }
@Test @Test
public void whenStringIsEncodedWithoutPadding() throws UnsupportedEncodingException { public void whenStringIsEncodedWithoutPadding() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
assertNotNull(encodedString); assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString); assertNotEquals(originalInput, encodedString);
} }
@Test @Test
public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException { public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException {
String originalInput = "test input"; final String originalInput = "test input";
String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes()); final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
byte[] decodedBytes = Base64.getDecoder().decode(encodedString); final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes); final String decodedString = new String(decodedBytes);
assertNotNull(decodedString); assertNotNull(decodedString);
assertEquals(originalInput, decodedString); assertEquals(originalInput, decodedString);
} }
@Test @Test
public void whenURLIsEncoded() throws UnsupportedEncodingException { public void whenURLIsEncoded() throws UnsupportedEncodingException {
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; final String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); final String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
assertNotNull(encodedURL); assertNotNull(encodedURL);
assertNotEquals(originalURL, encodedURL); assertNotEquals(originalURL, encodedURL);
} }
@Test @Test
public void whenURLIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException { public void whenURLIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException {
String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java"; final String originalURL = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes()); final String encodedURL = Base64.getUrlEncoder().encodeToString(originalURL.getBytes());
byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes()); final byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedURL.getBytes());
String decodedURL = new String(decodedBytes); final String decodedURL = new String(decodedBytes);
assertNotNull(decodedURL); assertNotNull(decodedURL);
assertEquals(originalURL, decodedURL); assertEquals(originalURL, decodedURL);
} }
@Test @Test
public void whenMIMEIsEncoded() throws UnsupportedEncodingException { public void whenMIMEIsEncoded() throws UnsupportedEncodingException {
StringBuilder buffer = getMimeBuffer(); final StringBuilder buffer = getMimeBuffer();
byte[] forEncode = buffer.toString().getBytes(); final byte[] forEncode = buffer.toString().getBytes();
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
assertNotNull(encodedMime); assertNotNull(encodedMime);
} }
@Test @Test
public void whenMIMEIsEncoded_thenMIMECanBeDecoded() throws UnsupportedEncodingException { public void whenMIMEIsEncoded_thenMIMECanBeDecoded() throws UnsupportedEncodingException {
StringBuilder buffer = getMimeBuffer(); final StringBuilder buffer = getMimeBuffer();
byte[] forEncode = buffer.toString().getBytes(); final byte[] forEncode = buffer.toString().getBytes();
String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode); final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime); final byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime);
String decodedMime = new String(decodedBytes); final String decodedMime = new String(decodedBytes);
assertNotNull(decodedMime); assertNotNull(decodedMime);
} }
//
private static StringBuilder getMimeBuffer() { private static StringBuilder getMimeBuffer() {
StringBuilder buffer = new StringBuilder(); final StringBuilder buffer = new StringBuilder();
for (int count = 0; count < 10; ++count) { for (int count = 0; count < 10; ++count) {
buffer.append(UUID.randomUUID().toString()); buffer.append(UUID.randomUUID().toString());
} }