| 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.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 |
| 58 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (50) |
Complexity: 7 |
Complexity Density: 0.16 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
|
| 89 |
6 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 114 |
1 |
@Test... |
| 115 |
|
public void compileWhenInCache() throws Exception |
| 116 |
|
{ |
| 117 |
|
|
| 118 |
1 |
when(cache.get(eq(lessResourceReference), eq(new FSSkinReference("skin")), |
| 119 |
|
eq(new NamedColorThemeReference("colorTheme")))).thenReturn("cached output"); |
| 120 |
|
|
| 121 |
|
|
| 122 |
1 |
assertEquals("cached output", |
| 123 |
|
mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false)); |
| 124 |
|
|
| 125 |
|
|
| 126 |
1 |
verify(cache, never()).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference), |
| 127 |
|
eq("cache output")); |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 130 |
1 |
@Test... |
| 131 |
|
public void compileWhenNotInCache() throws Exception |
| 132 |
|
{ |
| 133 |
|
|
| 134 |
1 |
when(cachedLESSCompiler.compute(eq(lessResourceReference), eq(false), eq(false), eq(true), eq("skin"))). |
| 135 |
|
thenReturn("compiled output"); |
| 136 |
|
|
| 137 |
|
|
| 138 |
1 |
assertEquals("compiled output", |
| 139 |
|
mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false)); |
| 140 |
|
|
| 141 |
|
|
| 142 |
1 |
verify(cache).set(eq(lessResourceReference), eq(skinReference), eq(colorThemeReference), |
| 143 |
|
eq("compiled output")); |
| 144 |
|
} |
| 145 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 146 |
1 |
@Test... |
| 147 |
|
public void compileWhenInCacheButForced() throws Exception |
| 148 |
|
{ |
| 149 |
|
|
| 150 |
1 |
when(cachedLESSCompiler.compute(eq(lessResourceReference), eq(false), eq(false), eq(true), eq("skin"))). |
| 151 |
|
thenReturn("compiled output"); |
| 152 |
|
|
| 153 |
|
|
| 154 |
1 |
assertEquals("compiled output", mocker.getComponentUnderTest().compile( |
| 155 |
|
lessResourceReference, false, false, "skin", true)); |
| 156 |
|
|
| 157 |
|
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 163 |
1 |
@Test... |
| 164 |
|
public void compileSkinFileWhenInCacheButCacheDisabled() throws Exception |
| 165 |
|
{ |
| 166 |
|
|
| 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 |
|
|
| 172 |
1 |
assertEquals("compiled output", |
| 173 |
|
mocker.getComponentUnderTest().compile(lessResourceReference, false, false, "skin", true)); |
| 174 |
|
|
| 175 |
|
|
| 176 |
1 |
verifyZeroInteractions(cache); |
| 177 |
|
} |
| 178 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 179 |
1 |
@Test... |
| 180 |
|
public void compileWhenInCacheAndHTMLExport() throws Exception |
| 181 |
|
{ |
| 182 |
|
|
| 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 |
|
|
| 189 |
1 |
assertEquals("cached output", |
| 190 |
|
mocker.getComponentUnderTest().compile(lessResourceReference, false, true, false)); |
| 191 |
|
|
| 192 |
|
|
| 193 |
1 |
verify(cachedLESSCompiler).compute(eq(lessResourceReference), eq(false), eq(true), |
| 194 |
|
eq(false), eq("skin")); |
| 195 |
|
|
| 196 |
|
|
| 197 |
1 |
verify(cache, never()).set(any(LESSResourceReference.class), any(SkinReference.class), |
| 198 |
|
any(ColorThemeReference.class), any()); |
| 199 |
|
} |
| 200 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 201 |
1 |
@Test... |
| 202 |
|
public void compileWhenError() throws Exception |
| 203 |
|
{ |
| 204 |
|
|
| 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 |
|
|
| 210 |
1 |
String result = mocker.getComponentUnderTest().compile(lessResourceReference, false, false, false); |
| 211 |
|
|
| 212 |
|
|
| 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 |
|
} |