location of data files is no longer the working directory, rather the location of the JAR file itself

Former-commit-id: b3f83447f9422618d6db60d49488830381787110
This commit is contained in:
Jeremy Long
2012-11-12 12:19:39 -05:00
parent 965687186c
commit 3fb9390040
7 changed files with 149 additions and 51 deletions

14
pom.xml
View File

@@ -206,13 +206,21 @@ along with DependencyCheck. If not, see <http://www.gnu.org/licenses/>.
<value>${project.build.directory}/cobertura/cobertura.ser</value> <value>${project.build.directory}/cobertura/cobertura.ser</value>
<workingDirectory>target</workingDirectory> <workingDirectory>target</workingDirectory>
</property> </property>
<property> <!--<property>
<name>cve</name> <name>cve</name>
<value>${project.build.directory}/data/cve</value> <value>${project.build.directory}/data/cve</value>
</property> </property>
<property> <property>
<name>cpe</name> <name>cpe</name>
<value>${project.build.directory}/data/cpe</value> <value>${project.build.directory}/data/cpe</value>
</property>-->
<property>
<name>cve</name>
<value>../data/cve</value>
</property>
<property>
<name>cpe</name>
<value>../data/cpe</value>
</property> </property>
</systemProperties> </systemProperties>
<excludes> <excludes>
@@ -337,9 +345,9 @@ along with DependencyCheck. If not, see <http://www.gnu.org/licenses/>.
<reportSets> <reportSets>
<reportSet> <reportSet>
<id>integration-tests</id> <id>integration-tests</id>
<reports> <!--<reports>
<report>failsafe-report-only</report> <report>failsafe-report-only</report>
</reports> </reports>-->
</reportSet> </reportSet>
</reportSets> </reportSets>
</plugin> </plugin>

View File

@@ -28,6 +28,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@@ -75,7 +76,11 @@ public class Index extends AbstractIndex implements CachedWebDataSource {
*/ */
public Directory getDirectory() throws IOException { public Directory getDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CPE_INDEX); String fileName = Settings.getString(Settings.KEYS.CPE_INDEX);
File path = new File(fileName); String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File path = new File(decodedPath + File.separator + fileName);
path = new File(path.getCanonicalPath());
Directory dir = FSDirectory.open(path); Directory dir = FSDirectory.open(path);
return dir; return dir;

View File

@@ -21,6 +21,7 @@ package org.codesecure.dependencycheck.data.nvdcve;
import java.io.*; import java.io.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -73,8 +74,13 @@ public class Index extends AbstractIndex implements CachedWebDataSource {
*/ */
public Directory getDirectory() throws IOException { public Directory getDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CVE_INDEX); String fileName = Settings.getString(Settings.KEYS.CVE_INDEX);
File path = new File(fileName); String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File path = new File(decodedPath + File.separator + fileName);
path = new File(path.getCanonicalPath());
Directory dir = FSDirectory.open(path); Directory dir = FSDirectory.open(path);
return dir; return dir;
} }

View File

@@ -40,35 +40,6 @@ public class IndexIntegrationTest extends BaseIndexTestCase {
public void tearDown() { 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());
}
instance.close();
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Index index = new Index();
Directory result = index.getDirectory();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cpe";
// TODO review the generated test code and remove the default call to fail.
assertTrue(result.toString().contains(exp));
}
/** /**
* Test of update method, of class Index. * Test of update method, of class Index.
*/ */

View File

@@ -0,0 +1,71 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.cpe;
import java.io.File;
import java.io.IOException;
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());
}
instance.close();
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Index index = new Index();
Directory result = index.getDirectory();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cpe";
// TODO review the generated test code and remove the default call to fail.
assertTrue(result.toString().contains(exp));
}
}

View File

@@ -16,7 +16,7 @@ import org.junit.*;
* @author Jeremy * @author Jeremy
*/ */
public class IndexIntegrationTest extends BaseIndexTestCase { public class IndexIntegrationTest extends BaseIndexTestCase {
public IndexIntegrationTest(String testName) { public IndexIntegrationTest(String testName) {
super(testName); super(testName);
} }
@@ -28,11 +28,11 @@ public class IndexIntegrationTest extends BaseIndexTestCase {
@AfterClass @AfterClass
public static void tearDownClass() throws Exception { public static void tearDownClass() throws Exception {
} }
@Before @Before
public void setUp() { public void setUp() {
} }
@After @After
public void tearDown() { public void tearDown() {
} }
@@ -45,19 +45,7 @@ public class IndexIntegrationTest extends BaseIndexTestCase {
System.out.println("retrieveCurrentTimestampFromWeb"); System.out.println("retrieveCurrentTimestampFromWeb");
Index instance = new Index(); Index instance = new Index();
Map<String, Index.NvdCveUrl> result = instance.retrieveCurrentTimestampsFromWeb(); Map<String, Index.NvdCveUrl> result = instance.retrieveCurrentTimestampsFromWeb();
assertEquals(12, result.size()); assertEquals(12, result.size());
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Index instance = new Index();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cve";
Directory result = instance.getDirectory();
assertTrue(result.toString().contains(exp));
} }
/** /**
@@ -81,5 +69,4 @@ public class IndexIntegrationTest extends BaseIndexTestCase {
//if an exception is thrown this test fails. However, because it depends on the //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. // order of the tests what this will return I am just testing for the exception.
} }
} }

View File

@@ -0,0 +1,50 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.nvdcve;
import java.io.File;
import java.util.Map;
import org.apache.lucene.store.Directory;
import static org.junit.Assert.assertTrue;
import org.junit.*;
/**
*
* @author Jeremy
*/
public class IndexTest extends BaseIndexTestCase {
public IndexTest(String testName) {
super(testName);
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Index instance = new Index();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cve";
Directory result = instance.getDirectory();
assertTrue(result.toString().contains(exp));
}
}