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 extends BaseTest {
43  
44      /**
45       * Test of getSuppressionRules method, of class SuppressionHandler.
46       *
47       * @throws Exception thrown if there is an exception....
48       */
49      @Test
50      public void testHandler() throws Exception {
51          //File file = new File(this.getClass().getClassLoader().getResource("suppressions.xml").getPath());
52          File file = BaseTest.getResourceAsFile(this, "suppressions.xml");
53  
54          //File schema = new File(this.getClass().getClassLoader().getResource("schema/suppression.xsd").getPath());
55          File schema = BaseTest.getResourceAsFile(this, "schema/suppression.xsd");
56          SuppressionHandler handler = new SuppressionHandler();
57  
58          SAXParserFactory factory = SAXParserFactory.newInstance();
59          factory.setNamespaceAware(true);
60          factory.setValidating(true);
61          SAXParser saxParser = factory.newSAXParser();
62          saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_LANGUAGE, SuppressionParser.W3C_XML_SCHEMA);
63          saxParser.setProperty(SuppressionParser.JAXP_SCHEMA_SOURCE, schema);
64          XMLReader xmlReader = saxParser.getXMLReader();
65          xmlReader.setErrorHandler(new SuppressionErrorHandler());
66          xmlReader.setContentHandler(handler);
67  
68          InputStream inputStream = new FileInputStream(file);
69          Reader reader = new InputStreamReader(inputStream, "UTF-8");
70          InputSource in = new InputSource(reader);
71          //in.setEncoding("UTF-8");
72  
73          xmlReader.parse(in);
74  
75          List<SuppressionRule> result = handler.getSuppressionRules();
76          assertTrue(result.size() > 3);
77          int baseCount = 0;
78          for (SuppressionRule r : result) {
79              if (r.isBase()) {
80                  baseCount++;
81              }
82          }
83          assertTrue(baseCount > 0);
84  
85      }
86  }