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

File CachedLESSCompilerTest.java

 

Code metrics

0
49
6
1
197
130
7
0.14
8.17
6
1.17

Classes

Class Line # Actions
CachedLESSCompilerTest 55 49 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 5 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.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.lesscss.compiler.LESSCompilerException;
28    import org.xwiki.lesscss.internal.LESSConfiguration;
29    import org.xwiki.lesscss.internal.compiler.less4j.Less4jCompiler;
30    import org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference;
31    import org.xwiki.lesscss.resources.LESSResourceReference;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import com.github.sommeri.less4j.Less4jException;
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.web.XWikiEngineContext;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.assertNotNull;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.never;
46    import static org.mockito.Mockito.times;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.verifyZeroInteractions;
49    import static org.mockito.Mockito.when;
50   
51    /**
52    * @since 6.4M2
53    * @version $Id: ad2213867b65ceedb3d1558fca4d7e299b7b5884 $
54    */
 
55    public class CachedLESSCompilerTest
56    {
57    @Rule
58    public MockitoComponentMockingRule<CachedLESSCompiler> mocker =
59    new MockitoComponentMockingRule<>(CachedLESSCompiler.class);
60   
61    private Provider<XWikiContext> xcontextProvider;
62   
63    private Less4jCompiler less4jCompiler;
64   
65    private LESSConfiguration lessConfiguration;
66   
67    private XWikiContext xcontext;
68   
69    private XWiki xwiki;
70   
71    private XWikiEngineContext engineContext;
72   
 
73  5 toggle @Before
74    public void setUp() throws Exception
75    {
76  5 less4jCompiler = mocker.getInstance(Less4jCompiler.class);
77  5 lessConfiguration = mocker.getInstance(LESSConfiguration.class);
78  5 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
79  5 xcontext = mock(XWikiContext.class);
80  5 when(xcontextProvider.get()).thenReturn(xcontext);
81  5 xwiki = mock(XWiki.class);
82  5 when(xcontext.getWiki()).thenReturn(xwiki);
83  5 engineContext = mock(XWikiEngineContext.class);
84  5 when(xwiki.getEngineContext()).thenReturn(engineContext);
85  5 when(xwiki.getSkin(xcontext)).thenReturn("skin");
86   
87  5 XWikiDocument mockDocument = mock(XWikiDocument.class);
88  5 when(mockDocument.getPrefixedFullName()).thenReturn("SomeContextDocument");
89  5 when(xcontext.getDoc()).thenReturn(mockDocument);
90   
91  5 when(lessConfiguration.getMaximumSimultaneousCompilations()).thenReturn(1);
92  5 when(lessConfiguration.isGenerateInlineSourceMaps()).thenReturn(false);
93    }
94   
 
95  1 toggle @Test
96    public void computeSkinFile() throws Exception
97    {
98    // Mocks
99  1 LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
100  1 when(resource.getContent(eq("skin2"))).thenReturn("Some LESS content");
101  1 when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).
102    thenReturn("Some Velocity-rendered LESS content");
103  1 when(less4jCompiler.compile(eq("Some Velocity-rendered LESS content"), eq("skin2"),
104    eq(false)))
105    .thenReturn("output");
106   
107    // Tests
108  1 assertEquals("output", mocker.getComponentUnderTest().compute(resource, false, true, true, "skin2"));
109   
110    // Verify
111  1 verify(xcontext, times(1)).put("skin", "skin2");
112  1 verify(xcontext, times(1)).put("skin", "skin");
113    }
114   
 
115  1 toggle @Test
116    public void computeSkinFileWithoutVelocity() throws Exception
117    {
118    // Mocks
119  1 LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
120  1 when(resource.getContent(eq("skin2"))).thenReturn("Some LESS content");
121  1 when(less4jCompiler.compile(eq("Some LESS content"), eq("skin2"),
122    eq(false))).thenReturn("output");
123   
124    // Tests
125  1 assertEquals("output", mocker.getComponentUnderTest().compute(resource, false, false, true, "skin2"));
126   
127    // Verify
128  1 verify(xcontext, never()).put(eq("skin"), any());
129    }
130   
 
131  1 toggle @Test
132    public void computeSkinFileWithoutLESS() throws Exception
133    {
134    // Mocks
135  1 LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
136  1 when(resource.getContent(eq("skin2"))).thenReturn("Some LESS content");
137  1 when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).
138    thenReturn("Some Velocity-rendered LESS content");
139   
140    // Tests
141  1 assertEquals("Some Velocity-rendered LESS content", mocker.getComponentUnderTest().compute(resource, false,
142    true, false, "skin2"));
143   
144    // Verify that the LESS compiler is never called
145  1 verifyZeroInteractions(less4jCompiler);
146    }
147   
 
148  1 toggle @Test
149    public void computeSkinFileWithMainStyleIncluded() throws Exception
150    {
151    // Mocks
152  1 LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
153  1 when(resource.getContent(eq("skin"))).thenReturn("Some LESS content");
154  1 when(
155    xwiki.evaluateVelocity(eq("@import (reference) \"style.less.vm\";\n" + "Some LESS content"),
156    eq("SomeContextDocument")))
157    .thenReturn("@import (reference) \"style.less.vm\";\n"
158    +"Some Velocity-rendered LESS content");
159  1 when(less4jCompiler.compile(eq("@import (reference) \"style.less.vm\";\n"
160    +"Some Velocity-rendered LESS content"), eq("skin"),
161    eq(false)))
162    .thenReturn("output");
163   
164    // Tests
165  1 assertEquals("output", mocker.getComponentUnderTest().compute(resource, true, true, true, "skin"));
166   
167    }
168   
 
169  1 toggle @Test
170    public void computeSkinFileWhenException() throws Exception
171    {
172    // Mocks
173  1 LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
174  1 when(resource.getContent(eq("skin"))).thenReturn("Some LESS content");
175  1 when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).
176    thenReturn("Some Velocity-rendered LESS content");
177  1 Less4jException lessCompilerException = mock(Less4jException.class);
178  1 when(less4jCompiler.compile(eq("Some Velocity-rendered LESS content"), eq("skin"),
179    eq(false))).
180    thenThrow(lessCompilerException);
181   
182    // Tests
183  1 LESSCompilerException caughtException = null;
184  1 try {
185  1 mocker.getComponentUnderTest().compute(resource, false, true, true, "skin");
186    } catch(LESSCompilerException e) {
187  1 caughtException = e;
188    }
189   
190    // Verify
191  1 assertNotNull(caughtException);
192  1 assertEquals(lessCompilerException, caughtException.getCause());
193  1 assertEquals("Failed to compile the resource ["+resource.toString()+"] with LESS.",
194    caughtException.getMessage());
195   
196    }
197    }