KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > tck > functional > FunctionalTestComponent


1 /*
2  * $Id: FunctionalTestComponent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.MuleException;
16 import org.mule.MuleManager;
17 import org.mule.config.i18n.Message;
18 import org.mule.impl.RequestContext;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.lifecycle.Callable;
21 import org.mule.util.StringMessageUtils;
22
23 /**
24  * <code>FunctionalTestComponent</code> is a component that can be used by
25  * functional tests. This component accepts an EventCallback that can be used to
26  * assert the state of the current event.
27  *
28  * @see EventCallback
29  */

30
31 public class FunctionalTestComponent implements Callable
32 {
33     protected transient Log logger = LogFactory.getLog(getClass());
34
35     private EventCallback eventCallback;
36     private String JavaDoc returnMessage = null;
37     private boolean throwException = false;
38
39     public Object JavaDoc onCall(UMOEventContext context) throws Exception JavaDoc
40     {
41         String JavaDoc contents = context.getTransformedMessageAsString();
42         String JavaDoc msg = null;
43         msg = StringMessageUtils.getBoilerPlate("Message Received in component: "
44                                                 + context.getComponentDescriptor().getName()
45                                                 + ". Content is: "
46                                                 + StringMessageUtils.truncate(contents, 100, true), '*', 80);
47         logger.info(msg);
48
49         if (eventCallback != null)
50         {
51             eventCallback.eventReceived(context, this);
52         }
53         String JavaDoc replyMessage;
54         if (returnMessage != null)
55         {
56             replyMessage = returnMessage;
57         }
58         else
59         {
60             replyMessage = contents + " Received";
61         }
62
63         MuleManager.getInstance().fireNotification(
64             new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
65
66         if (throwException)
67         {
68             throw new MuleException(Message.createStaticMessage("Functional Test Component Exception"));
69         }
70         return replyMessage;
71     }
72
73     public Object JavaDoc onReceive(Object JavaDoc data) throws Exception JavaDoc
74     {
75         String JavaDoc contents = data.toString();
76         UMOEventContext context = RequestContext.getEventContext();
77
78         String JavaDoc msg = null;
79         msg = StringMessageUtils.getBoilerPlate("Message Received in component: "
80                                                 + context.getComponentDescriptor().getName()
81                                                 + ". Content is: "
82                                                 + StringMessageUtils.truncate(contents, 100, true), '*', 80);
83         logger.info(msg);
84
85         if (eventCallback != null)
86         {
87             eventCallback.eventReceived(context, this);
88         }
89
90         String JavaDoc replyMessage;
91
92         if (returnMessage != null)
93         {
94             replyMessage = returnMessage;
95         }
96         else
97         {
98             replyMessage = contents + " Received";
99         }
100
101         MuleManager.getInstance().fireNotification(
102             new FunctionalTestNotification(context, replyMessage, FunctionalTestNotification.EVENT_RECEIVED));
103
104         if (throwException)
105         {
106             throw new MuleException(Message.createStaticMessage("Functional Test Component Exception"));
107         }
108
109         return replyMessage;
110     }
111
112     public EventCallback getEventCallback()
113     {
114         return eventCallback;
115     }
116
117     public void setEventCallback(EventCallback eventCallback)
118     {
119         this.eventCallback = eventCallback;
120     }
121
122     public String JavaDoc getReturnMessage()
123     {
124         return returnMessage;
125     }
126
127     public void setReturnMessage(String JavaDoc returnMessage)
128     {
129         this.returnMessage = returnMessage;
130     }
131
132     public boolean isThrowException()
133     {
134         return throwException;
135     }
136
137     public void setThrowException(boolean throwException)
138     {
139         this.throwException = throwException;
140     }
141
142 }
143
Popular Tags