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.data.nvdcve.DatabaseException;
39  import org.owasp.dependencycheck.utils.InvalidSettingException;
40  import org.owasp.dependencycheck.utils.Settings;
41  
42  /**
43   *
44   * @author Jeremy Long
45   */
46  public class BaseDependencyCheckMojoTest extends BaseTest {
47  
48      /**
49       * Checks if the test can be run. The test in this class fail, presumable
50       * due to jmockit, if the JDK is 1.8+.
51       *
52       * @return true if the JDK is below 1.8.
53       */
54      public boolean canRun() {
55          String version = System.getProperty("java.version");
56          int length = version.indexOf('.', version.indexOf('.') + 1);
57          version = version.substring(0, length);
58  
59          double v = Double.parseDouble(version);
60          return v == 1.7;
61      }
62  
63      /**
64       * Test of scanArtifacts method, of class BaseDependencyCheckMojo.
65       */
66      @Test
67      public void testScanArtifacts() throws DatabaseException, InvalidSettingException {
68          if (canRun()) {
69              MavenProject project = new MockUp<MavenProject>() {
70                  @Mock
71                  public Set<Artifact> getArtifacts() {
72                      Set<Artifact> artifacts = new HashSet<Artifact>();
73                      Artifact a = new ArtifactStub();
74                      try {
75                          File file = new File(Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
76                          a.setFile(file);
77                          artifacts.add(a);
78                      } catch (URISyntaxException ex) {
79                          Logger.getLogger(BaseDependencyCheckMojoTest.class.getName()).log(Level.SEVERE, null, ex);
80                      }
81                      //File file = new File(this.getClass().getClassLoader().getResource("daytrader-ear-2.1.7.ear").getPath());
82  
83                      return artifacts;
84                  }
85  
86                  @Mock
87                  public String getName() {
88                      return "test-project";
89                  }
90              }.getMockInstance();
91  
92              boolean autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE);
93              Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
94              MavenEngine engine = new MavenEngine(null, null);
95              Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, autoUpdate);
96  
97              assertTrue(engine.getDependencies().isEmpty());
98              BaseDependencyCheckMojoImpl instance = new BaseDependencyCheckMojoImpl();
99              try { //the mock above fails under some JDKs
100                 instance.scanArtifacts(project, engine);
101             } catch (NullPointerException ex) {
102                 Assume.assumeNoException(ex);
103             }
104             assertFalse(engine.getDependencies().isEmpty());
105             engine.cleanup();
106         }
107     }
108 
109     public class BaseDependencyCheckMojoImpl extends BaseDependencyCheckMojo {
110 
111         @Override
112         public void runCheck() throws MojoExecutionException, MojoFailureException {
113             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
114         }
115 
116         @Override
117         public String getName(Locale locale) {
118             return "test implementation";
119         }
120 
121         @Override
122         public String getDescription(Locale locale) {
123             return "test implementation";
124         }
125 
126         @Override
127         public boolean canGenerateReport() {
128             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
129         }
130     }
131 }