/*
* Copyright (c) 2010. All rights reserved.
*/
package ro.isdc.wro.extensions.processor;
import static ro.isdc.wro.util.WroTestUtils.initProcessor;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.concurrent.Callable;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.isdc.wro.WroRuntimeException;
import ro.isdc.wro.config.Context;
import ro.isdc.wro.extensions.processor.css.RhinoLessCssProcessor;
import ro.isdc.wro.extensions.processor.support.less.LessCss;
import ro.isdc.wro.model.resource.ResourceType;
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.model.resource.processor.impl.css.CssImportPreProcessor;
import ro.isdc.wro.model.resource.processor.support.ChainedProcessor;
import ro.isdc.wro.util.Function;
import ro.isdc.wro.util.WroTestUtils;
/**
* Test less css processor.
*
* @author Alex Objelean
* @created Created on Apr 21, 2010
*/
public class TestRhinoLessCssProcessor {
private static final Logger LOG = LoggerFactory.getLogger(TestRhinoLessCssProcessor.class);
@Before
public void setUp() {
Context.set(Context.standaloneContext());
}
@After
public void tearDown() {
Context.unset();
}
@Test
public void testFromFolder()
throws Exception {
final ResourcePreProcessor processor = new RhinoLessCssProcessor();
final URL url = getClass().getResource("lesscss");
final File testFolder = new File(url.getFile(), "test");
final File expectedFolder = new File(url.getFile(), "expected");
WroTestUtils.compareFromDifferentFoldersByExtension(testFolder, expectedFolder, "css", processor);
}
@Test
public void executeMultipleTimesDoesntThrowOutOfMemoryException() {
final LessCss lessCss = new LessCss();
for (int i = 0; i < 100; i++) {
lessCss.less("#id {.class {color: red;}}");
}
}
@Test
public void shouldBeThreadSafe()
throws Exception {
final RhinoLessCssProcessor processor = new RhinoLessCssProcessor() {
@Override
protected void onException(final WroRuntimeException e) {
throw e;
}
};
final Callable<Void> task = new Callable<Void>() {
@Override
public Void call() {
try {
processor.process(new StringReader("#id {.class {color: red;}}"), new StringWriter());
} catch (final Exception e) {
throw new RuntimeException(e);
}
return null;
}
};
WroTestUtils.runConcurrently(task);
}
/**
* Test that processing invalid less css produces exceptions
*/
@Test
public void shouldFailWhenInvalidLessCssIsProcessed()
throws Exception {
final ResourcePreProcessor processor = new RhinoLessCssProcessor() {
@Override
protected void onException(final WroRuntimeException e) {
LOG.debug("[FAIL] Exception message is: {}", e.getMessage());
throw e;
};
};
final URL url = getClass().getResource("lesscss");
final File testFolder = new File(url.getFile(), "invalid");
WroTestUtils.forEachFileInFolder(testFolder, new Function<File, Void>() {
@Override
public Void apply(final File input)
throws Exception {
try {
processor.process(null, new FileReader(input), new StringWriter());
Assert.fail("Expected to fail, but didn't");
} catch (final WroRuntimeException e) {
// expected to throw exception, continue
}
return null;
}
});
}
@Test
public void shouldBePossibleToExtendLessCssWithDifferentScriptStream() {
new LessCss() {
@Override
protected InputStream getScriptAsStream()
throws IOException {
return TestRhinoCoffeeScriptProcessor.class.getResourceAsStream("less.js");
}
}.less("#id {}");
}
@Test
public void shouldWorkProperlyWithCssImportPreProcessor()
throws Exception {
final ResourcePreProcessor processor = ChainedProcessor.create(initProcessor(new CssImportPreProcessor()),
initProcessor((ResourcePreProcessor) new RhinoLessCssProcessor()));
final URL url = getClass().getResource("lesscss");
final File testFolder = new File(url.getFile(), "test");
final File expectedFolder = new File(url.getFile(), "expectedUrlRewriting");
WroTestUtils.compareFromDifferentFoldersByExtension(testFolder, expectedFolder, "css", processor);
}
@Test
public void shouldSupportCorrectResourceTypes() {
WroTestUtils.assertProcessorSupportResourceTypes(new RhinoLessCssProcessor(), ResourceType.CSS);
}
}