/** * Tests extractCauseUnchecked() if the cause is a checked exception. */ @Test public void testExtractCauseUncheckedChecked() { final Exception ex = new Exception("Test"); final ConcurrentRuntimeException cex = ConcurrentUtils .extractCauseUnchecked(new ExecutionException(ex)); assertSame("Wrong cause", ex, cex.getCause()); }
/** * Tests handleCauseUnchecked() if the cause is a checked exception. */ @Test public void testHandleCauseUncheckedChecked() { final Exception ex = new Exception("Test"); try { ConcurrentUtils.handleCauseUnchecked(new ExecutionException(ex)); fail("ConcurrentRuntimeException not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertEquals("Wrong cause", ex, crex.getCause()); } }
/** * Tests whether exceptions are correctly handled by initializeUnchecked(). * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testInitializeUncheckedEx() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock .createMock(ConcurrentInitializer.class); final Exception cause = new Exception(); EasyMock.expect(init.get()).andThrow(new ConcurrentException(cause)); EasyMock.replay(init); try { ConcurrentUtils.initializeUnchecked(init); fail("Exception not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertSame("Wrong cause", cause, crex.getCause()); } EasyMock.verify(init); }
/** * Tests createIfAbsentUnchecked() if an exception is thrown. * * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it */ @Test public void testCreateIfAbsentUncheckedException() throws ConcurrentException { @SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock .createMock(ConcurrentInitializer.class); final Exception ex = new Exception(); EasyMock.expect(init.get()).andThrow(new ConcurrentException(ex)); EasyMock.replay(init); try { ConcurrentUtils.createIfAbsentUnchecked( new ConcurrentHashMap<String, Integer>(), "test", init); fail("Exception not thrown!"); } catch (final ConcurrentRuntimeException crex) { assertEquals("Wrong cause", ex, crex.getCause()); } EasyMock.verify(init); } }