/*
 * Copyright (C) 2014,2015 The Cat Hive Developers.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cathive.sass;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.junit.After;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;

/**
 * @see com.cathive.sass.SassTask
 */
public class SassTaskTest {

    private Path workingDirectory;
    private Path simpleScssPath;
    private Path complexScssPath;
    private Path includes1Path;
    private Path includes2Path;
    private Path buildFilePath;

    @Rule
    public final BuildFileRule buildRule = new BuildFileRule();

    @Before
    public void setUp() throws Exception {
        this.workingDirectory = Files.createTempDirectory("sass-java");
        this.simpleScssPath = this.workingDirectory.resolve("simple.scss");
        this.complexScssPath = this.workingDirectory.resolve("complex.scss");
        this.includes1Path = this.workingDirectory.resolve("includes1");
        this.includes2Path = this.workingDirectory.resolve("includes2");
        this.buildFilePath = this.workingDirectory.resolve("build.xml");

        Properties props = System.getProperties();
        props.setProperty("sass-java.test.workingdir", this.workingDirectory.toString());

        // Copies all the stuff that is needed for our tests to the temporary directory.
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("simple.scss"), this.simpleScssPath);
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("complex.scss"), this.complexScssPath);

        Files.createDirectories(includes1Path);
        Files.createDirectories(includes2Path);
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("includes1/_variables1.scss"), includes1Path.resolve("_variables1.scss"));
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("includes1/_common.scss"), includes1Path.resolve("_common.scss"));
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("includes2/_variables2.scss"), includes2Path.resolve("_variables2.scss"));
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("includes2/_common.scss"), includes2Path.resolve("_common.scss"));
        Files.copy(this.getClass().getClassLoader().getResourceAsStream("build.xml"), this.buildFilePath);
        buildRule.configureProject(this.buildFilePath.toString());
    }

    @After
    public void tearDown() throws Exception {
        Files.walkFileTree(this.workingDirectory, new SimpleFileVisitor<java.nio.file.Path>() {
            @Override
            public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }

    @Test
    public void testExecute() {
        testTask("test");
    }

    @Test
    public void testExecuteMissingIn() {
        try {
            buildRule.executeTarget("testInMissing");
            Assert.fail("BuildException should have been thrown");
        } catch (BuildException ex) {
            Assert.assertEquals("'in' must be set", ex.getMessage());
        }
    }

    @Test
    public void testWithPrecision() {
        testTask("testWithPrecision");
    }

    @Test
    public void testWithCompressedOutputstyle() {
        testTask("testWithCompressedOutputstyle");
    }

    @Test
    public void testWithSourcecomments() {
        testTask("testWithSourcecomments");
    }

    @Test
    public void testWithSourcemapembed() {
        testTask("testWithSourcemapembed");
    }

    @Test
    public void testWithSourcemapcontents() {
        testTask("testWithSourcemapcontents");
    }

    @Test
    public void testWithomitSourcemapurl() {
        testTask("testWithomitSourcemapurl");
    }

    @Test
    public void testWithSourcemapfile() {
        testTask("testWithSourcemapfile");
    }

    @Test
    public void testWithSourcemaproot() {
        testTask("testWithSourcemaproot");
    }

    /**
     * A helper for basic testing of Ant targets that expect to succeed.
     * It is expected that the Ant target performs a `clean` before running.
     *
     * @param targetName
     */
    private void testTask(final String targetName) {
        buildRule.executeTarget(targetName);
        Path outputPath = this.workingDirectory.resolve("output");
        Path expectedComplex = outputPath.resolve("complex.css");
        Path expectedSimple = outputPath.resolve("simple.css");
        Assert.assertTrue(expectedComplex.toFile().exists());
        Assert.assertTrue(expectedSimple.toFile().exists());
    }
}

@@ -1083,16 +1083,97 @@
 .Rule;%0A%0A
+import static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0A
 /**%0A * @
@@ -3317,656 +3317,8 @@
  %7D%0A%0A
-    @After%0A    public void tearDown() throws Exception %7B%0A        Files.walkFileTree(this.workingDirectory, new SimpleFileVisitor%3Cjava.nio.file.Path%3E() %7B%0A            @Override%0A            public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException %7B%0A                Files.delete(file);%0A                return FileVisitResult.CONTINUE;%0A            %7D%0A%0A            @Override%0A            public FileVisitResult postVisitDirectory(java.nio.file.Path dir, IOException exc) throws IOException %7B%0A                Files.delete(dir);%0A                return FileVisitResult.CONTINUE;%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     
@@ -3389,32 +3389,65 @@
     %7D%0A%0A    @Test
+(expected = BuildException.class)
 %0A    public void
@@ -3552,23 +3552,16 @@
         
-Assert.
 fail(%22Bu
@@ -3617,16 +3617,22 @@
  catch (
+final 
 BuildExc
@@ -3681,12 +3681,14 @@
 ls(%22
-'in'
+%5C%22in%5C%22
  mus
@@ -3712,24 +3712,46 @@
 Message());%0A
+            throw ex;%0A
         %7D%0A  
@@ -4759,40 +4759,8 @@
 ng.%0A
-     *%0A     * @param targetName%0A
     
@@ -4865,24 +4865,30 @@
 e);%0A        
+final 
 Path outputP
@@ -4934,32 +4934,38 @@
 tput%22);%0A        
+final 
 Path expectedCom
@@ -5014,16 +5014,22 @@
         
+final 
 Path exp
@@ -5076,39 +5076,32 @@
 .css%22);%0A        
-Assert.
 assertTrue(expec
@@ -5135,31 +5135,24 @@
 ));%0A        
-Assert.
 assertTrue(e

Fix issues with JUnit test.