View Javadoc
1   /*
2    * This file is part of dependency-check-maven.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
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.Assume;
37  import org.junit.Test;
38  import org.owasp.dependencycheck.Engine;
39  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
40  import org.owasp.dependencycheck.utils.InvalidSettingException;
41  import org.owasp.dependencycheck.utils.Settings;
42  
43  /**
44   *
45   * @author Jeremy Long
46   */
47  public class BaseDependencyCheckMojoTest extends BaseTest {
48  
49      /**
50       * Checks if the test can be run. The test in this class fail, presumable
51       * due to jmockit, if the JDK is 1.8+.
52       *
53       * @return true if the JDK is below 1.8.
54       */
55      public boolean canRun() {
56          String version = System.getProperty("java.version");
57          int length = version.indexOf('.', version.indexOf('.') + 1);
58          version = version.substring(0, length);
59  
60          double v = Double.parseDouble(version);
61          return v == 1.7;
62      }
63  
64      /**
65       * Test of scanArtifacts method, of class BaseDependencyCheckMojo.
66       */
67      @Test
68      public void testScanArtifacts() throws DatabaseException, InvalidSettingException {
69          if (canRun()) {
70              MavenProject project = new MockUp<MavenProject>() {
71                  @Mock
72                  public Set<Artifact> getArtifacts() {
73                      Set<Artifact> artifacts = new HashSet<Artifact>();
74                      Artifact a = new ArtifactStub();
75                      try {
76                          File file = new File(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
77                          a.setFile(file);
78                          artifacts.add(a);
79                      } catch (URISyntaxException ex) {
80                          Logger.getLogger(BaseDependencyCheckMojoTest.class.getName()).log(Level.SEVERE, null, ex);
81                      }
82                      //File file = new File(this.getClass().getClassLoader().getResource("daytrader-ear-2.1.7.ear").getPath());
83  
84                      return artifacts;
85                  }
86  
87                  @Mock
88                  public String getName() {
89                      return "test-project";
90                  }
91              }.getMockInstance();
92  
93              boolean autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE);
94              Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
95              Engine engine = new Engine();
96              Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
97  
98              assertTrue(engine.getDependencies().isEmpty());
99              BaseDependencyCheckMojoImpl instance = new BaseDependencyCheckMojoImpl();
100             try { //the mock above fails under some JDKs
101                 instance.scanArtifacts(project, engine);
102             } catch (NullPointerException ex) {
103                 Assume.assumeNoException(ex);
104             }
105             assertFalse(engine.getDependencies().isEmpty());
106             engine.cleanup();
107         }
108     }
109 
110     public class BaseDependencyCheckMojoImpl extends BaseDependencyCheckMojo {
111 
112         @Override
113         public void runCheck() throws MojoExecutionException, MojoFailureException {
114             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
115         }
116 
117         @Override
118         public String getName(Locale locale) {
119             return "test implementation";
120         }
121 
122         @Override
123         public String getDescription(Locale locale) {
124             return "test implementation";
125         }
126 
127         @Override
128         public boolean canGenerateReport() {
129             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
130         }
131     }
132 }