1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck;
12
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.FunctionalTestComponent;
26 import org.mule.util.IOUtils;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30
31
32
33
34
35
36
37
38 public abstract class FunctionalTestCase extends AbstractMuleTestCase
39 {
40 public FunctionalTestCase()
41 {
42 super();
43
44 setStartContext(true);
45 }
46
47 @Override
48 protected ConfigurationBuilder getBuilder() throws Exception
49 {
50 return new SpringXmlConfigurationBuilder(getConfigResources());
51 }
52
53 protected abstract String getConfigResources();
54
55
56
57
58
59
60
61
62 protected Object getComponent(String serviceName) throws Exception
63 {
64 final FlowConstruct flowConstruct = muleContext.getRegistry().lookupObject(serviceName);
65
66 if (flowConstruct != null)
67 {
68 return getComponent(flowConstruct);
69 }
70 else
71 {
72 throw new RegistrationException(MessageFactory.createStaticMessage("Service " + serviceName
73 + " not found in Registry"));
74 }
75 }
76
77
78
79
80
81
82
83
84 protected Object getComponent(FlowConstruct flowConstruct) throws Exception
85 {
86 if (flowConstruct instanceof Service)
87 {
88 return getComponentObject(((Service) flowConstruct).getComponent());
89 }
90 else if (flowConstruct instanceof SimpleService)
91 {
92 return getComponentObject(((SimpleService) flowConstruct).getComponent());
93 }
94 else if (flowConstruct instanceof SimpleFlowConstruct)
95 {
96 SimpleFlowConstruct flow = (SimpleFlowConstruct)flowConstruct;
97
98 for (MessageProcessor processor : flow.getMessageProcessors())
99 {
100 if(processor instanceof Component)
101 {
102 return getComponentObject(((Component) processor));
103 }
104 }
105
106 }
107 throw new RegistrationException(
108 MessageFactory.createStaticMessage("Can't get component from flow construct "
109 + flowConstruct.getName()));
110 }
111
112
113
114
115
116
117
118
119
120 protected FunctionalTestComponent getFunctionalTestComponent(String serviceName) throws Exception
121 {
122 return (FunctionalTestComponent) getComponent(serviceName);
123 }
124
125 protected String loadResourceAsString(String name) throws IOException
126 {
127 return IOUtils.getResourceAsString(name, getClass());
128 }
129
130 protected InputStream loadResource(String name) throws IOException
131 {
132 return IOUtils.getResourceAsStream(name, getClass());
133 }
134
135 private Object getComponentObject(Component component) throws Exception
136 {
137 if (component instanceof JavaComponent)
138 {
139 return ((AbstractJavaComponent) component).getObjectFactory().getInstance(muleContext);
140 }
141 else
142 {
143 fail("Component is not a JavaComponent and therefore has no component object instance");
144 return null;
145 }
146 }
147 }