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
51
52 public boolean canRun() {
53 String version = System.getProperty("java.version");
54 int length = version.indexOf('.', version.indexOf('.') + 1);
55 version = version.substring(0, length);
56
57 double v = Double.parseDouble(version);
58 return v == 1.7;
59 }
60
61
62
63
64 @Test
65 public void testScanArtifacts() throws DatabaseException, InvalidSettingException {
66
67 if (canRun()) {
68 MavenProject project = new MockUp<MavenProject>() {
69 @Mock
70 public Set<Artifact> getArtifacts() {
71 Set<Artifact> artifacts = new HashSet<Artifact>();
72 Artifact a = new ArtifactStub();
73 try {
74 File file = new File(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
75 a.setFile(file);
76 artifacts.add(a);
77 } catch (URISyntaxException ex) {
78 Logger.getLogger(BaseDependencyCheckMojoTest.class.getName()).log(Level.SEVERE, null, ex);
79 }
80
81
82 return artifacts;
83 }
84
85 @Mock
86 public String getName() {
87 return "test-project";
88 }
89 }.getMockInstance();
90
91 boolean autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE);
92 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
93 Engine engine = new Engine(null, null);
94 Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
95
96 assertTrue(engine.getDependencies().isEmpty());
97 BaseDependencyCheckMojoImpl instance = new BaseDependencyCheckMojoImpl();
98 instance.scanArtifacts(project, engine);
99 assertFalse(engine.getDependencies().isEmpty());
100 engine.cleanup();
101 }
102 }
103
104 public class BaseDependencyCheckMojoImpl extends BaseDependencyCheckMojo {
105
106 @Override
107 public void runCheck() throws MojoExecutionException, MojoFailureException {
108 throw new UnsupportedOperationException("Not supported yet.");
109 }
110
111 @Override
112 public String getName(Locale locale) {
113 return "test implementation";
114 }
115
116 @Override
117 public String getDescription(Locale locale) {
118 return "test implementation";
119 }
120
121 @Override
122 public boolean canGenerateReport() {
123 throw new UnsupportedOperationException("Not supported yet.");
124 }
125 }
126 }