1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.maven;
19
20 import java.io.File;
21 import java.net.URISyntaxException;
22 import java.util.HashSet;
23 import java.util.Locale;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import mockit.Mock;
28 import mockit.MockUp;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
33 import org.apache.maven.project.MavenProject;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertTrue;
36 import org.junit.Test;
37 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
38 import org.owasp.dependencycheck.utils.InvalidSettingException;
39 import org.owasp.dependencycheck.utils.Settings;
40
41
42
43
44
45 public class BaseDependencyCheckMojoTest extends BaseTest {
46
47
48
49
50 @Test
51 public void testScanArtifacts() throws DatabaseException, InvalidSettingException {
52 MavenProject project = new MockUp<MavenProject>() {
53 @Mock
54 public Set<Artifact> getArtifacts() {
55 Set<Artifact> artifacts = new HashSet<Artifact>();
56 Artifact a = new ArtifactStub();
57 try {
58 File file = new File(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
59 a.setFile(file);
60 artifacts.add(a);
61 } catch (URISyntaxException ex) {
62 Logger.getLogger(BaseDependencyCheckMojoTest.class.getName()).log(Level.SEVERE, null, ex);
63 }
64
65
66 return artifacts;
67 }
68
69 @Mock
70 public String getName() {
71 return "test-project";
72 }
73 }.getMockInstance();
74
75 boolean autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE);
76 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
77 Engine engine = new Engine(null, null);
78 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
79
80 assertTrue(engine.getDependencies().isEmpty());
81 BaseDependencyCheckMojoImpl instance = new BaseDependencyCheckMojoImpl();
82 instance.scanArtifacts(project, engine);
83 assertFalse(engine.getDependencies().isEmpty());
84 engine.cleanup();
85 }
86
87 public class BaseDependencyCheckMojoImpl extends BaseDependencyCheckMojo {
88
89 @Override
90 public void runCheck() throws MojoExecutionException, MojoFailureException {
91 throw new UnsupportedOperationException("Not supported yet.");
92 }
93
94 @Override
95 public String getName(Locale locale) {
96 return "test implementation";
97 }
98
99 @Override
100 public String getDescription(Locale locale) {
101 return "test implementation";
102 }
103
104 @Override
105 public boolean canGenerateReport() {
106 throw new UnsupportedOperationException("Not supported yet.");
107 }
108
109 }
110
111 }