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) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.analyzer;
19  
20  import mockit.Mocked;
21  import mockit.Verifications;
22  import org.junit.Test;
23  import org.owasp.dependencycheck.BaseTest;
24  import org.owasp.dependencycheck.Engine;
25  import org.owasp.dependencycheck.dependency.Dependency;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertTrue;
30  
31  /**
32   * @author Jeremy Long
33   */
34  public class DependencyBundlingAnalyzerTest extends BaseTest {
35  
36      @Mocked
37      Engine engineMock;
38  
39      /**
40       * Test of getName method, of class DependencyBundlingAnalyzer.
41       */
42      @Test
43      public void testGetName() {
44          DependencyBundlingAnalyzer instance = new DependencyBundlingAnalyzer();
45          String expResult = "Dependency Bundling Analyzer";
46          String result = instance.getName();
47          assertEquals(expResult, result);
48      }
49  
50      /**
51       * Test of getAnalysisPhase method, of class DependencyBundlingAnalyzer.
52       */
53      @Test
54      public void testGetAnalysisPhase() {
55          DependencyBundlingAnalyzer instance = new DependencyBundlingAnalyzer();
56          AnalysisPhase expResult = AnalysisPhase.FINAL;
57          AnalysisPhase result = instance.getAnalysisPhase();
58          assertEquals(expResult, result);
59      }
60  
61      /**
62       * Test of analyze method, of class DependencyBundlingAnalyzer.
63       * The actually passed dependency does not matter. The analyzer only runs once.
64       */
65      @Test
66      public void testAnalyze() throws Exception {
67          DependencyBundlingAnalyzer instance = new DependencyBundlingAnalyzer();
68  
69          // the actual dependency does not matter
70          assertFalse(instance.getAnalyzed());
71          instance.analyze(null, engineMock);
72  
73          // the second runs basically does nothing
74          assertTrue(instance.getAnalyzed());
75          instance.analyze(null, engineMock);
76          instance.analyze(null, engineMock);
77          instance.analyze(null, engineMock);
78          assertTrue(instance.getAnalyzed());
79  
80          new Verifications() {{
81              engineMock.getDependencies();
82              times = 2;
83          }};
84      }
85  
86      /**
87       * Test of isCore method, of class DependencyBundlingAnalyzer.
88       */
89      @Test
90      public void testIsCore() {
91          Dependency left = new Dependency();
92          Dependency right = new Dependency();
93  
94          left.setFileName("axis2-kernel-1.4.1.jar");
95          right.setFileName("axis2-adb-1.4.1.jar");
96          DependencyBundlingAnalyzer instance = new DependencyBundlingAnalyzer();
97          boolean expResult = true;
98          boolean result = instance.isCore(left, right);
99          assertEquals(expResult, result);
100 
101         left.setFileName("struts-1.2.7.jar");
102         right.setFileName("file.tar.gz\\file.tar\\struts.jar");
103 
104         expResult = true;
105         result = instance.isCore(left, right);
106         assertEquals(expResult, result);
107     }
108 
109     @Test
110     public void testFirstPathIsShortest() {
111         DependencyBundlingAnalyzer instance = new DependencyBundlingAnalyzer();
112 
113         String left = "./a/c.jar";
114         String right = "./d/e/f.jar";
115         boolean expResult = true;
116         boolean result = instance.firstPathIsShortest(left, right);
117         assertEquals(expResult, result);
118 
119         left = "./a/b/c.jar";
120         right = "./d/e/f.jar";
121         expResult = true;
122         result = instance.firstPathIsShortest(left, right);
123         assertEquals(expResult, result);
124 
125         left = "./d/b/c.jar";
126         right = "./a/e/f.jar";
127         expResult = false;
128         result = instance.firstPathIsShortest(left, right);
129         assertEquals(expResult, result);
130 
131         left = "./a/b/c.jar";
132         right = "./d/f.jar";
133         expResult = false;
134         result = instance.firstPathIsShortest(left, right);
135         assertEquals(expResult, result);
136 
137         left = "./a/b/c.jar";
138         right = "./a/b/c.jar";
139         expResult = true;
140         result = instance.firstPathIsShortest(left, right);
141         assertEquals(expResult, result);
142     }
143 }