From 963b1eae1c41c0a6f58d41b005516010c2e7fdb8 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Sat, 2 May 2015 20:53:35 -0400 Subject: [PATCH] fixed unit test Former-commit-id: d4560b518805dcdf20d17f92c7b214dad2fa9676 --- .../analyzer/PythonPackageAnalyzerTest.java | 115 +++++++++++++----- 1 file changed, 82 insertions(+), 33 deletions(-) diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/PythonPackageAnalyzerTest.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/PythonPackageAnalyzerTest.java index d6a7ad425..0b13dd153 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/PythonPackageAnalyzerTest.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/PythonPackageAnalyzerTest.java @@ -24,9 +24,14 @@ import java.util.Arrays; import java.util.HashSet; import org.apache.commons.lang.StringUtils; +import org.junit.After; +import static org.junit.Assert.assertTrue; +import org.junit.Before; import org.junit.Test; import org.owasp.dependencycheck.BaseTest; import org.owasp.dependencycheck.analyzer.exception.AnalysisException; +import org.owasp.dependencycheck.dependency.Dependency; +import org.owasp.dependencycheck.dependency.Evidence; /** * Unit tests for PythonPackageAnalyzer. @@ -35,39 +40,83 @@ import org.owasp.dependencycheck.analyzer.exception.AnalysisException; */ public class PythonPackageAnalyzerTest extends BaseTest { - /** - * Test of getName method, of class PythonPackageAnalyzer. - */ - @Test - public void testGetName() { - assertEquals("Analyzer name wrong.", "Python Distribution Analyzer", - new PythonDistributionAnalyzer().getName()); - } + /** + * The package analyzer to test. + */ + PythonPackageAnalyzer analyzer; - /** - * Test of getSupportedExtensions method, of class PythonPackageAnalyzer. - */ - @Test - public void testGetSupportedExtensions() { - final String[] expected = { "py" }; - assertEquals("Supported extensions should just have the following: " - + StringUtils.join(expected, ", "), - new HashSet(Arrays.asList(expected)), - new PythonPackageAnalyzer().getSupportedExtensions()); - } + /** + * Setup the PtyhonPackageAnalyzer. + * + * @throws Exception if there is a problem + */ + @Before + public void setUp() throws Exception { + analyzer = new PythonPackageAnalyzer(); + analyzer.setFilesMatched(true); + analyzer.initialize(); + } - /** - * Test of supportsExtension method, of class PythonPackageAnalyzer. - */ - @Test - public void testSupportsExtension() { - assertTrue("Should support \"py\" extension.", - new PythonPackageAnalyzer().supportsExtension("py")); - } + /** + * Cleanup any resources used. + * + * @throws Exception if there is a problem + */ + @After + public void tearDown() throws Exception { + analyzer.close(); + analyzer = null; + } - @Test - public void testAnalyzeSourceMetadata() throws AnalysisException { - PythonDistributionAnalyzerTest.eggtestAssertions(this, - "python/eggtest/__init__.py", new PythonPackageAnalyzer()); - } -} \ No newline at end of file + /** + * Test of getName method, of class PythonPackageAnalyzer. + */ + @Test + public void testGetName() { + assertEquals("Analyzer name wrong.", "Python Package Analyzer", + analyzer.getName()); + } + + /** + * Test of getSupportedExtensions method, of class PythonPackageAnalyzer. + */ + @Test + public void testGetSupportedExtensions() { + final String[] expected = {"py"}; + assertEquals("Supported extensions should just have the following: " + + StringUtils.join(expected, ", "), + new HashSet(Arrays.asList(expected)), + analyzer.getSupportedExtensions()); + } + + /** + * Test of supportsExtension method, of class PythonPackageAnalyzer. + */ + @Test + public void testSupportsExtension() { + assertTrue("Should support \"py\" extension.", + analyzer.supportsExtension("py")); + } + + @Test + public void testAnalyzeSourceMetadata() throws AnalysisException { + eggtestAssertions(this, + "python/eggtest/__init__.py"); + } + + public void eggtestAssertions(Object context, final String resource) throws AnalysisException { + boolean found = false; + final Dependency result = new Dependency(BaseTest.getResourceAsFile( + context, resource)); + analyzer.analyze(result, null); + assertTrue("Expected vendor evidence to contain \"example\".", result + .getVendorEvidence().toString().contains("example")); + for (final Evidence e : result.getVersionEvidence()) { + if ("0.0.1".equals(e.getValue())) { + found = true; + break; + } + } + assertTrue("Version 0.0.1 not found in EggTest dependency.", found); + } +}