package com.googlecode.jslint4java.maven;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;

// Urgh, JUnit 3. :(
public class JSLintMojoTest extends AbstractMojoTestCase {

    /** A directory containing a file with an error in. */
    private static final String BAD_JS = "bad-js";
    /** A directory containing javascript with no lint errors. */
    private static final String GOOD_JS = "good-js";
    private static final String POM_XML = "pom.xml";
    private File baseDir;
    private JSLintMojo mojo;
    private final FakeLog logger = new FakeLog();

    private File baseRelative(String child) {
        return new File(baseDir, child);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        URL pomResource = JSLintMojoTest.class.getResource(POM_XML);
        assertNotNull("Can't find '" + POM_XML + "' resource", pomResource);
        File pom = new File(pomResource.toURI());
        assertTrue(pom + " doesn't exist?", pom.exists());
        baseDir = pom.getParentFile();
        mojo = (JSLintMojo) lookupMojo("check", pom);
        mojo.setLog(logger);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testBasics() throws Exception {
        useGoodSource();
        mojo.execute();
    }

    public void testDefaultEncoding() {
        assertEquals("UTF-8", mojo.getEncoding());
    }

    public void testFailure() throws Exception {
        try {
            useBadSource();
            mojo.execute();
            fail("Should have one error.");
        } catch (MojoFailureException e) {
            assertEquals("JSLint found 1 problems in 1 files", e.getMessage());
        }
    }

    public void testLogToConsole() throws Exception {
        useBadSource();
        try {
            mojo.execute();
            fail("should have failed");
        } catch (MojoFailureException e) {
            assertTrue("we logged something", !logger.loggedItems.isEmpty());
            String expected = "bad.js:1:26: Expected ';' and instead saw '(end)'.";
            for (FakeLog.LogItem item : logger.loggedItems) {
                if (item.msg.toString().contains(expected)) {
                    assertTrue("Found expected log message", true);
                    return;
                }
            }
            fail("Didn't find error text in logs: " + expected);
        }

    }

    public void testOptions() throws Exception {
        useGoodSource();
        Map<String, String> options = new HashMap<String, String>();
        options.put("strict", "true");
        mojo.setOptions(options);
        try {
            mojo.execute();
            fail("should have failed due to lack of 'use strict'");
        } catch (MojoFailureException e) {
            assertTrue(true);
        }
    }

    // Check the stuff we specified is actually there.
    public void testOptionsFromPom() {
        Map<String, String> options = mojo.getOptions();
        assertEquals(1, options.size());
        assertEquals("true", options.get("undef"));
    }

    private void useBadSource() {
        mojo.setSourceDirectory(baseRelative(BAD_JS));
    }

    private void useGoodSource() {
        mojo.setSourceDirectory(baseRelative(GOOD_JS));
    }
}

@@ -57,16 +57,118 @@
 o.File;%0A
+import java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.net.URISyntaxException;%0A
 import j
@@ -811,16 +811,42 @@
 keLog();
+%0A    private File tempDir;
 %0A%0A    pr
@@ -941,461 +941,1247 @@
     
-@Override%0A    protec
+/**%0A     * Delete recursively.%0A     *%0A     * @see %7B@code http://stackoverflow.com/questions/779519/delete-files-recursively-in-java/779529#779529%7D%0A     */%0A    priva
 te
-d
  void 
-setUp() throws Exception %7B%0A        super.setUp();%0A        URL pomResource = JSLintMojoTest.class.getResource(POM_XML);%0A        assertNotNull(%22Can't find '%22 + POM_XML + %22' resource%22, pomResource);%0A        File pom = new File(pomResource.toURI());%0A        assertTrue(pom + %22 doesn't exist?%22, pom.exists());%0A        baseDir = pom.getParentFile();%0A        mojo = (JSLintMojo) lookupMojo(%22check%22, pom);%0A        mojo.setLog(logger
+delete(File f) throws FileNotFoundException %7B%0A        if (f.isDirectory()) %7B%0A            for (File kid : f.listFiles()) %7B%0A                delete(kid);%0A            %7D%0A        %7D%0A        if (!f.delete()) %7B%0A            throw new FileNotFoundException(%22Failed to delete file %22 + f);%0A        %7D%0A    %7D%0A%0A    private File getPom() throws URISyntaxException %7B%0A        URL pomResource = JSLintMojoTest.class.getResource(POM_XML);%0A        assertNotNull(%22Can't find '%22 + POM_XML + %22' resource%22, pomResource);%0A        File pom = new File(pomResource.toURI());%0A        assertTrue(pom + %22 doesn't exist?%22, pom.exists());%0A        return pom;%0A    %7D%0A%0A    @Override%0A    protected void setUp() throws Exception %7B%0A        super.setUp();%0A        setUpTempDir();%0A        File pom = getPom();%0A        baseDir = pom.getParentFile();%0A        mojo = (JSLintMojo) lookupMojo(%22check%22, pom);%0A        mojo.setLog(logger);%0A    %7D%0A%0A    private void setUpTempDir() throws IOException %7B%0A        tempDir = File.createTempFile(getClass().getName() + %22-%22, %22%22);%0A        tempDir.delete();%0A        tempDir.mkdir(
 );%0A 
@@ -2271,24 +2271,49 @@
 tearDown();%0A
+        delete(tempDir);%0A
     %7D%0A%0A    p

Add a temp directory.