javadocs + tests

This commit is contained in:
Mark Rekveld
2017-07-10 16:23:17 +02:00
parent cb3cf79beb
commit f2aa3f12be
3 changed files with 126 additions and 6 deletions

View File

@@ -57,7 +57,14 @@ import static org.owasp.dependencycheck.analyzer.AnalysisPhase.*;
*/ */
public class Engine implements FileFilter { public class Engine implements FileFilter {
/**
* {@link Engine} execution modes.
*/
public enum Mode { public enum Mode {
/**
* In evidence collection mode the {@link Engine} only collect evidence from the scan targets,
* and doesn't require a database.
*/
EVIDENCE_COLLECTION( EVIDENCE_COLLECTION(
false, false,
INITIAL, INITIAL,
@@ -65,6 +72,11 @@ public class Engine implements FileFilter {
INFORMATION_COLLECTION, INFORMATION_COLLECTION,
POST_INFORMATION_COLLECTION POST_INFORMATION_COLLECTION
), ),
/**
* In evidence processing mode the {@link Engine} processes the evidence collected using the
* {@link #EVIDENCE_COLLECTION} mode. Dependencies should be injected into the {@link Engine}
* using {@link Engine#setDependencies(List)}.
*/
EVIDENCE_PROCESSING( EVIDENCE_PROCESSING(
true, true,
PRE_IDENTIFIER_ANALYSIS, PRE_IDENTIFIER_ANALYSIS,
@@ -75,6 +87,9 @@ public class Engine implements FileFilter {
POST_FINDING_ANALYSIS, POST_FINDING_ANALYSIS,
FINAL FINAL
), ),
/**
* In standalone mode the {@link Engine} will collect and process evidence in a single execution.
*/
STANDALONE(true, AnalysisPhase.values()); STANDALONE(true, AnalysisPhase.values());
public final boolean requiresDatabase; public final boolean requiresDatabase;
@@ -117,18 +132,21 @@ public class Engine implements FileFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(Engine.class); private static final Logger LOGGER = LoggerFactory.getLogger(Engine.class);
/** /**
* Creates a new Engine. * Creates a new {@link Mode#STANDALONE} Engine.
*/ */
public Engine() { public Engine() {
this(Mode.STANDALONE); this(Mode.STANDALONE);
} }
/**
* Creates a new Engine.
*/
public Engine(Mode mode) { public Engine(Mode mode) {
this(Thread.currentThread().getContextClassLoader(), mode); this(Thread.currentThread().getContextClassLoader(), mode);
} }
/** /**
* Creates a new Engine. * Creates a new {@link Mode#STANDALONE} Engine.
* *
* @param serviceClassLoader a reference the class loader being used * @param serviceClassLoader a reference the class loader being used
*/ */
@@ -140,6 +158,7 @@ public class Engine implements FileFilter {
* Creates a new Engine. * Creates a new Engine.
* *
* @param serviceClassLoader a reference the class loader being used * @param serviceClassLoader a reference the class loader being used
* @param mode the mode of the engine
*/ */
public Engine(ClassLoader serviceClassLoader, Mode mode) { public Engine(ClassLoader serviceClassLoader, Mode mode) {
this.serviceClassLoader = serviceClassLoader; this.serviceClassLoader = serviceClassLoader;

View File

@@ -0,0 +1,81 @@
package org.owasp.dependencycheck;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;
import org.owasp.dependencycheck.analyzer.AnalysisPhase;
import org.owasp.dependencycheck.utils.Settings;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* @author Mark Rekveld
*/
public class EngineModeTest extends BaseTest {
@Rule
public TemporaryFolder tempDir = new TemporaryFolder();
@Rule
public TestName testName = new TestName();
private Engine engine;
@Before
public void setUp() throws Exception {
Settings.setString(Settings.KEYS.DATA_DIRECTORY, tempDir.newFolder().getAbsolutePath());
}
@After
public void tearDown() throws Exception {
engine.cleanup();
}
@Test
public void testEvidenceCollectionMode() throws Exception {
engine = new Engine(Engine.Mode.EVIDENCE_COLLECTION);
assertDatabase(false);
for (AnalysisPhase phase : Engine.Mode.EVIDENCE_COLLECTION.phases) {
assertThat(engine.getAnalyzers(phase), is(notNullValue()));
}
for (AnalysisPhase phase : Engine.Mode.EVIDENCE_PROCESSING.phases) {
assertThat(engine.getAnalyzers(phase), is(nullValue()));
}
}
@Test
public void testEvidenceProcessingMode() throws Exception {
engine = new Engine(Engine.Mode.EVIDENCE_PROCESSING);
assertDatabase(true);
for (AnalysisPhase phase : Engine.Mode.EVIDENCE_PROCESSING.phases) {
assertThat(engine.getAnalyzers(phase), is(notNullValue()));
}
for (AnalysisPhase phase : Engine.Mode.EVIDENCE_COLLECTION.phases) {
assertThat(engine.getAnalyzers(phase), is(nullValue()));
}
}
@Test
public void testStandaloneMode() throws Exception {
engine = new Engine(Engine.Mode.STANDALONE);
assertDatabase(true);
for (AnalysisPhase phase : Engine.Mode.STANDALONE.phases) {
assertThat(engine.getAnalyzers(phase), is(notNullValue()));
}
}
private void assertDatabase(boolean exists) throws Exception {
Path directory = Settings.getDataDirectory().toPath();
assertThat(Files.exists(directory), is(true));
assertThat(Files.isDirectory(directory), is(true));
Path database = directory.resolve(Settings.getString(Settings.KEYS.DB_FILE_NAME));
assertThat(Files.exists(database), is(exists));
}
}

View File

@@ -17,13 +17,18 @@
*/ */
package org.owasp.dependencycheck.analyzer; package org.owasp.dependencycheck.analyzer;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.owasp.dependencycheck.BaseDBTestCase; import org.owasp.dependencycheck.BaseDBTestCase;
import org.owasp.dependencycheck.utils.Settings; import org.owasp.dependencycheck.utils.Settings;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.owasp.dependencycheck.analyzer.AnalysisPhase.FINAL;
import static org.owasp.dependencycheck.analyzer.AnalysisPhase.INITIAL;
/** /**
* *
* @author Jeremy Long * @author Jeremy Long
@@ -46,7 +51,22 @@ public class AnalyzerServiceTest extends BaseDBTestCase {
} }
assertTrue("JarAnalyzer loaded", found); assertTrue("JarAnalyzer loaded", found);
} }
/**
* Test of getAnalyzers method, of class AnalyzerService.
*/
@Test
public void testGetAnalyzers_SpecificPhases() throws Exception {
AnalyzerService instance = new AnalyzerService(Thread.currentThread().getContextClassLoader());
List<Analyzer> result = instance.getAnalyzers(INITIAL, FINAL);
for (Analyzer a : result) {
if (a.getAnalysisPhase() != INITIAL && a.getAnalysisPhase() != FINAL) {
fail("Only expecting analyzers for phases " + INITIAL + " and " + FINAL);
}
}
}
/** /**
* Test of getAnalyzers method, of class AnalyzerService. * Test of getAnalyzers method, of class AnalyzerService.
*/ */