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