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

File Less4jCompilerTest.java

 

Code metrics

0
43
3
1
143
85
4
0.09
14.33
3
1.33

Classes

Class Line # Actions
Less4jCompilerTest 48 43 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 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.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 $Id: bac47e0fe7b0ba2c5bc5e97069761baac318ca6a $
47    */
 
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   
 
59  2 toggle @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   
 
67  1 toggle @Test
68    public void compile() throws Exception
69    {
70    // Mocks
71  1 when(skinManager.getSkin("skin")).thenReturn(skin);
72   
73    // Is is actually more an integration test than a unit test
74   
75    // Import 1
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    // Import 2
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    // Import 3
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    // Test
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    // Now with sourcemaps.
109  1 String result2 = mocker.getComponentUnderTest().compile(source.toString(), "skin", true);
110   
111    // Verify
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   
 
119  1 toggle @Test
120    public void compileWhenImportDoesNotExist() throws Exception
121    {
122    // Mocks
123  1 when(skinManager.getSkin("skin")).thenReturn(skin);
124   
125    // Is is actually more an integration test than a unit test
126   
127    // Test
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    // Verify
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    }