Skip to content

Commit

Permalink
[#noissue] Cleanup Assertions.assertThrows
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 30, 2023
1 parent 5c9fb5e commit ea23f30
Show file tree
Hide file tree
Showing 34 changed files with 162 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ private ClassLoader onLoadTest(Class<?> classLoaderType, Class<?> testClass) thr
ClassLoader cl = PinpointClassLoaderFactory.createClassLoader(this.getClass().getName(), urls, null, ProfilerLibs.PINPOINT_PROFILER_CLASS);
Assertions.assertSame(cl.getClass(), classLoaderType);

try {
Assertions.assertThrowsExactly(ClassNotFoundException.class, () -> {
cl.loadClass("test");
Assertions.fail();
} catch (ClassNotFoundException ignored) {
}
});

Class<?> selfLoadClass = cl.loadClass(testClass.getName());
Assertions.assertNotSame(testClass, selfLoadClass);
Expand All @@ -61,7 +59,6 @@ private ClassLoader onLoadTest(Class<?> classLoaderType, Class<?> testClass) thr
}



@Test
public void testBootstrapClassLoader() throws Exception {
ClassLoader classLoader = new ParallelClassLoader(this.getClass().getName(), new URL[0], null, ProfilerLibs.PINPOINT_PROFILER_CLASS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ private ClassLoader onLoadTest(Class<?> classLoaderType, Class<?> testClass) thr
ClassLoader cl = PinpointClassLoaderFactory.createClassLoader(this.getClass().getName(), urls, null, ProfilerLibs.PINPOINT_PROFILER_CLASS);
Assertions.assertSame(cl.getClass(), classLoaderType);

try {
Assertions.assertThrowsExactly(ClassNotFoundException.class, () -> {
cl.loadClass("test");
Assertions.fail();
} catch (ClassNotFoundException ignored) {
}
});

Class<?> selfLoadClass = cl.loadClass(testClass.getName());
Assertions.assertNotSame(testClass, selfLoadClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ public class DumpTypeTest {
public void find() {
DumpType none = DumpType.valueOf("ALWAYS");
logger.debug("type:{}", none);

try {
Assertions.assertThrows(Exception.class, () -> {
DumpType.valueOf("error");
Assertions.fail("not found");
} catch (IllegalArgumentException ignored) {

}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,29 @@ public class DefaultInterceptorRegistryAdaptorTest {
@Test
public void indexSize_0() {

try {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new DefaultInterceptorRegistryAdaptor(-1);
Assertions.fail();
} catch (IllegalArgumentException ignored) {
}

});
}

@Test
public void indexSize_1() {
try {
InterceptorRegistryAdaptor interceptorRegistry = new DefaultInterceptorRegistryAdaptor(0);
StaticAroundInterceptor mock = mock(StaticAroundInterceptor.class);
InterceptorRegistryAdaptor interceptorRegistry = new DefaultInterceptorRegistryAdaptor(0);
StaticAroundInterceptor mock = mock(StaticAroundInterceptor.class);

Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
interceptorRegistry.addInterceptor(mock);
Assertions.fail();
} catch (IndexOutOfBoundsException ignored) {
}
});
}

@Test
public void indexSize_2() {
InterceptorRegistryAdaptor interceptorRegistry = new DefaultInterceptorRegistryAdaptor(1);
interceptorRegistry.addInterceptor(mock(StaticAroundInterceptor.class));
try {

Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
interceptorRegistry.addInterceptor(mock(StaticAroundInterceptor.class));
Assertions.fail();
} catch (IndexOutOfBoundsException ignored) {
}
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ private ClassLoader onLoadTest(Class<?> classLoaderType, Class<?> testClass) thr
ClassLoader cl = PinpointClassLoaderFactory.createClassLoader(this.getClass().getName(), urls, null, ProfilerLibs.PINPOINT_PROFILER_CLASS);
Assertions.assertSame(cl.getClass(), classLoaderType);

try {
Assertions.assertThrowsExactly(ClassNotFoundException.class, () -> {
cl.loadClass("test");
Assertions.fail();
} catch (ClassNotFoundException ignored) {
}
});


Class<?> selfLoadClass = cl.loadClass(testClass.getName());
Assertions.assertNotSame(testClass, selfLoadClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ public void sqlDate() throws ClassNotFoundException {
logger.debug("sqlDate classLoader:{}", java.sql.Date.class.getClassLoader());
Class.forName("java.sql.Date", false, ClassLoader.getPlatformClassLoader());

try {
Assertions.assertThrowsExactly(ClassNotFoundException.class, () -> {
ClassLoader bootStrap = Object.class.getClassLoader();
Class.forName("java.sql.Date", false, bootStrap);
Assertions.fail();
} catch (ClassNotFoundException e) {
// skip
}
});
}

@Disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void startStop() {
receiver = new UDPReceiver("test", packetHandlerFactory, executor, 8, bindAddress, socketOptionApplier, pool);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
Assertions.fail(e.getMessage());
Assertions.fail(e.getMessage(), e);
} finally {
if (receiver != null) {
receiver.shutdown();
Expand Down Expand Up @@ -160,7 +160,7 @@ boolean validatePacket(DatagramPacket packet) {
Mockito.verify(mockExecutor).execute(any(Runnable.class));
} catch (Exception e) {
logger.debug(e.getMessage(), e);
Assertions.fail(e.getMessage());
Assertions.fail(e.getMessage(), e);
} finally {
if (receiver != null) {
receiver.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,14 @@ public void testPadBytes() {
public void testPadBytes_Error() {

Buffer buffer1_1 = new AutomaticBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_1.putPadBytes(new byte[11], 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer1_2 = new AutomaticBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_2.putPadBytes(new byte[20], 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer2 = new AutomaticBuffer(32);
buffer2.putPadBytes(new byte[10], 10);
Expand Down Expand Up @@ -118,18 +114,14 @@ public void testPadString() {
public void testPadString_Error() {

Buffer buffer1_1 = new AutomaticBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_1.putPadString(StringUtils.repeat("a", 11), 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer1_2 = new AutomaticBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_2.putPadString(StringUtils.repeat("a", 20), 10);
Assertions.fail("error");
} catch (Exception ignored) {
}
});

Buffer buffer2 = new AutomaticBuffer(32);
buffer2.putPadString(StringUtils.repeat("a", 10), 10);
Expand All @@ -151,12 +143,10 @@ public void testPut2PrefixedBytes() {

checkPut2PrefixedBytes(null);

try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
byte[] bytes4 = new byte[Short.MAX_VALUE + 1];
checkPut2PrefixedBytes(bytes4);
Assertions.fail("too large bytes");
} catch (IndexOutOfBoundsException ignored) {
}
});
}

private void checkPut2PrefixedBytes(byte[] bytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,22 @@ public void readPadBytes() {
public void testPadBytes_Error() {

Buffer buffer1_1 = new FixedBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_1.putPadBytes(new byte[11], 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer1_2 = new FixedBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_2.putPadBytes(new byte[20], 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer2 = new FixedBuffer(32);
buffer2.putPadBytes(new byte[10], 10);

}

@Test
public void testPadString() throws Exception {
public void testPadString() {
int TOTAL_LENGTH = 20;
int TEST_SIZE = 10;
int PAD_SIZE = TOTAL_LENGTH - TEST_SIZE;
Expand Down Expand Up @@ -178,17 +174,15 @@ public void readPadStringAndRightTrim() {
public void testPadString_Error() {

Buffer buffer1_1 = new FixedBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_1.putPadString(StringUtils.repeat("a", 11), 10);
} catch (IndexOutOfBoundsException ignored) {
}
});


Buffer buffer1_2 = new FixedBuffer(32);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
buffer1_2.putPadString(StringUtils.repeat("a", 20), 10);
Assertions.fail("error");
} catch (IndexOutOfBoundsException ignored) {
}
});

Buffer buffer2 = new FixedBuffer(32);
buffer2.putPadString(StringUtils.repeat("a", 10), 10);
Expand All @@ -206,12 +200,10 @@ public void testPut2PrefixedBytes() {
byte[] bytes = new byte[Short.MAX_VALUE];
checkPut2PrefixedBytes(BytesUtils.toString(bytes), endExpected, Short.MAX_VALUE * 2);

try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
byte[] bytes2 = new byte[Short.MAX_VALUE + 1];
checkPut2PrefixedBytes(BytesUtils.toString(bytes2), endExpected, Short.MAX_VALUE * 2);
Assertions.fail("too large bytes");
} catch (IndexOutOfBoundsException ignored) {
}
});

}

Expand Down Expand Up @@ -468,11 +460,9 @@ public void find_SVLong_errorCode() {
public void readVInt_errorCase() {
byte[] errorCode = new byte[]{-118, -41, -17, -117, -81, -115, -64, -64, -108, -88};
Buffer buffer = new FixedBuffer(errorCode);
try {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
buffer.readVInt();
Assertions.fail("invalid VInt");
} catch (IllegalArgumentException ignored) {
}
});

Assertions.assertEquals(0, buffer.getOffset());
}
Expand All @@ -481,11 +471,9 @@ public void readVInt_errorCase() {
public void readVLong_errorCase() {
byte[] errorCode = new byte[]{-25, -45, -47, -14, -16, -104, -53, -48, -72, -9};
Buffer buffer = new FixedBuffer(errorCode);
try {
Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> {
buffer.readVLong();
Assertions.fail("invalid VLong");
} catch (IllegalArgumentException ignored) {
}
});

Assertions.assertEquals(0, buffer.getOffset());
}
Expand Down Expand Up @@ -607,10 +595,10 @@ public void testBoolean() {

Buffer read = new FixedBuffer(buffer.getBuffer());
boolean b = read.readBoolean();
Assertions.assertEquals(true, b);
Assertions.assertTrue(b);

boolean c = read.readBoolean();
Assertions.assertEquals(false, c);
Assertions.assertFalse(c);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,20 @@ public class OffsetFixedBufferTest {
@Test
public void testFixedBuffer() {
new OffsetFixedBuffer(new byte[10], 10, 0);
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
new OffsetFixedBuffer(new byte[10], 11, 0);
Assertions.fail();
} catch (IndexOutOfBoundsException ignored) {
}
try {
});

Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
new OffsetFixedBuffer(new byte[10], -1, 0);
Assertions.fail();
} catch (IndexOutOfBoundsException ignored) {
}
});
}

@Test
public void testFixedBuffer_length() {
try {
Assertions.assertThrowsExactly(IndexOutOfBoundsException.class, () -> {
new OffsetFixedBuffer(new byte[10], 0, 11);
Assertions.fail();
} catch (IndexOutOfBoundsException e) {
}
});

new OffsetFixedBuffer(new byte[10], 0, 10);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ public void specifiedNamespace() {
@Test
public void nullQualifierShouldThrowException() {
Assertions.assertThrows(NullPointerException.class, () -> {
// Given
final String nullQualifier = null;
// When
cache.get(nullQualifier);
// Then
Assertions.fail();
cache.get(null);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ public void unregisteredAnnotationKeyTest() {
verifyAnnotationKey(annotationKeyRegistry, registeredAnnotationKey);

Assertions.assertSame(AnnotationKey.UNKNOWN, annotationKeyRegistry.findAnnotationKey(unregisteredAnnotationKey.getCode()));
try {
Assertions.assertThrowsExactly(NoSuchElementException.class, () -> {
annotationKeyRegistry.findAnnotationKeyByName(unregisteredAnnotationKey.getName());
Assertions.fail();
} catch (NoSuchElementException expected) {
}
});
}

@Test
Expand Down Expand Up @@ -156,11 +154,9 @@ private void verifyServiceType(ServiceTypeRegistry serviceTypeRegistry, ServiceT
}
}
Assertions.assertTrue(found);
try {
Assertions.assertThrows(Exception.class, () -> {
descMatchedServiceTypes.add(serviceType);
Assertions.fail("Adding to unmodifiable list should have failed");
} catch (Exception expected) {
}
}, "Adding to unmodifiable list should have failed");
}
}

Expand Down
Loading

0 comments on commit ea23f30

Please # to comment.