From 463826a4a96db0621974c3bf9aec583b3e4faebf Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Mon, 25 Feb 2008 16:22:16 +0000 Subject: [PATCH] [LMS-207] add: - Meaningful tests. SVN: 4477 --- .../common/utilities/ClientSafeException.java | 2 +- .../utilities/ClientSafeExceptionTest.java | 93 ++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ClientSafeException.java b/common/source/java/ch/systemsx/cisd/common/utilities/ClientSafeException.java index 72464dbba75..2900bd0eec4 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ClientSafeException.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ClientSafeException.java @@ -68,7 +68,7 @@ public final class ClientSafeException extends RuntimeException /** * Whether given <var>exception</var> is client-safe or not. */ - private final static boolean isClientSafe(final Exception exception) + final static boolean isClientSafe(final Exception exception) { assert exception != null : "Unspecified exception."; final String className = exception.getClass().getName(); diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClientSafeExceptionTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClientSafeExceptionTest.java index f5e5943d86a..8a5dde3e429 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClientSafeExceptionTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ClientSafeExceptionTest.java @@ -16,7 +16,18 @@ package ch.systemsx.cisd.common.utilities; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotSame; +import static org.testng.AssertJUnit.assertSame; +import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.fail; + +import java.util.Arrays; + import org.testng.annotations.Test; +import org.xml.sax.SAXException; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; /** * Test cases for the {@link ClientSafeExceptionTest}. @@ -26,9 +37,87 @@ import org.testng.annotations.Test; public final class ClientSafeExceptionTest { + private void checkReturnedClientSafeException(final String message, final Exception rootException, + final Exception clientSafeException) + { + if (ClientSafeException.isClientSafe(rootException)) + { + assertSame(clientSafeException, rootException); + } else + { + assertNotSame(clientSafeException, rootException); + assertTrue(clientSafeException instanceof ClientSafeException); + } + assertEquals(message, clientSafeException.getMessage()); + assertTrue(Arrays.equals(rootException.getStackTrace(), clientSafeException.getStackTrace())); + if (rootException.getCause() != null) + { + assertTrue(clientSafeException.getCause() != null); + } + } + + @Test + public final void testWithNullException() + { + try + { + ClientSafeException.createClientSafeExceptionIfNeeded(null); + fail("Null exception not allowed."); + } catch (final AssertionError ex) + { + // Nothing to do here. + } + } + + @Test + public final void testWithClientSafeExceptionOneLevel() + { + final String message = "Oooops!"; + final UserFailureException exception = new UserFailureException(message); + final Exception clientSafeException = ClientSafeException.createClientSafeExceptionIfNeeded(exception); + assertSame(clientSafeException, exception); + } + @Test - public final void test() + public final void testWithNonClientSafeExceptionOneLevel() { - System.out.println(null instanceof String); + final String message = "Oooops!"; + final Exception exception = new SAXException(message); + final Exception clientSafeException = ClientSafeException.createClientSafeExceptionIfNeeded(exception); + checkReturnedClientSafeException(message, exception, clientSafeException); } + + @Test + public final void testWithClientSafeExceptionMultipleLevel() + { + final String userFailureText = "Oooops!"; + final UserFailureException userFailureException = new UserFailureException(userFailureText); + final String runtimeText = "Oooops! I did it again..."; + final RuntimeException runtimeException = new RuntimeException(runtimeText, userFailureException); + final String unsupportedOperationText = "Wishiiiii!"; + final UnsupportedOperationException unsupportedOperationException = + new UnsupportedOperationException(unsupportedOperationText, runtimeException); + final Exception clientSafeException = + ClientSafeException.createClientSafeExceptionIfNeeded(unsupportedOperationException); + checkReturnedClientSafeException(unsupportedOperationText, unsupportedOperationException, clientSafeException); + checkReturnedClientSafeException(runtimeText, runtimeException, (Exception) clientSafeException.getCause()); + checkReturnedClientSafeException(userFailureText, userFailureException, (Exception) clientSafeException + .getCause().getCause()); + } + + @Test + public final void testWithNonClientSafeExceptionMultipleLevel() + { + } + + @Test + public final void testWithCheckedExceptionTunnel() + { + } + + @Test + public final void testWithGetCauseReturningItSelf() + { + } + } -- GitLab