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.suppression;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.util.List;
26  import javax.xml.parsers.SAXParser;
27  import javax.xml.parsers.SAXParserFactory;
28  import org.junit.After;
29  import org.junit.AfterClass;
30  import static org.junit.Assert.assertTrue;
31  import org.junit.Before;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.owasp.dependencycheck.BaseTest;
35  import org.xml.sax.InputSource;
36  import org.xml.sax.XMLReader;
37  
38  /**
39   *
40   * @author Jeremy Long
41   */
42  public class SuppressionHandlerTest {
43  
44      public SuppressionHandlerTest() {
45      }
46  
47      @BeforeClass
48      public static void setUpClass() {
49      }
50  
51      @AfterClass
52      public static void tearDownClass() {
53      }
54  
55      @Before
56      public void setUp() {
57      }
58  
59      @After
60      public void tearDown() {
61      }
62  
63      /**
64       * Test of getSuppressionRules method, of class SuppressionHandler.
65       *
66       * @throws Exception thrown if there is an exception....
67       */
68      @Test
69      public void testHandler() throws Exception {
70          //File file = new File(this.getClass().getClassLoader().getResource("suppressions.xml").getPath());
71          File file = BaseTest.getResourceAsFile(this, "suppressions.xml");
72  
73          //File schema = new File(this.getClass().getClassLoader().getResource("schema/suppression.xsd").getPath());
74          File schema = BaseTest.getResourceAsFile(this, "schema/suppression.xsd");
75          SuppressionHandler handler = new SuppressionHandler();
76  
77          SAXParserFactory factory = SAXParserFactory.newInstance();
78          factory.setNamespaceAware(true);
79          factory.setValidating(true);
80          SAXParser saxParser = factory.newSAXParser();
81          saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_LANGUAGE, SuppressionParser.W3C_XML_SCHEMA);
82          saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_SOURCE, schema);
83          XMLReader xmlReader = saxParser.getXMLReader();
84          xmlReader.setErrorHandler(new SuppressionErrorHandler());
85          xmlReader.setContentHandler(handler);
86  
87          InputStream inputStream = new FileInputStream(file);
88          Reader reader = new InputStreamReader(inputStream, "UTF-8");
89          InputSource in = new InputSource(reader);
90          //in.setEncoding("UTF-8");
91  
92          xmlReader.parse(in);
93  
94          List<SuppressionRule> result = handler.getSuppressionRules();
95          assertTrue(result.size() > 3);
96          int baseCount = 0;
97          for (SuppressionRule r : result) {
98              if (r.isBase()) {
99                  baseCount++;
100             }
101         }
102         assertTrue(baseCount > 0);
103 
104     }
105 }