use of com.github.sommeri.less4j.Less4jException in project xwiki-platform by xwiki.
the class CachedLESSCompiler method compute.
@Override
public String compute(LESSResourceReference lessResourceReference, boolean includeSkinStyle, boolean useVelocity, boolean useLESS, String skin) throws LESSCompilerException {
StringWriter source = new StringWriter();
try {
semaphore.acquire();
if (lessResourceReference instanceof LESSSkinFileResourceReference || includeSkinStyle) {
if (includeSkinStyle) {
// Add the import line to the LESS resource.
// We import this file to be able to use variables and mix-ins defined in it.
// But we don't want it in the output.
source.write(String.format("@import (reference) \"%s\";%s", MAIN_SKIN_STYLE_FILENAME, System.lineSeparator()));
}
// Get the content of the LESS resource
source.write(lessResourceReference.getContent(skin));
}
// Parse the LESS content with Velocity
String lessCode = source.toString();
if (useVelocity) {
lessCode = executeVelocity(lessCode, skin);
}
// Compile the LESS code
if (useLESS) {
return less4JCompiler.compile(lessCode, skin, lessConfiguration.isGenerateInlineSourceMaps());
}
// Otherwise return the raw LESS code
return lessCode;
} catch (Less4jException | InterruptedException e) {
throw new LESSCompilerException(String.format("Failed to compile the resource [%s] with LESS.", lessResourceReference), e);
} finally {
semaphore.release();
}
}
use of com.github.sommeri.less4j.Less4jException in project xwiki-platform by xwiki.
the class CachedLESSCompilerTest method computeSkinFileWhenException.
@Test
public void computeSkinFileWhenException() throws Exception {
// Mocks
LESSResourceReference resource = mock(LESSSkinFileResourceReference.class);
when(resource.getContent(eq("skin"))).thenReturn("Some LESS content");
when(xwiki.evaluateVelocity(eq("Some LESS content"), eq("SomeContextDocument"))).thenReturn("Some Velocity-rendered LESS content");
Less4jException lessCompilerException = mock(Less4jException.class);
when(less4jCompiler.compile(eq("Some Velocity-rendered LESS content"), eq("skin"), eq(false))).thenThrow(lessCompilerException);
// Tests
LESSCompilerException caughtException = null;
try {
mocker.getComponentUnderTest().compute(resource, false, true, true, "skin");
} catch (LESSCompilerException e) {
caughtException = e;
}
// Verify
assertNotNull(caughtException);
assertEquals(lessCompilerException, caughtException.getCause());
assertEquals("Failed to compile the resource [" + resource.toString() + "] with LESS.", caughtException.getMessage());
}
use of com.github.sommeri.less4j.Less4jException in project xwiki-platform by xwiki.
the class Less4jCompilerTest method compileWhenImportDoesNotExist.
@Test
public void compileWhenImportDoesNotExist() throws Exception {
// Mocks
when(skinManager.getSkin("skin")).thenReturn(skin);
// Is is actually more an integration test than a unit test
// Test
Less4jException caughtException = null;
try {
StringWriter source = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/style3.less").getFile()), source);
mocker.getComponentUnderTest().compile(source.toString(), "skin", false);
} catch (Less4jException e) {
caughtException = e;
}
// Verify
assertNotNull(caughtException);
StringWriter exceptionMessage = new StringWriter();
IOUtils.copy(new FileInputStream(getClass().getResource("/lessException.txt").getFile()), exceptionMessage);
assertEquals(exceptionMessage.toString(), caughtException.getMessage());
}
Aggregations