improved downloading of CPE data

This commit is contained in:
Jeremy Long
2012-09-14 06:14:54 -04:00
parent 46f0f891f3
commit c089ac330a
9 changed files with 247 additions and 94 deletions

View File

@@ -21,16 +21,16 @@ import org.junit.Test;
* @author jeremy
*/
public class CPEQueryTest extends BaseIndexTestCase {
public CPEQueryTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
@@ -51,15 +51,15 @@ public class CPEQueryTest extends BaseIndexTestCase {
String expResult = "cpe:/a:apache:struts:2.1.2";
List<Entry> result = instance.searchCPE(vendor, product, version);
assertEquals(expResult, result.get(0).getName());
//TODO - yeah, not a very good test as the results are the same with or without weighting...
Set<String> productWeightings = new HashSet<String>(1);
productWeightings.add("struts2");
Set<String> vendorWeightings = new HashSet<String>(1);
vendorWeightings.add("apache");
result = instance.searchCPE(vendor, product, version,productWeightings,vendorWeightings);
result = instance.searchCPE(vendor, product, version, productWeightings, vendorWeightings);
assertEquals(expResult, result.get(0).getName());
vendor = "apache software foundation";
@@ -67,13 +67,13 @@ public class CPEQueryTest extends BaseIndexTestCase {
version = "2.3.1.2";
//yes, this isn't right. we verify this with another method later
expResult = "cpe:/a:apache:struts";
expResult = "cpe:/a:apache:struts";
result = instance.searchCPE(vendor, product, version);
boolean startsWith = result.get(0).getName().startsWith(expResult);
assertTrue("CPE does not begin with apache struts",startsWith);
assertTrue("CPE does not begin with apache struts", startsWith);
instance.close();
}
/**
* Tests of buildSearch of class CPEQuery.
* @throws IOException is thrown when an IO Exception occurs.
@@ -97,15 +97,15 @@ public class CPEQueryTest extends BaseIndexTestCase {
String queryText = instance.buildSearch(vendor, product, version, null, null);
String expResult = " product:( struts 2 core ) vendor:( apache software foundation ) version:(2.1.2)";
assertTrue(expResult.equals(queryText));
queryText = instance.buildSearch(vendor, product, version, null, productWeightings);
expResult = " product:( struts^5 struts2^5 2 core ) vendor:( apache software foundation ) version:(2.1.2^0.2 )";
assertTrue(expResult.equals(queryText));
queryText = instance.buildSearch(vendor, product, version,vendorWeightings,null);
queryText = instance.buildSearch(vendor, product, version, vendorWeightings, null);
expResult = " product:( struts 2 core ) vendor:( apache^5 software foundation ) version:(2.1.2^0.2 )";
assertTrue(expResult.equals(queryText));
queryText = instance.buildSearch(vendor, product, version, vendorWeightings, productWeightings);
expResult = " product:( struts^5 struts2^5 2 core ) vendor:( apache^5 software foundation ) version:(2.1.2^0.2 )";
assertTrue(expResult.equals(queryText));
@@ -126,7 +126,6 @@ public class CPEQueryTest extends BaseIndexTestCase {
assertFalse(instance.isOpen());
}
/**
* Test of determineCPE method, of class CPEQuery.
* @throws Exception is thrown when an exception occurs
@@ -143,7 +142,7 @@ public class CPEQueryTest extends BaseIndexTestCase {
instance.determineCPE(depends);
instance.close();
assertTrue(depends.getCPEs().contains(expResult));
assertTrue(depends.getCPEs().size()==1);
assertTrue(depends.getCPEs().size() == 1);
}
@@ -163,7 +162,7 @@ public class CPEQueryTest extends BaseIndexTestCase {
String expResult = "cpe:/a:apache:struts:2.1.2";
List<Entry> result = instance.searchCPE(vendor, product, version);
assertEquals(expResult, result.get(0).getName());
vendor = "apache software foundation";
product = "struts 2 core";
version = "2.3.1.2";
@@ -172,7 +171,7 @@ public class CPEQueryTest extends BaseIndexTestCase {
result = instance.searchCPE(vendor, product, version);
boolean startsWith = result.get(0).getName().startsWith(expResult);
assertTrue("CPE Does not start with apache struts.", startsWith);
instance.close();
}
@@ -187,22 +186,21 @@ public class CPEQueryTest extends BaseIndexTestCase {
String product = "struts 2 core";
String version = "2.1.2";
String expResult = "cpe:/a:apache:struts:2.1.2";
CPEQuery instance = new CPEQuery();
instance.open();
//TODO - yeah, not a very good test as the results are the same with or without weighting...
Set<String> productWeightings = new HashSet<String>(1);
productWeightings.add("struts2");
Set<String> vendorWeightings = new HashSet<String>(1);
vendorWeightings.add("apache");
List<Entry> result = instance.searchCPE(vendor, product, version,productWeightings,vendorWeightings);
List<Entry> result = instance.searchCPE(vendor, product, version, productWeightings, vendorWeightings);
assertEquals(expResult, result.get(0).getName());
instance.close();
}
}

View File

@@ -0,0 +1,103 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.cpe;
import org.codesecure.dependencycheck.data.BaseIndexTestCase;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.store.Directory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public class IndexTest extends BaseIndexTestCase {
public IndexTest(String testCase) {
super(testCase);
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of open method, of class Index.
*/
@Test
public void testOpen() {
System.out.println("open");
Index instance = new Index();
try {
instance.open();
} catch (IOException ex) {
fail(ex.getMessage());
}
try {
instance.close();
} catch (CorruptIndexException ex) {
fail(ex.getMessage());
} catch (IOException ex) {
fail(ex.getMessage());
}
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Directory result = Index.getDirectory();
String exp = "\\target\\store\\cpe";
// TODO review the generated test code and remove the default call to fail.
assertTrue(result.toString().contains(exp));
}
/**
* Test of updateIndexFromWeb method, of class Index.
*/
@Test
public void testUpdateIndexFromWeb() throws Exception {
System.out.println("updateIndexFromWeb");
Index instance = new Index();
instance.updateIndexFromWeb();
}
/**
* Test of updateNeeded method, of class Index.
*/
@Test
public void testUpdateNeeded() throws Exception {
System.out.println("updateNeeded");
Index instance = new Index();
long expResult = 0L;
long result = instance.updateNeeded();
//if an exception is thrown this test fails. However, because it depends on the
// order of the tests what this will return I am just testing for the exception.
//assertTrue(expResult < result);
}
}