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.xml.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 static org.junit.Assert.assertTrue;
29  import org.junit.Test;
30  import org.owasp.dependencycheck.BaseTest;
31  import org.owasp.dependencycheck.utils.XmlUtils;
32  import org.xml.sax.InputSource;
33  import org.xml.sax.XMLReader;
34  
35  /**
36   *
37   * @author Jeremy Long
38   */
39  public class SuppressionHandlerTest extends BaseTest {
40  
41      /**
42       * Test of getSuppressionRules method, of class SuppressionHandler.
43       *
44       * @throws Exception thrown if there is an exception....
45       */
46      @Test
47      public void testHandler() throws Exception {
48          File file = BaseTest.getResourceAsFile(this, "suppressions.xml");
49          InputStream schemaStream = BaseTest.getResourceAsStream(this, "schema/suppression.xsd");
50  
51          SuppressionHandler handler = new SuppressionHandler();
52          SAXParser saxParser = XmlUtils.buildSecureSaxParser(schemaStream);
53          XMLReader xmlReader = saxParser.getXMLReader();
54          xmlReader.setErrorHandler(new SuppressionErrorHandler());
55          xmlReader.setContentHandler(handler);
56  
57          InputStream inputStream = new FileInputStream(file);
58          Reader reader = new InputStreamReader(inputStream, "UTF-8");
59          InputSource in = new InputSource(reader);
60          //in.setEncoding("UTF-8");
61  
62          xmlReader.parse(in);
63  
64          List<SuppressionRule> result = handler.getSuppressionRules();
65          assertTrue(result.size() > 3);
66          int baseCount = 0;
67          for (SuppressionRule r : result) {
68              if (r.isBase()) {
69                  baseCount++;
70              }
71          }
72          assertTrue(baseCount > 0);
73      }
74  }