BAEL-17341 Slice 8 | The top 100 articles should have their own package in the module (#7664)

This commit is contained in:
Dhawal Kapil
2019-08-29 20:16:51 +05:30
committed by Josh Cummings
parent 6b76d5ed0e
commit e8f83431b3
9 changed files with 11 additions and 11 deletions

View File

@@ -0,0 +1,56 @@
package com.baeldung.base64encodinganddecoding;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import static org.junit.Assert.*;
public class ApacheCommonsEncodeDecodeUnitTest {
// tests
@Test
public void whenStringIsEncoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final Base64 base64 = new Base64();
final String encodedString = new String(base64.encode(originalInput.getBytes()));
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final Base64 base64 = new Base64();
final String encodedString = new String(base64.encode(originalInput.getBytes()));
final String decodedString = new String(base64.decode(encodedString.getBytes()));
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
@Test
public void whenStringIsEncodedUsingStaticMethod() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncodedUsingStaticMethod_thenStringCanBeDecodedUsingStaticMethod() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
final String decodedString = new String(Base64.decodeBase64(encodedString.getBytes()));
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
}

View File

@@ -0,0 +1,109 @@
package com.baeldung.base64encodinganddecoding;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.UUID;
import static org.junit.Assert.*;
public class Java8EncodeDecodeUnitTest {
// tests
@Test
public void whenStringIsEncoded_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncoded_thenStringCanBeDecoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
final String decodedString = new String(decodedBytes);
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
@Test
public void whenStringIsEncodedWithoutPadding_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
assertNotNull(encodedString);
assertNotEquals(originalInput, encodedString);
}
@Test
public void whenStringIsEncodedWithoutPadding_thenStringCanBeDecoded() throws UnsupportedEncodingException {
final String originalInput = "test input";
final String encodedString = Base64.getEncoder().withoutPadding().encodeToString(originalInput.getBytes());
final byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
final String decodedString = new String(decodedBytes);
assertNotNull(decodedString);
assertEquals(originalInput, decodedString);
}
@Test
public void whenUrlIsEncoded_thenOk() throws UnsupportedEncodingException {
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
assertNotNull(encodedUrl);
assertNotEquals(originalUrl, encodedUrl);
}
@Test
public void whenUrlIsEncoded_thenURLCanBeDecoded() throws UnsupportedEncodingException {
final String originalUrl = "https://www.google.co.nz/?gfe_rd=cr&ei=dzbFVf&gws_rd=ssl#q=java";
final String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
final byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl.getBytes());
final String decodedUrl = new String(decodedBytes);
assertNotNull(decodedUrl);
assertEquals(originalUrl, decodedUrl);
}
@Test
public void whenMimeIsEncoded_thenOk() throws UnsupportedEncodingException {
final StringBuilder buffer = getMimeBuffer();
final byte[] forEncode = buffer.toString().getBytes();
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
assertNotNull(encodedMime);
}
@Test
public void whenMimeIsEncoded_thenItCanBeDecoded() throws UnsupportedEncodingException {
final StringBuilder buffer = getMimeBuffer();
final byte[] forEncode = buffer.toString().getBytes();
final String encodedMime = Base64.getMimeEncoder().encodeToString(forEncode);
final byte[] decodedBytes = Base64.getMimeDecoder().decode(encodedMime);
final String decodedMime = new String(decodedBytes);
assertNotNull(decodedMime);
}
//
private static StringBuilder getMimeBuffer() {
final StringBuilder buffer = new StringBuilder();
for (int count = 0; count < 10; ++count) {
buffer.append(UUID.randomUUID().toString());
}
return buffer;
}
}

View File

@@ -0,0 +1,59 @@
package com.baeldung.base64encodinganddecoding;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import javax.xml.bind.DatatypeConverter;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenConvertStringToByteArrayUsingStringClass_thenOk() {
final String originalInput = "test input";
byte[] result = originalInput.getBytes();
System.out.println(Arrays.toString(result));
assertEquals(originalInput.length(), result.length);
}
@Test
public void givenCharset_whenConvertStringToByteArrayUsingStringClass_thenOk() throws UnsupportedEncodingException {
final String originalInput = "test input";
byte[] result = originalInput.getBytes(StandardCharsets.UTF_16);
System.out.println(Arrays.toString(result));
assertTrue(originalInput.length() < result.length);
}
@Test
public void whenConvertStringToByteArrayUsingBase64Decoder_thenOk() {
final String originalInput = "dGVzdCBpbnB1dA==";
byte[] result = Base64.getDecoder().decode(originalInput);
assertEquals("test input", new String(result));
}
@Test
public void whenConvertStringToByteArrayUsingDatatypeConverter_thenOk() {
final String originalInput = "dGVzdCBpbnB1dA==";
byte[] result = DatatypeConverter.parseBase64Binary(originalInput);
assertEquals("test input", new String(result));
}
@Test
public void whenConvertStringToByteArray_thenOk(){
String originalInput = "7465737420696E707574";
byte[] result = DatatypeConverter.parseHexBinary(originalInput);
System.out.println(Arrays.toString(result));
assertEquals("test input", new String(result));
}
}