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 static org.easymock.EasyMock.expect; |
37 |
|
import static org.easymock.EasyMock.verify; |
38 |
|
import static org.easymock.classextension.EasyMock.createNiceMock; |
39 |
|
import static org.easymock.classextension.EasyMock.replay; |
40 |
|
import freemarker.cache.StringTemplateLoader; |
41 |
|
import info.magnolia.cms.beans.config.ServerConfiguration; |
42 |
|
import info.magnolia.cms.core.AggregationState; |
43 |
|
import info.magnolia.cms.core.Content; |
44 |
|
import info.magnolia.cms.i18n.DefaultI18nContentSupport; |
45 |
|
import info.magnolia.cms.i18n.EmptyMessages; |
46 |
|
import info.magnolia.cms.i18n.I18nContentSupport; |
47 |
|
import info.magnolia.context.MgnlContext; |
48 |
|
import info.magnolia.context.WebContext; |
49 |
|
import info.magnolia.freemarker.FreemarkerConfig; |
50 |
|
import info.magnolia.freemarker.FreemarkerHelper; |
51 |
|
import info.magnolia.link.LinkTransformerManager; |
52 |
|
import info.magnolia.module.templating.Paragraph; |
53 |
|
import info.magnolia.module.templating.RenderableDefinition; |
54 |
|
import info.magnolia.module.templating.RenderingModel; |
55 |
|
import info.magnolia.module.templating.RenderingModelImpl; |
56 |
|
import info.magnolia.module.templating.engine.DefaultRenderingEngine; |
57 |
|
import info.magnolia.module.templating.engine.RenderingEngine; |
58 |
|
import info.magnolia.test.ComponentsTestUtil; |
59 |
|
import info.magnolia.test.MgnlTestCase; |
60 |
|
import info.magnolia.test.mock.MockContent; |
61 |
|
import info.magnolia.test.mock.MockNodeData; |
62 |
|
|
63 |
|
import java.io.StringWriter; |
64 |
|
import java.util.HashMap; |
65 |
|
import java.util.Locale; |
66 |
|
import java.util.Map; |
67 |
|
|
68 |
|
|
69 |
|
@author |
70 |
|
@version |
71 |
|
|
|
|
| 98.9% |
Uncovered Elements: 1 (93) |
Complexity: 7 |
Complexity Density: 0.08 |
|
72 |
|
public class FreemarkerParagraphRendererTest extends MgnlTestCase { |
73 |
|
private StringTemplateLoader tplLoader; |
74 |
|
private FreemarkerParagraphRenderer renderer; |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
|
76 |
5
|
protected void setUp() throws Exception {... |
77 |
5
|
super.setUp(); |
78 |
|
|
79 |
5
|
tplLoader = new StringTemplateLoader(); |
80 |
5
|
final FreemarkerConfig fmTemplateLoader = new FreemarkerConfig(); |
81 |
5
|
fmTemplateLoader.addTemplateLoader(tplLoader); |
82 |
5
|
ComponentsTestUtil.setInstance(FreemarkerConfig.class, fmTemplateLoader); |
83 |
|
|
84 |
5
|
final FreemarkerHelper freemarkerHelper = new FreemarkerHelper(); |
85 |
5
|
renderer = new FreemarkerParagraphRenderer(freemarkerHelper); |
86 |
|
|
87 |
5
|
final ServerConfiguration serverConfiguration = new ServerConfiguration(); |
88 |
5
|
serverConfiguration.setDefaultBaseUrl("http://myTests:1234/yay"); |
89 |
5
|
ComponentsTestUtil.setInstance(ServerConfiguration.class, serverConfiguration); |
90 |
5
|
ComponentsTestUtil.setInstance(LinkTransformerManager.class, new LinkTransformerManager()); |
91 |
5
|
ComponentsTestUtil.setImplementation(I18nContentSupport.class, DefaultI18nContentSupport.class); |
92 |
5
|
ComponentsTestUtil.setImplementation(RenderingEngine.class, DefaultRenderingEngine.class); |
93 |
|
|
94 |
5
|
final WebContext context = createNiceMock(WebContext.class); |
95 |
5
|
AggregationState state = new AggregationState(); |
96 |
5
|
state.setLocale(Locale.ENGLISH); |
97 |
5
|
expect(context.getAggregationState()).andStubReturn(state); |
98 |
5
|
expect(context.getLocale()).andReturn(Locale.ENGLISH); |
99 |
|
|
100 |
5
|
MgnlContext.setInstance(context); |
101 |
5
|
replay(context); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
104 |
1
|
public void testWorksWithNonActionParagraphAndContentIsExposedToFreemarker() throws Exception {... |
105 |
1
|
tplLoader.putTemplate("test_noclass.ftl", "This is a test template, rendering the content node under ${content.@handle} with UUID ${content.@uuid}.\n" + |
106 |
|
"The value of the foo property is ${content.foo}."); |
107 |
|
|
108 |
1
|
final MockContent c = new MockContent("plop"); |
109 |
1
|
c.setUUID("123"); |
110 |
1
|
c.addNodeData(new MockNodeData("foo", "bar")); |
111 |
|
|
112 |
1
|
final StringWriter out = new StringWriter(); |
113 |
1
|
final Paragraph p = new Paragraph(); |
114 |
1
|
p.setName("test-para"); |
115 |
1
|
p.setTemplatePath("test_noclass.ftl"); |
116 |
1
|
renderer.render(c, p, out); |
117 |
|
|
118 |
1
|
assertEquals("This is a test template, rendering the content node under /plop with UUID 123.\n" + |
119 |
|
"The value of the foo property is bar.", out.toString()); |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
122 |
1
|
public void testActionClassGetsExecutedAndIsPutOnContextAlongWithResultAndContent() throws Exception {... |
123 |
1
|
tplLoader.putTemplate("test_action.ftl", "${content.boo} : ${model.pouet} : ${actionResult}"); |
124 |
1
|
final Paragraph par = new Paragraph(); |
125 |
1
|
par.setName("test-with-action"); |
126 |
1
|
par.setTemplatePath("test_action.ftl"); |
127 |
1
|
par.setModelClass(SimpleTestState.class); |
128 |
1
|
final MockContent c = new MockContent("plop"); |
129 |
1
|
c.addNodeData(new MockNodeData("boo", "yay")); |
130 |
1
|
final StringWriter out = new StringWriter(); |
131 |
1
|
renderer.render(c, par, out); |
132 |
1
|
assertEquals("yay : it works : success", out.toString()); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
135 |
1
|
public void testActionGetsPopulated() throws Exception {... |
136 |
1
|
final WebContext context = createNiceMock(WebContext.class); |
137 |
1
|
Map<String,String> params=new HashMap<String,String>(); |
138 |
1
|
params.put("blah", "tralala"); |
139 |
1
|
params.put("foo", "bar"); |
140 |
1
|
expect(context.getParameters()).andReturn(params); |
141 |
1
|
expect(context.getMessages("testmessages")).andReturn(new EmptyMessages()); |
142 |
1
|
expect(context.getMessages()).andReturn(new EmptyMessages()); |
143 |
1
|
expect(context.getLocale()).andStubReturn(Locale.ENGLISH); |
144 |
1
|
expect(context.getContextPath()).andReturn("/pouet"); |
145 |
1
|
expect(context.getServletContext()).andReturn(null); |
146 |
1
|
expect(context.getAggregationState()).andStubReturn(new AggregationState()); |
147 |
1
|
replay(context); |
148 |
1
|
MgnlContext.setInstance(context); |
149 |
|
|
150 |
1
|
tplLoader.putTemplate("test_action.ftl", "${content.boo} : ${model.pouet} : ${model.blah} : ${actionResult}"); |
151 |
1
|
final Paragraph par = new Paragraph(); |
152 |
1
|
par.setName("test-with-action"); |
153 |
1
|
par.setI18nBasename("testmessages"); |
154 |
1
|
par.setTemplatePath("test_action.ftl"); |
155 |
1
|
par.setModelClass(SimpleTestState.class); |
156 |
1
|
final MockContent c = new MockContent("plop"); |
157 |
1
|
c.addNodeData(new MockNodeData("boo", "yay")); |
158 |
1
|
final StringWriter out = new StringWriter(); |
159 |
1
|
renderer.render(c, par, out); |
160 |
1
|
assertEquals("yay : it works : tralala : success", out.toString()); |
161 |
|
} |
162 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
1
PASS
|
|
163 |
1
|
public void testCantRenderWithoutParagraphPathCorrectlySet() throws Exception {... |
164 |
1
|
tplLoader.putTemplate("foo", ""); |
165 |
1
|
final Content c = new MockContent("pouet"); |
166 |
1
|
final Paragraph paragraph = new Paragraph(); |
167 |
1
|
paragraph.setName("plop"); |
168 |
1
|
try { |
169 |
1
|
renderer.render(c, paragraph, new StringWriter()); |
170 |
0
|
fail("should have failed"); |
171 |
|
} catch (IllegalStateException e) { |
172 |
1
|
assertEquals("Unable to render info.magnolia.module.templating.Paragraph plop in page /pouet: templatePath not set.", e.getMessage()); |
173 |
|
} |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1
PASS
|
|
176 |
1
|
public void testSkipRendering() throws Exception {... |
177 |
1
|
final WebContext webContext = createNiceMock(WebContext.class); |
178 |
1
|
MgnlContext.setInstance(webContext); |
179 |
1
|
final AggregationState aggState = new AggregationState(); |
180 |
1
|
expect(webContext.getAggregationState()).andReturn(aggState); |
181 |
1
|
replay(webContext); |
182 |
1
|
final Content c = new MockContent("pouet"); |
183 |
1
|
final Paragraph par = new Paragraph(); |
184 |
1
|
par.setName("plop"); |
185 |
1
|
par.setTemplatePath("do_not_render_me.ftl"); |
186 |
1
|
par.setModelClass(SkippableTestState.class); |
187 |
1
|
final FreemarkerParagraphRenderer renderer = new FreemarkerParagraphRenderer(); |
188 |
1
|
final StringWriter out = new StringWriter(); |
189 |
1
|
renderer.render(c, par, out); |
190 |
1
|
assertTrue(out.getBuffer().length() == 0); |
191 |
1
|
verify(webContext); |
192 |
|
} |
193 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 6 |
Complexity Density: 1 |
|
194 |
|
public static final class SimpleTestState extends RenderingModelImpl{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
195 |
2
|
public SimpleTestState(Content content, RenderableDefinition definition, RenderingModel parent) {... |
196 |
2
|
super(content, definition, parent); |
197 |
|
} |
198 |
|
|
199 |
|
private String pouet = "it works"; |
200 |
|
private String blah; |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
202 |
2
|
public String execute() {... |
203 |
2
|
return "success"; |
204 |
|
} |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
2
|
public String getPouet() {... |
207 |
2
|
return pouet; |
208 |
|
} |
209 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
0
|
public void setPouet(String pouet) {... |
211 |
0
|
this.pouet = pouet; |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
1
|
public String getBlah() {... |
215 |
1
|
return blah; |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
218 |
1
|
public void setBlah(String blah) {... |
219 |
1
|
this.blah = blah; |
220 |
|
} |
221 |
|
} |
222 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
223 |
|
public static final class SkippableTestState extends RenderingModelImpl { |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
225 |
1
|
public SkippableTestState(Content content, RenderableDefinition definition, RenderingModel parent) {... |
226 |
1
|
super(content, definition, parent); |
227 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
1
|
@Override... |
229 |
|
public String execute() { |
230 |
1
|
return RenderingModel.SKIP_RENDERING; |
231 |
|
} |
232 |
|
} |
233 |
|
} |