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
This commit is contained in:
Kiran 2017-08-28 21:48:44 +05:30 committed by adamd1985
parent c5d77f424d
commit 73526dd0a8
3 changed files with 180 additions and 2 deletions

View File

@ -522,7 +522,7 @@
<properties> <properties>
<multiverse.version>0.7.0</multiverse.version> <multiverse.version>0.7.0</multiverse.version>
<cglib.version>3.2.4</cglib.version> <cglib.version>3.2.4</cglib.version>
<commons-lang.version>3.5</commons-lang.version> <commons-lang.version>3.6</commons-lang.version>
<commons-text.version>1.1</commons-text.version> <commons-text.version>1.1</commons-text.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version> <commons-beanutils.version>1.9.3</commons-beanutils.version>
<commons-chain.version>1.2</commons-chain.version> <commons-chain.version>1.2</commons-chain.version>
@ -565,4 +565,4 @@
<vavr.version>0.9.0</vavr.version> <vavr.version>0.9.0</vavr.version>
<geotools.version>15.2</geotools.version> <geotools.version>15.2</geotools.version>
</properties> </properties>
</project> </project>

View File

@ -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());
}
}

View File

@ -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<Integer> 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();
}
}
}