1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
53 |
|
@version |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (55) |
Complexity: 7 |
Complexity Density: 0.14 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
|
73 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
95 |
1 |
@Test... |
96 |
|
public void computeSkinFile() throws Exception |
97 |
|
{ |
98 |
|
|
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 |
|
|
108 |
1 |
assertEquals("output", mocker.getComponentUnderTest().compute(resource, false, true, true, "skin2")); |
109 |
|
|
110 |
|
|
111 |
1 |
verify(xcontext, times(1)).put("skin", "skin2"); |
112 |
1 |
verify(xcontext, times(1)).put("skin", "skin"); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void computeSkinFileWithoutVelocity() throws Exception |
117 |
|
{ |
118 |
|
|
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 |
|
|
125 |
1 |
assertEquals("output", mocker.getComponentUnderTest().compute(resource, false, false, true, "skin2")); |
126 |
|
|
127 |
|
|
128 |
1 |
verify(xcontext, never()).put(eq("skin"), any()); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void computeSkinFileWithoutLESS() throws Exception |
133 |
|
{ |
134 |
|
|
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 |
|
|
141 |
1 |
assertEquals("Some Velocity-rendered LESS content", mocker.getComponentUnderTest().compute(resource, false, |
142 |
|
true, false, "skin2")); |
143 |
|
|
144 |
|
|
145 |
1 |
verifyZeroInteractions(less4jCompiler); |
146 |
|
} |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
148 |
1 |
@Test... |
149 |
|
public void computeSkinFileWithMainStyleIncluded() throws Exception |
150 |
|
{ |
151 |
|
|
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 |
|
|
165 |
1 |
assertEquals("output", mocker.getComponentUnderTest().compute(resource, true, true, true, "skin")); |
166 |
|
|
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
169 |
1 |
@Test... |
170 |
|
public void computeSkinFileWhenException() throws Exception |
171 |
|
{ |
172 |
|
|
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 |
|
|
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 |
|
|
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 |
|
} |