From fa82cae6cec581cee0f4e6de5fc115a54ea7c4e9 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 23 Feb 2009 13:30:32 +0000
Subject: [PATCH] CFX-120 Accepted packages are provided when calling. New
 test.

SVN: 9921
---
 .../cisd/common/utilities/ExceptionUtils.java | 60 ++++++++++++-------
 .../common/utilities/ExceptionUtilsTest.java  | 30 +++++++---
 2 files changed, 62 insertions(+), 28 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ExceptionUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/ExceptionUtils.java
index 09200484fcd..673df949801 100644
--- a/common/source/java/ch/systemsx/cisd/common/utilities/ExceptionUtils.java
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/ExceptionUtils.java
@@ -16,6 +16,11 @@
 
 package ch.systemsx.cisd.common.utilities;
 
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
 import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.common.exceptions.MasqueradingException;
 
@@ -27,15 +32,10 @@ import ch.systemsx.cisd.common.exceptions.MasqueradingException;
 public final class ExceptionUtils
 {
     /**
-     * Accepted packages for dependencies.
-     * <p>
-     * So these packages are accepted and are not going to be masquerading.
-     * </p>
+     * Default packages (and subpackages) for not been masqueraded.
      */
-    // TODO 2008-09-18, Tomasz Pylak: those packages should not be listed in common package!
     private final static String[] ACCEPTED_PACKAGE_NAME_DEPENDENCIES =
-                { "java.lang", "ch.systemsx.cisd.common", "ch.systemsx.cisd.lims.base",
-                        "ch.systemsx.cisd.openbis.generic.shared.dto" };
+                { "java.lang", "ch.systemsx.cisd.common"};
 
     ExceptionUtils()
     {
@@ -43,14 +43,16 @@ public final class ExceptionUtils
     }
 
     /**
-     * Creates a new {@link MasqueradingException} from given <var>exception</var> only if it is
-     * needed ({@link #isCandidateForMasquerading(Exception)} returns <code>true</code>).
+     * Creates a new {@link MasqueradingException} from given <var>exception</var> and collection
+     * of packages which are not masqueraded (in addition to the default packages) only if it is
+     * needed ({@link #isCandidateForMasquerading(Exception, Collection)} returns <code>true</code>).
      * Otherwise returns given <var>exception</var>.
      */
-    private final static Exception createMasqueradingException(final Exception exception)
+    private final static Exception createMasqueradingException(final Exception exception,
+            final Collection<String> acceptedPackages)
     {
         final Exception rootException = CheckedExceptionTunnel.unwrapIfNecessary(exception);
-        if (isCandidateForMasquerading(rootException))
+        if (isCandidateForMasquerading(rootException, acceptedPackages))
         {
             return new MasqueradingException(rootException);
         } else
@@ -61,7 +63,7 @@ public final class ExceptionUtils
 
     /** Recursively copies cause exception from <var>fromException</var> to <var>toException</var>. */
     private final static void copyCauseException(final Exception fromException,
-            final Exception toException)
+            final Exception toException, final Collection<String> acceptedPackages)
     {
         assert fromException != null : "Unspecified 'from' Exception.";
         assert toException != null : "Unspecified 'to' Exception.";
@@ -70,7 +72,8 @@ public final class ExceptionUtils
                         .getCause(fromException);
         if (fromCauseException != null && fromCauseException != fromException)
         {
-            final Exception toCauseException = createMasqueradingException(fromCauseException);
+            final Exception toCauseException =
+                    createMasqueradingException(fromCauseException, acceptedPackages);
             if (toException.getCause() != toCauseException)
             {
                 if (ClassUtils.setFieldValue(toException, "cause", toCauseException) == false)
@@ -79,18 +82,23 @@ public final class ExceptionUtils
                             toCauseException);
                 }
             }
-            copyCauseException(fromCauseException, toCauseException);
+            copyCauseException(fromCauseException, toCauseException, acceptedPackages);
         }
     }
 
     /**
      * Whether given <var>exception</var> is a candidate for masquerading or not.
+     * 
+     * @return <code>true</code> if the fully qualified class name of <code>exception</code>
+     *         doesn't start with a package name from <code>acceptedPackages</code> or
+     *         <code>java.lang, ch.systemsx.cisd.common</code>.
      */
-    final static boolean isCandidateForMasquerading(final Exception exception)
+    final static boolean isCandidateForMasquerading(final Exception exception,
+            final Collection<String> acceptedPackages)
     {
         assert exception != null : "Unspecified exception.";
         final String className = exception.getClass().getName();
-        for (final String packageName : ACCEPTED_PACKAGE_NAME_DEPENDENCIES)
+        for (final String packageName : createSetOfAcceptedPackages(acceptedPackages))
         {
             if (className.startsWith(packageName))
             {
@@ -99,16 +107,26 @@ public final class ExceptionUtils
         }
         return true;
     }
+    
+    private static Set<String> createSetOfAcceptedPackages(Collection<String> acceptedPackages)
+    {
+        final LinkedHashSet<String> set = new LinkedHashSet<String>();
+        set.addAll(Arrays.asList(ACCEPTED_PACKAGE_NAME_DEPENDENCIES));
+        set.addAll(acceptedPackages);
+        return set;
+    }
 
     /**
-     * Analyzes given <var>exception</var> and makes it independent to packages outside the ones
-     * specified in an internal list, <code>ACCEPTED_PACKAGE_NAME_DEPENDENCIES</code>.
+     * Analyzes given <var>exception</var> and makes it independent to packages outside the
+     * specified collection or <code>java.lang, ch.systemsx.cisd.common</code>.
      */
-    public final static Exception createMasqueradingExceptionIfNeeded(final Exception exception)
+    public final static Exception createMasqueradingExceptionIfNeeded(final Exception exception,
+            final Collection<String> acceptedPackages)
     {
         assert exception != null : "Unspecified SQL Exception.";
-        final Exception clientSafeException = createMasqueradingException(exception);
-        copyCauseException(exception, clientSafeException);
+        final Exception clientSafeException =
+                createMasqueradingException(exception, acceptedPackages);
+        copyCauseException(exception, clientSafeException, acceptedPackages);
         return clientSafeException;
     }
 
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExceptionUtilsTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExceptionUtilsTest.java
index 2ddd864c1cd..23d8c48796d 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExceptionUtilsTest.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/ExceptionUtilsTest.java
@@ -28,6 +28,7 @@ import java.io.IOException;
 import java.security.DigestException;
 import java.text.ParseException;
 import java.util.Arrays;
+import java.util.Collections;
 
 import org.testng.annotations.Test;
 import org.xml.sax.SAXException;
@@ -70,7 +71,7 @@ public final class ExceptionUtilsTest
     {
         try
         {
-            ExceptionUtils.createMasqueradingExceptionIfNeeded(null);
+            ExceptionUtils.createMasqueradingExceptionIfNeeded(null, Collections.<String>emptyList());
             fail("Null exception not allowed.");
         } catch (final AssertionError ex)
         {
@@ -84,7 +85,18 @@ public final class ExceptionUtilsTest
         final String message = "Oooops!";
         final UserFailureException exception = new UserFailureException(message);
         final Exception clientSafeException =
-                ExceptionUtils.createMasqueradingExceptionIfNeeded(exception);
+                ExceptionUtils.createMasqueradingExceptionIfNeeded(exception, Collections
+                        .<String> emptyList());
+        checkReturnedClientSafeException(message, exception, clientSafeException, true);
+    }
+
+    @Test
+    public final void testWithClientSafeExceptionOneLevelFromSpecifiedPackage()
+    {
+        final String message = "Oooops!";
+        final Exception exception = new SAXException(message);
+        final Exception clientSafeException =
+                ExceptionUtils.createMasqueradingExceptionIfNeeded(exception, Arrays.asList("org.xml"));
         checkReturnedClientSafeException(message, exception, clientSafeException, true);
     }
 
@@ -94,10 +106,11 @@ public final class ExceptionUtilsTest
         final String message = "Oooops!";
         final Exception exception = new SAXException(message);
         final Exception clientSafeException =
-                ExceptionUtils.createMasqueradingExceptionIfNeeded(exception);
+            ExceptionUtils.createMasqueradingExceptionIfNeeded(exception, Collections
+                    .<String> emptyList());
         checkReturnedClientSafeException(message, exception, clientSafeException, false);
     }
-
+    
     @Test
     public final void testWithClientSafeExceptionMultipleLevel()
     {
@@ -110,7 +123,8 @@ public final class ExceptionUtilsTest
         final UnsupportedOperationException unsupportedOperationException =
                 new UnsupportedOperationException(unsupportedOperationText, runtimeException);
         final Exception clientSafeException =
-                ExceptionUtils.createMasqueradingExceptionIfNeeded(unsupportedOperationException);
+                ExceptionUtils.createMasqueradingExceptionIfNeeded(unsupportedOperationException,
+                        Collections.<String> emptyList());
         checkReturnedClientSafeException(unsupportedOperationText, unsupportedOperationException,
                 clientSafeException, true);
         checkReturnedClientSafeException(runtimeText, runtimeException,
@@ -130,7 +144,8 @@ public final class ExceptionUtilsTest
         final DigestException digestException =
                 new DigestException(digestExceptionText, runtimeException);
         final Exception clientSafeException =
-                ExceptionUtils.createMasqueradingExceptionIfNeeded(digestException);
+                ExceptionUtils.createMasqueradingExceptionIfNeeded(digestException, Collections
+                        .<String> emptyList());
         checkReturnedClientSafeException(digestExceptionText, digestException, clientSafeException,
                 false);
         checkReturnedClientSafeException(runtimeText, runtimeException,
@@ -147,7 +162,8 @@ public final class ExceptionUtilsTest
         final RuntimeException checkedExceptionTunnel =
                 CheckedExceptionTunnel.wrapIfNecessary(ioException);
         final Exception clientSafeException =
-                ExceptionUtils.createMasqueradingExceptionIfNeeded(checkedExceptionTunnel);
+                ExceptionUtils.createMasqueradingExceptionIfNeeded(checkedExceptionTunnel,
+                        Collections.<String> emptyList());
         assertNotSame(clientSafeException, checkedExceptionTunnel);
         assertNotSame(clientSafeException, ioException);
         assertTrue(clientSafeException instanceof MasqueradingException);
-- 
GitLab