From 31b7ced8c4c96bbae3c1cabb12a9e0bfa821e40c Mon Sep 17 00:00:00 2001 From: nguyennamthai Date: Sun, 5 Mar 2017 17:19:42 +0700 Subject: [PATCH] Introduction to Apache Commons Lang 3 (#1301) --- apache-commons/pom.xml | 36 +++++ .../commons/lang3/ArrayUtilsTest.java | 137 ++++++++++++++++++ .../commons/lang3/StringUtilsTest.java | 99 +++++++++++++ 3 files changed, 272 insertions(+) create mode 100644 apache-commons/pom.xml create mode 100644 apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java create mode 100644 apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java diff --git a/apache-commons/pom.xml b/apache-commons/pom.xml new file mode 100644 index 0000000000..6e5c2065db --- /dev/null +++ b/apache-commons/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + apache-commons + 0.0.1-SNAPSHOT + + 4.12 + 3.6.0 + + + + + maven-compiler-plugin + ${compiler.version} + + 1.8 + 1.8 + + + + + + + org.apache.commons + commons-lang3 + 3.5 + + + junit + junit + ${junit.version} + test + + + \ No newline at end of file diff --git a/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java b/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java new file mode 100644 index 0000000000..b579458730 --- /dev/null +++ b/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java @@ -0,0 +1,137 @@ +package com.baeldung.commons.lang3; + +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; + +public class ArrayUtilsTest { + @Test + public void givenArray_whenAddingElementAtSpecifiedPosition_thenCorrect() { + int[] oldArray = { 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.add(oldArray, 0, 1); + int[] expectedArray = { 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenAddingElementAtTheEnd_thenCorrect() { + int[] oldArray = { 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.add(oldArray, 1); + int[] expectedArray = { 2, 3, 4, 5, 1 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenAddingAllElementsAtTheEnd_thenCorrect() { + int[] oldArray = { 0, 1, 2 }; + int[] newArray = ArrayUtils.addAll(oldArray, 3, 4, 5); + int[] expectedArray = { 0, 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingElementAtSpecifiedPosition_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.remove(oldArray, 1); + int[] expectedArray = { 1, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAllElementsAtSpecifiedPositions_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.removeAll(oldArray, 1, 3); + int[] expectedArray = { 1, 3, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAnElement_thenCorrect() { + int[] oldArray = { 1, 2, 3, 3, 4 }; + int[] newArray = ArrayUtils.removeElement(oldArray, 3); + int[] expectedArray = { 1, 2, 3, 4 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingElements_thenCorrect() { + int[] oldArray = { 1, 2, 3, 3, 4 }; + int[] newArray = ArrayUtils.removeElements(oldArray, 2, 3, 5); + int[] expectedArray = { 1, 3, 4 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAllElementOccurences_thenCorrect() { + int[] oldArray = { 1, 2, 2, 2, 3 }; + int[] newArray = ArrayUtils.removeAllOccurences(oldArray, 2); + int[] expectedArray = { 1, 3 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenCheckingExistingElement_thenCorrect() { + int[] array = { 1, 3, 5, 7, 9 }; + boolean evenContained = ArrayUtils.contains(array, 2); + boolean oddContained = ArrayUtils.contains(array, 7); + assertEquals(false, evenContained); + assertEquals(true, oddContained); + } + + @Test + public void givenArray_whenReversingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.reverse(originalArray, 1, 4); + int[] expectedArray = { 1, 4, 3, 2, 5 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenReversingAllElements_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.reverse(originalArray); + int[] expectedArray = { 5, 4, 3, 2, 1 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenShiftingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.shift(originalArray, 1, 4, 1); + int[] expectedArray = { 1, 4, 2, 3, 5 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenShiftingAllElements_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.shift(originalArray, 1); + int[] expectedArray = { 5, 1, 2, 3, 4 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenExtractingElements_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.subarray(oldArray, 2, 7); + int[] expectedArray = { 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenSwapingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.swap(originalArray, 0, 3, 2); + int[] expectedArray = { 4, 5, 3, 1, 2 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.swap(originalArray, 0, 3); + int[] expectedArray = { 4, 2, 3, 1, 5 }; + assertArrayEquals(expectedArray, originalArray); + } +} diff --git a/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java b/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java new file mode 100644 index 0000000000..a17a8ea60d --- /dev/null +++ b/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java @@ -0,0 +1,99 @@ +package com.baeldung.commons.lang3; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +public class StringUtilsTest { + @Test + public void givenString_whenCheckingContainsAny_thenCorrect() { + String string = "baeldung.com"; + boolean contained1 = StringUtils.containsAny(string, 'a', 'b', 'c'); + boolean contained2 = StringUtils.containsAny(string, 'x', 'y', 'z'); + boolean contained3 = StringUtils.containsAny(string, "abc"); + boolean contained4 = StringUtils.containsAny(string, "xyz"); + assertTrue(contained1); + assertFalse(contained2); + assertTrue(contained3); + assertFalse(contained4); + } + + @Test + public void givenString_whenCheckingContainsIgnoreCase_thenCorrect() { + String string = "baeldung.com"; + boolean contained = StringUtils.containsIgnoreCase(string, "BAELDUNG"); + assertTrue(contained); + } + + @Test + public void givenString_whenCountingMatches_thenCorrect() { + String string = "welcome to www.baeldung.com"; + int charNum = StringUtils.countMatches(string, 'w'); + int stringNum = StringUtils.countMatches(string, "com"); + assertEquals(4, charNum); + assertEquals(2, stringNum); + } + + @Test + public void givenString_whenAppendingAndPrependingIfMissing_thenCorrect() { + String string = "baeldung.com"; + String stringWithSuffix = StringUtils.appendIfMissing(string, ".com"); + String stringWithPrefix = StringUtils.prependIfMissing(string, "www."); + assertEquals("baeldung.com", stringWithSuffix); + assertEquals("www.baeldung.com", stringWithPrefix); + } + + @Test + public void givenString_whenSwappingCase_thenCorrect() { + String originalString = "baeldung.COM"; + String swappedString = StringUtils.swapCase(originalString); + assertEquals("BAELDUNG.com", swappedString); + } + + @Test + public void givenString_whenCapitalizing_thenCorrect() { + String originalString = "baeldung"; + String capitalizedString = StringUtils.capitalize(originalString); + assertEquals("Baeldung", capitalizedString); + } + + @Test + public void givenString_whenUncapitalizing_thenCorrect() { + String originalString = "Baeldung"; + String uncapitalizedString = StringUtils.uncapitalize(originalString); + assertEquals("baeldung", uncapitalizedString); + } + + @Test + public void givenString_whenReversingCharacters_thenCorrect() { + String originalString = "baeldung"; + String reversedString = StringUtils.reverse(originalString); + assertEquals("gnudleab", reversedString); + } + + @Test + public void givenString_whenReversingWithDelimiter_thenCorrect() { + String originalString = "www.baeldung.com"; + String reversedString = StringUtils.reverseDelimited(originalString, '.'); + assertEquals("com.baeldung.www", reversedString); + } + + @Test + public void givenString_whenRotatingTwoPositions_thenCorrect() { + String originalString = "baeldung"; + String rotatedString = StringUtils.rotate(originalString, 4); + assertEquals("dungbael", rotatedString); + } + + @Test + public void givenTwoStrings_whenComparing_thenCorrect() { + String tutorials = "Baeldung Tutorials"; + String courses = "Baeldung Courses"; + String diff1 = StringUtils.difference(tutorials, courses); + String diff2 = StringUtils.difference(courses, tutorials); + assertEquals("Courses", diff1); + assertEquals("Tutorials", diff2); + } +}