1
2
3
4
5
6
7 package org.mule.tck.junit4;
8
9 import static org.junit.Assert.fail;
10
11 import org.mule.MessageExchangePattern;
12 import org.mule.api.MuleEvent;
13 import org.mule.api.component.Component;
14 import org.mule.api.component.JavaComponent;
15 import org.mule.api.config.ConfigurationBuilder;
16 import org.mule.api.construct.FlowConstruct;
17 import org.mule.api.processor.MessageProcessor;
18 import org.mule.api.registry.RegistrationException;
19 import org.mule.api.service.Service;
20 import org.mule.component.AbstractJavaComponent;
21 import org.mule.config.i18n.MessageFactory;
22 import org.mule.config.spring.SpringXmlConfigurationBuilder;
23 import org.mule.construct.SimpleFlowConstruct;
24 import org.mule.construct.SimpleService;
25 import org.mule.tck.functional.FlowAssert;
26 import org.mule.tck.functional.FunctionalTestComponent;
27 import org.mule.util.IOUtils;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33
34
35
36
37
38
39 public abstract class FunctionalTestCase extends AbstractMuleContextTestCase
40 {
41 public FunctionalTestCase()
42 {
43 super();
44
45 setStartContext(true);
46 }
47
48 @Override
49 protected ConfigurationBuilder getBuilder() throws Exception
50 {
51 return new SpringXmlConfigurationBuilder(getConfigResources());
52 }
53
54 protected abstract String getConfigResources();
55
56
57
58
59
60
61
62
63 protected Object getComponent(String serviceName) throws Exception
64 {
65 final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
66
67 if (flowConstruct != null)
68 {
69 return getComponent(flowConstruct);
70 }
71 else
72 {
73 throw new RegistrationException(MessageFactory.createStaticMessage("Service " + serviceName
74 + " not found in Registry"));
75 }
76 }
77
78
79
80
81
82
83
84
85 protected Object getComponent(FlowConstruct flowConstruct) throws Exception
86 {
87 if (flowConstruct instanceof Service)
88 {
89 return getComponentObject(((Service) flowConstruct).getComponent());
90 }
91 else if (flowConstruct instanceof SimpleService)
92 {
93 return getComponentObject(((SimpleService) flowConstruct).getComponent());
94 }
95 else if (flowConstruct instanceof SimpleFlowConstruct)
96 {
97 SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct;
98
99 for (MessageProcessor processor : flow.getMessageProcessors())
100 {
101 if(processor instanceof Component)
102 {
103 return getComponentObject(((Component) processor));
104 }
105 }
106
107 }
108 throw new RegistrationException(
109 MessageFactory.createStaticMessage("Can't get component from flow construct "
110 + flowConstruct.getName()));
111 }
112
113
114
115
116
117
118
119
120
121 protected FunctionalTestComponent getFunctionalTestComponent(String serviceName) throws Exception
122 {
123 return (FunctionalTestComponent) getComponent(serviceName);
124 }
125
126 protected FlowConstruct getFlowConstruct(String name) throws Exception
127 {
128 return muleContext.getRegistry().lookupFlowConstruct(name);
129 }
130
131 protected String loadResourceAsString(String name) throws IOException
132 {
133 return IOUtils.getResourceAsString(name, getClass());
134 }
135
136 protected InputStream loadResource(String name) throws IOException
137 {
138 return IOUtils.getResourceAsStream(name, getClass());
139 }
140
141 private Object getComponentObject(Component component) throws Exception
142 {
143 if (component instanceof JavaComponent)
144 {
145 return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
146 }
147 else
148 {
149 fail("Component is not a JavaComponent and therefore has no component object instance");
150 return null;
151 }
152 }
153
154 protected void testFlow(String flowName) throws Exception
155 {
156 testFlow(flowName, getTestEvent("data", MessageExchangePattern.ONE_WAY));
157 }
158
159 protected void testFlow(String flowName, MuleEvent event) throws Exception
160 {
161 SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(flowName);
162 flow.process(event);
163 FlowAssert.verify(flowName);
164 }
165 }