Unnecessary @SuppressWarnings("unchecked")

This commit is contained in:
Gary Gregory 2022-03-07 15:35:51 -05:00
parent faa24452b5
commit 1230298044
1 changed files with 26 additions and 62 deletions

View File

@ -107,8 +107,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testExtractCauseError() { public void testExtractCauseError() {
final Error err = new AssertionError("Test"); final Error err = new AssertionError("Test");
final AssertionError e = final AssertionError e = assertThrows(AssertionError.class, () -> ConcurrentUtils.extractCause(new ExecutionException(err)));
assertThrows(AssertionError.class, () -> ConcurrentUtils.extractCause(new ExecutionException(err)));
assertEquals(err, e, "Wrong error"); assertEquals(err, e, "Wrong error");
} }
@ -127,8 +126,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testExtractCauseChecked() { public void testExtractCauseChecked() {
final Exception ex = new Exception("Test"); final Exception ex = new Exception("Test");
final ConcurrentException cex = ConcurrentUtils final ConcurrentException cex = ConcurrentUtils.extractCause(new ExecutionException(ex));
.extractCause(new ExecutionException(ex));
assertSame(ex, cex.getCause(), "Wrong cause"); assertSame(ex, cex.getCause(), "Wrong cause");
} }
@ -164,8 +162,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testExtractCauseUncheckedUncheckedException() { public void testExtractCauseUncheckedUncheckedException() {
final RuntimeException rex = new RuntimeException("Test"); final RuntimeException rex = new RuntimeException("Test");
final RuntimeException r = final RuntimeException r = assertThrows(RuntimeException.class, () -> ConcurrentUtils.extractCauseUnchecked(new ExecutionException(rex)));
assertThrows(RuntimeException.class, () -> ConcurrentUtils.extractCauseUnchecked(new ExecutionException(rex)));
assertEquals(rex, r, "Wrong exception"); assertEquals(rex, r, "Wrong exception");
} }
@ -175,8 +172,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testExtractCauseUncheckedChecked() { public void testExtractCauseUncheckedChecked() {
final Exception ex = new Exception("Test"); final Exception ex = new Exception("Test");
final ConcurrentRuntimeException cex = ConcurrentUtils final ConcurrentRuntimeException cex = ConcurrentUtils.extractCauseUnchecked(new ExecutionException(ex));
.extractCauseUnchecked(new ExecutionException(ex));
assertSame(ex, cex.getCause(), "Wrong cause"); assertSame(ex, cex.getCause(), "Wrong cause");
} }
@ -196,8 +192,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testHandleCauseUncheckedException() { public void testHandleCauseUncheckedException() {
final RuntimeException rex = new RuntimeException("Test"); final RuntimeException rex = new RuntimeException("Test");
final RuntimeException r = final RuntimeException r = assertThrows(RuntimeException.class, () -> ConcurrentUtils.handleCause(new ExecutionException(rex)));
assertThrows(RuntimeException.class, () -> ConcurrentUtils.handleCause(new ExecutionException(rex)));
assertEquals(rex, r, "Wrong exception"); assertEquals(rex, r, "Wrong exception");
} }
@ -207,15 +202,13 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testHandleCauseChecked() { public void testHandleCauseChecked() {
final Exception ex = new Exception("Test"); final Exception ex = new Exception("Test");
final ConcurrentException cex = final ConcurrentException cex = assertThrows(ConcurrentException.class, () -> ConcurrentUtils.handleCause(new ExecutionException(ex)));
assertThrows(ConcurrentException.class, () -> ConcurrentUtils.handleCause(new ExecutionException(ex)));
assertEquals(ex, cex.getCause(), "Wrong cause"); assertEquals(ex, cex.getCause(), "Wrong cause");
} }
/** /**
* Tests handleCause() for a null parameter or a null cause. In this case * Tests handleCause() for a null parameter or a null cause. In this case the method should do nothing. We can only test
* the method should do nothing. We can only test that no exception is * that no exception is thrown.
* thrown.
* *
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/ */
@ -241,8 +234,7 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testHandleCauseUncheckedUncheckedException() { public void testHandleCauseUncheckedUncheckedException() {
final RuntimeException rex = new RuntimeException("Test"); final RuntimeException rex = new RuntimeException("Test");
final RuntimeException r = final RuntimeException r = assertThrows(RuntimeException.class, () -> ConcurrentUtils.handleCauseUnchecked(new ExecutionException(rex)));
assertThrows(RuntimeException.class, () -> ConcurrentUtils.handleCauseUnchecked(new ExecutionException(rex)));
assertEquals(rex, r, "Wrong exception"); assertEquals(rex, r, "Wrong exception");
} }
@ -252,21 +244,19 @@ public class ConcurrentUtilsTest {
@Test @Test
public void testHandleCauseUncheckedChecked() { public void testHandleCauseUncheckedChecked() {
final Exception ex = new Exception("Test"); final Exception ex = new Exception("Test");
final ConcurrentRuntimeException crex = final ConcurrentRuntimeException crex = assertThrows(ConcurrentRuntimeException.class,
assertThrows(ConcurrentRuntimeException.class, () -> ConcurrentUtils.handleCauseUnchecked(new ExecutionException(ex))); () -> ConcurrentUtils.handleCauseUnchecked(new ExecutionException(ex)));
assertEquals(ex, crex.getCause(), "Wrong cause"); assertEquals(ex, crex.getCause(), "Wrong cause");
} }
/** /**
* Tests handleCauseUnchecked() for a null parameter or a null cause. In * Tests handleCauseUnchecked() for a null parameter or a null cause. In this case the method should do nothing. We can
* this case the method should do nothing. We can only test that no * only test that no exception is thrown.
* exception is thrown.
*/ */
@Test @Test
public void testHandleCauseUncheckedNull() { public void testHandleCauseUncheckedNull() {
ConcurrentUtils.handleCauseUnchecked(null); ConcurrentUtils.handleCauseUnchecked(null);
ConcurrentUtils.handleCauseUnchecked(new ExecutionException("Test", ConcurrentUtils.handleCauseUnchecked(new ExecutionException("Test", null));
null));
} }
/** /**
@ -286,10 +276,7 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testInitialize() throws ConcurrentException { public void testInitialize() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Object> init = EasyMock
.createMock(ConcurrentInitializer.class);
final Object result = new Object(); final Object result = new Object();
EasyMock.expect(init.get()).andReturn(result); EasyMock.expect(init.get()).andReturn(result);
EasyMock.replay(init); EasyMock.replay(init);
@ -320,10 +307,7 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testInitializeUnchecked() throws ConcurrentException { public void testInitializeUnchecked() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Object> init = EasyMock
.createMock(ConcurrentInitializer.class);
final Object result = new Object(); final Object result = new Object();
EasyMock.expect(init.get()).andReturn(result); EasyMock.expect(init.get()).andReturn(result);
EasyMock.replay(init); EasyMock.replay(init);
@ -338,15 +322,11 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testInitializeUncheckedEx() throws ConcurrentException { public void testInitializeUncheckedEx() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Object> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Object> init = EasyMock
.createMock(ConcurrentInitializer.class);
final Exception cause = new Exception(); final Exception cause = new Exception();
EasyMock.expect(init.get()).andThrow(new ConcurrentException(cause)); EasyMock.expect(init.get()).andThrow(new ConcurrentException(cause));
EasyMock.replay(init); EasyMock.replay(init);
final ConcurrentRuntimeException crex = final ConcurrentRuntimeException crex = assertThrows(ConcurrentRuntimeException.class, () -> ConcurrentUtils.initializeUnchecked(init));
assertThrows(ConcurrentRuntimeException.class, () -> ConcurrentUtils.initializeUnchecked(init));
assertSame(cause, crex.getCause(), "Wrong cause"); assertSame(cause, crex.getCause(), "Wrong cause");
EasyMock.verify(init); EasyMock.verify(init);
} }
@ -427,10 +407,7 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testCreateIfAbsentKeyPresent() throws ConcurrentException { public void testCreateIfAbsentKeyPresent() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Integer> init = EasyMock
.createMock(ConcurrentInitializer.class);
EasyMock.replay(init); EasyMock.replay(init);
final String key = "testKey"; final String key = "testKey";
final Integer value = 42; final Integer value = 42;
@ -448,10 +425,7 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testCreateIfAbsentKeyNotPresent() throws ConcurrentException { public void testCreateIfAbsentKeyNotPresent() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Integer> init = EasyMock
.createMock(ConcurrentInitializer.class);
final String key = "testKey"; final String key = "testKey";
final Integer value = 42; final Integer value = 42;
EasyMock.expect(init.get()).andReturn(value); EasyMock.expect(init.get()).andReturn(value);
@ -469,10 +443,7 @@ public class ConcurrentUtilsTest {
*/ */
@Test @Test
public void testCreateIfAbsentNullMap() throws ConcurrentException { public void testCreateIfAbsentNullMap() throws ConcurrentException {
@SuppressWarnings("unchecked") final ConcurrentInitializer<Integer> init = EasyMock.createMock(ConcurrentInitializer.class);
final
ConcurrentInitializer<Integer> init = EasyMock
.createMock(ConcurrentInitializer.class);
EasyMock.replay(init); EasyMock.replay(init);
assertNull(ConcurrentUtils.createIfAbsent(null, "test", init), "Wrong result"); assertNull(ConcurrentUtils.createIfAbsent(null, "test", init), "Wrong result");
EasyMock.verify(init); EasyMock.verify(init);
@ -501,8 +472,7 @@ public class ConcurrentUtilsTest {
final String key = "testKey"; final String key = "testKey";
final Integer value = 42; final Integer value = 42;
final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); final ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>();
assertEquals(value, ConcurrentUtils.createIfAbsentUnchecked(map, key, new ConstantInitializer<>(value)), assertEquals(value, ConcurrentUtils.createIfAbsentUnchecked(map, key, new ConstantInitializer<>(value)), "Wrong result");
"Wrong result");
assertEquals(value, map.get(key), "Wrong value in map"); assertEquals(value, map.get(key), "Wrong value in map");
} }
@ -512,19 +482,13 @@ public class ConcurrentUtilsTest {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/ */
@Test @Test
public void testCreateIfAbsentUncheckedException() public void testCreateIfAbsentUncheckedException() throws ConcurrentException {
throws ConcurrentException { final ConcurrentInitializer<Integer> init = EasyMock.createMock(ConcurrentInitializer.class);
@SuppressWarnings("unchecked")
final
ConcurrentInitializer<Integer> init = EasyMock
.createMock(ConcurrentInitializer.class);
final Exception ex = new Exception(); final Exception ex = new Exception();
EasyMock.expect(init.get()).andThrow(new ConcurrentException(ex)); EasyMock.expect(init.get()).andThrow(new ConcurrentException(ex));
EasyMock.replay(init); EasyMock.replay(init);
final ConcurrentRuntimeException crex = final ConcurrentRuntimeException crex = assertThrows(ConcurrentRuntimeException.class,
assertThrows( () -> ConcurrentUtils.createIfAbsentUnchecked(new ConcurrentHashMap<>(), "test", init));
ConcurrentRuntimeException.class,
() -> ConcurrentUtils.createIfAbsentUnchecked(new ConcurrentHashMap<>(), "test", init));
assertEquals(ex, crex.getCause(), "Wrong cause"); assertEquals(ex, crex.getCause(), "Wrong cause");
EasyMock.verify(init); EasyMock.verify(init);
} }