From 73526dd0a8f44dc87040df3ba0a8c0eb3b9aa39b Mon Sep 17 00:00:00 2001 From: Kiran Date: Mon, 28 Aug 2017 21:48:44 +0530 Subject: [PATCH] BAEL - 971 Code Changes (#2492) * BAEL-971 Code BAEL- 971 Introduction to Apache Commons Lang3 code * BAEL - 971 Code BAEL - 971 : Introduction to Apache Commons Lang3 * Updated the commons.lang version from V3.5 to V3.6 --- libraries/pom.xml | 4 +- .../commons/lang3/BuilderMethods.java | 60 +++++++++ .../commons/lang3/Lang3UtilsTest.java | 118 ++++++++++++++++++ 3 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java create mode 100644 libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 6d1098246e..cf16f0fb79 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -522,7 +522,7 @@ 0.7.0 3.2.4 - 3.5 + 3.6 1.1 1.9.3 1.2 @@ -565,4 +565,4 @@ 0.9.0 15.2 - \ No newline at end of file + diff --git a/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java new file mode 100644 index 0000000000..c64f7e7511 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/lang3/BuilderMethods.java @@ -0,0 +1,60 @@ +package com.baeldung.commons.lang3; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class BuilderMethods { + + private final int intValue; + private final String strSample; + + public BuilderMethods(final int newId, final String newName) { + this.intValue = newId; + this.strSample = newName; + } + + public int getId() { + return this.intValue; + } + + public String getName() { + return this.strSample; + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(this.intValue) + .append(this.strSample) + .toHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof BuilderMethods == false) { + return false; + } + if (this == obj) { + return true; + } + final BuilderMethods otherObject = (BuilderMethods) obj; + + return new EqualsBuilder().append(this.intValue, otherObject.intValue) + .append(this.strSample, otherObject.strSample) + .isEquals(); + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("INTVALUE", this.intValue) + .append("STRINGVALUE", this.strSample) + .toString(); + } + + public static void main(final String[] arguments) { + final BuilderMethods simple1 = new BuilderMethods(1, "The First One"); + System.out.println(simple1.getName()); + System.out.println(simple1.hashCode()); + System.out.println(simple1.toString()); + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java new file mode 100644 index 0000000000..2e74ad3c24 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java @@ -0,0 +1,118 @@ +package com.baeldung.commons.lang3; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.File; +import java.lang.reflect.Field; +import java.util.Locale; +import java.util.concurrent.Future; + +import org.apache.commons.lang3.ArchUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.lang3.arch.Processor; +import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException; +import org.apache.commons.lang3.concurrent.ConcurrentUtils; +import org.apache.commons.lang3.event.EventUtils; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.commons.lang3.time.FastDateFormat; +import org.junit.Assert; +import org.junit.Test; + +public class Lang3UtilsTest { + + @Test + public void test_to_Boolean_fromString() { + assertFalse(BooleanUtils.toBoolean("off")); + assertTrue(BooleanUtils.toBoolean("true")); + assertTrue(BooleanUtils.toBoolean("tRue")); + assertFalse(BooleanUtils.toBoolean("no")); + assertFalse(BooleanUtils.isTrue(Boolean.FALSE)); + assertFalse(BooleanUtils.isTrue(null)); + } + + @Test + public void testGetUserHome() { + final File dir = SystemUtils.getUserHome(); + Assert.assertNotNull(dir); + Assert.assertTrue(dir.exists()); + } + + @Test + public void testGetJavaHome() { + final File dir = SystemUtils.getJavaHome(); + Assert.assertNotNull(dir); + Assert.assertTrue(dir.exists()); + } + + @Test + public void testProcessorArchType() { + Processor processor = ArchUtils.getProcessor("x86"); + assertTrue(processor.is32Bit()); + assertFalse(processor.is64Bit()); + } + + @Test + public void testProcessorArchType64Bit() { + Processor processor = ArchUtils.getProcessor("x86_64"); + assertFalse(processor.is32Bit()); + assertTrue(processor.is64Bit()); + } + + @Test(expected = IllegalArgumentException.class) + public void testConcurrentRuntimeExceptionCauseError() { + new ConcurrentRuntimeException("An error", new Error()); + } + + @Test + public void testConstantFuture_Integer() throws Exception { + Future test = ConcurrentUtils.constantFuture(5); + assertTrue(test.isDone()); + assertSame(5, test.get()); + assertFalse(test.isCancelled()); + } + + @Test + public void testFieldUtilsGetAllFields() { + final Field[] fieldsNumber = Number.class.getDeclaredFields(); + assertArrayEquals(fieldsNumber, FieldUtils.getAllFields(Number.class)); + } + + @Test + public void test_getInstance_String_Locale() { + final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.US); + final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY); + + assertNotSame(format1, format3); + } + + @Test + public void testAddEventListenerThrowsException() { + final ExceptionEventSource src = new ExceptionEventSource(); + try { + EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { + @Override + public void propertyChange(final PropertyChangeEvent e) { + // Do nothing! + } + }); + fail("Add method should have thrown an exception, so method should fail."); + } catch (final RuntimeException e) { + + } + } + + public static class ExceptionEventSource { + public void addPropertyChangeListener(final PropertyChangeListener listener) { + throw new RuntimeException(); + } + } + +}