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.paragraphs; |
35 |
|
|
36 |
|
import info.magnolia.cms.core.SystemProperty; |
37 |
|
import info.magnolia.module.templating.ModelExecutionFilter; |
38 |
|
import info.magnolia.module.templating.RenderableDefinition; |
39 |
|
import info.magnolia.module.templating.RenderingModel; |
40 |
|
import info.magnolia.module.templating.RenderingModelImpl; |
41 |
|
import info.magnolia.cms.core.AggregationState; |
42 |
|
import info.magnolia.cms.core.Content; |
43 |
|
import info.magnolia.cms.util.ContentWrapper; |
44 |
|
import info.magnolia.context.Context; |
45 |
|
import info.magnolia.context.MgnlContext; |
46 |
|
import info.magnolia.context.WebContext; |
47 |
|
import info.magnolia.module.templating.AbstractRenderer; |
48 |
|
import info.magnolia.module.templating.Paragraph; |
49 |
|
import info.magnolia.module.templating.engine.DefaultRenderingEngine; |
50 |
|
import info.magnolia.module.templating.engine.RenderingEngine; |
51 |
|
import info.magnolia.test.ComponentsTestUtil; |
52 |
|
import info.magnolia.test.mock.MockContent; |
53 |
|
import junit.framework.TestCase; |
54 |
|
import static org.easymock.EasyMock.*; |
55 |
|
import static org.easymock.classextension.EasyMock.createNiceMock; |
56 |
|
import static org.easymock.classextension.EasyMock.replay; |
57 |
|
|
58 |
|
import java.io.StringWriter; |
59 |
|
import java.lang.reflect.InvocationTargetException; |
60 |
|
import java.lang.reflect.Method; |
61 |
|
import java.util.HashMap; |
62 |
|
import java.util.Map; |
63 |
|
|
64 |
|
|
65 |
|
@author |
66 |
|
@version |
67 |
|
|
|
|
| 96.8% |
Uncovered Elements: 3 (93) |
Complexity: 12 |
Complexity Density: 0.14 |
|
68 |
|
public class JspParagraphRendererTest extends TestCase { |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
70 |
5
|
protected void setUp() throws Exception {... |
71 |
5
|
super.setUp(); |
72 |
|
|
73 |
5
|
MgnlContext.setInstance(null); |
74 |
|
|
75 |
|
|
76 |
5
|
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
79 |
5
|
protected void tearDown() throws Exception {... |
80 |
5
|
MgnlContext.setInstance(null); |
81 |
5
|
ComponentsTestUtil.clear(); |
82 |
5
|
SystemProperty.getProperties().clear(); |
83 |
5
|
super.tearDown(); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
1
PASS
|
|
86 |
1
|
public void testExposesNodesAsMaps() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {... |
87 |
1
|
final WebContext magnoliaCtx = createStrictMock(WebContext.class); |
88 |
1
|
MgnlContext.setInstance(magnoliaCtx); |
89 |
1
|
ComponentsTestUtil.setImplementation(RenderingEngine.class, DefaultRenderingEngine.class); |
90 |
|
|
91 |
1
|
final Content page = createStrictMock(Content.class); |
92 |
|
|
93 |
1
|
expect(page.getHandle()).andReturn("/myPage").times(2); |
94 |
1
|
final Content paragraph = createStrictMock(Content.class); |
95 |
|
|
96 |
1
|
final AggregationState aggState = new AggregationState(); |
97 |
1
|
aggState.setMainContent(page); |
98 |
1
|
expect(magnoliaCtx.getAggregationState()).andStubReturn(aggState); |
99 |
|
|
100 |
1
|
replay(magnoliaCtx, page, paragraph); |
101 |
1
|
final Map templateCtx = new HashMap(); |
102 |
|
|
103 |
1
|
final JspParagraphRenderer renderer = new JspParagraphRenderer(); |
104 |
|
|
105 |
|
|
106 |
1
|
Method setupContextMethod = AbstractRenderer.class.getDeclaredMethod("setupContext", new Class[]{Map.class, Content.class, RenderableDefinition.class, RenderingModel.class, Object.class}); |
107 |
1
|
setupContextMethod.setAccessible(true); |
108 |
1
|
setupContextMethod.invoke(renderer, new Object[]{templateCtx, paragraph, null, null, null}); |
109 |
|
|
110 |
|
|
111 |
1
|
assertEquals("Unexpected amount of objects in context", 7, templateCtx.size()); |
112 |
1
|
assertTrue(templateCtx.get("actpage") instanceof Map); |
113 |
1
|
assertEquals(page, unwrap((Content) templateCtx.get("actpage"))); |
114 |
1
|
assertTrue(templateCtx.get("content") instanceof Map); |
115 |
1
|
assertEquals(paragraph, unwrap((Content) templateCtx.get("content"))); |
116 |
|
|
117 |
1
|
verify(magnoliaCtx, page, paragraph); |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 2 |
Complexity Density: 0.14 |
1
PASS
|
|
152 |
1
|
public void testCantRenderWithoutParagraphPathCorrectlySet() throws Exception {... |
153 |
1
|
final WebContext webContext = createNiceMock(WebContext.class); |
154 |
1
|
MgnlContext.setInstance(webContext); |
155 |
1
|
final AggregationState aggState = new AggregationState(); |
156 |
1
|
expect(webContext.getAggregationState()).andReturn(aggState); |
157 |
1
|
replay(webContext); |
158 |
1
|
final Content c = new MockContent("pouet"); |
159 |
1
|
final Paragraph paragraph = new Paragraph(); |
160 |
1
|
paragraph.setName("plop"); |
161 |
1
|
final JspParagraphRenderer renderer = new JspParagraphRenderer(); |
162 |
1
|
try { |
163 |
1
|
renderer.render(c, paragraph, new StringWriter()); |
164 |
0
|
fail("should have failed"); |
165 |
|
} catch (IllegalStateException e) { |
166 |
1
|
assertEquals("Unable to render info.magnolia.module.templating.Paragraph plop in page /pouet: templatePath not set.", e.getMessage()); |
167 |
|
} |
168 |
1
|
verify(webContext); |
169 |
|
} |
170 |
|
|
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1
PASS
|
|
172 |
1
|
public void testSkipRendering() throws Exception {... |
173 |
1
|
final WebContext webContext = createNiceMock(WebContext.class); |
174 |
1
|
MgnlContext.setInstance(webContext); |
175 |
1
|
final AggregationState aggState = new AggregationState(); |
176 |
1
|
expect(webContext.getAggregationState()).andReturn(aggState); |
177 |
1
|
replay(webContext); |
178 |
1
|
final Content c = new MockContent("pouet"); |
179 |
1
|
final Paragraph par = new Paragraph(); |
180 |
1
|
par.setName("plop"); |
181 |
1
|
par.setTemplatePath("do_not_render_me.jsp"); |
182 |
1
|
par.setModelClass(SkippableTestState.class); |
183 |
1
|
final JspParagraphRenderer renderer = new JspParagraphRenderer(); |
184 |
1
|
final StringWriter out = new StringWriter(); |
185 |
1
|
renderer.render(c, par, out); |
186 |
1
|
assertTrue(out.getBuffer().length() == 0); |
187 |
1
|
verify(webContext); |
188 |
|
} |
189 |
|
|
190 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
1
PASS
|
|
191 |
1
|
public void testShouldFailIfNoContextIsSet() throws Exception {... |
192 |
1
|
final JspParagraphRenderer renderer = new JspParagraphRenderer(); |
193 |
1
|
try { |
194 |
1
|
final Paragraph p = new Paragraph(); |
195 |
1
|
p.setName("plop"); |
196 |
1
|
p.setTemplatePath("/foo/bar.jsp"); |
197 |
1
|
renderer.render(null, p, new StringWriter()); |
198 |
0
|
fail("should have failed"); |
199 |
|
} catch (IllegalStateException e) { |
200 |
1
|
assertEquals("MgnlContext is not set for this thread", e.getMessage()); |
201 |
|
} |
202 |
|
} |
203 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 2 |
Complexity Density: 0.13 |
1
PASS
|
|
204 |
1
|
public void testShouldFailIfContextIsNotWebContext() throws Exception {... |
205 |
1
|
Content content = createStrictMock(Content.class); |
206 |
1
|
expect(content.getUUID()).andReturn("content-uuid"); |
207 |
|
|
208 |
1
|
Context context = createStrictMock(Context.class); |
209 |
1
|
expect(context.getAttribute("info.magnolia.module.templating.RenderingModel")).andReturn(null); |
210 |
1
|
expect(context.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + "content-uuid")).andReturn(null); |
211 |
1
|
replay(content, context); |
212 |
|
|
213 |
1
|
MgnlContext.setInstance(context); |
214 |
1
|
final JspParagraphRenderer renderer = new JspParagraphRenderer(); |
215 |
1
|
try { |
216 |
1
|
final Paragraph p = new Paragraph(); |
217 |
1
|
p.setName("plop"); |
218 |
1
|
p.setTemplatePath("/foo/bar.jsp"); |
219 |
1
|
renderer.render(content, p, new StringWriter()); |
220 |
0
|
fail("should have failed"); |
221 |
|
} catch (IllegalStateException e) { |
222 |
1
|
assertEquals("JspParagraphRenderer can only be used with a WebContext", e.getMessage()); |
223 |
|
} |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
226 |
6
|
private Content unwrap(Content c) {... |
227 |
6
|
if (c instanceof ContentWrapper) { |
228 |
4
|
return unwrap(((ContentWrapper) c).getWrappedContent()); |
229 |
|
} |
230 |
2
|
return c; |
231 |
|
} |
232 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
233 |
|
public static final class SkippableTestState extends RenderingModelImpl { |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
235 |
1
|
public SkippableTestState(Content content, RenderableDefinition definition, RenderingModel parent) {... |
236 |
1
|
super(content, definition, parent); |
237 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
1
|
@Override... |
239 |
|
public String execute() { |
240 |
1
|
return RenderingModel.SKIP_RENDERING; |
241 |
|
} |
242 |
|
} |
243 |
|
} |