1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.templating.test; |
35 |
|
|
36 |
|
import static org.easymock.classextension.EasyMock.*; |
37 |
|
import freemarker.core.Environment; |
38 |
|
import freemarker.template.TemplateException; |
39 |
|
import freemarker.template.TemplateExceptionHandler; |
40 |
|
import info.magnolia.cms.beans.config.ContentRepository; |
41 |
|
import info.magnolia.cms.beans.config.ServerConfiguration; |
42 |
|
import info.magnolia.cms.beans.config.URI2RepositoryManager; |
43 |
|
import info.magnolia.cms.core.Content; |
44 |
|
import info.magnolia.cms.i18n.DefaultI18nContentSupport; |
45 |
|
import info.magnolia.cms.i18n.I18nContentSupport; |
46 |
|
import info.magnolia.cms.util.ContentUtil; |
47 |
|
import info.magnolia.context.MgnlContext; |
48 |
|
import info.magnolia.freemarker.FreemarkerConfig; |
49 |
|
import info.magnolia.link.LinkTransformerManager; |
50 |
|
import info.magnolia.module.templating.Paragraph; |
51 |
|
import info.magnolia.module.templating.ParagraphManager; |
52 |
|
import info.magnolia.module.templating.ParagraphRenderer; |
53 |
|
import info.magnolia.module.templating.ParagraphRendererManager; |
54 |
|
import info.magnolia.module.templating.RenderException; |
55 |
|
import info.magnolia.module.templating.RenderingModel; |
56 |
|
import info.magnolia.module.templating.Template; |
57 |
|
import info.magnolia.module.templating.TemplateManager; |
58 |
|
import info.magnolia.module.templating.TemplateRenderer; |
59 |
|
import info.magnolia.module.templating.TemplateRendererManager; |
60 |
|
import info.magnolia.module.templating.engine.DefaultRenderingEngine; |
61 |
|
import info.magnolia.module.templating.engine.RenderingEngine; |
62 |
|
import info.magnolia.module.templating.paragraphs.FreemarkerParagraphRenderer; |
63 |
|
import info.magnolia.module.templating.renderers.FreemarkerTemplateRenderer; |
64 |
|
import info.magnolia.objectfactory.Components; |
65 |
|
import info.magnolia.test.ComponentsTestUtil; |
66 |
|
import info.magnolia.test.MgnlTestCase; |
67 |
|
import info.magnolia.test.mock.MockUtil; |
68 |
|
import info.magnolia.test.mock.MockWebContext; |
69 |
|
|
70 |
|
import java.io.InputStream; |
71 |
|
import java.io.StringWriter; |
72 |
|
import java.io.Writer; |
73 |
|
import java.net.MalformedURLException; |
74 |
|
import java.net.URL; |
75 |
|
import java.net.URLClassLoader; |
76 |
|
import java.util.HashMap; |
77 |
|
import java.util.HashSet; |
78 |
|
import java.util.Locale; |
79 |
|
import java.util.Map; |
80 |
|
import java.util.Set; |
81 |
|
|
82 |
|
import javax.servlet.ServletContext; |
83 |
|
import javax.servlet.http.HttpServletRequest; |
84 |
|
import javax.servlet.http.HttpServletResponse; |
85 |
|
|
86 |
|
import org.easymock.IAnswer; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| 0% |
Uncovered Elements: 139 (139) |
Complexity: 33 |
Complexity Density: 0.32 |
|
93 |
|
public abstract class AbstractRenderTestCase extends MgnlTestCase { |
94 |
|
|
95 |
|
protected static final String TEST_CONTEXT = "/test-context"; |
96 |
|
|
97 |
|
protected static final String TEST_BASE_URL = "http://testdomain.com:1234" + TEST_CONTEXT; |
98 |
|
|
99 |
|
protected static final String FREEMARKER_TYPE = "freemarker"; |
100 |
|
|
101 |
|
protected static final byte[] DUMMY_BYTES = new byte[]{'D', 'U', 'M', 'M', 'Y'}; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
private Map<String, Paragraph> paragraphs; |
107 |
|
private Map<String, Template> templates; |
108 |
|
private Map<String, ParagraphRenderer> paragraphRenderers; |
109 |
|
private Map<String, TemplateRenderer> templateRenderers; |
110 |
|
|
111 |
|
private MockWebContext mockWebContext; |
112 |
|
|
113 |
|
private ServerConfiguration serverConfiguration; |
114 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
|
115 |
0
|
@Override... |
116 |
|
protected void setUp() throws Exception { |
117 |
0
|
super.setUp(); |
118 |
|
|
119 |
0
|
paragraphs = new HashMap<String, Paragraph>(); |
120 |
0
|
templates = new HashMap<String, Template>(); |
121 |
0
|
paragraphRenderers = new HashMap<String, ParagraphRenderer>(); |
122 |
0
|
templateRenderers = new HashMap<String, TemplateRenderer>(); |
123 |
|
|
124 |
0
|
serverConfiguration = new ServerConfiguration(); |
125 |
0
|
ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration); |
126 |
|
|
127 |
0
|
ServletContext servletContext = createFreemarkerFriendlyServletContext(); |
128 |
|
|
129 |
|
|
130 |
0
|
mockWebContext = (MockWebContext) MgnlContext.getWebContext(); |
131 |
0
|
mockWebContext.setServletContext(servletContext); |
132 |
0
|
mockWebContext.setRequest(createNiceMock(HttpServletRequest.class)); |
133 |
0
|
mockWebContext.setResponse(createNiceMock(HttpServletResponse.class)); |
134 |
|
|
135 |
|
|
136 |
0
|
FreemarkerConfig freemarkerConfig = new FreemarkerConfig(); |
137 |
0
|
freemarkerConfig.setTemplateExceptionHandler(new TemplateExceptionHandler() { |
138 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
0
|
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {... |
140 |
|
|
141 |
0
|
throw new RuntimeException(te); |
142 |
|
} |
143 |
|
}); |
144 |
0
|
ComponentsTestUtil.setInstance(FreemarkerConfig.class, freemarkerConfig); |
145 |
|
|
146 |
0
|
setUpManagers(); |
147 |
|
|
148 |
0
|
setRenderingEngine(new DefaultRenderingEngine()); |
149 |
|
|
150 |
|
|
151 |
0
|
setI18NContentSupport(new DefaultI18nContentSupport()); |
152 |
0
|
setLinkTransformerManager(new LinkTransformerManager()); |
153 |
0
|
setURI2RepositoryManager(new URI2RepositoryManager()); |
154 |
|
|
155 |
|
|
156 |
0
|
registerParagraphRenderer(FREEMARKER_TYPE, new FreemarkerParagraphRenderer()); |
157 |
0
|
registerTemplateRenderer(FREEMARKER_TYPE, new FreemarkerTemplateRenderer()); |
158 |
|
|
159 |
|
|
160 |
0
|
setDefaultBaseUrl(TEST_BASE_URL); |
161 |
0
|
setContextPath(TEST_CONTEXT); |
162 |
0
|
setLocale(Locale.ENGLISH); |
163 |
0
|
setPublicInstance(); |
164 |
|
|
165 |
0
|
MockUtil.createAndSetHierarchyManager(ContentRepository.WEBSITE); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
@link |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0
|
protected MockWebContext getWebContext() {... |
172 |
0
|
return mockWebContext; |
173 |
|
} |
174 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
0
|
protected Paragraph registerParagraph(String name, Paragraph paragraph) {... |
176 |
0
|
return paragraphs.put(name, paragraph); |
177 |
|
} |
178 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
179 |
0
|
protected Paragraph registerParagraph(String name, String templatePath, String type, Class<? extends RenderingModel> modelClass) {... |
180 |
0
|
Paragraph paragraph = new Paragraph(); |
181 |
0
|
paragraph.setName(name); |
182 |
0
|
paragraph.setTemplatePath(templatePath); |
183 |
0
|
paragraph.setType(type); |
184 |
0
|
if (modelClass != null) { |
185 |
0
|
paragraph.setModelClass(modelClass); |
186 |
|
} |
187 |
0
|
registerParagraph(name, paragraph); |
188 |
0
|
return paragraph; |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
0
|
protected void registerParagraphRenderer(String name, ParagraphRenderer paragraphRenderer) {... |
192 |
0
|
paragraphRenderers.put(name, paragraphRenderer); |
193 |
|
} |
194 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
195 |
0
|
protected Template registerTemplate(String name, String templatePath, String type, Class<? extends RenderingModel> modelClass) {... |
196 |
0
|
Template template = new Template(); |
197 |
0
|
template.setName(name); |
198 |
0
|
template.setTemplatePath(templatePath); |
199 |
0
|
template.setType(type); |
200 |
0
|
if (modelClass != null) { |
201 |
0
|
template.setModelClass(modelClass); |
202 |
|
} |
203 |
0
|
registerTemplate(name, template); |
204 |
0
|
return template; |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0
|
protected Template registerTemplate(String name, Template template) {... |
208 |
0
|
return templates.put(name, template); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0
|
protected void registerTemplateRenderer(String name, TemplateRenderer templateRenderer) {... |
212 |
0
|
templateRenderers.put(name, templateRenderer); |
213 |
|
} |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
215 |
0
|
protected String render(Content content) throws RenderException {... |
216 |
0
|
RenderingEngine renderingEngine = Components.getSingleton(RenderingEngine.class); |
217 |
0
|
StringWriter out = new StringWriter(); |
218 |
0
|
renderingEngine.render(content, out); |
219 |
0
|
return out.toString(); |
220 |
|
} |
221 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
0
|
protected void setAuthorInstance() {... |
223 |
0
|
serverConfiguration.setAdmin(true); |
224 |
|
} |
225 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
0
|
protected void setContextPath(String contextPath) {... |
227 |
0
|
getWebContext().setContextPath(contextPath); |
228 |
|
} |
229 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
0
|
protected void setDefaultBaseUrl(String baseUrl) {... |
231 |
0
|
serverConfiguration.setDefaultBaseUrl(baseUrl); |
232 |
|
} |
233 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
0
|
protected void setEditMode(boolean editMode) {... |
235 |
0
|
MgnlContext.getAggregationState().setPreviewMode(!editMode); |
236 |
|
} |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
0
|
protected void setI18NContentSupport(I18nContentSupport i18nContentSupport) {... |
239 |
0
|
ComponentsTestUtil.setInstance(I18nContentSupport.class, i18nContentSupport); |
240 |
|
} |
241 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
242 |
0
|
protected void setLinkTransformerManager(LinkTransformerManager linkTransformerManager) {... |
243 |
0
|
ComponentsTestUtil.setInstance(LinkTransformerManager.class, linkTransformerManager); |
244 |
|
} |
245 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
0
|
protected void setLocale(Locale locale) {... |
247 |
0
|
getWebContext().setLocale(locale); |
248 |
|
} |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
0
|
protected void setPublicInstance() {... |
251 |
0
|
serverConfiguration.setAdmin(false); |
252 |
|
} |
253 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
254 |
0
|
protected void setRenderingEngine(RenderingEngine renderingEngine) {... |
255 |
0
|
ComponentsTestUtil.setInstance(RenderingEngine.class, renderingEngine); |
256 |
|
} |
257 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
258 |
0
|
protected void setURI2RepositoryManager(URI2RepositoryManager uri2repositoryManager) {... |
259 |
0
|
ComponentsTestUtil.setInstance(URI2RepositoryManager.class, uri2repositoryManager); |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
267 |
0
|
private ServletContext createFreemarkerFriendlyServletContext() throws MalformedURLException {... |
268 |
0
|
ServletContext servletContext = createNiceMock(ServletContext.class); |
269 |
|
|
270 |
|
|
271 |
0
|
URLClassLoader urlClassLoader = (URLClassLoader) getClass().getClassLoader(); |
272 |
0
|
Set<String> pathes = new HashSet<String>(); |
273 |
0
|
URL[] urls = urlClassLoader.getURLs(); |
274 |
0
|
for (int i = 0; i < urls.length; i++) { |
275 |
0
|
URL url = urls[i]; |
276 |
0
|
pathes.add(url.toExternalForm()); |
277 |
|
} |
278 |
0
|
expect(servletContext.getResourcePaths("/WEB-INF/lib")).andStubReturn(pathes); |
279 |
|
|
280 |
|
|
281 |
0
|
expect(servletContext.getResourceAsStream((String) anyObject())).andStubAnswer(new IAnswer<InputStream>() { |
282 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
283 |
0
|
public InputStream answer() throws Throwable {... |
284 |
0
|
String path = (String) getCurrentArguments()[0]; |
285 |
0
|
if (path.equals("/WEB-INF/web.xml")) { |
286 |
0
|
return null; |
287 |
|
} |
288 |
0
|
return new URL(path).openStream(); |
289 |
|
} |
290 |
|
}); |
291 |
|
|
292 |
|
|
293 |
0
|
expect(servletContext.getResource((String) anyObject())).andStubAnswer(new IAnswer<URL>() { |
294 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
295 |
0
|
public URL answer() throws Throwable {... |
296 |
0
|
String path = (String) getCurrentArguments()[0]; |
297 |
0
|
return new URL(path); |
298 |
|
} |
299 |
|
}); |
300 |
0
|
replay(servletContext); |
301 |
0
|
return servletContext; |
302 |
|
} |
303 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
304 |
0
|
private void setUpManagers() {... |
305 |
|
|
306 |
0
|
ParagraphRendererManager paragraphRendererManager = createMock(ParagraphRendererManager.class); |
307 |
0
|
expect(paragraphRendererManager.getRenderer((String) anyObject())).andStubAnswer(new IAnswer<ParagraphRenderer>() { |
308 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
309 |
0
|
public ParagraphRenderer answer() throws Throwable {... |
310 |
0
|
return paragraphRenderers.get(getCurrentArguments()[0]); |
311 |
|
} |
312 |
|
}); |
313 |
0
|
replay(paragraphRendererManager); |
314 |
0
|
ComponentsTestUtil.setInstance(ParagraphRendererManager.class, paragraphRendererManager); |
315 |
|
|
316 |
0
|
TemplateRendererManager templateRendererManager = createMock(TemplateRendererManager.class); |
317 |
0
|
expect(templateRendererManager.getRenderer((String) anyObject())).andStubAnswer(new IAnswer<TemplateRenderer>() { |
318 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
319 |
0
|
public TemplateRenderer answer() throws Throwable {... |
320 |
0
|
return templateRenderers.get(getCurrentArguments()[0]); |
321 |
|
} |
322 |
|
}); |
323 |
0
|
replay(templateRendererManager); |
324 |
0
|
ComponentsTestUtil.setInstance(TemplateRendererManager.class, templateRendererManager); |
325 |
|
|
326 |
0
|
ParagraphManager paragraphManager = createMock(ParagraphManager.class); |
327 |
0
|
expect(paragraphManager.getParagraphDefinition((String) anyObject())).andStubAnswer(new IAnswer<Paragraph>() { |
328 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
329 |
0
|
public Paragraph answer() throws Throwable {... |
330 |
0
|
return paragraphs.get(getCurrentArguments()[0]); |
331 |
|
} |
332 |
|
}); |
333 |
0
|
replay(paragraphManager); |
334 |
0
|
ComponentsTestUtil.setInstance(ParagraphManager.class, paragraphManager); |
335 |
|
|
336 |
0
|
TemplateManager templateManager = createMock(TemplateManager.class); |
337 |
0
|
expect(templateManager.getTemplateDefinition((String) anyObject())).andStubAnswer(new IAnswer<Template>() { |
338 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
339 |
0
|
public Template answer() throws Throwable {... |
340 |
0
|
return templates.get(getCurrentArguments()[0]); |
341 |
|
} |
342 |
|
}); |
343 |
0
|
replay(templateManager); |
344 |
0
|
ComponentsTestUtil.setInstance(TemplateManager.class, templateManager); |
345 |
|
} |
346 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
0
|
protected String render(String website, String path) throws RenderException {... |
348 |
0
|
return render(ContentUtil.getContent(ContentRepository.WEBSITE, path)); |
349 |
|
} |
350 |
|
|
351 |
|
} |