Java tutorial
/* * Copyright (C) 2016 FormKiQ Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.formkiq.core.util; import static com.formkiq.core.util.Resources.getResourceAsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.util.UUID; import org.apache.commons.lang3.tuple.Pair; import org.junit.Test; /** * Test cases for Strings. * */ public class StringsTest { /** * Test Base64 Image to Bytes and back. * @throws IOException IOException */ @Test public void testBase64StringToImg01() throws IOException { // given String sig = getResourceAsString("/signature.txt"); // when Pair<byte[], String> result = Strings.base64StringToImg(sig); // then final int len = 4541; assertEquals(len, result.getLeft().length); assertEquals("png", result.getRight()); // when String imgsrc = Strings.bytesToImg(result.getLeft(), result.getRight()); // then String expect = "data:image/png;base64," + "iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAA"; assertTrue(sig.startsWith(expect)); assertTrue(imgsrc.startsWith(expect)); } /** * testDecode01(). */ @Test public void testDecode01() { // given String s = "dGVzdDpwYXNz"; String deliminator = Strings.DEFAULT_DELIM; // when String[] results = Strings.decode(s, deliminator); // then assertEquals(2, results.length); assertEquals("test", results[0]); assertEquals("pass", results[1]); } /** * testEncode01(). */ @Test public void testEncode01() { // given String s0 = "test"; String s1 = "pass"; String deliminator = Strings.DEFAULT_DELIM; // when String result = Strings.encode(s0, s1, deliminator); // then assertEquals("dGVzdDpwYXNz", result); } /** * testExtractLabelAndValue01(). */ @Test public void testExtractLabelAndValue01() { // given String s = "name[1]"; // when Pair<String, String> results = Strings.extractLabelAndValue(s); // then assertEquals("name", results.getLeft()); assertEquals("1", results.getRight()); } /** * testExtractLabelAndValue02(). */ @Test public void testExtractLabelAndValue02() { // given String s = "name"; // when Pair<String, String> results = Strings.extractLabelAndValue(s); // then assertEquals("name", results.getLeft()); assertEquals("name", results.getRight()); } /** * testExtractLabelAndValue03(). */ @Test public void testExtractLabelAndValue03() { // given String s = null; // when Pair<String, String> results = Strings.extractLabelAndValue(s); // then assertEquals("", results.getLeft()); assertEquals("", results.getRight()); } /** * testGenerateOffsetToken01(). */ @Test public void testGenerateOffsetToken01() { // given final int offset = 10; final int maxResults = 25; // when String result = Strings.generateOffsetToken(offset, maxResults); // then assertEquals("MTA6MjU=", result); } /** * testGetMaxResults01(). */ @Test public void testGetMaxResults01() { // given String token = "MTA6MjU="; final int maxResults = 25; final int defaultMaxResults = 100; // when int result = Strings.getMaxResults(token, defaultMaxResults); // then assertEquals(maxResults, result); } /** * testGetMaxResults02(). * bad token */ @Test public void testGetMaxResults02() { // given String token = "MTAa6MjU="; final int defaultMaxResults = 100; // when int result = Strings.getMaxResults(token, defaultMaxResults); // then assertEquals(defaultMaxResults, result); } /** * testGetMaxResults03(). * null */ @Test public void testGetMaxResults03() { // given String token = null; final int defaultMaxResults = 100; // when int result = Strings.getMaxResults(token, defaultMaxResults); // then assertEquals(defaultMaxResults, result); } /** * testGetOffset01(). */ @Test public void testGetOffset01() { // given String token = "MTA6MjU="; final int offset = 10; // when int result = Strings.getOffset(token); // then assertEquals(offset, result); } /** * testGetOffset02(). * bad token */ @Test public void testGetOffset02() { // given String token = "MTAa6MjU="; // when int result = Strings.getOffset(token); // then assertEquals(0, result); } /** * testGetOffset03(). * bad token */ @Test public void testGetOffset03() { // given String token = null; // when int result = Strings.getOffset(token); // then assertEquals(0, result); } /** * testHasAtLeast1Letter01(). */ @Test public void testHasAtLeast1Letter01() { // given String s = "as123fvdvf"; // when boolean result = Strings.hasAtLeast1Letter(s); // then assertTrue(result); } /** * testHasAtLeast1Letter02(). */ @Test public void testHasAtLeast1Letter02() { // given String s = "- - -"; // when boolean result = Strings.hasAtLeast1Letter(s); // then assertFalse(result); } /** * Valid. */ @Test public void testIsUUID01() { // given String uuid = UUID.randomUUID().toString(); // when boolean result = Strings.isUUID(uuid); // then assertTrue(result); } /** * In Valid. */ @Test public void testIsUUID02() { // given String uuid = "asda"; // when boolean result = Strings.isUUID(uuid); // then assertFalse(result); } }