Updated Lang3Utils.java (#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
This commit is contained in:
parent
f3557dec41
commit
a3f70aafe4
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.commons.lang3;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.concurrent.LazyInitializer;
|
||||||
|
|
||||||
|
public class SampleLazyInitializer extends LazyInitializer<SampleObject> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SampleObject initialize() {
|
||||||
|
return new SampleObject();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.commons.lang3;
|
||||||
|
|
||||||
|
public class SampleObject {
|
||||||
|
|
||||||
|
//Ignored
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.commons.lang3;
|
package com.baeldung.commons.lang3;
|
||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotSame;
|
import static org.junit.Assert.assertNotSame;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
@ -12,12 +13,14 @@ import java.beans.PropertyChangeListener;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
import org.apache.commons.lang3.ArchUtils;
|
import org.apache.commons.lang3.ArchUtils;
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
import org.apache.commons.lang3.SystemUtils;
|
import org.apache.commons.lang3.SystemUtils;
|
||||||
import org.apache.commons.lang3.arch.Processor;
|
import org.apache.commons.lang3.arch.Processor;
|
||||||
|
import org.apache.commons.lang3.concurrent.ConcurrentException;
|
||||||
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
|
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
|
||||||
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
|
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
|
||||||
import org.apache.commons.lang3.event.EventUtils;
|
import org.apache.commons.lang3.event.EventUtils;
|
||||||
@ -97,22 +100,37 @@ public class Lang3UtilsTest {
|
|||||||
public void testAddEventListenerThrowsException() {
|
public void testAddEventListenerThrowsException() {
|
||||||
final ExceptionEventSource src = new ExceptionEventSource();
|
final ExceptionEventSource src = new ExceptionEventSource();
|
||||||
try {
|
try {
|
||||||
EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() {
|
EventUtils.addEventListener(src, PropertyChangeListener.class, (PropertyChangeEvent e) -> {
|
||||||
@Override
|
/* Change event*/});
|
||||||
public void propertyChange(final PropertyChangeEvent e) {
|
|
||||||
// Do nothing!
|
|
||||||
}
|
|
||||||
});
|
|
||||||
fail("Add method should have thrown an exception, so method should fail.");
|
fail("Add method should have thrown an exception, so method should fail.");
|
||||||
} catch (final RuntimeException e) {
|
} catch (final RuntimeException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ConcurrentExceptionSample() throws ConcurrentException {
|
||||||
|
final Error err = new AssertionError("Test");
|
||||||
|
try {
|
||||||
|
ConcurrentUtils.handleCause(new ExecutionException(err));
|
||||||
|
fail("Error not thrown!");
|
||||||
|
} catch (final Error e) {
|
||||||
|
assertEquals("Wrong error", err, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class ExceptionEventSource {
|
public static class ExceptionEventSource {
|
||||||
public void addPropertyChangeListener(final PropertyChangeListener listener) {
|
public void addPropertyChangeListener(final PropertyChangeListener listener) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLazyInitializer() throws Exception {
|
||||||
|
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
|
||||||
|
SampleObject sampleObjectOne = sampleLazyInitializer.get();
|
||||||
|
SampleObject sampleObjectTwo = sampleLazyInitializer.get();
|
||||||
|
assertEquals(sampleObjectOne, sampleObjectTwo);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user