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.less4j; |
21 |
|
|
22 |
|
import java.io.FileInputStream; |
23 |
|
import java.io.StringWriter; |
24 |
|
|
25 |
|
import org.apache.commons.io.IOUtils; |
26 |
|
import org.junit.Before; |
27 |
|
import org.junit.Rule; |
28 |
|
import org.junit.Test; |
29 |
|
import org.xwiki.skin.Resource; |
30 |
|
import org.xwiki.skin.Skin; |
31 |
|
import org.xwiki.skin.SkinManager; |
32 |
|
import org.xwiki.template.Template; |
33 |
|
import org.xwiki.template.TemplateContent; |
34 |
|
import org.xwiki.template.TemplateManager; |
35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
36 |
|
|
37 |
|
import com.github.sommeri.less4j.Less4jException; |
38 |
|
|
39 |
|
import static org.junit.Assert.assertEquals; |
40 |
|
import static org.junit.Assert.assertNotNull; |
41 |
|
import static org.junit.Assert.assertTrue; |
42 |
|
import static org.mockito.Mockito.mock; |
43 |
|
import static org.mockito.Mockito.when; |
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (46) |
Complexity: 4 |
Complexity Density: 0.09 |
|
48 |
|
public class Less4jCompilerTest |
49 |
|
{ |
50 |
|
@Rule |
51 |
|
public MockitoComponentMockingRule<Less4jCompiler> mocker = new MockitoComponentMockingRule<>(Less4jCompiler.class); |
52 |
|
|
53 |
|
private TemplateManager templateManager; |
54 |
|
|
55 |
|
private SkinManager skinManager; |
56 |
|
|
57 |
|
private Skin skin; |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
59 |
2 |
@Before... |
60 |
|
public void setUp() throws Exception |
61 |
|
{ |
62 |
2 |
templateManager = mocker.getInstance(TemplateManager.class); |
63 |
2 |
skinManager = mocker.getInstance(SkinManager.class); |
64 |
2 |
skin = mock(Skin.class); |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
67 |
1 |
@Test... |
68 |
|
public void compile() throws Exception |
69 |
|
{ |
70 |
|
|
71 |
1 |
when(skinManager.getSkin("skin")).thenReturn(skin); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
1 |
when(skin.getResource("less/style.less.vm")).thenReturn(mock(Resource.class)); |
77 |
1 |
StringWriter import1source = new StringWriter(); |
78 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/style.less.vm").getFile()), import1source); |
79 |
1 |
when(templateManager.renderFromSkin("less/style.less.vm", skin)).thenReturn(import1source.toString()); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
1 |
when(skin.getResource("less/subdir/import2.less")).thenReturn(mock(Resource.class)); |
84 |
1 |
Template import2 = mock(Template.class); |
85 |
1 |
when(templateManager.getTemplate("less/subdir/import2.less", skin)).thenReturn(import2); |
86 |
1 |
TemplateContent importContent2 = mock(TemplateContent.class); |
87 |
1 |
when(import2.getContent()).thenReturn(importContent2); |
88 |
1 |
StringWriter import2source = new StringWriter(); |
89 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/import2.less").getFile()), import2source); |
90 |
1 |
when(importContent2.getContent()).thenReturn(import2source.toString()); |
91 |
|
|
92 |
|
|
93 |
1 |
when(skin.getResource("less/subdir/import3.less")).thenReturn(mock(Resource.class)); |
94 |
1 |
Template import3 = mock(Template.class); |
95 |
1 |
when(templateManager.getTemplate("less/subdir/import3.less", skin)).thenReturn(import3); |
96 |
1 |
TemplateContent importContent3 = mock(TemplateContent.class); |
97 |
1 |
when(import3.getContent()).thenReturn(importContent3); |
98 |
1 |
StringWriter import3source = new StringWriter(); |
99 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/import3.less").getFile()), import3source); |
100 |
1 |
when(importContent3.getContent()).thenReturn(import3source.toString()); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
1 |
StringWriter source = new StringWriter(); |
105 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.less").getFile()), source); |
106 |
1 |
String result = mocker.getComponentUnderTest().compile(source.toString(), "skin", false); |
107 |
|
|
108 |
|
|
109 |
1 |
String result2 = mocker.getComponentUnderTest().compile(source.toString(), "skin", true); |
110 |
|
|
111 |
|
|
112 |
1 |
StringWriter expected = new StringWriter(); |
113 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.css").getFile()), expected); |
114 |
1 |
assertEquals(expected.toString(), result); |
115 |
|
|
116 |
1 |
assertTrue(result2.contains("/*# sourceMappingURL=data:application/json;base64,")); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
1PASS
|
|
119 |
1 |
@Test... |
120 |
|
public void compileWhenImportDoesNotExist() throws Exception |
121 |
|
{ |
122 |
|
|
123 |
1 |
when(skinManager.getSkin("skin")).thenReturn(skin); |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
1 |
Less4jException caughtException = null; |
129 |
1 |
try { |
130 |
1 |
StringWriter source = new StringWriter(); |
131 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.less").getFile()), source); |
132 |
1 |
mocker.getComponentUnderTest().compile(source.toString(), "skin", false); |
133 |
|
} catch (Less4jException e) { |
134 |
1 |
caughtException = e; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
1 |
assertNotNull(caughtException); |
139 |
1 |
StringWriter exceptionMessage = new StringWriter(); |
140 |
1 |
IOUtils.copy(new FileInputStream(getClass().getResource("/lessException.txt").getFile()), exceptionMessage); |
141 |
1 |
assertEquals(exceptionMessage.toString(), caughtException.getMessage()); |
142 |
|
} |
143 |
|
} |