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) 2016 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.xml.hints;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.io.UnsupportedEncodingException;
28  import java.util.List;
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.parsers.SAXParser;
31  import javax.xml.parsers.SAXParserFactory;
32  import org.junit.Test;
33  import static org.junit.Assert.*;
34  import org.owasp.dependencycheck.BaseTest;
35  import org.owasp.dependencycheck.xml.suppression.SuppressionErrorHandler;
36  import org.owasp.dependencycheck.xml.suppression.SuppressionHandler;
37  import org.owasp.dependencycheck.xml.suppression.SuppressionParser;
38  import org.owasp.dependencycheck.xml.suppression.SuppressionRule;
39  import org.xml.sax.Attributes;
40  import org.xml.sax.InputSource;
41  import org.xml.sax.SAXException;
42  import org.xml.sax.SAXNotRecognizedException;
43  import org.xml.sax.SAXNotSupportedException;
44  import org.xml.sax.XMLReader;
45  
46  /**
47   *
48   * @author Jeremy Long
49   */
50  public class HintHandlerTest extends BaseTest {
51      
52      @Test
53      public void testHandler() throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException, FileNotFoundException, UnsupportedEncodingException, IOException {
54          File file = BaseTest.getResourceAsFile(this, "hints.xml");
55          File schema = BaseTest.getResourceAsFile(this, "schema/dependency-hint.1.1.xsd");
56          HintHandler handler = new HintHandler();
57  
58          SAXParserFactory factory = SAXParserFactory.newInstance();
59          factory.setNamespaceAware(true);
60          factory.setValidating(true);
61          SAXParser saxParser = factory.newSAXParser();
62          saxParser.setProperty(HintParser.JAXP_SCHEMA_LANGUAGE, HintParser.W3C_XML_SCHEMA);
63          saxParser.setProperty(HintParser.JAXP_SCHEMA_SOURCE, schema);
64          XMLReader xmlReader = saxParser.getXMLReader();
65          xmlReader.setErrorHandler(new HintErrorHandler());
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          xmlReader.parse(in);
72  
73          List<HintRule> result = handler.getHintRules();
74          assertEquals("two hint rules should have been loaded",2,result.size());
75      }
76  
77  }