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  
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.BuildFileRule;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Rule;
27  import org.junit.Test;
28  import org.junit.rules.ExpectedException;
29  import org.owasp.dependencycheck.BaseDBTestCase;
30  import org.owasp.dependencycheck.utils.Settings;
31  
32  import static org.junit.Assert.assertTrue;
33  
34  
35  /**
36   *
37   * @author Jeremy Long
38   */
39  public class DependencyCheckTaskTest {
40  
41      @Rule
42      public BuildFileRule buildFileRule = new BuildFileRule();
43  
44      @Rule
45      public ExpectedException expectedException = ExpectedException.none();
46  
47      @Before
48      public void setUp() throws Exception {
49          Settings.initialize();
50          BaseDBTestCase.ensureDBExists();
51          final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath();
52          buildFileRule.configureProject(buildFile);
53      }
54  
55      @After
56      public void tearDown() {
57          //no cleanup...
58          //executeTarget("cleanup");
59          Settings.cleanup(true);
60      }
61  
62      /**
63       * Test of addFileSet method, of class DependencyCheckTask.
64       */
65      @Test
66      public void testAddFileSet() throws Exception {
67          File report = new File("target/dependency-check-report.html");
68          if (report.exists()) {
69              if (!report.delete()) {
70                  throw new Exception("Unable to delete 'target/DependencyCheck-Report.html' prior to test.");
71              }
72          }
73          buildFileRule.executeTarget("test.fileset");
74  
75          assertTrue("DependencyCheck report was not generated", report.exists());
76  
77      }
78  
79      /**
80       * Test of addFileList method, of class DependencyCheckTask.
81       *
82       * @throws Exception
83       */
84      @Test
85      public void testAddFileList() throws Exception {
86          File report = new File("target/dependency-check-report.xml");
87          if (report.exists()) {
88              if (!report.delete()) {
89                  throw new Exception("Unable to delete 'target/DependencyCheck-Report.xml' prior to test.");
90              }
91          }
92          buildFileRule.executeTarget("test.filelist");
93  
94          assertTrue("DependencyCheck report was not generated", report.exists());
95      }
96  
97      /**
98       * Test of addDirSet method, of class DependencyCheckTask.
99       *
100      * @throws Exception
101      */
102     @Test
103     public void testAddDirSet() throws Exception {
104         File report = new File("target/dependency-check-vulnerability.html");
105         if (report.exists()) {
106             if (!report.delete()) {
107                 throw new Exception("Unable to delete 'target/DependencyCheck-Vulnerability.html' prior to test.");
108             }
109         }
110         buildFileRule.executeTarget("test.dirset");
111         assertTrue("DependencyCheck report was not generated", report.exists());
112     }
113 
114     /**
115      * Test of getFailBuildOnCVSS method, of class DependencyCheckTask.
116      */
117     @Test
118     public void testGetFailBuildOnCVSS() {
119         expectedException.expect(BuildException.class);
120         buildFileRule.executeTarget("failCVSS");
121     }
122 }