Skip to content

Commit

Permalink
:fix: retrieving exception details should never result in another exc…
Browse files Browse the repository at this point in the history
…eption to be thrown (NPE in this case)

Signed-off-by: dseurotech <davide.salvador@eurotech.com>
  • Loading branch information
dseurotech authored and Coduz committed Dec 21, 2022
1 parent 6b4f499 commit 11c4cc4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public void restApiRuntimeExceptionCodeTest() {
Assert.assertNull("Null expected.", restApiRuntimeException.getCause());
}

@Test(expected = NullPointerException.class)
@Test
public void restApiRuntimeExceptionNullCodeTest() {
RestApiRuntimeException restApiRuntimeException = new RestApiRuntimeException(null);

Assert.assertNull("Null expected.", restApiRuntimeException.getCode());
Assert.assertNull("Null expected.", restApiRuntimeException.getCause());
restApiRuntimeException.getMessage();
Assert.assertEquals("Error: ", restApiRuntimeException.getMessage());
}

@Test
Expand All @@ -62,13 +62,13 @@ public void restApiRuntimeExceptionCodeArgumentsTest() {
Assert.assertNull("Null expected.", restApiRuntimeException.getCause());
}

@Test(expected = NullPointerException.class)
@Test
public void restApiRuntimeExceptionNullCodeArgumentsTest() {
RestApiRuntimeException restApiRuntimeException = new RestApiRuntimeException(null, stringObject, intObject, charObject);

Assert.assertNull("Null expected.", restApiRuntimeException.getCode());
Assert.assertNull("Null expected.", restApiRuntimeException.getCause());
restApiRuntimeException.getMessage();
Assert.assertEquals("Error: String Object, 10, c", restApiRuntimeException.getMessage());
}

@Test
Expand All @@ -89,14 +89,13 @@ public void restApiRuntimeExceptionCodeCauseArgumentsTest() {
Assert.assertEquals("Expected and actual values should be the same.", cause, restApiRuntimeException.getCause());
}

@Test(expected = NullPointerException.class)
@Test
public void restApiRuntimeExceptionNullCodeCauseArgumentsTest() {
RestApiRuntimeException restApiRuntimeException = new RestApiRuntimeException(null, cause, stringObject, intObject, charObject);

Assert.assertNull("Null expected.", restApiRuntimeException.getCode());
Assert.assertEquals("Expected and actual values should be the same.", cause, restApiRuntimeException.getCause());

restApiRuntimeException.getMessage();
Assert.assertEquals("Error: String Object, 10, c", restApiRuntimeException.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ private static String getMessagePattern(@NotNull String resourceBundleName, @Not
LOG.warn("Could not load exception messages for resource: {} in locale: {}. A generic error message will be printed.", resourceBundleName, locale);
return null;
}
if (code == null) {
LOG.warn("Could not load exception messages for null code. A generic error message will be printed.");
return null;
}
try {
messagePattern = resourceBundle.getString(code.name());
} catch (MissingResourceException mre) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ public void getLocalizedMessageMissingResourceTest() {
ExceptionMessageUtils.getLocalizedMessage("kapua-service-error-messages", locale, mockedKapuaErrorCode, objectList);
}

@Test(expected = NullPointerException.class)
public void getLocalizedMessageNullKapuaErrorCodeTest() {
ExceptionMessageUtils.getLocalizedMessage(resourceBundleName[0], locale, null, objectList);
@Test
public void nullErrorCodeLeadsToDefaultErrorMessage() {
final String got = ExceptionMessageUtils.getLocalizedMessage(resourceBundleName[0], locale, null, objectList);
Assert.assertEquals("Error: 0, 10, 100000, String, c, -10, -1000000000, -100000000000, 10, 10.0, null, 10.1, true, false", got);
}

@Test
Expand Down
70 changes: 10 additions & 60 deletions service/api/src/test/java/org/eclipse/kapua/KapuaExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,8 @@ public void kapuaExceptionNullKapuaErrorCodeParameterTest() {
Assert.assertNull("Null expected.", kapuaException.getCode());
Assert.assertNull("Null expected.", kapuaException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaException.getKapuaErrorMessagesBundle());
try {
kapuaException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: ", kapuaException.getMessage());
Assert.assertEquals("Error: ", kapuaException.getLocalizedMessage());
}

@Test
Expand All @@ -85,18 +75,8 @@ public void kapuaExceptionNullKapuaErrorCodeObjectParametersTest() {
Assert.assertNull("Null expected.", kapuaException.getCode());
Assert.assertNull("Null expected.", kapuaException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaException.getKapuaErrorMessagesBundle());
try {
kapuaException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: user, 2, c", kapuaException.getMessage());
Assert.assertEquals("Error: user, 2, c", kapuaException.getLocalizedMessage());
}

@Test
Expand All @@ -115,18 +95,8 @@ public void kapuaExceptionNullKapuaErrorCodeNullObjectParametersTest() {
Assert.assertNull("Null expected.", kapuaException.getCode());
Assert.assertNull("Null expected.", kapuaException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaException.getKapuaErrorMessagesBundle());
try {
kapuaException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: ", kapuaException.getMessage());
Assert.assertEquals("Error: ", kapuaException.getLocalizedMessage());
}

@Test
Expand All @@ -148,18 +118,8 @@ public void kapuaExceptionNullKapuaErrorCodeThrowableObjectParametersTest() {
Assert.assertNull("Null expected.", kapuaException.getCode());
Assert.assertEquals("Expected and actual values should be the same.", throwable, kapuaException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaException.getKapuaErrorMessagesBundle());
try {
kapuaException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: user, 2, c", kapuaException.getMessage());
Assert.assertEquals("Error: user, 2, c", kapuaException.getLocalizedMessage());
}
}

Expand All @@ -182,18 +142,8 @@ public void kapuaExceptionNullKapuaErrorCodeThrowableNullObjectParametersTest()
Assert.assertNull("Null expected.", kapuaException.getCode());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaException.getKapuaErrorMessagesBundle());
Assert.assertEquals("Expected and actual values should be the same.", throwable, kapuaException.getCause());
try {
kapuaException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: ", kapuaException.getMessage());
Assert.assertEquals("Error: ", kapuaException.getLocalizedMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void kapuaIllegalArgumentExceptionKapuaErrorCodesStringParametersTest() {
}
}

@Test(expected = NullPointerException.class)
@Test
public void kapuaIllegalArgumentExceptionNullKapuaErrorCodesStringParametersTest() {
for (String name : argumentName) {
for (String value : argumentValue) {
Expand All @@ -122,26 +122,42 @@ public void kapuaIllegalArgumentExceptionNullKapuaErrorCodesStringParametersTest
Assert.assertEquals("Expected and actual values should be the same.", name, kapuaIllegalArgumentException.getArgumentName());
Assert.assertEquals("Expected and actual values should be the same.", value, kapuaIllegalArgumentException.getArgumentValue());
Assert.assertNull("Null expected.", kapuaIllegalArgumentException.getCause());
kapuaIllegalArgumentException.getMessage();
Assert.assertEquals(String.format("Error: %s, %s", name, value), kapuaIllegalArgumentException.getMessage());
}
}
}

@Test(expected = KapuaIllegalArgumentException.class)
@Test
public void throwingExceptionStringParametersTest() throws KapuaIllegalArgumentException {
for (String name : argumentName) {
for (String value : argumentValue) {
throw new KapuaIllegalArgumentException(name, value);
try {
throw new KapuaIllegalArgumentException(name, value);
} catch (KapuaIllegalArgumentException ex) {
Assert.assertEquals(
String.format("An illegal value was provided for the argument %s: %s.", name, value),
ex.getMessage());
} catch (Throwable t) {
Assert.fail();
}
}
}
}

@Test(expected = KapuaIllegalArgumentException.class)
@Test
public void throwingExceptionKapuaErrorCodesStringParametersTest() throws KapuaIllegalArgumentException {
for (String name : argumentName) {
for (String value : argumentValue) {
for (KapuaErrorCodes code : kapuaErrorCodes) {
throw new KapuaIllegalArgumentException(code, name, value);
try {
throw new KapuaIllegalArgumentException(code, name, value);
} catch (KapuaIllegalArgumentException ex) {
//We just want to check that this type of exception is thrown two lines above... very low value test
//correct error messages are tested separately above
Assert.assertTrue(true);
} catch (Throwable t) {
Assert.fail();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,8 @@ public void kapuaRuntimeExceptionNullKapuaErrorCodeParameterTest() {
Assert.assertNull("Null expected.", kapuaRuntimeException.getCode());
Assert.assertNull("Null expected.", kapuaRuntimeException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaRuntimeException.getKapuaErrorMessagesBundle());
try {
kapuaRuntimeException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaRuntimeException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: ", kapuaRuntimeException.getMessage());
Assert.assertEquals("Error: ", kapuaRuntimeException.getLocalizedMessage());
}

@Test
Expand Down Expand Up @@ -99,18 +89,8 @@ public void kapuaRuntimeExceptionNullKapuaErrorCodeObjectParametersTest() {
Assert.assertNull("Null expected.", kapuaRuntimeException.getCode());
Assert.assertNull("Null expected.", kapuaRuntimeException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaRuntimeException.getKapuaErrorMessagesBundle());
try {
kapuaRuntimeException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaRuntimeException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: user, 1, c", kapuaRuntimeException.getMessage());
Assert.assertEquals("Error: user, 1, c", kapuaRuntimeException.getLocalizedMessage());
}

@Test
Expand All @@ -132,18 +112,8 @@ public void kapuaRuntimeExceptionNullKapuaErrorCodeThrowableObjectParametesTest(
Assert.assertNull("Null expected.", kapuaRuntimeException.getCode());
Assert.assertEquals("Expected and actual values should be the same.", throwable, kapuaRuntimeException.getCause());
Assert.assertEquals("Expected and actual values should be the same.", expectedErrorMessage, kapuaRuntimeException.getKapuaErrorMessagesBundle());
try {
kapuaRuntimeException.getMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
try {
kapuaRuntimeException.getLocalizedMessage();
Assert.fail("NullPointerException expected.");
} catch (Exception e) {
Assert.assertEquals("NullPointerException expected.", new NullPointerException().toString(), e.toString());
}
Assert.assertEquals("Error: user, 1, c", kapuaRuntimeException.getMessage());
Assert.assertEquals("Error: user, 1, c", kapuaRuntimeException.getLocalizedMessage());
}
}

Expand All @@ -165,7 +135,7 @@ public void internalErrorCauseMessageTest() {

for (Throwable throwable : throwables) {
for (String msg : messages) {
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(throwable, msg), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(throwable, msg), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertEquals("Expected and actual values should be the same.", new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, throwable, msg).toString(), KapuaRuntimeException.internalError(throwable, msg).toString());
Assert.assertEquals("Expected and actual values should be the same.", "An internal error occurred: " + msg + ".", KapuaRuntimeException.internalError(throwable, msg).getMessage());
Assert.assertEquals("Expected and actual values should be the same.", throwable, KapuaRuntimeException.internalError(throwable, msg).getCause());
Expand All @@ -184,7 +154,7 @@ public void internalErrorCauseTest() {
String[] arguments = {"Message", "java.lang.Throwable"};

for (int i = 0; i < throwables.length; i++) {
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(throwables[i]), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(throwables[i]), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertEquals("Expected and actual values should be the same.", new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, throwables[i], arguments[i]).toString(), KapuaRuntimeException.internalError(throwables[i]).toString());
Assert.assertEquals("Expected and actual values should be the same.", expectedMessage[i], KapuaRuntimeException.internalError(throwables[i]).getMessage());
Assert.assertEquals("Expected and actual values should be the same.", throwables[i], KapuaRuntimeException.internalError(throwables[i]).getCause());
Expand All @@ -204,7 +174,7 @@ public void internalErrorMessageTest() {
String[] messages = {"Message", null};

for (String msg : messages) {
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(msg), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertThat("Instance of KapuaRuntimeException expected.", KapuaRuntimeException.internalError(msg), IsInstanceOf.instanceOf(KapuaRuntimeException.class));
Assert.assertEquals("Expected and actual values should be the same.", new KapuaRuntimeException(KapuaErrorCodes.INTERNAL_ERROR, null, msg).toString(), KapuaRuntimeException.internalError(msg).toString());
Assert.assertEquals("Expected and actual values should be the same.", "An internal error occurred: " + msg + ".", KapuaRuntimeException.internalError(msg).getMessage());
Assert.assertNull("Null expected.", KapuaRuntimeException.internalError(msg).getCause());
Expand Down

0 comments on commit 11c4cc4

Please sign in to comment.