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.modules.resources.templating; |
35 |
|
|
36 |
|
|
37 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
38 |
|
import static org.hamcrest.Matchers.*; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
import info.magnolia.context.Context; |
42 |
|
import info.magnolia.context.WebContext; |
43 |
|
import info.magnolia.module.resources.ResourceLinker; |
44 |
|
import info.magnolia.module.resources.ResourcesModule; |
45 |
|
import info.magnolia.objectfactory.guice.GuiceUtils; |
46 |
|
import info.magnolia.resourceloader.dummy.DummyResourceOrigin; |
47 |
|
|
48 |
|
import java.util.Arrays; |
49 |
|
import java.util.List; |
50 |
|
|
51 |
|
import javax.inject.Provider; |
52 |
|
|
53 |
|
import org.junit.Before; |
54 |
|
import org.junit.Test; |
55 |
|
|
56 |
|
|
57 |
|
@link |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (41) |
Complexity: 11 |
Complexity Density: 0.37 |
|
59 |
|
public class ResourcesTemplatingFunctionsTest { |
60 |
|
|
61 |
|
private ResourcesTemplatingFunctions resfn; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
63 |
8 |
@Before... |
64 |
|
public void setUp() throws Exception { |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
8 |
DummyResourceOrigin dummyResourceOrigin = new DummyResourceOrigin( |
72 |
|
"/foobar-module", |
73 |
|
"/foobar-module/webresources", |
74 |
|
"/foobar-module/webresources/css", |
75 |
|
"/foobar-module/webresources/css/a.css", |
76 |
|
"/foobar-module/webresources/css/b.css", |
77 |
|
"/foobar-module/webresources/css/wrong.txt", |
78 |
|
"/foobar-module/webresources/js", |
79 |
|
"/foobar-module/webresources/js/a.js", |
80 |
|
"/foobar-module/webresources/js/b.js", |
81 |
|
"/foobar-module/webresources/js/bad.jsp", |
82 |
|
"/module2", |
83 |
|
"/module2/file.js", |
84 |
|
"/module2/file.css" |
85 |
|
); |
86 |
|
|
87 |
8 |
WebContext context = mock(WebContext.class); |
88 |
8 |
final Provider<Context> contextProvider = GuiceUtils.providerForInstance(context); |
89 |
8 |
doReturn("/ctxPath").when(context).getContextPath(); |
90 |
8 |
ResourceLinker resourceLinker = new ResourceLinker(dummyResourceOrigin, |
91 |
|
GuiceUtils.providerForInstance(new ResourcesModule()), contextProvider) { |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
4 |
@Override... |
94 |
|
protected String fingerPrintFor(info.magnolia.resourceloader.Resource resource) { |
95 |
4 |
return "1968-01-24-18-35-00-000"; |
96 |
|
} |
97 |
|
|
98 |
|
}; |
99 |
|
|
100 |
8 |
resfn = new ResourcesTemplatingFunctions(dummyResourceOrigin, resourceLinker); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void cssOnePattern() { |
105 |
|
|
106 |
1 |
String foobarCssLinks = resfn.css("/foobar-module/webresources/css/.*css"); |
107 |
1 |
String allCssLinks = resfn.css(".*.css"); |
108 |
1 |
String txtLinks = resfn.css(".*.txt"); |
109 |
|
|
110 |
|
|
111 |
1 |
assertThat(splitResFnResult(foobarCssLinks), containsInAnyOrder( |
112 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>", |
113 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>" |
114 |
|
)); |
115 |
1 |
assertThat(splitResFnResult(allCssLinks), containsInAnyOrder( |
116 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>", |
117 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>", |
118 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/module2/file.css\"/>" |
119 |
|
)); |
120 |
1 |
assertThat(splitResFnResult(txtLinks), containsInAnyOrder( |
121 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/wrong.txt\"/>" |
122 |
|
)); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void onePatternNoMatch() { |
127 |
|
|
128 |
1 |
String noMatch = resfn.css("/tarzan.*.css"); |
129 |
|
|
130 |
1 |
assertThat(noMatch, isEmptyOrNullString()); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
public void cssPatternList() { |
135 |
|
|
136 |
1 |
String cssFrom2Modules = resfn.css(Arrays.asList("/foobar.*.css", "/module2.*.css")); |
137 |
|
|
138 |
|
|
139 |
1 |
assertThat(splitResFnResult(cssFrom2Modules), containsInAnyOrder( |
140 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>", |
141 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b.css\"/>", |
142 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/module2/file.css\"/>" |
143 |
|
)); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void cssWithOtherAttributes() { |
148 |
|
|
149 |
1 |
String linkWithAttributeSingleQuoted = resfn.css("/foobar-module.*a.css", "media='all'"); |
150 |
1 |
String linkWithAttributeDoubleQuoted = resfn.css("/foobar-module.*a.css", "media=\"all\""); |
151 |
|
|
152 |
|
|
153 |
1 |
assertThat(splitResFnResult(linkWithAttributeSingleQuoted), containsInAnyOrder( |
154 |
|
"<link rel=\"stylesheet\" type=\"text/css\" media='all' href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>" |
155 |
|
)); |
156 |
1 |
assertThat(splitResFnResult(linkWithAttributeDoubleQuoted), containsInAnyOrder( |
157 |
|
"<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a.css\"/>" |
158 |
|
)); |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
161 |
1 |
@Test... |
162 |
|
public void cachedCss() { |
163 |
|
|
164 |
1 |
String foobarCssLinks = resfn.cachedCss("/foobar-module/webresources/css/.*css"); |
165 |
|
|
166 |
|
|
167 |
1 |
assertThat(splitResFnResult(foobarCssLinks), containsInAnyOrder( |
168 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/a~1968-01-24-18-35-00-000~cache.css\"/>", |
169 |
|
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/ctxPath/.resources/foobar-module/webresources/css/b~1968-01-24-18-35-00-000~cache.css\"/>" |
170 |
|
)); |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
173 |
1 |
@Test... |
174 |
|
public void jsOnePattern() { |
175 |
|
|
176 |
1 |
String fooBarJsLinks = resfn.js("/foobar.*.js"); |
177 |
|
|
178 |
|
|
179 |
1 |
assertThat(splitResFnResult(fooBarJsLinks), containsInAnyOrder( |
180 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a.js\"></script>", |
181 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b.js\"></script>" |
182 |
|
)); |
183 |
|
} |
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
185 |
1 |
@Test... |
186 |
|
public void jsPatternList() { |
187 |
|
|
188 |
1 |
String jsFrom2Modules = resfn.js(Arrays.asList("/foobar.*.js", "/module2.*.js")); |
189 |
|
|
190 |
|
|
191 |
1 |
assertThat(splitResFnResult(jsFrom2Modules), containsInAnyOrder( |
192 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a.js\"></script>", |
193 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b.js\"></script>", |
194 |
|
"<script src=\"/ctxPath/.resources/module2/file.js\"></script>" |
195 |
|
)); |
196 |
|
|
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
199 |
1 |
@Test... |
200 |
|
public void cachedJs() { |
201 |
|
|
202 |
1 |
String fooBarJsLinksCached = resfn.cachedJs("/foobar.*.js"); |
203 |
|
|
204 |
|
|
205 |
1 |
assertThat(splitResFnResult(fooBarJsLinksCached), containsInAnyOrder( |
206 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/a~1968-01-24-18-35-00-000~cache.js\"></script>", |
207 |
|
"<script src=\"/ctxPath/.resources/foobar-module/webresources/js/b~1968-01-24-18-35-00-000~cache.js\"></script>" |
208 |
|
)); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
10 |
private List<String> splitResFnResult(String fullResfnResult) {... |
215 |
10 |
return Arrays.asList(fullResfnResult.split("\\n")); |
216 |
|
} |
217 |
|
|
218 |
|
} |