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) 2014 Jeremy Long. All Rights Reserved. 17 */ 18 package org.owasp.dependencycheck.data.nuget; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.InputStream; 22 import java.io.PrintStream; 23 import static org.junit.Assert.assertEquals; 24 import org.junit.Test; 25 import org.owasp.dependencycheck.BaseTest; 26 27 /** 28 * 29 * @author colezlaw 30 * 31 */ 32 public class XPathNuspecParserTest extends BaseTest { 33 34 /** 35 * Test all the valid components. 36 * 37 * @throws Exception if anything goes sideways. 38 */ 39 @Test testGoodDocument()40 public void testGoodDocument() throws Exception { 41 NuspecParser parser = new XPathNuspecParser(); 42 //InputStream is = XPathNuspecParserTest.class.getClassLoader().getResourceAsStream("log4net.2.0.3.nuspec"); 43 InputStream is = BaseTest.getResourceAsStream(this, "log4net.2.0.3.nuspec"); 44 NugetPackage np = parser.parse(is); 45 assertEquals("log4net", np.getId()); 46 assertEquals("2.0.3", np.getVersion()); 47 assertEquals("log4net [1.2.13]", np.getTitle()); 48 assertEquals("Apache Software Foundation", np.getAuthors()); 49 assertEquals("Apache Software Foundation", np.getOwners()); 50 assertEquals("http://logging.apache.org/log4net/license.html", np.getLicenseUrl()); 51 } 52 53 /** 54 * Expect a NuspecParseException when what we pass isn't even XML. 55 * 56 * @throws Exception we expect this. 57 */ 58 @Test(expected = NuspecParseException.class) testMissingDocument()59 public void testMissingDocument() throws Exception { 60 NuspecParser parser = new XPathNuspecParser(); 61 //InputStream is = XPathNuspecParserTest.class.getClassLoader().getResourceAsStream("dependencycheck.properties"); 62 InputStream is = BaseTest.getResourceAsStream(this, "dependencycheck.properties"); 63 64 //hide the fatal message from the core parser 65 final ByteArrayOutputStream myOut = new ByteArrayOutputStream(); 66 System.setErr(new PrintStream(myOut)); 67 68 parser.parse(is); 69 } 70 71 /** 72 * Expect a NuspecParseException when it's valid XML, but not a Nuspec. 73 * 74 * @throws Exception we expect this. 75 */ 76 @Test(expected = NuspecParseException.class) testNotNuspec()77 public void testNotNuspec() throws Exception { 78 NuspecParser parser = new XPathNuspecParser(); 79 //InputStream is = XPathNuspecParserTest.class.getClassLoader().getResourceAsStream("suppressions.xml"); 80 InputStream is = BaseTest.getResourceAsStream(this, "suppressions.xml"); 81 parser.parse(is); 82 } 83 } 84