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

File DefaultLESSCompilerTest.java

 

Code metrics

0
43
7
1
220
138
7
0.16
6.14
7
1

Classes

Class Line # Actions
DefaultLESSCompilerTest 59 43 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.internal.compiler;
21   
22    import javax.inject.Provider;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.lesscss.compiler.LESSCompilerException;
29    import org.xwiki.lesscss.internal.LESSContext;
30    import org.xwiki.lesscss.internal.cache.LESSResourcesCache;
31    import org.xwiki.lesscss.internal.colortheme.ColorThemeReference;
32    import org.xwiki.lesscss.internal.colortheme.ColorThemeReferenceFactory;
33    import org.xwiki.lesscss.internal.colortheme.CurrentColorThemeGetter;
34    import org.xwiki.lesscss.internal.colortheme.NamedColorThemeReference;
35    import org.xwiki.lesscss.internal.skin.FSSkinReference;
36    import org.xwiki.lesscss.internal.skin.SkinReference;
37    import org.xwiki.lesscss.internal.skin.SkinReferenceFactory;
38    import org.xwiki.lesscss.resources.LESSResourceReference;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40   
41    import com.xpn.xwiki.XWiki;
42    import com.xpn.xwiki.XWikiContext;
43   
44    import static org.junit.Assert.assertEquals;
45    import static org.junit.Assert.assertTrue;
46    import static org.mockito.ArgumentMatchers.any;
47    import static org.mockito.ArgumentMatchers.anyBoolean;
48    import static org.mockito.ArgumentMatchers.eq;
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.never;
51    import static org.mockito.Mockito.times;
52    import static org.mockito.Mockito.verify;
53    import static org.mockito.Mockito.verifyZeroInteractions;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * @version $Id: 85e0bc65c14828f8cf890c84d0eaa881f006dd58 $
58    */
 
59    public class DefaultLESSCompilerTest
60    {
61    @Rule
62    public MockitoComponentMockingRule<DefaultLESSCompiler> mocker =
63    new MockitoComponentMockingRule<>(DefaultLESSCompiler.class);
64   
65    private LESSResourcesCache cache;
66   
67    private CachedLESSCompiler cachedLESSCompiler;
68   
69    private Provider<XWikiContext> xcontextProvider;
70   
71    private CurrentColorThemeGetter currentColorThemeGetter;
72   
73    private SkinReferenceFactory skinReferenceFactory;
74   
75    private ColorThemeReferenceFactory colorThemeReferenceFactory;
76   
77    private LESSContext lessContext;
78   
79    private XWikiContext xcontext;
80   
81    private XWiki xwiki;
82   
83    private LESSResourceReference lessResourceReference;
84   
85    private SkinReference skinReference = new FSSkinReference("skin");
86   
87    private ColorThemeReference colorThemeReference = new NamedColorThemeReference("colorTheme");
88   
 
89  6 toggle @Before
90    public void setUp() throws Exception
91    {
92  6 cache = mocker.getInstance(LESSResourcesCache.class);
93  6 cachedLESSCompiler = mocker.getInstance(CachedLESSCompiler.class);
94  6 currentColorThemeGetter = mocker.getInstance(CurrentColorThemeGetter.class);
95  6 skinReferenceFactory = mocker.getInstance(SkinReferenceFactory.class);
96  6 colorThemeReferenceFactory = mocker.getInstance(ColorThemeReferenceFactory.class);
97  6 lessContext = mocker.getInstance(LESSContext.class);
98  6 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
99  6 xcontext = mock(XWikiContext.class);
100  6 when(xcontextProvider.get()).thenReturn(xcontext);
101  6 xwiki = mock(XWiki.class);
102  6 when(xcontext.getWiki()).thenReturn(xwiki);
103  6 when(xwiki.getSkin(xcontext)).thenReturn("skin");
104  6 when(currentColorThemeGetter.getCurrentColorTheme(true, "default")).thenReturn("colorTheme");
105  6 when(skinReferenceFactory.createReference("skin")).thenReturn(skinReference);
106  6 when(colorThemeReferenceFactory.createReference("colorTheme")).thenReturn(colorThemeReference);
107   
108  6 lessResourceReference = mock(LESSResourceReference.class);
109   
110  6 when(cache.getMutex(eq(lessResourceReference), eq(new FSSkinReference("skin")),
111    eq(new NamedColorThemeReference("colorTheme")))).thenReturn("mutex");
112    }
113   
 
114  1 toggle @Test
115    public void compileWhenInCache() throws Exception
116    {
117    // Mocks
118  1 when(cache.get(eq(lessResourceReference), eq(new FSSkinReference("skin")),
119    eq(new NamedColorThemeReference("colorTheme")))).thenReturn("cached output");
120   
121    // Test
122  1 assertEquals("cached output",
123    mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false));
124   
125    // Verify
126  1 verify(cache, never()).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference),
127    eq("cache output"));
128    }
129   
 
130  1 toggle @Test
131    public void compileWhenNotInCache() throws Exception
132    {
133    // Mocks
134  1 when(cachedLESSCompiler.compute(eq(lessResourceReference), eq(false), eq(false), eq(true), eq("skin"))).
135    thenReturn("compiled output");
136   
137    // Test
138  1 assertEquals("compiled output",
139    mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false));
140   
141    // Verify
142  1 verify(cache).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference),
143    eq("compiled output"));
144    }
145   
 
146  1 toggle @Test
147    public void compileWhenInCacheButForced() throws Exception
148    {
149    // Mocks
150  1 when(cachedLESSCompiler.compute(eq(lessResourceReference), eq(false), eq(false), eq(true), eq("skin"))).
151    thenReturn("compiled output");
152   
153    // Test
154  1 assertEquals("compiled output", mocker.getComponentUnderTest().compile(
155    lessResourceReference, false, false, "skin", true));
156   
157    // Verify
158  1 verify(cache, times(1)).set(any(LESSResourceReference.class), any(SkinReference.class),
159    any(ColorThemeReference.class), any());
160  1 verify(cache, never()).get(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference));
161    }
162   
 
163  1 toggle @Test
164    public void compileSkinFileWhenInCacheButCacheDisabled() throws Exception
165    {
166    // Mock
167  1 when(lessContext.isCacheDisabled()).thenReturn(true);
168  1 when(cachedLESSCompiler.compute(eq(lessResourceReference), eq(false), eq(false),eq(true), eq("skin"))).
169    thenReturn("compiled output");
170   
171    // Test
172  1 assertEquals("compiled output",
173    mocker.getComponentUnderTest().compile(lessResourceReference, false, false, "skin", true));
174   
175    // Verify that the cache is disabled
176  1 verifyZeroInteractions(cache);
177    }
178   
 
179  1 toggle @Test
180    public void compileWhenInCacheAndHTMLExport() throws Exception
181    {
182    // Mocks
183  1 when(cache.get(eq(lessResourceReference), eq(skinReference),
184    eq(colorThemeReference))).thenReturn("cached output");
185   
186  1 when(lessContext.isHtmlExport()).thenReturn(true);
187   
188    // Test
189  1 assertEquals("cached output",
190    mocker.getComponentUnderTest().compile(lessResourceReference, false, true, false));
191   
192    // Verify that the velocity is executed
193  1 verify(cachedLESSCompiler).compute(eq(lessResourceReference), eq(false), eq(true),
194    eq(false), eq("skin"));
195   
196    // Verify we don't put anything in the cache
197  1 verify(cache, never()).set(any(LESSResourceReference.class), any(SkinReference.class),
198    any(ColorThemeReference.class), any());
199    }
200   
 
201  1 toggle @Test
202    public void compileWhenError() throws Exception
203    {
204    // Mocks
205  1 LESSCompilerException expectedException = new LESSCompilerException("an exception");
206  1 when(cachedLESSCompiler.compute(any(LESSResourceReference.class), anyBoolean(), anyBoolean(), anyBoolean(),
207    any())).thenThrow(expectedException);
208   
209    // Test
210  1 String result = mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false);
211   
212    // Asserts
213  1 assertTrue(StringUtils.startsWith(result, "/* org.xwiki.lesscss.compiler.LESSCompilerException: an exception"));
214  1 assertTrue(StringUtils.endsWith(result, "*/"));
215  1 verify(cache).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference), eq(result));
216  1 verify(mocker.getMockedLogger()).error(eq("Error during the compilation of the resource [{}]."),
217    eq(lessResourceReference), eq(expectedException));
218    }
219   
220    }