View Javadoc
1   /*
2    * This file is part of dependency-check-core.
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) 2016 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck;
19  
20  import mockit.Expectations;
21  import mockit.Mocked;
22  import org.junit.Test;
23  import org.owasp.dependencycheck.analyzer.Analyzer;
24  import org.owasp.dependencycheck.analyzer.JarAnalyzer;
25  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
26  import org.owasp.dependencycheck.dependency.Dependency;
27  import org.owasp.dependencycheck.exception.ExceptionCollection;
28  
29  import java.io.File;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.concurrent.ExecutorService;
33  import java.util.concurrent.Executors;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertTrue;
37  
38  /**
39   * @author Jeremy Long
40   */
41  public class EngineTest extends BaseDBTestCase {
42  
43      @Mocked
44      Analyzer analyzer;
45  
46      @Mocked
47      AnalysisTask analysisTask;
48  
49  
50      /**
51       * Test of scanFile method, of class Engine.
52       */
53      @Test
54      public void testScanFile() throws DatabaseException {
55          Engine instance = new Engine();
56          instance.addFileTypeAnalyzer(new JarAnalyzer());
57          File file = BaseTest.getResourceAsFile(this, "dwr.jar");
58          Dependency dwr = instance.scanFile(file);
59          file = BaseTest.getResourceAsFile(this, "org.mortbay.jmx.jar");
60          instance.scanFile(file);
61          assertEquals(2, instance.getDependencies().size());
62  
63          file = BaseTest.getResourceAsFile(this, "dwr.jar");
64          Dependency secondDwr = instance.scanFile(file);
65  
66          assertEquals(2, instance.getDependencies().size());
67          assertTrue(dwr == secondDwr);
68      }
69  
70      @Test(expected = ExceptionCollection.class)
71      public void exceptionDuringAnalysisTaskExecutionIsFatal() throws DatabaseException, ExceptionCollection {
72          final ExecutorService executorService = Executors.newFixedThreadPool(3);
73          final Engine instance = new Engine();
74          final List<Throwable> exceptions = new ArrayList<Throwable>();
75  
76          new Expectations() {{
77              analysisTask.call();
78              result = new IllegalStateException("Analysis task execution threw an exception");
79          }};
80  
81          final List<AnalysisTask> failingAnalysisTask = new ArrayList<AnalysisTask>();
82          failingAnalysisTask.add(analysisTask);
83  
84          new Expectations(instance) {{
85              instance.getExecutorService(analyzer);
86              result = executorService;
87  
88              instance.getAnalysisTasks(analyzer, exceptions);
89              result = failingAnalysisTask;
90          }};
91  
92          instance.executeAnalysisTasks(analyzer, exceptions);
93  
94          assertTrue(executorService.isShutdown());
95      }
96  }