/*
 * Copyright (c) 2010. All rights reserved.
 */
package ro.isdc.wro.extensions.processor;

import junit.framework.Assert;
import org.apache.commons.io.filefilter.WildcardFileFilter;
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.extensions.processor.css.LessCssProcessor;
import ro.isdc.wro.extensions.processor.css.RubySassCssProcessor;
import ro.isdc.wro.extensions.processor.css.SassCssProcessor;
import ro.isdc.wro.extensions.processor.support.less.LessCss;
import ro.isdc.wro.extensions.processor.support.sass.RubySassEngine;
import ro.isdc.wro.model.resource.processor.ResourcePostProcessor;
import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
import ro.isdc.wro.util.Function;
import ro.isdc.wro.util.Transformers;
import ro.isdc.wro.util.WroTestUtils;

import java.io.*;
import java.net.URL;
import java.util.concurrent.Callable;


/**
 * Test ruby sass css processor.
 *
 * @author Simon van der Sluis
 * @created Created on Apr 21, 2010
 */
public class TestRubySassCssProcessor
{

  private static final Logger LOG = LoggerFactory.getLogger(TestRubySassCssProcessor.class);

  /** Location (base) of ruby sass css test resources. */
  private final URL url = getClass().getResource("rubysasscss");

  /** A RubySassEngine to test */
  private RubySassCssProcessor rubySassCss;

  @Before
  public void initEngine() {
    rubySassCss = new RubySassCssProcessor() {
      @Override
      protected void onException(final WroRuntimeException e) {
        LOG.debug("[FAIL] Exception message is: {}", e.getMessage());
        throw e;
      }
    };
  }

  @Test
  public void testFromFolder()
    throws IOException {
    final File testFolder = new File(url.getFile(), "test");
    final File expectedFolder = new File(url.getFile(), "expected");
    WroTestUtils.compareFromDifferentFoldersByName(testFolder, expectedFolder, "scss", "css", rubySassCss);
  }

  @Test
  public void executeMultipleTimesDoesntThrowOutOfMemoryException() {
    RubySassEngine engine = new RubySassEngine();
    for (int i = 0; i < 100; i++) {
      engine.process("#navbar {width: 80%;}");
    }
  }

  @Test
  public void shouldBeThreadSafe() throws Exception {
    final Callable<Void> task = new Callable<Void>() {
      public Void call() {
        try {
          rubySassCss.process(new StringReader("#navbar {width: 80%;}"), new StringWriter());
        } catch (final Exception e) {
          throw new RuntimeException(e);
        }
        return null;
      }
    };
    WroTestUtils.runConcurrently(task);
  }

  /**
   * Test that processing invalid sass css produces exceptions
   *
   * @throws Exception
   */
  @Test
  public void shouldFailWhenInvalidSassCssIsProcessed() throws Exception {
    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 {
          rubySassCss.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;
      }
    });
  }

}

@@ -99,90 +99,225 @@
 rt j
-unit.framework.Assert;%0Aimport org.apache.commons.io.filefilter.WildcardFileFilter;
+ava.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0Aimport java.io.StringReader;%0Aimport java.io.StringWriter;%0Aimport java.net.URL;%0Aimport java.util.concurrent.Callable;%0A%0Aimport junit.framework.Assert;%0A
 %0Aimp
@@ -418,16 +418,17 @@
 actory;%0A
+%0A
 import r
@@ -463,70 +463,8 @@
 on;%0A
-import ro.isdc.wro.extensions.processor.css.LessCssProcessor;%0A
 impo
@@ -569,480 +569,107 @@
 sor.
-css.SassCssProcessor;%0Aimport ro.isdc.wro.extensions.processor.support.less.LessCss;%0Aimport ro.isdc.wro.extensions.processor.support.sass.RubySassEngine;%0Aimport ro.isdc.wro.model.resource.processor.ResourcePostProcessor;%0Aimport ro.isdc.wro.model.resource.processor.ResourcePreProcessor;%0Aimport ro.isdc.wro.util.Function;%0Aimport ro.isdc.wro.util.Transformers;%0Aimport ro.isdc.wro.util.WroTestUtils;%0A%0Aimport java.io.*;%0Aimport java.net.URL;%0Aimport java.util.concurrent.Callable
+support.sass.RubySassEngine;%0Aimport ro.isdc.wro.util.Function;%0Aimport ro.isdc.wro.util.WroTestUtils
 ;%0A%0A%0A
@@ -707,16 +707,17 @@
 ssor.%0A *
+ 
 %0A * @aut
@@ -821,11 +821,13 @@
 ssor
-%0A
+ 
 %7B%0A
+  
 %0A  p
@@ -916,16 +916,18 @@
 class);%0A
+  
 %0A  /** L
@@ -1042,16 +1042,18 @@
 scss%22);%0A
+  
 %0A  /** A
@@ -1123,16 +1123,18 @@
 assCss;%0A
+  
 %0A  @Befo
@@ -1394,24 +1394,26 @@
 %0A    %7D;%0A  %7D%0A
+  
 %0A  @Test%0A  p
@@ -1440,24 +1440,26 @@
 older()%0A    
+  
 throws IOExc
@@ -1705,24 +1705,26 @@
 ssCss);%0A  %7D%0A
+  
 %0A  @Test%0A  p
@@ -1933,16 +1933,18 @@
   %7D%0A  %7D%0A
+  
 %0A  @Test
@@ -1978,16 +1978,22 @@
 adSafe()
+%0A     
  throws 
@@ -2363,16 +2363,18 @@
 k);%0A  %7D%0A
+  
 %0A  /**%0A 
@@ -2439,16 +2439,17 @@
 ons%0A   *
+ 
 %0A   * @t
@@ -2533,16 +2533,22 @@
 cessed()
+%0A     
  throws 
@@ -2997,16 +2997,17 @@
       //
+ 
 expected
@@ -3087,11 +3087,13 @@
 %7D);%0A  %7D%0A
+  
 %0A%7D%0A

remove unused imports