updated to use directory of jar instead of working directory to store data

Former-commit-id: 93b0784cd0f38445c0550737b2377618f4ebee1e
This commit is contained in:
Jeremy Long
2012-11-12 14:48:39 -05:00
parent 5c83671739
commit aa795ee7eb
11 changed files with 28 additions and 46 deletions

13
pom.xml
View File

@@ -216,11 +216,11 @@ along with DependencyCheck. If not, see <http://www.gnu.org/licenses/>.
</property>-->
<property>
<name>cve</name>
<value>../data/cve</value>
<value>target/data/cve</value>
</property>
<property>
<name>cpe</name>
<value>../data/cpe</value>
<value>target/data/cpe</value>
</property>
</systemProperties>
<excludes>
@@ -236,11 +236,11 @@ along with DependencyCheck. If not, see <http://www.gnu.org/licenses/>.
<systemProperties>
<property>
<name>cve</name>
<value>../data/cve</value>
<value>target/data/cve</value>
</property>
<property>
<name>cpe</name>
<value>../data/cpe</value>
<value>target/data/cpe</value>
</property>
</systemProperties>
</configuration>
@@ -357,9 +357,10 @@ along with DependencyCheck. If not, see <http://www.gnu.org/licenses/>.
<reportSets>
<reportSet>
<id>integration-tests</id>
<!--<reports>
<reports>
<report>report-only</report>
<report>failsafe-report-only</report>
</reports>-->
</reports>
</reportSet>
</reportSets>
</plugin>

View File

@@ -22,7 +22,6 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.LogManager;
@@ -70,31 +69,11 @@ public class App {
app.run(args);
}
private static File getLoggingDirectory() throws IOException {
String fileName = "logs";
String filePath = App.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (!exePath.isDirectory()) {
exePath = exePath.getParentFile();
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());
return path;
}
private static void prepareLogger() {
//while java doc for JUL says to use preferences api - it throws an exception...
//Preferences.systemRoot().put("java.util.logging.config.file", "log.properties");
//System.getProperties().put("java.util.logging.config.file", "configuration/log.properties");
File dir;
try {
dir = getLoggingDirectory();
} catch (IOException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, "Unable to get default logging location, "
+ "using the working directory instead.", ex);
dir = new File("logs");
}
File dir = new File("logs");
if (!dir.exists()) {
dir.mkdir();

View File

@@ -93,8 +93,10 @@ public class Index extends AbstractIndex implements CachedWebDataSource {
String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (!exePath.isDirectory()) {
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());

View File

@@ -91,8 +91,10 @@ public class Index extends AbstractIndex implements CachedWebDataSource {
String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (!exePath.isDirectory()) {
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());

View File

@@ -20,4 +20,4 @@ java.util.logging.FileHandler.level=FINEST
# %g - generation number for rotating logs
# %u - unique number to avoid conflicts
# FileHandler writes to %h/demo0.log by default.
java.util.logging.FileHandler.pattern=./logs/DependencyCheck%g.log
java.util.logging.FileHandler.pattern=./logs/DependencyCheck%u.log

View File

@@ -34,11 +34,13 @@ public abstract class BaseIndexTestCase extends TestCase {
protected static File getDataDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CPE_INDEX);
String filePath = BaseIndexTestCase.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (!exePath.isDirectory()) {
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File(".");
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());

View File

@@ -13,8 +13,6 @@ import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.codesecure.dependencycheck.dependency.Dependency;
import org.codesecure.dependencycheck.analyzer.JarAnalyzer;
import org.codesecure.dependencycheck.dependency.Evidence;
import org.codesecure.dependencycheck.dependency.Evidence.Confidence;
import org.junit.Test;
/**
@@ -133,6 +131,7 @@ public class CPEAnalyzerTest extends BaseIndexTestCase {
expResult = "cpe:/a:apache:struts:2.3.1.2";
result = instance.searchCPE(vendor, product, version);
assertEquals(expResult, result.get(0).getName());
instance.close();
}

View File

@@ -36,11 +36,13 @@ public abstract class BaseIndexTestCase extends TestCase {
protected static File getDataDirectory() throws IOException {
String fileName = Settings.getString(Settings.KEYS.CVE_INDEX);
String filePath = BaseIndexTestCase.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String filePath = Index.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(filePath, "UTF-8");
File exePath = new File(decodedPath);
if (!exePath.isDirectory()) {
if (exePath.getName().toLowerCase().endsWith(".jar")) {
exePath = exePath.getParentFile();
} else {
exePath = new File( "." );
}
File path = new File(exePath.getCanonicalFile() + File.separator + fileName);
path = new File(path.getCanonicalPath());

View File

@@ -4,11 +4,8 @@
*/
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.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.*;
/**

View File

@@ -5,7 +5,6 @@
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.*;
@@ -45,8 +44,7 @@ public class IndexTest extends BaseIndexTestCase {
Index instance = new Index();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cve";
Directory result = instance.getDirectory();
assertTrue("Recieved '" + result.toString() + "' and excpected '" + exp + "'.",
result.toString().contains(exp));
assertTrue(result.toString().contains(exp));
}
}

View File

@@ -38,7 +38,7 @@ public class SettingsTest extends TestCase {
public void testGetString() {
System.out.println("getString");
String key = Settings.KEYS.CPE_INDEX;
String expResult = "../data/cpe";
String expResult = "target/data/cpe";
String result = Settings.getString(key);
assertTrue(result.endsWith(expResult));
}