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.blossom.template; |
35 |
|
|
36 |
|
import java.io.IOException; |
37 |
|
import java.lang.reflect.InvocationTargetException; |
38 |
|
import java.lang.reflect.Method; |
39 |
|
|
40 |
|
import javax.servlet.ServletException; |
41 |
|
import javax.servlet.http.HttpServletRequest; |
42 |
|
import javax.servlet.http.HttpServletResponse; |
43 |
|
|
44 |
|
import org.junit.After; |
45 |
|
import org.junit.Before; |
46 |
|
import org.junit.Test; |
47 |
|
import org.springframework.util.ReflectionUtils; |
48 |
|
import org.springframework.web.servlet.HandlerExecutionChain; |
49 |
|
import org.springframework.web.servlet.ModelAndView; |
50 |
|
|
51 |
|
import static junit.framework.Assert.assertNull; |
52 |
|
import static org.junit.Assert.assertEquals; |
53 |
|
import static org.junit.Assert.assertFalse; |
54 |
|
import static org.junit.Assert.assertSame; |
55 |
|
import static org.junit.Assert.assertTrue; |
56 |
|
import static org.junit.Assert.fail; |
57 |
|
|
58 |
|
import info.magnolia.module.blossom.annotation.Area; |
59 |
|
import info.magnolia.module.blossom.annotation.I18nBasename; |
60 |
|
import info.magnolia.module.blossom.annotation.Template; |
61 |
|
import info.magnolia.module.blossom.annotation.TemplateDescription; |
62 |
|
import info.magnolia.module.blossom.annotation.TernaryBoolean; |
63 |
|
import info.magnolia.module.blossom.dispatcher.BlossomDispatcher; |
64 |
|
import info.magnolia.rendering.template.TemplateAvailability; |
65 |
|
import info.magnolia.rendering.template.configured.DefaultTemplateAvailability; |
66 |
|
import info.magnolia.test.ComponentsTestUtil; |
67 |
|
|
68 |
|
|
69 |
|
@since |
70 |
|
|
|
|
| 90.9% |
Uncovered Elements: 8 (88) |
Complexity: 23 |
Complexity Density: 0.37 |
|
71 |
|
public class TemplateDefinitionBuilderTest { |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
9 |
@Before... |
74 |
|
public void setUp() { |
75 |
9 |
ComponentsTestUtil.setImplementation(TemplateAvailability.class, DefaultTemplateAvailability.class); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
9 |
@After... |
79 |
|
public void tearDown() { |
80 |
9 |
ComponentsTestUtil.clear(); |
81 |
|
} |
82 |
|
|
83 |
|
@I18nBasename("some-base-name") |
84 |
|
@Template(title = "Text", visible = false, id = "my-module:pages/folder/name", type="some-type", subtype = "some-subtype") |
85 |
|
@TemplateDescription("Text template") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
86 |
|
public static class TestTemplate { |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
public void testTemplateDescription() throws Exception { |
91 |
|
|
92 |
1 |
TestTemplate template = new TestTemplate(); |
93 |
1 |
BlossomDispatcher dispatcher = createMockDispatcher(); |
94 |
|
|
95 |
1 |
BlossomTemplateDefinition description = buildTemplateDefinition(dispatcher, template, "/test/test/"); |
96 |
|
|
97 |
1 |
assertEquals("Text", description.getTitle()); |
98 |
1 |
assertEquals("my-module:pages/folder/name", description.getId()); |
99 |
1 |
assertEquals("name", description.getName()); |
100 |
1 |
assertEquals("/test/test/", description.getHandlerPath()); |
101 |
1 |
assertFalse(description.getVisible()); |
102 |
1 |
assertEquals("some-type", description.getType()); |
103 |
1 |
assertEquals("some-subtype", description.getSubtype()); |
104 |
1 |
assertEquals("Text template", description.getDescription()); |
105 |
1 |
assertSame(dispatcher, description.getDispatcher()); |
106 |
1 |
assertSame(template, description.getHandler()); |
107 |
1 |
assertEquals("some-base-name", description.getI18nBasename()); |
108 |
1 |
assertNull(description.getWritable()); |
109 |
1 |
assertNull(description.getMoveable()); |
110 |
1 |
assertNull(description.getDeletable()); |
111 |
|
} |
112 |
|
|
113 |
|
@Template(id = "my-module:pages/folder/name", writable = TernaryBoolean.TRUE, moveable = TernaryBoolean.FALSE, deletable = TernaryBoolean.TRUE) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
114 |
|
public static class TestTemplateWithFlags { |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
117 |
1 |
@Test... |
118 |
|
public void testTemplateDescriptionWithFlags() throws Exception { |
119 |
|
|
120 |
1 |
TestTemplateWithFlags template = new TestTemplateWithFlags(); |
121 |
1 |
BlossomDispatcher dispatcher = createMockDispatcher(); |
122 |
|
|
123 |
1 |
BlossomTemplateDefinition description = buildTemplateDefinition(dispatcher, template, "/test/test/"); |
124 |
|
|
125 |
1 |
assertTrue(description.getWritable()); |
126 |
1 |
assertFalse(description.getMoveable()); |
127 |
1 |
assertTrue(description.getDeletable()); |
128 |
|
} |
129 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
130 |
|
public static class TemplateWithoutTemplateAnnotation { |
131 |
|
} |
132 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
public void testMissingTemplateAnnotation() { |
135 |
1 |
try { |
136 |
1 |
buildTemplateDefinition(new TemplateWithoutTemplateAnnotation()); |
137 |
0 |
fail(); |
138 |
|
} catch (IllegalArgumentException e) { |
139 |
1 |
assertEquals("Could not resolve template id, @Template is not present on class [info.magnolia.module.blossom.template.TemplateDefinitionBuilderTest$TemplateWithoutTemplateAnnotation]", e.getMessage()); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
@Area(value = "Area", maxComponents = 3) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
144 |
|
public static class AreaWithMaxComponentsSet { |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
147 |
1 |
@Test... |
148 |
|
public void testMaximumComponents() { |
149 |
1 |
BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithMaxComponentsSet()); |
150 |
1 |
assertEquals(new Integer(3), definition.getMaxComponents()); |
151 |
|
} |
152 |
|
|
153 |
|
@Area(value = "Area") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
154 |
|
public static class AreaWithoutMaxComponentsSet { |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testMaximumComponentsNullWhenNotSet() { |
159 |
1 |
BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutMaxComponentsSet()); |
160 |
1 |
assertNull(definition.getMaxComponents()); |
161 |
|
} |
162 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
163 |
|
public static class AbstractTestTemplate { |
164 |
|
|
165 |
|
@Area("areaInSuperclass") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
166 |
|
public static class AreaInSuperclass { |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
|
@Template(id = "", title = "") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
171 |
|
public static class TestTemplateWithAreasFromSuperclass extends AbstractTestTemplate { |
172 |
|
|
173 |
|
@Area("areaInClass") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
174 |
|
public static class AreaInClass { |
175 |
|
} |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
178 |
1 |
@Test... |
179 |
|
public void testFindsAreasInSuperclass() { |
180 |
|
|
181 |
1 |
TestTemplateWithAreasFromSuperclass template = new TestTemplateWithAreasFromSuperclass(); |
182 |
1 |
AbstractTestTemplate.AreaInSuperclass areaInSuperclass = new AbstractTestTemplate.AreaInSuperclass(); |
183 |
1 |
TestTemplateWithAreasFromSuperclass.AreaInClass areaInClass = new TestTemplateWithAreasFromSuperclass.AreaInClass(); |
184 |
|
|
185 |
1 |
DetectedHandlersMetaData detectedHandlers = new DetectedHandlersMetaData(); |
186 |
1 |
detectedHandlers.addArea(new HandlerMetaData(areaInSuperclass, "")); |
187 |
1 |
detectedHandlers.addArea(new HandlerMetaData(areaInClass, "")); |
188 |
1 |
HandlerMetaData templateMetaData = new HandlerMetaData(template, ""); |
189 |
1 |
detectedHandlers.addTemplate(templateMetaData); |
190 |
|
|
191 |
1 |
BlossomTemplateDefinition templateDefinition = new TemplateDefinitionBuilder().buildTemplateDefinition( |
192 |
|
null, |
193 |
|
detectedHandlers, |
194 |
|
templateMetaData); |
195 |
|
|
196 |
1 |
assertEquals(2, templateDefinition.getAreas().size()); |
197 |
1 |
assertSame(areaInClass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInClass")).getHandler()); |
198 |
1 |
assertSame(areaInSuperclass, ((BlossomAreaDefinition) templateDefinition.getAreas().get("areaInSuperclass")).getHandler()); |
199 |
|
} |
200 |
|
|
201 |
|
@Area(value = "Area", createAreaNode = TernaryBoolean.TRUE) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
202 |
|
public static class AreaWithoutCreateAreaNodeSetToTrue { |
203 |
|
} |
204 |
|
|
205 |
|
@Area(value = "Area", createAreaNode = TernaryBoolean.FALSE) |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
206 |
|
public static class AreaWithoutCreateAreaNodeSetToFalse { |
207 |
|
} |
208 |
|
|
209 |
|
@Area(value = "Area") |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
210 |
|
public static class AreaWithoutCreateAreaNodeNotSet { |
211 |
|
} |
212 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
213 |
1 |
@Test... |
214 |
|
public void testCreateAreaNodeSetToTrue() throws InvocationTargetException, IllegalAccessException { |
215 |
1 |
BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToTrue()); |
216 |
1 |
Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class); |
217 |
1 |
if (method != null) { |
218 |
1 |
assertEquals(Boolean.TRUE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition)); |
219 |
|
} |
220 |
|
} |
221 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
222 |
1 |
@Test... |
223 |
|
public void testCreateAreaNodeSetToFalse() throws InvocationTargetException, IllegalAccessException { |
224 |
1 |
BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeSetToFalse()); |
225 |
1 |
Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class); |
226 |
1 |
if (method != null) { |
227 |
1 |
assertEquals(Boolean.FALSE, ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition)); |
228 |
|
} |
229 |
|
} |
230 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
231 |
1 |
@Test... |
232 |
|
public void testCreateAreaNodeNotSet() throws InvocationTargetException, IllegalAccessException { |
233 |
1 |
BlossomAreaDefinition definition = buildAreaDefinition(new AreaWithoutCreateAreaNodeNotSet()); |
234 |
1 |
Method method = ReflectionUtils.findMethod(definition.getClass(), "setCreateAreaNode", Boolean.class); |
235 |
1 |
if (method != null) { |
236 |
1 |
assertNull(ReflectionUtils.findMethod(definition.getClass(), "getCreateAreaNode").invoke(definition)); |
237 |
|
} |
238 |
|
} |
239 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
240 |
9 |
public static BlossomTemplateDefinition buildTemplateDefinition(Object template) {... |
241 |
9 |
return buildTemplateDefinition(null, template, "/foobar"); |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
11 |
public static BlossomTemplateDefinition buildTemplateDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {... |
245 |
11 |
return new TemplateDefinitionBuilder().buildTemplateDefinition( |
246 |
|
dispatcher, |
247 |
|
new DetectedHandlersMetaData(), |
248 |
|
new HandlerMetaData(template, handlerPath, template.getClass())); |
249 |
|
} |
250 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
251 |
7 |
public static BlossomAreaDefinition buildAreaDefinition(Object template) {... |
252 |
7 |
return buildAreaDefinition(null, template, "/foobar"); |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
7 |
public static BlossomAreaDefinition buildAreaDefinition(BlossomDispatcher dispatcher, Object template, String handlerPath) {... |
256 |
7 |
return new TemplateDefinitionBuilder().buildAreaDefinition( |
257 |
|
dispatcher, |
258 |
|
new DetectedHandlersMetaData(), |
259 |
|
new HandlerMetaData(template, handlerPath, template.getClass())); |
260 |
|
} |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
2 |
private BlossomDispatcher createMockDispatcher() {... |
263 |
2 |
return new BlossomDispatcher() { |
264 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
265 |
0 |
@Override... |
266 |
|
public ModelAndView executeChain(HandlerExecutionChain mappedHandler, HttpServletRequest request, HttpServletResponse response) throws Exception { |
267 |
0 |
return null; |
268 |
|
} |
269 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
270 |
0 |
@Override... |
271 |
|
public void forward(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
272 |
|
} |
273 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
274 |
0 |
@Override... |
275 |
|
public void include(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
276 |
|
} |
277 |
|
}; |
278 |
|
} |
279 |
|
} |