1
2
3
4
5
6
7 package org.mule.module.ibeans.annotations;
8
9 import org.mule.api.config.ConfigurationBuilder;
10 import org.mule.api.config.MuleProperties;
11 import org.mule.api.transformer.Transformer;
12 import org.mule.api.transformer.TransformerException;
13 import org.mule.module.ibeans.config.IBeanHolderConfigurationBuilder;
14 import org.mule.module.ibeans.spi.MuleIBeansPlugin;
15 import org.mule.tck.junit4.AbstractMuleContextTestCase;
16 import org.mule.util.IOUtils;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.ibeans.annotation.Return;
26 import org.ibeans.api.DataType;
27 import org.ibeans.api.Response;
28 import org.ibeans.api.channel.MimeType;
29 import org.ibeans.api.channel.MimeTypes;
30 import org.ibeans.impl.support.datatype.CollectionDataType;
31 import org.ibeans.impl.support.datatype.DataTypeFactory;
32 import org.ibeans.impl.test.MockIBean;
33 import org.ibeans.impl.test.MockMessageCallback;
34 import org.ibeans.spi.IBeansPlugin;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37
38 import static org.junit.Assert.assertNotNull;
39
40 public abstract class AbstractIBeansTestCase extends AbstractMuleContextTestCase
41 {
42 protected IBeansPlugin plugin;
43
44 protected AbstractIBeansTestCase()
45 {
46 setStartContext(true);
47 setDisposeContextPerClass(true);
48 }
49
50 protected IBeansPlugin createPlugin()
51 {
52 return new MuleIBeansPlugin(muleContext);
53 }
54
55 @Override
56 protected void doSetUp() throws Exception
57 {
58 plugin = createPlugin();
59
60 muleContext.getRegistry().registerObject("testcase", this);
61 }
62
63 @Override
64 protected void addBuilders(List<ConfigurationBuilder> builders)
65 {
66 IBeanHolderConfigurationBuilder builder = new IBeanHolderConfigurationBuilder("org.mule");
67 builders.add(builder);
68 }
69
70 protected Answer withXmlData(final String resource, final Object ibean)
71 {
72 return withData(resource, MimeTypes.XML, null, ibean);
73 }
74
75 protected Answer withRssData(final String resource, final Object ibean)
76 {
77 return withData(resource, MimeTypes.RSS, null, ibean);
78 }
79
80 protected Answer withAtomData(final String resource, final Object ibean)
81 {
82 return withData(resource, MimeTypes.ATOM, null, ibean);
83 }
84
85 protected Answer withJsonData(final String resource, final Object ibean)
86 {
87 return withData(resource, MimeTypes.JSON, null, ibean);
88 }
89
90 protected Answer withTextData(final String resource, final Object ibean)
91 {
92 return withData(resource, MimeTypes.TEXT, null, ibean);
93 }
94
95
96
97
98
99
100
101
102 protected Answer withData(final String resource, final Class returnType)
103 {
104 return new Answer()
105 {
106 public Object answer(InvocationOnMock
107 invocation) throws Throwable
108 {
109 return loadData(resource, DataTypeFactory.create(returnType));
110 }
111 };
112 }
113
114
115
116
117
118
119
120
121
122
123 protected Answer withData(final String resource, final MimeType mimeType, final MockMessageCallback callback, final Object ibean)
124 {
125 return new Answer()
126 {
127 public Object answer(InvocationOnMock invocation) throws Throwable
128 {
129 MimeType mime = mimeType;
130 DataType ret = ((MockIBean)ibean).ibeanReturnType();
131 if(ret!=null) ret.setMimeType(mime.toString());
132 Object data;
133
134
135
136
137
138
139
140 if (ret == null || invocation.getMethod().isAnnotationPresent(Return.class) ||
141 !invocation.getMethod().getReturnType().isAssignableFrom(ret.getType()))
142 {
143 ret = DataTypeFactory.createFromReturnType(invocation.getMethod());
144 mime = null;
145 }
146
147 data = loadData(resource, ret);
148 ((MockIBean)ibean).ibeanSetMimeType(mime);
149 ((MockIBean)ibean).ibeanSetMessageCallback(callback);
150
151 Response response;
152 Map<String, Object> headers = null;
153 if(mime!=null)
154 {
155 headers = new HashMap<String, Object>();
156 headers.put(MuleProperties.CONTENT_TYPE_PROPERTY, mime.toString());
157 }
158 response = plugin.createResponse(data, headers, null);
159 if(callback!=null)
160 {
161 callback.onMessage(response);
162 }
163 return response;
164 }
165 };
166 }
167
168 protected <T> T loadData(String resource, DataType<T> type) throws IOException, TransformerException
169 {
170 InputStream in = IOUtils.getResourceAsStream(resource, getClass());
171 assertNotNull("Resource stream for: " + resource + " must not be null", in);
172 return getDataAs(in, type);
173 }
174
175 protected <T> T getDataAs(InputStream data, DataType<T> as) throws TransformerException
176 {
177 org.mule.api.transformer.DataType muleDT;
178 if(as instanceof CollectionDataType)
179 {
180 muleDT = new org.mule.transformer.types.CollectionDataType(
181 (Class<? extends Collection>)as.getType(),
182 ((CollectionDataType) as).getItemType(),
183 as.getMimeType());
184 }
185 else
186 {
187 muleDT = new org.mule.transformer.types.SimpleDataType(
188 as.getType(),
189 as.getMimeType());
190 }
191 Transformer t = muleContext.getRegistry().lookupTransformer(org.mule.transformer.types.DataTypeFactory.create(data.getClass()), muleDT);
192 return (T)t.transform(data);
193 }
194
195 }