Search in sources :

Example 1 with Less4jException

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();
    }
}
Also used : StringWriter(java.io.StringWriter) Less4jException(com.github.sommeri.less4j.Less4jException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) LESSSkinFileResourceReference(org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)

Example 2 with Less4jException

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());
}
Also used : LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference) Less4jException(com.github.sommeri.less4j.Less4jException) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) Test(org.junit.Test)

Example 3 with Less4jException

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());
}
Also used : StringWriter(java.io.StringWriter) Less4jException(com.github.sommeri.less4j.Less4jException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Less4jException (com.github.sommeri.less4j.Less4jException)3 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 LESSCompilerException (org.xwiki.lesscss.compiler.LESSCompilerException)2 FileInputStream (java.io.FileInputStream)1 LESSSkinFileResourceReference (org.xwiki.lesscss.internal.resources.LESSSkinFileResourceReference)1 LESSResourceReference (org.xwiki.lesscss.resources.LESSResourceReference)1