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 | 0 | super(); |
44 | |
|
45 | 0 | setStartContext(true); |
46 | 0 | } |
47 | |
|
48 | |
@Override |
49 | |
protected ConfigurationBuilder getBuilder() throws Exception |
50 | |
{ |
51 | 0 | 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 | 0 | final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName); |
66 | |
|
67 | 0 | if (flowConstruct != null) |
68 | |
{ |
69 | 0 | return getComponent(flowConstruct); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | 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 | 0 | if (flowConstruct instanceof Service) |
88 | |
{ |
89 | 0 | return getComponentObject(((Service) flowConstruct).getComponent()); |
90 | |
} |
91 | 0 | else if (flowConstruct instanceof SimpleService) |
92 | |
{ |
93 | 0 | return getComponentObject(((SimpleService) flowConstruct).getComponent()); |
94 | |
} |
95 | 0 | else if (flowConstruct instanceof SimpleFlowConstruct) |
96 | |
{ |
97 | 0 | SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct; |
98 | |
|
99 | 0 | for (MessageProcessor processor : flow.getMessageProcessors()) |
100 | |
{ |
101 | 0 | if(processor instanceof Component) |
102 | |
{ |
103 | 0 | return getComponentObject(((Component) processor)); |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
} |
108 | 0 | 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 | 0 | return (FunctionalTestComponent) getComponent(serviceName); |
124 | |
} |
125 | |
|
126 | |
protected FlowConstruct getFlowConstruct(String name) throws Exception |
127 | |
{ |
128 | 0 | return muleContext.getRegistry().lookupFlowConstruct(name); |
129 | |
} |
130 | |
|
131 | |
protected String loadResourceAsString(String name) throws IOException |
132 | |
{ |
133 | 0 | return IOUtils.getResourceAsString(name, getClass()); |
134 | |
} |
135 | |
|
136 | |
protected InputStream loadResource(String name) throws IOException |
137 | |
{ |
138 | 0 | return IOUtils.getResourceAsStream(name, getClass()); |
139 | |
} |
140 | |
|
141 | |
private Object getComponentObject(Component component) throws Exception |
142 | |
{ |
143 | 0 | if (component instanceof JavaComponent) |
144 | |
{ |
145 | 0 | return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 0 | fail("Component is not a JavaComponent and therefore has no component object instance"); |
150 | 0 | return null; |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
protected void testFlow(String flowName) throws Exception |
155 | |
{ |
156 | 0 | testFlow(flowName, getTestEvent("data", MessageExchangePattern.ONE_WAY)); |
157 | 0 | } |
158 | |
|
159 | |
protected void testFlow(String flowName, MuleEvent event) throws Exception |
160 | |
{ |
161 | 0 | SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(flowName); |
162 | 0 | flow.process(event); |
163 | 0 | FlowAssert.verify(flowName); |
164 | 0 | } |
165 | |
} |