mirror of
https://github.com/ysoftdevs/DependencyCheck.git
synced 2026-03-24 01:51:49 +01:00
updated how initial test data is updated
Former-commit-id: c63f49d89c63446c9ed73800e8cdd17f0a977986
This commit is contained in:
@@ -18,30 +18,18 @@
|
|||||||
*/
|
*/
|
||||||
package org.owasp.dependencycheck.data.cpe;
|
package org.owasp.dependencycheck.data.cpe;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import junit.framework.TestCase;
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipInputStream;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.owasp.dependencycheck.data.nvdcve.BaseDBTestCase;
|
import org.owasp.dependencycheck.data.nvdcve.BaseDBTestCase;
|
||||||
import org.owasp.dependencycheck.utils.Settings;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Jeremy Long (jeremy.long@owasp.org)
|
* @author Jeremy Long (jeremy.long@owasp.org)
|
||||||
*/
|
*/
|
||||||
public abstract class BaseIndexTestCase {
|
public abstract class BaseIndexTestCase extends TestCase {
|
||||||
|
|
||||||
protected static final int BUFFER_SIZE = 2048;
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUpClass() throws Exception {
|
public static void setUpClass() throws Exception {
|
||||||
@@ -52,88 +40,15 @@ public abstract class BaseIndexTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
ensureIndexExists();
|
|
||||||
BaseDBTestCase.ensureDBExists();
|
BaseDBTestCase.ensureDBExists();
|
||||||
|
super.setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
@Override
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
}
|
super.tearDown();
|
||||||
|
|
||||||
protected static File getDataDirectory() throws IOException {
|
|
||||||
final String fileName = Settings.getString(Settings.KEYS.CPE_DATA_DIRECTORY);
|
|
||||||
final String dataDirectory = Settings.getString(Settings.KEYS.DATA_DIRECTORY);
|
|
||||||
return new File(dataDirectory, fileName);
|
|
||||||
//return FileUtils.getDataDirectory(fileName, Index.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ensureIndexExists() throws Exception {
|
|
||||||
//String indexPath = Settings.getString(Settings.KEYS.CPE_DATA_DIRECTORY);
|
|
||||||
String indexPath = getDataDirectory().getCanonicalPath();
|
|
||||||
java.io.File f = new File(indexPath);
|
|
||||||
|
|
||||||
if (!f.exists() || (f.isDirectory() && f.listFiles().length == 0)) {
|
|
||||||
f.mkdirs();
|
|
||||||
FileInputStream fis = null;
|
|
||||||
ZipInputStream zin = null;
|
|
||||||
try {
|
|
||||||
File path = new File(BaseIndexTestCase.class.getClassLoader().getResource("index.cpe.zip").getPath());
|
|
||||||
fis = new FileInputStream(path);
|
|
||||||
zin = new ZipInputStream(new BufferedInputStream(fis));
|
|
||||||
ZipEntry entry;
|
|
||||||
while ((entry = zin.getNextEntry()) != null) {
|
|
||||||
if (entry.isDirectory()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
FileOutputStream fos = null;
|
|
||||||
BufferedOutputStream dest = null;
|
|
||||||
try {
|
|
||||||
File o = new File(indexPath, entry.getName());
|
|
||||||
o.createNewFile();
|
|
||||||
fos = new FileOutputStream(o, false);
|
|
||||||
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
|
|
||||||
byte data[] = new byte[BUFFER_SIZE];
|
|
||||||
int count;
|
|
||||||
while ((count = zin.read(data, 0, BUFFER_SIZE)) != -1) {
|
|
||||||
dest.write(data, 0, count);
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.getLogger(BaseIndexTestCase.class.getName()).log(Level.FINEST, null, ex);
|
|
||||||
} finally {
|
|
||||||
if (dest != null) {
|
|
||||||
try {
|
|
||||||
dest.flush();
|
|
||||||
dest.close();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(BaseIndexTestCase.class.getName()).log(Level.FINEST, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fos != null) {
|
|
||||||
try {
|
|
||||||
fos.close();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(BaseIndexTestCase.class.getName()).log(Level.FINEST, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (zin != null) {
|
|
||||||
zin.close();
|
|
||||||
}
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(BaseIndexTestCase.class.getName()).log(Level.FINEST, null, ex);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (fis != null) {
|
|
||||||
fis.close();
|
|
||||||
}
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Logger.getLogger(BaseIndexTestCase.class.getName()).log(Level.FINEST, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user