refactored initialization of the analyzer

Former-commit-id: c9e32fbd039c87eafc25bf0bf62dad23c6a7279a
This commit is contained in:
Jeremy Long
2015-05-02 07:31:05 -04:00
parent 4f18e9ee7f
commit b6c0426c1c

View File

@@ -24,6 +24,8 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.owasp.dependencycheck.BaseTest; import org.owasp.dependencycheck.BaseTest;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
@@ -37,18 +39,32 @@ import org.owasp.dependencycheck.dependency.Evidence;
*/ */
public class PythonDistributionAnalyzerTest extends BaseTest { public class PythonDistributionAnalyzerTest extends BaseTest {
PythonDistributionAnalyzer analyzer;
@Before
public void setUp() throws Exception {
analyzer = new PythonDistributionAnalyzer();
analyzer.setFilesMatched(true);
analyzer.initialize();
}
@After
public void tearDown() throws Exception {
analyzer.close();
analyzer = null;
}
/** /**
* Test of getName method, of class PythonDistributionAnalyzer. * Test of getName method, of class PythonDistributionAnalyzer.
*/ */
@Test @Test
public void testGetName() { public void testGetName() {
assertEquals("Analyzer name wrong.", "Python Distribution Analyzer", assertEquals("Analyzer name wrong.", "Python Distribution Analyzer",
new PythonDistributionAnalyzer().getName()); analyzer.getName());
} }
/** /**
* Test of getSupportedExtensions method, of class * Test of getSupportedExtensions method, of class PythonDistributionAnalyzer.
* PythonDistributionAnalyzer.
*/ */
@Test @Test
public void testGetSupportedExtensions() { public void testGetSupportedExtensions() {
@@ -56,7 +72,7 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
assertEquals("Supported extensions should just have the following: " assertEquals("Supported extensions should just have the following: "
+ StringUtils.join(expected, ", "), + StringUtils.join(expected, ", "),
new HashSet<String>(Arrays.asList(expected)), new HashSet<String>(Arrays.asList(expected)),
new PythonDistributionAnalyzer().getSupportedExtensions()); analyzer.getSupportedExtensions());
} }
/** /**
@@ -64,7 +80,6 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
*/ */
@Test @Test
public void testSupportsExtension() { public void testSupportsExtension() {
final PythonDistributionAnalyzer analyzer = new PythonDistributionAnalyzer();
assertTrue("Should support \"whl\" extension.", assertTrue("Should support \"whl\" extension.",
analyzer.supportsExtension("whl")); analyzer.supportsExtension("whl"));
assertTrue("Should support \"egg\" extension.", assertTrue("Should support \"egg\" extension.",
@@ -80,8 +95,7 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test of inspect method, of class PythonDistributionAnalyzer.
* *
* @throws Exception * @throws Exception is thrown when an exception occurs.
* is thrown when an exception occurs.
*/ */
@Test @Test
public void testAnalyzeWheel() throws AnalysisException { public void testAnalyzeWheel() throws AnalysisException {
@@ -92,8 +106,7 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test of inspect method, of class PythonDistributionAnalyzer.
* *
* @throws Exception * @throws Exception is thrown when an exception occurs.
* is thrown when an exception occurs.
*/ */
@Test @Test
public void testAnalyzeSitePackage() throws AnalysisException { public void testAnalyzeSitePackage() throws AnalysisException {
@@ -106,10 +119,10 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
private void djangoAssertions(final Dependency result) private void djangoAssertions(final Dependency result)
throws AnalysisException { throws AnalysisException {
new PythonDistributionAnalyzer().analyze(result, null); boolean found = false;
analyzer.analyze(result, null);
assertTrue("Expected vendor evidence to contain \"djangoproject\".", assertTrue("Expected vendor evidence to contain \"djangoproject\".",
result.getVendorEvidence().toString().contains("djangoproject")); result.getVendorEvidence().toString().contains("djangoproject"));
boolean found = false;
for (final Evidence e : result.getVersionEvidence()) { for (final Evidence e : result.getVersionEvidence()) {
if ("Version".equals(e.getName()) && "1.7.2".equals(e.getValue())) { if ("Version".equals(e.getName()) && "1.7.2".equals(e.getValue())) {
found = true; found = true;
@@ -122,38 +135,33 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
@Test @Test
public void testAnalyzeEggInfoFolder() throws AnalysisException { public void testAnalyzeEggInfoFolder() throws AnalysisException {
eggtestAssertions(this, eggtestAssertions(this,
"python/site-packages/EggTest.egg-info/PKG-INFO", "python/site-packages/EggTest.egg-info/PKG-INFO");
new PythonDistributionAnalyzer());
} }
@Test @Test
public void testAnalyzeEggArchive() throws AnalysisException { public void testAnalyzeEggArchive() throws AnalysisException {
eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.egg", eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.egg");
new PythonDistributionAnalyzer());
} }
@Test @Test
public void testAnalyzeEggArchiveNamedZip() throws AnalysisException { public void testAnalyzeEggArchiveNamedZip() throws AnalysisException {
eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.zip", eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.zip");
new PythonDistributionAnalyzer());
} }
@Test @Test
public void testAnalyzeEggFolder() throws AnalysisException { public void testAnalyzeEggFolder() throws AnalysisException {
eggtestAssertions( eggtestAssertions(
this, this,
"python/site-packages/EggTest-0.0.1-py2.7.egg/EGG-INFO/PKG-INFO", "python/site-packages/EggTest-0.0.1-py2.7.egg/EGG-INFO/PKG-INFO");
new PythonDistributionAnalyzer());
} }
public static void eggtestAssertions(Object context, final String resource, public void eggtestAssertions(Object context, final String resource) throws AnalysisException {
Analyzer analyzer) throws AnalysisException { boolean found = false;
final Dependency result = new Dependency(BaseTest.getResourceAsFile( final Dependency result = new Dependency(BaseTest.getResourceAsFile(
context, resource)); context, resource));
analyzer.analyze(result, null); analyzer.analyze(result, null);
assertTrue("Expected vendor evidence to contain \"example\".", result assertTrue("Expected vendor evidence to contain \"example\".", result
.getVendorEvidence().toString().contains("example")); .getVendorEvidence().toString().contains("example"));
boolean found = false;
for (final Evidence e : result.getVersionEvidence()) { for (final Evidence e : result.getVersionEvidence()) {
if ("0.0.1".equals(e.getValue())) { if ("0.0.1".equals(e.getValue())) {
found = true; found = true;