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:
Kiran 2017-09-16 22:19:21 +05:30 committed by adamd1985
parent f3557dec41
commit a3f70aafe4
3 changed files with 42 additions and 6 deletions

View File

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

View File

@ -0,0 +1,7 @@
package com.baeldung.commons.lang3;
public class SampleObject {
//Ignored
}

View File

@ -1,6 +1,7 @@
package com.baeldung.commons.lang3;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
@ -12,12 +13,14 @@ import java.beans.PropertyChangeListener;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
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.ConcurrentException;
import org.apache.commons.lang3.concurrent.ConcurrentRuntimeException;
import org.apache.commons.lang3.concurrent.ConcurrentUtils;
import org.apache.commons.lang3.event.EventUtils;
@ -97,22 +100,37 @@ public class Lang3UtilsTest {
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!
}
});
EventUtils.addEventListener(src, PropertyChangeListener.class, (PropertyChangeEvent e) -> {
/* Change event*/});
fail("Add method should have thrown an exception, so method should fail.");
} 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 void addPropertyChangeListener(final PropertyChangeListener listener) {
throw new RuntimeException();
}
}
@Test
public void testLazyInitializer() throws Exception {
SampleLazyInitializer sampleLazyInitializer = new SampleLazyInitializer();
SampleObject sampleObjectOne = sampleLazyInitializer.get();
SampleObject sampleObjectTwo = sampleLazyInitializer.get();
assertEquals(sampleObjectOne, sampleObjectTwo);
}
}