View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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.analyzer;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.owasp.dependencycheck.BaseTest;
23  import org.owasp.dependencycheck.Engine;
24  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
25  import org.owasp.dependencycheck.dependency.Dependency;
26  import org.owasp.dependencycheck.suppression.SuppressionParseException;
27  import org.owasp.dependencycheck.suppression.SuppressionRule;
28  import org.owasp.dependencycheck.utils.Settings;
29  import org.slf4j.LoggerFactory;
30  
31  import java.net.MalformedURLException;
32  import java.net.URISyntaxException;
33  import java.util.List;
34  import java.util.Set;
35  
36  import static org.junit.Assert.assertNull;
37  import static org.junit.Assert.assertTrue;
38  
39  /**
40   * @author Jeremy Long
41   */
42  public class AbstractSuppressionAnalyzerTest extends BaseTest {
43  
44      private AbstractSuppressionAnalyzer instance;
45  
46      @Before
47      public void createObjectUnderTest() throws Exception {
48          instance = new AbstractSuppressionAnalyzerImpl();
49      }
50  
51      /**
52       * Test of getSupportedExtensions method, of class AbstractSuppressionAnalyzer.
53       */
54      @Test
55      public void testGetSupportedExtensions() {
56          Set<String> result = instance.getSupportedExtensions();
57          assertNull(result);
58      }
59  
60      /**
61       * Test of getRules method, of class AbstractSuppressionAnalyzer for suppression file declared as URL.
62       */
63      @Test
64      public void testGetRulesFromSuppressionFileFromURL() throws Exception {
65          setSupressionFileFromURL();
66          instance.initialize();
67          int expCount = 5;
68          List<SuppressionRule> result = instance.getRules();
69          assertTrue(expCount <= result.size());
70      }
71  
72      /**
73       * Test of getRules method, of class AbstractSuppressionAnalyzer for suppression file declared as URL.
74       */
75      @Test
76      public void testGetRulesFromSuppressionFileInClasspath() throws Exception {
77          Settings.setString(Settings.KEYS.SUPPRESSION_FILE, "suppressions.xml");
78          instance.initialize();
79          int expCount = 5;
80          List<SuppressionRule> result = instance.getRules();
81          assertTrue(expCount <= result.size());
82      }
83  
84      @Test(expected = SuppressionParseException.class)
85      public void testFailureToLocateSuppressionFileAnywhere() throws Exception {
86          Settings.setString(Settings.KEYS.SUPPRESSION_FILE, "doesnotexist.xml");
87          instance.initialize();
88      }
89  
90      private void setSupressionFileFromURL() throws Exception {
91          try {
92              final String uri = this.getClass().getClassLoader().getResource("suppressions.xml").toURI().toURL().toString();
93              Settings.setString(Settings.KEYS.SUPPRESSION_FILE, uri);
94          } catch (URISyntaxException ex) {
95              LoggerFactory.getLogger(AbstractSuppressionAnalyzerTest.class).error("", ex);
96          } catch (MalformedURLException ex) {
97              LoggerFactory.getLogger(AbstractSuppressionAnalyzerTest.class).error("", ex);
98          }
99      }
100 
101     public class AbstractSuppressionAnalyzerImpl extends AbstractSuppressionAnalyzer {
102 
103         @Override
104         public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
105             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
106         }
107 
108         @Override
109         public String getName() {
110             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
111         }
112 
113         @Override
114         public AnalysisPhase getAnalysisPhase() {
115             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
116         }
117     }
118 
119 }