View Javadoc

1   /*
2    * $Id: FunctionalTestComponent.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tck.functional;
12  
13  import org.mule.MuleException;
14  import org.mule.MuleManager;
15  import org.mule.config.i18n.MessageFactory;
16  import org.mule.impl.RequestContext;
17  import org.mule.umo.UMOEventContext;
18  import org.mule.umo.lifecycle.Callable;
19  import org.mule.util.StringMessageUtils;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * <code>FunctionalTestComponent</code> is a component that can be used by
26   * functional tests. This component accepts an EventCallback that can be used to
27   * assert the state of the current event.
28   * 
29   * @see EventCallback
30   */
31  
32  public class FunctionalTestComponent implements Callable
33  {
34      protected transient Log logger = LogFactory.getLog(getClass());
35  
36      public static final int STREAM_SAMPLE_SIZE = 4;
37      public static final int STREAM_BUFFER_SIZE = 4096;
38      private EventCallback eventCallback;
39      private Object returnMessage = null;
40      private boolean appendComponentName = false;
41      private boolean throwException = false;
42  
43      public Object onCall(UMOEventContext context) throws Exception
44      {
45          String contents = context.getTransformedMessageAsString();
46          String msg = StringMessageUtils.getBoilerPlate("Message Received in component: "
47                          + context.getComponentDescriptor().getName() + ". Content is: "
48                          + StringMessageUtils.truncate(contents, 100, true), '*', 80);
49  
50          logger.info(msg);
51  
52          if (eventCallback != null)
53          {
54              eventCallback.eventReceived(context, this);
55          }
56  
57          Object replyMessage;
58          if (returnMessage != null)
59          {
60              replyMessage = returnMessage;
61          }
62          else
63          {
64              replyMessage = contents + " Received" + (appendComponentName ?  " " + context.getComponentDescriptor().getName() : "");
65          }
66  
67          MuleManager.getInstance().fireNotification(
68              new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
69  
70          if (throwException)
71          {
72              throw new MuleException(MessageFactory.createStaticMessage("Functional Test Component Exception"));
73          }
74  
75          return replyMessage;
76      }
77  
78      public Object onReceive(Object data) throws Exception
79      {
80          UMOEventContext context = RequestContext.getEventContext();
81  
82          String contents = data.toString();
83          String msg = StringMessageUtils.getBoilerPlate("Message Received in component: "
84                          + context.getComponentDescriptor().getName() + ". Content is: "
85                          + StringMessageUtils.truncate(contents, 100, true), '*', 80);
86  
87          logger.info(msg);
88  
89          if (eventCallback != null)
90          {
91              eventCallback.eventReceived(context, this);
92          }
93  
94          Object replyMessage;
95          if (returnMessage != null)
96          {
97              replyMessage = returnMessage;
98          }
99          else
100         {
101             replyMessage = contents + " Received";
102         }
103 
104         MuleManager.getInstance().fireNotification(
105             new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
106 
107         if (throwException)
108         {
109             throw new MuleException(MessageFactory.createStaticMessage("Functional Test Component Exception"));
110         }
111 
112         return replyMessage;
113     }
114 
115     public EventCallback getEventCallback()
116     {
117         return eventCallback;
118     }
119 
120     public void setEventCallback(EventCallback eventCallback)
121     {
122         this.eventCallback = eventCallback;
123     }
124 
125     public Object getReturnMessage()
126     {
127         return returnMessage;
128     }
129 
130     public void setReturnMessage(Object returnMessage)
131     {
132         this.returnMessage = returnMessage;
133     }
134 
135     public boolean isThrowException()
136     {
137         return throwException;
138     }
139 
140     public void setThrowException(boolean throwException)
141     {
142         this.throwException = throwException;
143     }
144 
145     public boolean isAppendComponentName()
146     {
147         return appendComponentName;
148     }
149 
150     public void setAppendComponentName(boolean appendComponentName)
151     {
152         this.appendComponentName = appendComponentName;
153     }
154 
155 }
156