1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.lesscss; |
21 |
|
|
22 |
|
import javax.inject.Provider; |
23 |
|
|
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Rule; |
26 |
|
import org.junit.Test; |
27 |
|
import org.xwiki.lesscss.compiler.LESSCompiler; |
28 |
|
import org.xwiki.lesscss.compiler.LESSCompilerException; |
29 |
|
import org.xwiki.lesscss.internal.cache.ColorThemeCache; |
30 |
|
import org.xwiki.lesscss.internal.cache.LESSResourcesCache; |
31 |
|
import org.xwiki.lesscss.internal.colortheme.ColorTheme; |
32 |
|
import org.xwiki.lesscss.internal.colortheme.ColorThemeReference; |
33 |
|
import org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory; |
34 |
|
import org.xwiki.lesscss.internal.colortheme.LESSColorThemeConverter; |
35 |
|
import org.xwiki.lesscss.internal.skin.SkinReference; |
36 |
|
import org.xwiki.lesscss.internal.skin.SkinReferenceFactory; |
37 |
|
import org.xwiki.lesscss.resources.LESSResourceReference; |
38 |
|
import org.xwiki.lesscss.resources.LESSResourceReferenceFactory; |
39 |
|
import org.xwiki.model.reference.DocumentReference; |
40 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
41 |
|
import org.xwiki.security.authorization.Right; |
42 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
43 |
|
|
44 |
|
import com.xpn.xwiki.XWikiContext; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
|
47 |
|
import static org.junit.Assert.assertEquals; |
48 |
|
import static org.junit.Assert.assertFalse; |
49 |
|
import static org.junit.Assert.assertTrue; |
50 |
|
import static org.mockito.Mockito.mock; |
51 |
|
import static org.mockito.Mockito.verify; |
52 |
|
import static org.mockito.Mockito.verifyZeroInteractions; |
53 |
|
import static org.mockito.Mockito.when; |
54 |
|
|
55 |
|
|
56 |
|
@link |
57 |
|
|
58 |
|
@since |
59 |
|
@version |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (136) |
Complexity: 15 |
Complexity Density: 0.12 |
|
61 |
|
public class LessCompilerScriptServiceTest |
62 |
|
{ |
63 |
|
@Rule |
64 |
|
public MockitoComponentMockingRule<LessCompilerScriptService> mocker = |
65 |
|
new MockitoComponentMockingRule<>(LessCompilerScriptService.class); |
66 |
|
|
67 |
|
private LESSCompiler lessCompiler; |
68 |
|
|
69 |
|
private LESSResourceReferenceFactory lessResourceReferenceFactory; |
70 |
|
|
71 |
|
private LESSResourcesCache lessCache; |
72 |
|
|
73 |
|
private ColorThemeCache colorThemeCache; |
74 |
|
|
75 |
|
private LESSColorThemeConverter lessColorThemeConverter; |
76 |
|
|
77 |
|
private AuthorizationManager authorizationManager; |
78 |
|
|
79 |
|
private Provider<XWikiContext> xcontextProvider; |
80 |
|
|
81 |
|
private XWikiContext xcontext; |
82 |
|
|
83 |
|
private SkinReferenceFactory skinReferenceFactory; |
84 |
|
|
85 |
|
private ColorThemeReferenceFactory colorThemeReferenceFactory; |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
87 |
14 |
@Before... |
88 |
|
public void setUp() throws Exception |
89 |
|
{ |
90 |
14 |
lessCompiler = mocker.getInstance(LESSCompiler.class); |
91 |
14 |
lessColorThemeConverter = mocker.getInstance(LESSColorThemeConverter.class); |
92 |
14 |
lessCache = mocker.getInstance(LESSResourcesCache.class); |
93 |
14 |
lessResourceReferenceFactory = mocker.getInstance(LESSResourceReferenceFactory.class); |
94 |
14 |
colorThemeCache = mocker.getInstance(ColorThemeCache.class); |
95 |
14 |
authorizationManager = mocker.getInstance(AuthorizationManager.class); |
96 |
14 |
skinReferenceFactory = mocker.getInstance(SkinReferenceFactory.class); |
97 |
14 |
colorThemeReferenceFactory = mocker.getInstance(ColorThemeReferenceFactory.class); |
98 |
14 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
99 |
14 |
xcontext = mock(XWikiContext.class); |
100 |
14 |
when(xcontextProvider.get()).thenReturn(xcontext); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void compileSkinFile() throws Exception |
105 |
|
{ |
106 |
|
|
107 |
1 |
LESSResourceReference style = mock(LESSResourceReference.class); |
108 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style); |
109 |
|
|
110 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style.less"); |
111 |
|
|
112 |
1 |
verify(lessCompiler).compile(style, false, true, false); |
113 |
|
|
114 |
|
|
115 |
1 |
LESSResourceReference style2 = mock(LESSResourceReference.class); |
116 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style2.less")).thenReturn(style2); |
117 |
|
|
118 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style2.less", true); |
119 |
|
|
120 |
1 |
verify(lessCompiler).compile(style2, false, true, true); |
121 |
|
|
122 |
|
|
123 |
1 |
LESSResourceReference style3 = mock(LESSResourceReference.class); |
124 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style3.less")).thenReturn(style3); |
125 |
|
|
126 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style3.less", false); |
127 |
|
|
128 |
1 |
verify(lessCompiler).compile(style3, false, true, false); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void compileSkinFileWithOtherSkin() throws Exception |
133 |
|
{ |
134 |
|
|
135 |
1 |
LESSResourceReference style = mock(LESSResourceReference.class); |
136 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style); |
137 |
|
|
138 |
|
|
139 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style.less", "skin1"); |
140 |
|
|
141 |
1 |
verify(lessCompiler).compile(style, false, true, "skin1", false); |
142 |
|
|
143 |
|
|
144 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style.less", "skin2"); |
145 |
|
|
146 |
1 |
verify(lessCompiler).compile(style, false, true, "skin2", false); |
147 |
|
|
148 |
|
|
149 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style.less", "skin3", false); |
150 |
|
|
151 |
1 |
verify(lessCompiler).compile(style, false, true, "skin3", false); |
152 |
|
|
153 |
|
|
154 |
1 |
mocker.getComponentUnderTest().compileSkinFile("style.less", "skin4", true); |
155 |
|
|
156 |
1 |
verify(lessCompiler).compile(style, false, true, "skin4", true); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
159 |
1 |
@Test... |
160 |
|
public void compileSkinFileWhenException() throws Exception |
161 |
|
{ |
162 |
|
|
163 |
1 |
LESSResourceReference style = mock(LESSResourceReference.class); |
164 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style); |
165 |
|
|
166 |
1 |
Exception exception = new LESSCompilerException("Exception with LESS", null); |
167 |
1 |
when(lessCompiler.compile(style, false, true, false)).thenThrow(exception); |
168 |
|
|
169 |
|
|
170 |
1 |
String result = mocker.getComponentUnderTest().compileSkinFile("style.less", false); |
171 |
|
|
172 |
|
|
173 |
1 |
assertEquals("LESSCompilerException: Exception with LESS", result); |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
176 |
1 |
@Test... |
177 |
|
public void compileSkinFileWithOtherSkinWhenException() throws Exception |
178 |
|
{ |
179 |
|
|
180 |
1 |
LESSResourceReference style = mock(LESSResourceReference.class); |
181 |
1 |
when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style); |
182 |
|
|
183 |
1 |
Exception exception = new LESSCompilerException("Exception with LESS", null); |
184 |
1 |
when(lessCompiler.compile(style, false, true, "flamingo", false)).thenThrow(exception); |
185 |
|
|
186 |
|
|
187 |
1 |
String result = mocker.getComponentUnderTest().compileSkinFile("style.less", "flamingo", false); |
188 |
|
|
189 |
|
|
190 |
1 |
assertEquals("LESSCompilerException: Exception with LESS", result); |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
193 |
1 |
@Test... |
194 |
|
public void getColorThemeFromSkinFile() throws Exception |
195 |
|
{ |
196 |
|
|
197 |
1 |
mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less"); |
198 |
|
|
199 |
1 |
verify(lessColorThemeConverter).getColorThemeFromSkinFile("style.less", false); |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
202 |
1 |
@Test... |
203 |
|
public void getColorThemeFromSkinFileWithOtherSkin() throws Exception |
204 |
|
{ |
205 |
|
|
206 |
1 |
mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less", "flamingo"); |
207 |
|
|
208 |
1 |
verify(lessColorThemeConverter).getColorThemeFromSkinFile("style.less", "flamingo", false); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void getColorThemeFromSkinFileWithException() throws Exception |
213 |
|
{ |
214 |
|
|
215 |
1 |
Exception exception = new LESSCompilerException("Exception with LESS", null); |
216 |
1 |
when(lessColorThemeConverter.getColorThemeFromSkinFile("style.less", false)).thenThrow(exception); |
217 |
|
|
218 |
|
|
219 |
1 |
ColorTheme result = mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less"); |
220 |
|
|
221 |
|
|
222 |
1 |
assertEquals(0, result.size()); |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
225 |
1 |
@Test... |
226 |
|
public void getColorThemeFromSkinFileWithOtherSkinAndException() throws Exception |
227 |
|
{ |
228 |
|
|
229 |
1 |
Exception exception = new LESSCompilerException("Exception with LESS", null); |
230 |
1 |
when(lessColorThemeConverter.getColorThemeFromSkinFile("style.less", "flamingo", false)).thenThrow(exception); |
231 |
|
|
232 |
|
|
233 |
1 |
ColorTheme result = mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less", "flamingo"); |
234 |
|
|
235 |
|
|
236 |
1 |
assertEquals(0, result.size()); |
237 |
|
} |
238 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
239 |
1 |
@Test... |
240 |
|
public void clearCacheWithRights() throws Exception |
241 |
|
{ |
242 |
|
|
243 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
244 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
245 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
246 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
247 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
248 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
249 |
|
|
250 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(true); |
251 |
|
|
252 |
|
|
253 |
1 |
assertTrue(mocker.getComponentUnderTest().clearCache()); |
254 |
|
|
255 |
|
|
256 |
1 |
verify(lessCache).clear(); |
257 |
1 |
verify(colorThemeCache).clear(); |
258 |
|
} |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
260 |
1 |
@Test... |
261 |
|
public void clearCacheWithoutRights() throws Exception |
262 |
|
{ |
263 |
|
|
264 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
265 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
266 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
267 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
268 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
269 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
270 |
|
|
271 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(false); |
272 |
|
|
273 |
|
|
274 |
1 |
assertFalse(mocker.getComponentUnderTest().clearCache()); |
275 |
|
|
276 |
|
|
277 |
1 |
verifyZeroInteractions(lessCache); |
278 |
1 |
verifyZeroInteractions(colorThemeCache); |
279 |
|
} |
280 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
281 |
1 |
@Test... |
282 |
|
public void clearCacheFromColorThemeWithRights() throws Exception |
283 |
|
{ |
284 |
|
|
285 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
286 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
287 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
288 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
289 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
290 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
291 |
|
|
292 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(true); |
293 |
|
|
294 |
1 |
ColorThemeReference colorThemeReference = mock(ColorThemeReference.class); |
295 |
1 |
when(colorThemeReferenceFactory.createReference("colorTheme")).thenReturn(colorThemeReference); |
296 |
|
|
297 |
|
|
298 |
1 |
assertTrue(mocker.getComponentUnderTest().clearCacheFromColorTheme("colorTheme")); |
299 |
|
|
300 |
|
|
301 |
1 |
verify(lessCache).clearFromColorTheme(colorThemeReference); |
302 |
1 |
verify(colorThemeCache).clearFromColorTheme(colorThemeReference); |
303 |
|
} |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
305 |
1 |
@Test... |
306 |
|
public void clearCacheFromColorThemeWithoutRights() throws Exception |
307 |
|
{ |
308 |
|
|
309 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
310 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
311 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
312 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
313 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
314 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
315 |
|
|
316 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(false); |
317 |
|
|
318 |
|
|
319 |
1 |
assertFalse(mocker.getComponentUnderTest().clearCacheFromColorTheme("colorTheme")); |
320 |
|
|
321 |
|
|
322 |
1 |
verifyZeroInteractions(lessCache); |
323 |
1 |
verifyZeroInteractions(colorThemeCache); |
324 |
|
} |
325 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
326 |
1 |
@Test... |
327 |
|
public void clearCacheFromSkinWithRights() throws Exception |
328 |
|
{ |
329 |
|
|
330 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
331 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
332 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
333 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
334 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
335 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
336 |
|
|
337 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(true); |
338 |
|
|
339 |
1 |
SkinReference skinReference = mock(SkinReference.class); |
340 |
1 |
when(skinReferenceFactory.createReference("skin")).thenReturn(skinReference); |
341 |
|
|
342 |
|
|
343 |
1 |
assertTrue(mocker.getComponentUnderTest().clearCacheFromSkin("skin")); |
344 |
|
|
345 |
|
|
346 |
1 |
verify(lessCache).clearFromSkin(skinReference); |
347 |
1 |
verify(colorThemeCache).clearFromSkin(skinReference); |
348 |
|
} |
349 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
350 |
1 |
@Test... |
351 |
|
public void clearCacheFromSkinWithoutRights() throws Exception |
352 |
|
{ |
353 |
|
|
354 |
1 |
XWikiDocument doc = mock(XWikiDocument.class); |
355 |
1 |
DocumentReference authorReference = new DocumentReference("wiki", "Space", "User"); |
356 |
1 |
when(xcontext.getDoc()).thenReturn(doc); |
357 |
1 |
when(doc.getAuthorReference()).thenReturn(authorReference); |
358 |
1 |
DocumentReference currentDocReference = new DocumentReference("wiki", "Space", "Page"); |
359 |
1 |
when(doc.getDocumentReference()).thenReturn(currentDocReference); |
360 |
|
|
361 |
1 |
when(authorizationManager.hasAccess(Right.PROGRAM, authorReference, currentDocReference)).thenReturn(false); |
362 |
|
|
363 |
|
|
364 |
1 |
assertFalse(mocker.getComponentUnderTest().clearCacheFromSkin("skin")); |
365 |
|
|
366 |
|
|
367 |
1 |
verifyZeroInteractions(lessCache); |
368 |
1 |
verifyZeroInteractions(colorThemeCache); |
369 |
|
} |
370 |
|
|
371 |
|
} |