You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by mc...@apache.org on 2008/04/17 14:29:53 UTC

svn commit: r649064 - in /incubator/tuscany/java/sca/itest/oneway/src: main/java/org/apache/tuscany/sca/itest/oneway/ main/java/org/apache/tuscany/sca/itest/oneway/impl/ main/resources/OneWayContribution/META-INF/sca-deployables/ test/java/org/apache/t...

Author: mcombellack
Date: Thu Apr 17 05:29:51 2008
New Revision: 649064

URL: http://svn.apache.org/viewvc?rev=649064&view=rev
Log:
TUSCANY-2225 - Added unit test to check that when a @OneWay method that uses the NonBlockingInterceptor throws an exception, the Exception is logged to the JDK Logger

Modified:
    incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayClient.java
    incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayService.java
    incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayClientImpl.java
    incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayServiceImpl.java
    incubator/tuscany/java/sca/itest/oneway/src/main/resources/OneWayContribution/META-INF/sca-deployables/oneWay.composite
    incubator/tuscany/java/sca/itest/oneway/src/test/java/org/apache/tuscany/sca/itest/oneway/OneWayTestCase.java

Modified: incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayClient.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayClient.java?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayClient.java (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayClient.java Thu Apr 17 05:29:51 2008
@@ -29,4 +29,9 @@
 public interface OneWayClient { 
 	
 	public int doSomething(int count);	
+
+    /**
+     * This method will invoke a @OneWay method that throws an exception.
+     */
+    void doSomethingWhichThrowsException();
 }

Modified: incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayService.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayService.java?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayService.java (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/OneWayService.java Thu Apr 17 05:29:51 2008
@@ -34,4 +34,9 @@
     @OneWay
     public void doSomething(int count);
 
+    /**
+     * This one way method will throw a NullPointerException.
+     */
+    @OneWay
+    void doSomethingWhichThrowsException();
 }

Modified: incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayClientImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayClientImpl.java?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayClientImpl.java (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayClientImpl.java Thu Apr 17 05:29:51 2008
@@ -20,6 +20,7 @@
 
 import org.apache.tuscany.sca.itest.oneway.OneWayClient;
 import org.apache.tuscany.sca.itest.oneway.OneWayService;
+import org.junit.Assert;
 import org.osoa.sca.annotations.Reference;
 
 
@@ -50,4 +51,12 @@
         
         return count;
     }	
+
+    /**
+     * {@inheritDoc}
+     */
+    public void doSomethingWhichThrowsException() {
+        Assert.assertNotNull(oneWayService);
+        oneWayService.doSomethingWhichThrowsException();
+    }
 }

Modified: incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayServiceImpl.java?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayServiceImpl.java (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/main/java/org/apache/tuscany/sca/itest/oneway/impl/OneWayServiceImpl.java Thu Apr 17 05:29:51 2008
@@ -30,11 +30,23 @@
 
 
 public class OneWayServiceImpl implements OneWayService {
+
     /**
-     * Counts the number of invocations to doSomething()
+     * This is the error message that will be in the Exception thrown by
+     * the doSomethingWhichThrowsException() method.
+     */
+    public static final String EXCEPTION_MESSAGE = "Sample RuntimeException from a @OneWay method";
+
+    /**
+     * Counts the number of invocations to doSomething().
      */
     public static final AtomicInteger callCount = new AtomicInteger();
 
+    /**
+     * Counts the number of invocations of the doSomethingWhichThrowsException() method.
+     */
+    public static final AtomicInteger CALL_COUNT_FOR_THROWS_EXCEPTION_METHOD = new AtomicInteger();
+
     public void doSomething(int count){
         callCount.incrementAndGet();
 
@@ -43,4 +55,13 @@
         
         
     }	
+
+    /**
+     * {@inheritDoc}
+     */
+    public void doSomethingWhichThrowsException() {
+        System.out.println("OneWay invoked. About to throw an Exception");
+        CALL_COUNT_FOR_THROWS_EXCEPTION_METHOD.incrementAndGet();
+        throw new NullPointerException(EXCEPTION_MESSAGE);
+    }
 }

Modified: incubator/tuscany/java/sca/itest/oneway/src/main/resources/OneWayContribution/META-INF/sca-deployables/oneWay.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/main/resources/OneWayContribution/META-INF/sca-deployables/oneWay.composite?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/main/resources/OneWayContribution/META-INF/sca-deployables/oneWay.composite (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/main/resources/OneWayContribution/META-INF/sca-deployables/oneWay.composite Thu Apr 17 05:29:51 2008
@@ -36,5 +36,23 @@
             <binding.ws/>
         </service>
     </component> 	        
-     
+
+
+    <!-- OneWay Client and Server Components that use the standard SCA Binding so we can validate -->
+    <!-- that the NonBlockingInterceptor logs an message when an Exception is thrown by the code -->
+    <!-- that is called. See TUSCANY-2225 --> 
+    <component name="OneWayClientComponentSCABinding">
+        <implementation.java class="org.apache.tuscany.sca.itest.oneway.impl.OneWayClientImpl"/>
+        <reference name="oneWayService" target="OneWayServiceComponentSCABinding">
+            <interface.java interface="org.apache.tuscany.sca.itest.oneway.OneWayService" />
+        </reference>
+    </component> 
+
+    <component name="OneWayServiceComponentSCABinding">
+        <implementation.java class="org.apache.tuscany.sca.itest.oneway.impl.OneWayServiceImpl"/>
+        <service name="OneWayService">
+            <interface.java interface="org.apache.tuscany.sca.itest.oneway.OneWayService" /> 
+        </service>
+    </component>
+
 </composite>

Modified: incubator/tuscany/java/sca/itest/oneway/src/test/java/org/apache/tuscany/sca/itest/oneway/OneWayTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/itest/oneway/src/test/java/org/apache/tuscany/sca/itest/oneway/OneWayTestCase.java?rev=649064&r1=649063&r2=649064&view=diff
==============================================================================
--- incubator/tuscany/java/sca/itest/oneway/src/test/java/org/apache/tuscany/sca/itest/oneway/OneWayTestCase.java (original)
+++ incubator/tuscany/java/sca/itest/oneway/src/test/java/org/apache/tuscany/sca/itest/oneway/OneWayTestCase.java Thu Apr 17 05:29:51 2008
@@ -19,10 +19,17 @@
 
 package org.apache.tuscany.sca.itest.oneway;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
 import javax.xml.namespace.QName;
 
 import junit.framework.Assert;
 
+import org.apache.tuscany.sca.core.invocation.NonBlockingInterceptor;
 import org.apache.tuscany.sca.domain.SCADomain;
 import org.apache.tuscany.sca.itest.oneway.impl.OneWayClientImpl;
 import org.apache.tuscany.sca.itest.oneway.impl.OneWayServiceImpl;
@@ -94,4 +101,123 @@
         }
     }
 
+    /**
+     * This method will invoke a @OneWay method that throws an exception
+     * when invoked over a SCA Binding which uses the NonBlockingInterceptor and
+     * ensure that the Exception is logged.
+     * See TUSCANY-2225
+     */
+    @Test
+    public void testOneWayUsingNonBlockingInterceptorThrowsAnException() {
+        OneWayClient client =
+            domain.getService(OneWayClient.class, "OneWayClientComponentSCABinding");
+
+        // We need to modify the JDK Logger for the NonBlockingInterceptor so we
+        // can check that it logs a message for the @OneWay invocation that throws
+        // an Exception
+        Logger nbiLogger = Logger.getLogger(NonBlockingInterceptor.class.getName());
+        DummyJDKHandler handler = new DummyJDKHandler();
+        nbiLogger.addHandler(handler);
+
+        // Add a message on the console to explain the stack dump that is going to be
+        // displayed and state that this is not a problem but expected behaviour
+        System.out.println();
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println("IMPORTANT: The error message that appears on the console");
+        System.out.println("below is an expected error if it is a NullPointerException");
+        System.out.println(" with the message of:");
+        System.out.println("  \"" + OneWayServiceImpl.EXCEPTION_MESSAGE + "\"");
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println();
+        System.out.flush();
+
+        // Invoke the @OneWay method that throws an exception
+        client.doSomethingWhichThrowsException();
+
+        // The logging is done asynchronously so we will need to wait a bit before
+        // the log message appears.
+        long start = System.currentTimeMillis();
+        boolean logged = false;
+        while (System.currentTimeMillis() - start < MAX_SLEEP_TIME && !logged) {
+            // Has the log message been logged?
+            if (handler.exceptionLogged.get()) {
+                logged = true;
+            } else {
+                try {
+                    Thread.sleep(25);
+                } catch (InterruptedException ex) {
+                    Assert.fail("Unexpected exception " + ex);
+                }
+            }
+        }
+
+        // Add a message explaining that errors beyond this point should be reported 
+        System.out.println();
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println("End of expected exceptions. Any errors beyond this point are errors!");
+        System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
+        System.out.println();
+        System.out.flush();
+
+        // Remove the handler
+        nbiLogger.removeHandler(handler);
+
+        // Make sure that the exception was logged
+        Assert.assertTrue(handler.exceptionLogged.get());
+    }
+
+    /**
+     * A handler that is added to the JDK Logging system to examine the log messages
+     * generated to ensure that a @OneWay method that throws an Exception will 
+     * generate a log message.
+     */
+    private class DummyJDKHandler extends Handler {
+
+        /**
+         * Used to track whether the exception has been logged.
+         */
+        private AtomicBoolean exceptionLogged = new AtomicBoolean(false);
+
+        /**
+         * Constructor.
+         */
+        private DummyJDKHandler() {
+            super.setLevel(Level.ALL);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void close() throws SecurityException {
+            // Nothing to do
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void flush() {
+            // Nothing to do
+        }
+
+        /**
+         * Examines the LogRecord and checks whether it matches the one we are looking for.
+         * If it matches, it will set exceptionLogged to True.
+         * 
+         * @param record The Log Record that is being published
+         */
+        @Override
+        public void publish(LogRecord record) {
+            // The log message we are looking for is Severe
+            if (record.getLevel() == Level.SEVERE) {
+                if (record.getThrown() != null
+                        && record.getThrown().toString().indexOf(
+                                OneWayServiceImpl.EXCEPTION_MESSAGE) != -1) {
+                    // We have found our Exception.
+                    exceptionLogged.set(true);
+                }
+            }
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org