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.urimapping; |
35 |
|
|
36 |
|
import org.junit.After; |
37 |
|
import org.junit.Before; |
38 |
|
import org.junit.Test; |
39 |
|
import org.springframework.aop.support.AopUtils; |
40 |
|
import org.springframework.beans.BeansException; |
41 |
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
42 |
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
43 |
|
import org.springframework.context.support.ClassPathXmlApplicationContext; |
44 |
|
import org.springframework.mock.web.MockHttpServletRequest; |
45 |
|
|
46 |
|
import static org.junit.Assert.assertEquals; |
47 |
|
import static org.junit.Assert.assertNull; |
48 |
|
import static org.junit.Assert.assertTrue; |
49 |
|
import static org.mockito.Mockito.mock; |
50 |
|
|
51 |
|
import info.magnolia.cms.beans.config.VirtualURIMapping; |
52 |
|
import info.magnolia.context.MgnlContext; |
53 |
|
import info.magnolia.module.blossom.annotation.VirtualURIMapper; |
54 |
|
import info.magnolia.module.blossom.dispatcher.BlossomDispatcher; |
55 |
|
import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAwareBeanPostProcessor; |
56 |
|
import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent; |
57 |
|
import info.magnolia.test.mock.MockWebContext; |
58 |
|
|
59 |
|
|
60 |
|
@since |
61 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (37) |
Complexity: 6 |
Complexity Density: 0.19 |
|
62 |
|
public class BlossomVirtualURIMapperTest { |
63 |
|
|
64 |
|
@VirtualURIMapper |
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
65 |
|
public static class IncorrectURIMapping implements VirtualURIMapping { |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
67 |
8 |
@Override... |
68 |
|
public MappingResult mapURI(String uri) { |
69 |
8 |
if (uri.equals("/original-6")) { |
70 |
1 |
MappingResult mappingResult = new MappingResult(); |
71 |
1 |
mappingResult.setToURI("/virtual-6"); |
72 |
1 |
mappingResult.setLevel(14); |
73 |
1 |
return mappingResult; |
74 |
|
} else { |
75 |
7 |
return null; |
76 |
|
} |
77 |
|
} |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
79 |
0 |
public String mapper7(String from) {... |
80 |
0 |
return from.equals("/original-7") ? "/virtual-7" : null; |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
|
private BlossomVirtualURIMapping mapping = new BlossomVirtualURIMapping(); |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
86 |
2 |
@Before... |
87 |
|
public void setUp() { |
88 |
2 |
MockHttpServletRequest request = new MockHttpServletRequest(); |
89 |
2 |
MockWebContext webContext = new MockWebContext(); |
90 |
2 |
webContext.setRequest(request); |
91 |
2 |
MgnlContext.setInstance(webContext); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
94 |
2 |
@After... |
95 |
|
public void tearDown() { |
96 |
2 |
MgnlContext.setInstance(null); |
97 |
2 |
mapping.clear(); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
100 |
1 |
@Test... |
101 |
|
public void testBeanDetection() { |
102 |
|
|
103 |
1 |
assertNull(mapping.mapURI("/original-1")); |
104 |
1 |
assertNull(mapping.mapURI("/original-2")); |
105 |
|
|
106 |
1 |
ClassPathXmlApplicationContext context = createContextWithDispatcher("classpath:info/magnolia/module/blossom/urimapping/BlossomVirtualURIMapper-test.xml"); |
107 |
|
|
108 |
1 |
assertNull(mapping.mapURI("/does-not-match-a-virtual-uri-mapping")); |
109 |
1 |
assertEquals("/virtual-1", mapping.mapURI("/original-1").getToURI()); |
110 |
1 |
assertEquals("/virtual-2", mapping.mapURI("/original-2").getToURI()); |
111 |
1 |
assertEquals("/virtual-3", mapping.mapURI("/original-3").getToURI()); |
112 |
1 |
assertNull(mapping.mapURI("/original-4")); |
113 |
1 |
assertNull(mapping.mapURI("/original-5")); |
114 |
1 |
assertEquals("/virtual-6", mapping.mapURI("/original-6").getToURI()); |
115 |
1 |
assertNull(mapping.mapURI("/original-7")); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void testBeanDetectionWithProxies() { |
120 |
|
|
121 |
1 |
assertNull(mapping.mapURI("/original-2")); |
122 |
1 |
assertNull(mapping.mapURI("/original-3")); |
123 |
|
|
124 |
1 |
ClassPathXmlApplicationContext context = createContextWithDispatcher("classpath:info/magnolia/module/blossom/urimapping/BlossomVirtualURIMapper-proxy-test.xml"); |
125 |
|
|
126 |
1 |
assertTrue(AopUtils.isCglibProxy(context.getBean("proxiedMapping"))); |
127 |
|
|
128 |
1 |
assertNull(mapping.mapURI("/does-not-match-a-virtual-uri-mapping")); |
129 |
1 |
assertEquals("/virtual-2", mapping.mapURI("/original-2").getToURI()); |
130 |
1 |
assertEquals("/virtual-3", mapping.mapURI("/original-3").getToURI()); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
133 |
2 |
private ClassPathXmlApplicationContext createContextWithDispatcher(String configLocation) {... |
134 |
2 |
final BlossomDispatcher dispatcher = mock(BlossomDispatcher.class); |
135 |
2 |
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {configLocation}, false); |
136 |
2 |
context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
2 |
@Override... |
138 |
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
139 |
2 |
beanFactory.addBeanPostProcessor(new BlossomDispatcherAwareBeanPostProcessor(dispatcher)); |
140 |
|
} |
141 |
|
}); |
142 |
2 |
context.refresh(); |
143 |
2 |
context.publishEvent(new BlossomDispatcherInitializedEvent(dispatcher)); |
144 |
2 |
return context; |
145 |
|
} |
146 |
|
} |