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