1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.lesscss

File LessCompilerScriptServiceTest.java

 

Code metrics

0
121
15
1
371
227
15
0.12
8.07
15
1

Classes

Class Line # Actions
LessCompilerScriptServiceTest 61 121 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Test class for {@link org.xwiki.lesscss.LessCompilerScriptService}.
57    *
58    * @since 6.1M1
59    * @version $Id: 3e97f63deddcdbdf3d79aa3c6201353538b02d2d $
60    */
 
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   
 
87  14 toggle @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   
 
103  1 toggle @Test
104    public void compileSkinFile() throws Exception
105    {
106    // Mock
107  1 LESSResourceReference style = mock(LESSResourceReference.class);
108  1 when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style);
109    // Test
110  1 mocker.getComponentUnderTest().compileSkinFile("style.less");
111    // Verify
112  1 verify(lessCompiler).compile(style, false, true, false);
113   
114    // Mock
115  1 LESSResourceReference style2 = mock(LESSResourceReference.class);
116  1 when(lessResourceReferenceFactory.createReferenceForSkinFile("style2.less")).thenReturn(style2);
117    // Test
118  1 mocker.getComponentUnderTest().compileSkinFile("style2.less", true);
119    // Verify
120  1 verify(lessCompiler).compile(style2, false, true, true);
121   
122    // Mock
123  1 LESSResourceReference style3 = mock(LESSResourceReference.class);
124  1 when(lessResourceReferenceFactory.createReferenceForSkinFile("style3.less")).thenReturn(style3);
125    // Test
126  1 mocker.getComponentUnderTest().compileSkinFile("style3.less", false);
127    // Verify
128  1 verify(lessCompiler).compile(style3, false, true, false);
129    }
130   
 
131  1 toggle @Test
132    public void compileSkinFileWithOtherSkin() throws Exception
133    {
134    // Mock
135  1 LESSResourceReference style = mock(LESSResourceReference.class);
136  1 when(lessResourceReferenceFactory.createReferenceForSkinFile("style.less")).thenReturn(style);
137   
138    // Test
139  1 mocker.getComponentUnderTest().compileSkinFile("style.less", "skin1");
140    // Verify
141  1 verify(lessCompiler).compile(style, false, true, "skin1", false);
142   
143    // Test
144  1 mocker.getComponentUnderTest().compileSkinFile("style.less", "skin2");
145    // Verify
146  1 verify(lessCompiler).compile(style, false, true, "skin2", false);
147   
148    // Test
149  1 mocker.getComponentUnderTest().compileSkinFile("style.less", "skin3", false);
150    // Verify
151  1 verify(lessCompiler).compile(style, false, true, "skin3", false);
152   
153    // Test
154  1 mocker.getComponentUnderTest().compileSkinFile("style.less", "skin4", true);
155    // Verify
156  1 verify(lessCompiler).compile(style, false, true, "skin4", true);
157    }
158   
 
159  1 toggle @Test
160    public void compileSkinFileWhenException() throws Exception
161    {
162    // Mocks
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    // Test
170  1 String result = mocker.getComponentUnderTest().compileSkinFile("style.less", false);
171   
172    // Verify
173  1 assertEquals("LESSCompilerException: Exception with LESS", result);
174    }
175   
 
176  1 toggle @Test
177    public void compileSkinFileWithOtherSkinWhenException() throws Exception
178    {
179    // Mocks
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    // Test
187  1 String result = mocker.getComponentUnderTest().compileSkinFile("style.less", "flamingo", false);
188   
189    // Verify
190  1 assertEquals("LESSCompilerException: Exception with LESS", result);
191    }
192   
 
193  1 toggle @Test
194    public void getColorThemeFromSkinFile() throws Exception
195    {
196    // Test
197  1 mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less");
198    // Verify
199  1 verify(lessColorThemeConverter).getColorThemeFromSkinFile("style.less", false);
200    }
201   
 
202  1 toggle @Test
203    public void getColorThemeFromSkinFileWithOtherSkin() throws Exception
204    {
205    // Test
206  1 mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less", "flamingo");
207    // Verify
208  1 verify(lessColorThemeConverter).getColorThemeFromSkinFile("style.less", "flamingo", false);
209    }
210   
 
211  1 toggle @Test
212    public void getColorThemeFromSkinFileWithException() throws Exception
213    {
214    // Mocks
215  1 Exception exception = new LESSCompilerException("Exception with LESS", null);
216  1 when(lessColorThemeConverter.getColorThemeFromSkinFile("style.less", false)).thenThrow(exception);
217   
218    // Test
219  1 ColorTheme result = mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less");
220   
221    // Verify
222  1 assertEquals(0, result.size());
223    }
224   
 
225  1 toggle @Test
226    public void getColorThemeFromSkinFileWithOtherSkinAndException() throws Exception
227    {
228    // Mocks
229  1 Exception exception = new LESSCompilerException("Exception with LESS", null);
230  1 when(lessColorThemeConverter.getColorThemeFromSkinFile("style.less", "flamingo", false)).thenThrow(exception);
231   
232    // Test
233  1 ColorTheme result = mocker.getComponentUnderTest().getColorThemeFromSkinFile("style.less", "flamingo");
234   
235    // Verify
236  1 assertEquals(0, result.size());
237    }
238   
 
239  1 toggle @Test
240    public void clearCacheWithRights() throws Exception
241    {
242    // Mocks
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    // Tests
253  1 assertTrue(mocker.getComponentUnderTest().clearCache());
254   
255    // Verify
256  1 verify(lessCache).clear();
257  1 verify(colorThemeCache).clear();
258    }
259   
 
260  1 toggle @Test
261    public void clearCacheWithoutRights() throws Exception
262    {
263    // Mocks
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    // Tests
274  1 assertFalse(mocker.getComponentUnderTest().clearCache());
275   
276    // Verify
277  1 verifyZeroInteractions(lessCache);
278  1 verifyZeroInteractions(colorThemeCache);
279    }
280   
 
281  1 toggle @Test
282    public void clearCacheFromColorThemeWithRights() throws Exception
283    {
284    // Mocks
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    // Tests
298  1 assertTrue(mocker.getComponentUnderTest().clearCacheFromColorTheme("colorTheme"));
299   
300    // Verify
301  1 verify(lessCache).clearFromColorTheme(colorThemeReference);
302  1 verify(colorThemeCache).clearFromColorTheme(colorThemeReference);
303    }
304   
 
305  1 toggle @Test
306    public void clearCacheFromColorThemeWithoutRights() throws Exception
307    {
308    // Mocks
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    // Tests
319  1 assertFalse(mocker.getComponentUnderTest().clearCacheFromColorTheme("colorTheme"));
320   
321    // Verify
322  1 verifyZeroInteractions(lessCache);
323  1 verifyZeroInteractions(colorThemeCache);
324    }
325   
 
326  1 toggle @Test
327    public void clearCacheFromSkinWithRights() throws Exception
328    {
329    // Mocks
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    // Tests
343  1 assertTrue(mocker.getComponentUnderTest().clearCacheFromSkin("skin"));
344   
345    // Verify
346  1 verify(lessCache).clearFromSkin(skinReference);
347  1 verify(colorThemeCache).clearFromSkin(skinReference);
348    }
349   
 
350  1 toggle @Test
351    public void clearCacheFromSkinWithoutRights() throws Exception
352    {
353    // Mocks
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    // Tests
364  1 assertFalse(mocker.getComponentUnderTest().clearCacheFromSkin("skin"));
365   
366    // Verify
367  1 verifyZeroInteractions(lessCache);
368  1 verifyZeroInteractions(colorThemeCache);
369    }
370   
371    }