Skip to content

Commit 73526dd

Browse files
kirangonellaadamd1985
authored andcommitted
BAEL - 971 Code Changes (eugenp#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
1 parent c5d77f4 commit 73526dd

3 files changed

Lines changed: 180 additions & 2 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@
522522
<properties>
523523
<multiverse.version>0.7.0</multiverse.version>
524524
<cglib.version>3.2.4</cglib.version>
525-
<commons-lang.version>3.5</commons-lang.version>
525+
<commons-lang.version>3.6</commons-lang.version>
526526
<commons-text.version>1.1</commons-text.version>
527527
<commons-beanutils.version>1.9.3</commons-beanutils.version>
528528
<commons-chain.version>1.2</commons-chain.version>
@@ -565,4 +565,4 @@
565565
<vavr.version>0.9.0</vavr.version>
566566
<geotools.version>15.2</geotools.version>
567567
</properties>
568-
</project>
568+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.commons.lang3;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
7+
public class BuilderMethods {
8+
9+
private final int intValue;
10+
private final String strSample;
11+
12+
public BuilderMethods(final int newId, final String newName) {
13+
this.intValue = newId;
14+
this.strSample = newName;
15+
}
16+
17+
public int getId() {
18+
return this.intValue;
19+
}
20+
21+
public String getName() {
22+
return this.strSample;
23+
}
24+
25+
@Override
26+
public int hashCode() {
27+
return new HashCodeBuilder().append(this.intValue)
28+
.append(this.strSample)
29+
.toHashCode();
30+
}
31+
32+
@Override
33+
public boolean equals(Object obj) {
34+
if (obj instanceof BuilderMethods == false) {
35+
return false;
36+
}
37+
if (this == obj) {
38+
return true;
39+
}
40+
final BuilderMethods otherObject = (BuilderMethods) obj;
41+
42+
return new EqualsBuilder().append(this.intValue, otherObject.intValue)
43+
.append(this.strSample, otherObject.strSample)
44+
.isEquals();
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return new ToStringBuilder(this).append("INTVALUE", this.intValue)
50+
.append("STRINGVALUE", this.strSample)
51+
.toString();
52+
}
53+
54+
public static void main(final String[] arguments) {
55+
final BuilderMethods simple1 = new BuilderMethods(1, "The First One");
56+
System.out.println(simple1.getName());
57+
System.out.println(simple1.hashCode());
58+
System.out.println(simple1.toString());
59+
}
60+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.baeldung.commons.lang3;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotSame;
6+
import static org.junit.Assert.assertSame;
7+
import static org.junit.Assert.assertTrue;
8+
import static org.junit.Assert.fail;
9+
10+
import java.beans.PropertyChangeEvent;
11+
import java.beans.PropertyChangeListener;
12+
import java.io.File;
13+
import java.lang.reflect.Field;
14+
import java.util.Locale;
15+
import java.util.concurrent.Future;
16+
17+
import org.apache.commons.lang3.ArchUtils;
18+
import org.apache.commons.lang3.BooleanUtils;
19+
import org.apache.commons.lang3.SystemUtils;
20+
import org.apache.commons.lang3.arch.Processor;
21+
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
22+
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
23+
import org.apache.commons.lang3.event.EventUtils;
24+
import org.apache.commons.lang3.reflect.FieldUtils;
25+
import org.apache.commons.lang3.time.FastDateFormat;
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
public class Lang3UtilsTest {
30+
31+
@Test
32+
public void test_to_Boolean_fromString() {
33+
assertFalse(BooleanUtils.toBoolean("off"));
34+
assertTrue(BooleanUtils.toBoolean("true"));
35+
assertTrue(BooleanUtils.toBoolean("tRue"));
36+
assertFalse(BooleanUtils.toBoolean("no"));
37+
assertFalse(BooleanUtils.isTrue(Boolean.FALSE));
38+
assertFalse(BooleanUtils.isTrue(null));
39+
}
40+
41+
@Test
42+
public void testGetUserHome() {
43+
final File dir = SystemUtils.getUserHome();
44+
Assert.assertNotNull(dir);
45+
Assert.assertTrue(dir.exists());
46+
}
47+
48+
@Test
49+
public void testGetJavaHome() {
50+
final File dir = SystemUtils.getJavaHome();
51+
Assert.assertNotNull(dir);
52+
Assert.assertTrue(dir.exists());
53+
}
54+
55+
@Test
56+
public void testProcessorArchType() {
57+
Processor processor = ArchUtils.getProcessor("x86");
58+
assertTrue(processor.is32Bit());
59+
assertFalse(processor.is64Bit());
60+
}
61+
62+
@Test
63+
public void testProcessorArchType64Bit() {
64+
Processor processor = ArchUtils.getProcessor("x86_64");
65+
assertFalse(processor.is32Bit());
66+
assertTrue(processor.is64Bit());
67+
}
68+
69+
@Test(expected = IllegalArgumentException.class)
70+
public void testConcurrentRuntimeExceptionCauseError() {
71+
new ConcurrentRuntimeException("An error", new Error());
72+
}
73+
74+
@Test
75+
public void testConstantFuture_Integer() throws Exception {
76+
Future<Integer> test = ConcurrentUtils.constantFuture(5);
77+
assertTrue(test.isDone());
78+
assertSame(5, test.get());
79+
assertFalse(test.isCancelled());
80+
}
81+
82+
@Test
83+
public void testFieldUtilsGetAllFields() {
84+
final Field[] fieldsNumber = Number.class.getDeclaredFields();
85+
assertArrayEquals(fieldsNumber, FieldUtils.getAllFields(Number.class));
86+
}
87+
88+
@Test
89+
public void test_getInstance_String_Locale() {
90+
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.US);
91+
final FastDateFormat format3 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
92+
93+
assertNotSame(format1, format3);
94+
}
95+
96+
@Test
97+
public void testAddEventListenerThrowsException() {
98+
final ExceptionEventSource src = new ExceptionEventSource();
99+
try {
100+
EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() {
101+
@Override
102+
public void propertyChange(final PropertyChangeEvent e) {
103+
// Do nothing!
104+
}
105+
});
106+
fail("Add method should have thrown an exception, so method should fail.");
107+
} catch (final RuntimeException e) {
108+
109+
}
110+
}
111+
112+
public static class ExceptionEventSource {
113+
public void addPropertyChangeListener(final PropertyChangeListener listener) {
114+
throw new RuntimeException();
115+
}
116+
}
117+
118+
}

0 commit comments

Comments
 (0)