axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hadd...@apache.org
Subject cvs commit: xml-axis/java/test/chains TestChainFault.java
Date Sat, 24 Aug 2002 23:27:30 GMT
haddadc     2002/08/24 16:27:30

  Modified:    java/src/org/apache/axis SimpleChain.java
               java/test/chains TestChainFault.java
  Log:
  Extended doVisiting to attach fault details to messagecontext.  this enables the onFault
method of the chain handler(s) to gain access to the reason for them being called to work.
 Fixes Bug #12005 in bugzilla and provides the ability for a handler to log fault details.
  
  Revision  Changes    Path
  1.50      +17 -6     xml-axis/java/src/org/apache/axis/SimpleChain.java
  
  Index: SimpleChain.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/SimpleChain.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- SimpleChain.java	3 Jul 2002 17:50:30 -0000	1.49
  +++ SimpleChain.java	24 Aug 2002 23:27:30 -0000	1.50
  @@ -10,7 +10,7 @@
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  + *    notice, this list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
  @@ -18,7 +18,7 @@
    *    distribution.
    *
    * 3. The end-user documentation included with the redistribution,
  - *    if any, must include the following acknowledgment:  
  + *    if any, must include the following acknowledgment:
    *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowledgment may appear in the software itself,
  @@ -26,7 +26,7 @@
    *
    * 4. The names "Axis" and "Apache Software Foundation" must
    *    not be used to endorse or promote products derived from this
  - *    software without prior written permission. For written 
  + *    software without prior written permission. For written
    *    permission, please contact apache@apache.org.
    *
    * 5. Products derived from this software may not be called "Apache",
  @@ -55,6 +55,9 @@
   
   package org.apache.axis ;
   
  +import org.apache.axis.Message;
  +import org.apache.axis.message.SOAPEnvelope;
  +import org.apache.axis.message.SOAPFault;
   import org.apache.axis.handlers.BasicHandler;
   import org.apache.axis.strategies.InvocationStrategy;
   import org.apache.axis.strategies.WSDLGenStrategy;
  @@ -118,7 +121,7 @@
   
          invoked = true;
           doVisiting(msgContext, iVisitor);
  - 
  +
           if (log.isDebugEnabled()) {
               log.debug("Exit: SimpleChain::invoke");
           }
  @@ -151,6 +154,14 @@
                   i++;
               }
           } catch( AxisFault f ) {
  +           // Attach the fault to the response message; enabling access to the
  +           // fault details while inside the handler onFault methods.
  +            SOAPEnvelope env = new SOAPEnvelope();
  +            SOAPFault faultEl = new SOAPFault(f);
  +            env.clearBody();
  +            env.addBodyElement(faultEl);
  +            Message respMsg = new Message(env);
  +            msgContext.setResponseMessage(respMsg);
               while( --i >= 0 )
                   ((Handler) handlers.elementAt( i )).onFault( msgContext );
               throw f;
  @@ -193,7 +204,7 @@
               throw new InternalException(
                 JavaUtils.getMessage("addAfterInvoke00",
                                      "SimpleChain::addHandler"));
  -        
  +
           handlers.add( handler );
       }
   
  @@ -204,7 +215,7 @@
       public Handler[] getHandlers() {
           if (handlers.size() == 0)
               return null;
  -        
  +
           Handler [] ret = new Handler[handlers.size()];
           return( (Handler[]) handlers.toArray(ret) );
       }
  
  
  
  1.2       +90 -4     xml-axis/java/test/chains/TestChainFault.java
  
  Index: TestChainFault.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/chains/TestChainFault.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestChainFault.java	10 Jan 2002 20:01:01 -0000	1.1
  +++ TestChainFault.java	24 Aug 2002 23:27:30 -0000	1.2
  @@ -54,6 +54,10 @@
    */
   package test.chains;
   
  +import javax.xml.soap.SOAPBody;
  +import javax.xml.soap.SOAPEnvelope;
  +import org.apache.axis.Message;
  +
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  @@ -64,8 +68,16 @@
   import org.apache.axis.MessageContext;
   import org.apache.axis.AxisFault;
   
  +/**
  +* Used to verify that Faults are processed properly in the Handler chain
  +* @author Russell Butek (butek@us.ibm.com)
  +* @author Chris Haddad <haddadc@cobia.net>
  +*/
   public class TestChainFault extends TestCase
   {
  +  // correlation message
  +    public static String FAULT_MESSAGE = "Blew a gasket!";
  +
       public TestChainFault (String name) {
           super(name);
       }
  @@ -102,6 +114,7 @@
       private class TestHandler extends BasicHandler {
           private int chainPos;
           private boolean doFault = false;
  +        private String stFaultCatch = null;
   
           /* The following state really relates to a Message Context, so this Handler
            * must not be used for more than one Message Context. However, it
  @@ -117,12 +130,15 @@
               doFault = true;
           }
   
  +        public void setFaultCatch(String stValue) { stFaultCatch = stValue; }
  +        public String getFaultCatch() { return stFaultCatch; }
  +
           public void invoke(MessageContext msgContext) throws AxisFault {
               TestMessageContext mc = (TestMessageContext)msgContext;
               assertEquals("Handler.invoke out of sequence", chainPos, mc.count());
               invoked = true;
               if (doFault) {
  -                throw new AxisFault();
  +                throw new AxisFault(TestChainFault.FAULT_MESSAGE);
               }
               mc.incCount();
           }
  @@ -132,14 +148,39 @@
               mc.decCount();
               assertEquals("Handler.onFault out of sequence", chainPos, mc.count());
               assertTrue("Handler.onFault protocol error", invoked);
  +            // grap the Soap Fault String
  +            stFaultCatch = getFaultString(msgContext);
           }
       }
   
  +    /**
  +    * Extract the fault string from the Soap Response
  +    *
  +    **/
  +    String getFaultString(MessageContext msgContext) {
  +      String stRetval = null;
  +      Message message = msgContext.getResponseMessage();
  +      try {
  +          if (message != null) {
  +            SOAPBody oBody  = message.getSOAPEnvelope().getBody();
  +            stRetval = oBody.getFault().getFaultString();
  +          }
  +      }
  +      catch (javax.xml.soap.SOAPException e) {
  +          assertTrue("Unforseen soap exception", false);
  +      }
  +      catch (AxisFault f) {
  +          assertTrue("Unforseen axis fault", false);
  +      }
  +
  +      return stRetval;
  +    }
  +
       public void testSimpleChainFaultAfterInvoke()
       {
           try {
               SimpleChain c = new SimpleChain();
  -            
  +
               for (int i = 0; i < 5; i++) {
                   c.addHandler(new TestHandler(i));
               }
  @@ -154,11 +195,12 @@
           }
       }
   
  +
       public void testSimpleChainFaultDuringInvoke()
       {
           try {
               SimpleChain c = new SimpleChain();
  -            
  +
               for (int i = 0; i < 5; i++) {
                   TestHandler th = new TestHandler(i);
                   if (i == 3) {
  @@ -166,7 +208,7 @@
                   }
                   c.addHandler(th);
               }
  -            
  +
   
               TestMessageContext mc = new TestMessageContext();
               try {
  @@ -180,4 +222,48 @@
               assertTrue("Unexpected exception", false);
           }
       }
  +
  +/**
  +* Ensure that the fault detail is being passed back
  +* to handlers that executed prior to the fault
  +**/
  +    public void testFaultDetailAvailableDuringInvoke()
  +    {
  +      // the handler instance to validate
  +      // NOTE:must execute before the handler that throws the fault
  +      TestHandler testHandler = null;
  +
  +        try {
  +            SimpleChain c = new SimpleChain();
  +
  +            for (int i = 0; i < 5; i++) {
  +                TestHandler th = new TestHandler(i);
  +                if (i == 2)
  +                  testHandler = th;
  +
  +                if (i == 3) {
  +                    th.setToFault();
  +                }
  +                c.addHandler(th);
  +            }
  +
  +
  +            TestMessageContext mc = new TestMessageContext();
  +            try {
  +                c.invoke(mc);
  +                assertTrue("Testcase error - didn't throw fault", false);
  +            } catch (AxisFault f) {
  +                // did we save off the fault string?
  +              assertEquals("faultstring does not match constant",
  +                testHandler.getFaultCatch(),TestChainFault.FAULT_MESSAGE);
  +                // does saved faultString match AxisFault?
  +              assertEquals("Fault not caught by handler",
  +                testHandler.getFaultCatch(),f.getFaultString());
  +            }
  +
  +        } catch (Exception ex) {
  +            assertTrue("Unexpected exception", false);
  +        }
  +    }
  +
   }
  
  
  

Mime
View raw message