Skip to content

Commit a3f70aa

Browse files
kirangonellaadamd1985
authored andcommitted
Updated Lang3Utils.java (eugenp#2584)
* Updated Lang3Utils.java Added a new test for ConcurrentException and updated other exception with Lambda * LazyInitializer sample files * Updated file with LazyInitializer Unit Test
1 parent f3557de commit a3f70aa

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.commons.lang3;
2+
3+
import org.apache.commons.lang3.concurrent.LazyInitializer;
4+
5+
public class SampleLazyInitializer extends LazyInitializer<SampleObject> {
6+
7+
@Override
8+
protected SampleObject initialize() {
9+
return new SampleObject();
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.commons.lang3;
2+
3+
public class SampleObject {
4+
5+
//Ignored
6+
7+
}

‎libraries/src/test/java/com/baeldung/commons/lang3/Lang3UtilsTest.java‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.commons.lang3;
22

33
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotSame;
67
import static org.junit.Assert.assertSame;
@@ -12,12 +13,14 @@
1213
import java.io.File;
1314
import java.lang.reflect.Field;
1415
import java.util.Locale;
16+
import java.util.concurrent.ExecutionException;
1517
import java.util.concurrent.Future;
1618

1719
import org.apache.commons.lang3.ArchUtils;
1820
import org.apache.commons.lang3.BooleanUtils;
1921
import org.apache.commons.lang3.SystemUtils;
2022
import org.apache.commons.lang3.arch.Processor;
23+
import org.apache.commons.lang3.concurrent.ConcurrentException;
2124
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
2225
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
2326
import org.apache.commons.lang3.event.EventUtils;
@@ -97,22 +100,37 @@ public void test_getInstance_String_Locale() {
97100
public void testAddEventListenerThrowsException() {
98101
final ExceptionEventSource src = new ExceptionEventSource();
99102
try {
100-
EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() {
101-
@Override
102-
public void propertyChange(final PropertyChangeEvent e) {
103-
// Do nothing!
104-
}
105-
});
103+
EventUtils.addEventListener(src, PropertyChangeListener.class, (PropertyChangeEvent e) -> {
104+
/* Change event*/});
106105
fail("Add method should have thrown an exception, so method should fail.");
107106
} catch (final RuntimeException e) {
108107

109108
}
110109
}
111110

111+
@Test
112+
public void ConcurrentExceptionSample() throws ConcurrentException {
113+
final Error err = new AssertionError("Test");
114+
try {
115+
ConcurrentUtils.handleCause(new ExecutionException(err));
116+
fail("Error not thrown!");
117+
} catch (final Error e) {
118+
assertEquals("Wrong error", err, e);
119+
}
120+
}
121+
112122
public static class ExceptionEventSource {
113123
public void addPropertyChangeListener(final PropertyChangeListener listener) {
114124
throw new RuntimeException();
115125
}
116126
}
117127

128+
@Test
129+
public void testLazyInitializer() throws Exception {
130+
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
131+
SampleObject sampleObjectOne = sampleLazyInitializer.get();
132+
SampleObject sampleObjectTwo = sampleLazyInitializer.get();
133+
assertEquals(sampleObjectOne, sampleObjectTwo);
134+
}
135+
118136
}

0 commit comments

Comments
 (0)