BAEL-971 - An Introduction to Apache Commons Lang (#5165)

* Initial Commit

* Update SystemsUtilsUnitTest.java

* Update SystemsUtilsUnitTest.java

* Update ConstructorUtilsUnitTest.java

* Update FieldUtilsUnitTest.java

* Update ConstructorUtilsUnitTest.java

* Update MethodUtilsUnitTest.java

* Update BasicThreadFactoryUnitTest.java

* Update ConstructorUtilsUnitTest.java
This commit is contained in:
Alejandro Gervasio 2018-09-13 02:11:30 -03:00 committed by Grzegorz Piwowarek
parent 107e6a75be
commit 332061d8bd
19 changed files with 622 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.baeldung.commons.lang3.application;
public class Application {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.commons.lang3.beans;
public class User {
private final String name;
private final String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "User{" + "name=" + name + ", email=" + email + '}';
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.commons.lang3.beans;
import org.apache.commons.lang3.concurrent.LazyInitializer;
public class UserInitializer extends LazyInitializer<User> {
@Override
protected User initialize() {
return new User("John", "john@domain.com");
}
}

View File

@ -0,0 +1,84 @@
package com.baeldung.commons.lang3.test;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class ArrayUtilsUnitTest {
@Test
public void givenArrayUtilsClass_whenCalledtoString_thenCorrect() {
String[] array = {"a", "b", "c"};
assertThat(ArrayUtils.toString(array)).isEqualTo("{a,b,c}");
}
@Test
public void givenArrayUtilsClass_whenCalledtoStringIfArrayisNull_thenCorrect() {
String[] array = null;
assertThat(ArrayUtils.toString(array, "Array is null")).isEqualTo("Array is null");
}
@Test
public void givenArrayUtilsClass_whenCalledhashCode_thenCorrect() {
String[] array = {"a", "b", "c"};
assertThat(ArrayUtils.hashCode(array)).isEqualTo(997619);
}
@Test
public void givenArrayUtilsClass_whenCalledtoMap_thenCorrect() {
String[][] array = {{"1", "one", }, {"2", "two", }, {"3", "three"}};
Map map = new HashMap();
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
assertThat(ArrayUtils.toMap(array)).isEqualTo(map);
}
@Test
public void givenArrayUtilsClass_whenCallednullToEmptyStringArray_thenCorrect() {
String[] array = null;
assertThat(ArrayUtils.nullToEmpty(array)).isEmpty();
}
@Test
public void givenArrayUtilsClass_whenCallednullToEmptyObjectArray_thenCorrect() {
Object[] array = null;
assertThat(ArrayUtils.nullToEmpty(array)).isEmpty();
}
@Test
public void givenArrayUtilsClass_whenCalledsubarray_thenCorrect() {
int[] array = {1, 2, 3};
int[] expected = {1};
assertThat(ArrayUtils.subarray(array, 0, 1)).isEqualTo(expected);
}
@Test
public void givenArrayUtilsClass_whenCalledisSameLength_thenCorrect() {
int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
assertThat(ArrayUtils.isSameLength(array1, array2)).isTrue();
}
@Test
public void givenArrayUtilsClass_whenCalledreverse_thenCorrect() {
int[] array1 = {1, 2, 3};
int[] array2 = {3, 2, 1};
ArrayUtils.reverse(array1);
assertThat(array1).isEqualTo(array2);
}
@Test
public void givenArrayUtilsClass_whenCalledIndexOf_thenCorrect() {
int[] array = {1, 2, 3};
assertThat(ArrayUtils.indexOf(array, 1, 0)).isEqualTo(0);
}
@Test
public void givenArrayUtilsClass_whenCalledcontains_thenCorrect() {
int[] array = {1, 2, 3};
assertThat(ArrayUtils.contains(array, 1)).isTrue();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class BasicThreadFactoryUnitTest {
@Test
public void givenBasicThreadFactoryInstance_whenCalledBuilder_thenCorrect() {
BasicThreadFactory factory = new BasicThreadFactory.Builder()
.namingPattern("workerthread-%d")
.daemon(true)
.priority(Thread.MAX_PRIORITY)
.build();
assertThat(factory).isInstanceOf(BasicThreadFactory.class);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.commons.lang3.test;
import com.baeldung.commons.lang3.beans.User;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class ConstructorUtilsUnitTest {
@Test
public void givenConstructorUtilsClass_whenCalledgetAccessibleConstructor_thenCorrect() {
assertThat(ConstructorUtils.getAccessibleConstructor(User.class, String.class, String.class)).isInstanceOf(Constructor.class);
}
@Test
public void givenConstructorUtilsClass_whenCalledinvokeConstructor_thenCorrect() throws Exception {
assertThat(ConstructorUtils.invokeConstructor(User.class, "name", "email")).isInstanceOf(User.class);
}
@Test
public void givenConstructorUtilsClass_whenCalledinvokeExactConstructor_thenCorrect() throws Exception {
String[] args = {"name", "email"};
Class[] parameterTypes= {String.class, String.class};
assertThat(ConstructorUtils.invokeExactConstructor(User.class, args, parameterTypes)).isInstanceOf(User.class);
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.commons.lang3.test;
import com.baeldung.commons.lang3.beans.User;
import org.apache.commons.lang3.reflect.FieldUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class FieldUtilsUnitTest {
private static User user;
@BeforeClass
public static void setUpUserInstance() {
user = new User("Julie", "julie@domain.com");
}
@Test
public void givenFieldUtilsClass_whenCalledgetField_thenCorrect() {
assertThat(FieldUtils.getField(User.class, "name", true).getName()).isEqualTo("name");
}
@Test
public void givenFieldUtilsClass_whenCalledgetFieldForceAccess_thenCorrect() {
assertThat(FieldUtils.getField(User.class, "name", true).getName()).isEqualTo("name");
}
@Test
public void givenFieldUtilsClass_whenCalledgetDeclaredFieldForceAccess_thenCorrect() {
assertThat(FieldUtils.getDeclaredField(User.class, "name", true).getName()).isEqualTo("name");
}
@Test
public void givenFieldUtilsClass_whenCalledgetAllField_thenCorrect() {
assertThat(FieldUtils.getAllFields(User.class).length).isEqualTo(2);
}
@Test
public void givenFieldUtilsClass_whenCalledreadField_thenCorrect() throws IllegalAccessException {
assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie");
}
@Test
public void givenFieldUtilsClass_whenCalledreadDeclaredField_thenCorrect() throws IllegalAccessException {
assertThat(FieldUtils.readDeclaredField(user, "name", true)).isEqualTo("Julie");
}
@Test
public void givenFieldUtilsClass_whenCalledwriteField_thenCorrect() throws IllegalAccessException {
FieldUtils.writeField(user, "name", "Julie", true);
assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie");
}
@Test
public void givenFieldUtilsClass_whenCalledwriteDeclaredField_thenCorrect() throws IllegalAccessException {
FieldUtils.writeDeclaredField(user, "name", "Julie", true);
assertThat(FieldUtils.readField(user, "name", true)).isEqualTo("Julie");
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.math.Fraction;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class FractionUnitTest {
@Test
public void givenFractionClass_whenCalledgetFraction_thenCorrect() {
assertThat(Fraction.getFraction(5, 6)).isInstanceOf(Fraction.class);
}
@Test
public void givenTwoFractionInstances_whenCalledadd_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(1, 4);
Fraction fraction2 = Fraction.getFraction(3, 4);
assertThat(fraction1.add(fraction2).toString()).isEqualTo("1/1");
}
@Test
public void givenTwoFractionInstances_whenCalledsubstract_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(3, 4);
Fraction fraction2 = Fraction.getFraction(1, 4);
assertThat(fraction1.subtract(fraction2).toString()).isEqualTo("1/2");
}
@Test
public void givenTwoFractionInstances_whenCalledmultiply_thenCorrect() {
Fraction fraction1 = Fraction.getFraction(3, 4);
Fraction fraction2 = Fraction.getFraction(1, 4);
assertThat(fraction1.multiplyBy(fraction2).toString()).isEqualTo("3/16");
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class HashCodeBuilderUnitTest {
@Test
public void givenHashCodeBuilderInstance_whenCalledtoHashCode_thenCorrect() {
int hashcode = new HashCodeBuilder(17, 37)
.append("John")
.append("john@domain.com")
.toHashCode();
assertThat(hashcode).isEqualTo(1269178828);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.tuple.ImmutablePair;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class ImmutablePairUnitTest {
private static ImmutablePair<String, String> immutablePair;
@BeforeClass
public static void setUpImmutablePairInstance() {
immutablePair = new ImmutablePair<>("leftElement", "rightElement");
}
@Test
public void givenImmutablePairInstance_whenCalledgetLeft_thenCorrect() {
assertThat(immutablePair.getLeft()).isEqualTo("leftElement");
}
@Test
public void givenImmutablePairInstance_whenCalledgetRight_thenCorrect() {
assertThat(immutablePair.getRight()).isEqualTo("rightElement");
}
@Test
public void givenImmutablePairInstance_whenCalledof_thenCorrect() {
assertThat(ImmutablePair.of("leftElement", "rightElement")).isInstanceOf(ImmutablePair.class);
}
@Test(expected = UnsupportedOperationException.class)
public void givenImmutablePairInstance_whenCalledSetValue_thenThrowUnsupportedOperationException() {
immutablePair.setValue("newValue");
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class ImmutableTripleUnitTest {
private static ImmutableTriple<String, String, String> immutableTriple;
@BeforeClass
public static void setUpImmutableTripleInstance() {
immutableTriple = new ImmutableTriple<>("leftElement", "middleElement", "rightElement");
}
@Test
public void givenImmutableTripleInstance_whenCalledgetLeft_thenCorrect() {
assertThat(immutableTriple.getLeft()).isEqualTo("leftElement");
}
@Test
public void givenImmutableTripleInstance_whenCalledgetMiddle_thenCorrect() {
assertThat(immutableTriple.getMiddle()).isEqualTo("middleElement");
}
@Test
public void givenImmutableInstance_whenCalledgetRight_thenCorrect() {
assertThat(immutableTriple.getRight()).isEqualTo("rightElement");
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.commons.lang3.test;
import com.baeldung.commons.lang3.beans.User;
import com.baeldung.commons.lang3.beans.UserInitializer;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class LazyInitializerUnitTest {
@Test
public void givenLazyInitializerInstance_whenCalledget_thenCorrect() throws ConcurrentException {
UserInitializer userInitializer = new UserInitializer();
assertThat(userInitializer.get()).isInstanceOf(User.class);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.commons.lang3.test;
import com.baeldung.commons.lang3.beans.User;
import java.lang.reflect.Method;
import org.apache.commons.lang3.reflect.MethodUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class MethodUtilsUnitTest {
@Test
public void givenMethodUtilsClass_whenCalledgetAccessibleMethod_thenCorrect() {
assertThat(MethodUtils.getAccessibleMethod(User.class, "getName")).isInstanceOf(Method.class);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.mutable.MutableObject;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class MutableObjectUnitTest {
private static MutableObject mutableObject;
@BeforeClass
public static void setUpMutableObject() {
mutableObject = new MutableObject("Initial value");
}
@Test
public void givenMutableObject_whenCalledgetValue_thenCorrect() {
assertThat(mutableObject.getValue()).isInstanceOf(String.class);
}
@Test
public void givenMutableObject_whenCalledsetValue_thenCorrect() {
mutableObject.setValue("Another value");
assertThat(mutableObject.getValue()).isEqualTo("Another value");
}
@Test
public void givenMutableObject_whenCalledtoString_thenCorrect() {
assertThat(mutableObject.toString()).isEqualTo("Another value");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.tuple.MutablePair;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class MutablePairUnitTest {
private static MutablePair<String, String> mutablePair;
@BeforeClass
public static void setUpMutablePairInstance() {
mutablePair = new MutablePair<>("leftElement", "rightElement");
}
@Test
public void givenMutablePairInstance_whenCalledgetLeft_thenCorrect() {
assertThat(mutablePair.getLeft()).isEqualTo("leftElement");
}
@Test
public void givenMutablePairInstance_whenCalledgetRight_thenCorrect() {
assertThat(mutablePair.getRight()).isEqualTo("rightElement");
}
@Test
public void givenMutablePairInstance_whenCalledsetLeft_thenCorrect() {
mutablePair.setLeft("newLeftElement");
assertThat(mutablePair.getLeft()).isEqualTo("newLeftElement");
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.math.NumberUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class NumberUtilsUnitTest {
@Test
public void givenNumberUtilsClass_whenCalledcompareWithIntegers_thenCorrect() {
assertThat(NumberUtils.compare(1, 1)).isEqualTo(0);
}
@Test
public void givenNumberUtilsClass_whenCalledcompareWithLongs_thenCorrect() {
assertThat(NumberUtils.compare(1L, 1L)).isEqualTo(0);
}
@Test
public void givenNumberUtilsClass_whenCalledcreateNumber_thenCorrect() {
assertThat(NumberUtils.createNumber("123456")).isEqualTo(123456);
}
@Test
public void givenNumberUtilsClass_whenCalledisDigits_thenCorrect() {
assertThat(NumberUtils.isDigits("123456")).isTrue();
}
@Test
public void givenNumberUtilsClass_whenCallemaxwithIntegerArray_thenCorrect() {
int[] array = {1, 2, 3, 4, 5, 6};
assertThat(NumberUtils.max(array)).isEqualTo(6);
}
@Test
public void givenNumberUtilsClass_whenCalleminwithIntegerArray_thenCorrect() {
int[] array = {1, 2, 3, 4, 5, 6};
assertThat(NumberUtils.min(array)).isEqualTo(1);
}
@Test
public void givenNumberUtilsClass_whenCalleminwithByteArray_thenCorrect() {
byte[] array = {1, 2, 3, 4, 5, 6};
assertThat(NumberUtils.min(array)).isEqualTo((byte) 1);
}
}

View File

@ -0,0 +1,73 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class StringUtilsUnitTest {
@Test
public void givenStringUtilsClass_whenCalledisBlank_thenCorrect() {
assertThat(StringUtils.isBlank(" ")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisEmpty_thenCorrect() {
assertThat(StringUtils.isEmpty("")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisAllLowerCase_thenCorrect() {
assertThat(StringUtils.isAllLowerCase("abd")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisAllUpperCase_thenCorrect() {
assertThat(StringUtils.isAllUpperCase("ABC")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisMixedCase_thenCorrect() {
assertThat(StringUtils.isMixedCase("abC")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisAlpha_thenCorrect() {
assertThat(StringUtils.isAlpha("abc")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledisAlphanumeric_thenCorrect() {
assertThat(StringUtils.isAlphanumeric("abc123")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledcontains_thenCorrect() {
assertThat(StringUtils.contains("abc", "ab")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledcontainsAny_thenCorrect() {
assertThat(StringUtils.containsAny("abc", 'a', 'b', 'c')).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledcontainsIgnoreCase_thenCorrect() {
assertThat(StringUtils.containsIgnoreCase("abc", "ABC")).isTrue();
}
@Test
public void givenStringUtilsClass_whenCalledswapCase_thenCorrect() {
assertThat(StringUtils.swapCase("abc")).isEqualTo("ABC");
}
@Test
public void givenStringUtilsClass_whenCalledreverse_thenCorrect() {
assertThat(StringUtils.reverse("abc")).isEqualTo("cba");
}
@Test
public void givenStringUtilsClass_whenCalleddifference_thenCorrect() {
assertThat(StringUtils.difference("abc", "abd")).isEqualTo("d");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.commons.lang3.test;
import java.io.File;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class SystemsUtilsUnitTest {
@Test
public void givenSystemUtilsClass_whenCalledgetJavaHome_thenCorrect() {
assertThat(SystemUtils.getJavaHome()).isEqualTo(new File("/usr/lib/jvm/java-8-oracle/jre"));
}
@Test
public void givenSystemUtilsClass_whenCalledgetUserHome_thenCorrect() {
assertThat(SystemUtils.getUserHome()).isEqualTo(new File("/home/travis"));
}
@Test
public void givenSystemUtilsClass_whenCalledisJavaVersionAtLeast_thenCorrect() {
assertThat(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_RECENT)).isTrue();
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.commons.lang3.test;
import org.apache.commons.lang3.tuple.Triple;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.BeforeClass;
import org.junit.Test;
public class TripleUnitTest {
private static Triple<String, String, String> triple;
@BeforeClass
public static void setUpTripleInstance() {
triple = Triple.of("leftElement", "middleElement", "rightElement");
}
@Test
public void givenTripleInstance_whenCalledgetLeft_thenCorrect() {
assertThat(triple.getLeft()).isEqualTo("leftElement");
}
@Test
public void givenTripleInstance_whenCalledgetMiddle_thenCorrect() {
assertThat(triple.getMiddle()).isEqualTo("middleElement");
}
@Test
public void givenTripleInstance_whenCalledgetRight_thenCorrect() {
assertThat(triple.getRight()).isEqualTo("rightElement");
}
}