/*
 * This file is part of dependency-check-ant.
 *
 * 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.
 *
 * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 */
package org.owasp.dependencycheck.taskdefs;

import java.io.File;
import org.apache.tools.ant.BuildFileTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.owasp.dependencycheck.data.nvdcve.BaseDBTestCase;
import org.owasp.dependencycheck.utils.Settings;

/**
 *
 * @author Jeremy Long
 */
public class DependencyCheckTaskTest extends BuildFileTest {
    //TODO: The use of deprecated class BuildFileTestcan possibly
    //be replaced with BuildFileRule. However, it currently isn't included in the ant-testutil jar.
    //This should be fixed in ant-testutil 1.9.5, so we can check back once that has been released.
    //Reference: http://mail-archives.apache.org/mod_mbox/ant-user/201406.mbox/%3C000001cf87ba$8949b690$9bdd23b0$@de%3E

    @Before
    @Override
    public void setUp() throws Exception {
        Settings.initialize();
        BaseDBTestCase.ensureDBExists();
        final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath();
        configureProject(buildFile);
    }

    @After
    @Override
    public void tearDown() {
        //no cleanup...
        //executeTarget("cleanup");
        Settings.cleanup(true);
    }

    /**
     * Test of addFileSet method, of class DependencyCheckTask.
     */
    @Test
    public void testAddFileSet() throws Exception {
        File report = new File("target/dependency-check-report.html");
        if (report.exists()) {
            if (!report.delete()) {
                throw new Exception("Unable to delete 'target/DependencyCheck-Report.html' prior to test.");
            }
        }
        executeTarget("test.fileset");

        assertTrue("DependencyCheck report was not generated", report.exists());

    }

    /**
     * Test of addFileList method, of class DependencyCheckTask.
     *
     * @throws Exception
     */
    @Test
    public void testAddFileList() throws Exception {
        File report = new File("target/dependency-check-report.xml");
        if (report.exists()) {
            if (!report.delete()) {
                throw new Exception("Unable to delete 'target/DependencyCheck-Report.xml' prior to test.");
            }
        }
        executeTarget("test.filelist");

        assertTrue("DependencyCheck report was not generated", report.exists());
    }

    /**
     * Test of addDirSet method, of class DependencyCheckTask.
     *
     * @throws Exception
     */
    @Test
    public void testAddDirSet() throws Exception {
        File report = new File("target/dependency-check-vulnerability.html");
        if (report.exists()) {
            if (!report.delete()) {
                throw new Exception("Unable to delete 'target/DependencyCheck-Vulnerability.html' prior to test.");
            }
        }
        executeTarget("test.dirset");
        assertTrue("DependencyCheck report was not generated", report.exists());
    }

    /**
     * Test of getFailBuildOnCVSS method, of class DependencyCheckTask.
     */
    @Test
    public void testGetFailBuildOnCVSS() {
        expectBuildException("failCVSS", "asdfasdfscore");
        System.out.println(this.getOutput());
    }
}

@@ -728,16 +728,61 @@
 io.File;
+%0A%0Aimport org.apache.tools.ant.BuildException;
 %0Aimport 
@@ -811,20 +811,20 @@
 uildFile
-Test
+Rule
 ;%0Aimport
@@ -883,20 +883,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0Aimport
@@ -1065,16 +1065,61 @@
 tings;%0A%0A
+import static org.junit.Assert.assertTrue;%0A%0A%0A
 /**%0A *%0A 
@@ -1185,443 +1185,176 @@
 est 
-extends BuildFileTest %7B%0A    //TODO: The use of deprecated class BuildFileTestcan possibly%0A    //be replaced with BuildFileRule. However, it currently isn't included in the ant-testutil jar.%0A    //This should be fixed in ant-testutil 1.9.5, so we can check back once that has been released.%0A    //Reference: http://mail-archives.apache.org/mod_mbox/ant-user/201406.mbox/%253C000001cf87ba$8949b690$9bdd23b0$@de%253E%0A%0A    @Before%0A    @Overrid
+%7B%0A%0A    @Rule%0A    public BuildFileRule buildFile = new BuildFileRule();%0A%0A    @Rule%0A    public ExpectedException expectedException = ExpectedException.none();%0A%0A    @Befor
 e%0A  
@@ -1576,16 +1576,31 @@
         
+this.buildFile.
 configur
@@ -1642,22 +1642,8 @@
 ter%0A
-    @Override%0A
     
@@ -2179,32 +2179,42 @@
       %7D%0A        
+buildFile.
 executeTarget(%22t
@@ -2763,32 +2763,42 @@
       %7D%0A        
+buildFile.
 executeTarget(%22t
@@ -3367,16 +3367,26 @@
         
+buildFile.
 executeT
@@ -3654,96 +3654,92 @@
 pect
-BuildException(%22failCVSS%22, %22asdfasdfscore%22);%0A        System.out.println(this.getOutput()
+edException.expect(BuildException.class);%0A        buildFile.executeTarget(%22failCVSS%22
 );%0A 

Replace deprecated use of extending BuildFileTest with BuildFileRule now that the latter is available in ant-testutil