View Javadoc
1   /*
2    * This file is part of dependency-check-ant.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.taskdefs;
19  
20  import java.io.File;
21  import org.apache.tools.ant.BuildFileTest;
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.owasp.dependencycheck.data.nvdcve.BaseDBTestCase;
26  import org.owasp.dependencycheck.utils.Settings;
27  
28  /**
29   *
30   * @author Jeremy Long
31   */
32  public class DependencyCheckTaskTest extends BuildFileTest {
33      //TODO: The use of deprecated class BuildFileTestcan possibly
34      //be replaced with BuildFileRule. However, it currently isn't included in the ant-testutil jar.
35      //This should be fixed in ant-testutil 1.9.5, so we can check back once that has been released.
36      //Reference: http://mail-archives.apache.org/mod_mbox/ant-user/201406.mbox/%3C000001cf87ba$8949b690$9bdd23b0$@de%3E
37  
38      @Before
39      @Override
40      public void setUp() throws Exception {
41          Settings.initialize();
42          BaseDBTestCase.ensureDBExists();
43          final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath();
44          configureProject(buildFile);
45      }
46  
47      @After
48      @Override
49      public void tearDown() {
50          //no cleanup...
51          //executeTarget("cleanup");
52          Settings.cleanup(true);
53      }
54  
55      /**
56       * Test of addFileSet method, of class DependencyCheckTask.
57       */
58      @Test
59      public void testAddFileSet() throws Exception {
60          File report = new File("target/dependency-check-report.html");
61          if (report.exists()) {
62              if (!report.delete()) {
63                  throw new Exception("Unable to delete 'target/DependencyCheck-Report.html' prior to test.");
64              }
65          }
66          executeTarget("test.fileset");
67  
68          assertTrue("DependencyCheck report was not generated", report.exists());
69  
70      }
71  
72      /**
73       * Test of addFileList method, of class DependencyCheckTask.
74       *
75       * @throws Exception
76       */
77      @Test
78      public void testAddFileList() throws Exception {
79          File report = new File("target/dependency-check-report.xml");
80          if (report.exists()) {
81              if (!report.delete()) {
82                  throw new Exception("Unable to delete 'target/DependencyCheck-Report.xml' prior to test.");
83              }
84          }
85          executeTarget("test.filelist");
86  
87          assertTrue("DependencyCheck report was not generated", report.exists());
88      }
89  
90      /**
91       * Test of addDirSet method, of class DependencyCheckTask.
92       *
93       * @throws Exception
94       */
95      @Test
96      public void testAddDirSet() throws Exception {
97          File report = new File("target/dependency-check-vulnerability.html");
98          if (report.exists()) {
99              if (!report.delete()) {
100                 throw new Exception("Unable to delete 'target/DependencyCheck-Vulnerability.html' prior to test.");
101             }
102         }
103         executeTarget("test.dirset");
104         assertTrue("DependencyCheck report was not generated", report.exists());
105     }
106 
107     /**
108      * Test of getFailBuildOnCVSS method, of class DependencyCheckTask.
109      */
110     @Test
111     public void testGetFailBuildOnCVSS() {
112         expectBuildException("failCVSS", "asdfasdfscore");
113         System.out.println(this.getOutput());
114     }
115 }