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.utils;
19  
20  import java.util.Arrays;
21  import java.util.Iterator;
22  import java.util.List;
23  import static org.junit.Assert.assertArrayEquals;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import org.junit.Test;
27  import org.owasp.dependencycheck.BaseTest;
28  
29  /**
30   *
31   * @author Jeremy Long
32   */
33  public class DependencyVersionTest extends BaseTest {
34  
35      /**
36       * Test of parseVersion method, of class DependencyVersion.
37       */
38      @Test
39      public void testParseVersion() {
40          String version = "1.2r1";
41          DependencyVersion instance = new DependencyVersion();
42          instance.parseVersion(version);
43          List<String> parts = instance.getVersionParts();
44          assertEquals(3, parts.size());
45          assertEquals("1", parts.get(0));
46          assertEquals("2", parts.get(1));
47          assertEquals("r1", parts.get(2));
48  
49          instance.parseVersion("x6.0");
50          parts = instance.getVersionParts();
51          assertEquals(2, parts.size());
52          assertEquals("x6", parts.get(0));
53          assertEquals("0", parts.get(1));
54          // TODO(code review): should this be here/do something?
55          //assertEquals("0", parts.get(2));
56  
57      }
58  
59      /**
60       * Test of iterator method, of class DependencyVersion.
61       */
62      @Test
63      public void testIterator() {
64          DependencyVersion instance = new DependencyVersion("1.2.3");
65          Iterator<String> result = instance.iterator();
66          assertTrue(result.hasNext());
67          int count = 1;
68          while (result.hasNext()) {
69              String v = result.next();
70              assertTrue(String.valueOf(count++).equals(v));
71          }
72      }
73  
74      /**
75       * Test of toString method, of class DependencyVersion.
76       */
77      @Test
78      public void testToString() {
79          DependencyVersion instance = new DependencyVersion("1.2.3r1");
80          String expResult = "1.2.3.r1";
81          String result = instance.toString();
82          assertEquals(expResult, result);
83      }
84  
85      /**
86       * Test of equals method, of class DependencyVersion.
87       */
88      @Test
89      public void testEquals() {
90          DependencyVersion obj = new DependencyVersion("1.2.3.r1");
91          DependencyVersion instance = new DependencyVersion("1.2.3");
92          boolean expResult = false;
93          boolean result = instance.equals(obj);
94          assertEquals(expResult, result);
95          obj = new DependencyVersion("1.2.3");
96          expResult = true;
97          result = instance.equals(obj);
98          assertEquals(expResult, result);
99          
100         instance = new DependencyVersion("2.0.0");
101         obj = new DependencyVersion("2");
102         expResult = false;
103         result = instance.equals(obj);
104         assertEquals(expResult, result);
105         
106         obj = new DependencyVersion("2.0");
107         expResult = true;
108         result = instance.equals(obj);
109         assertEquals(expResult, result);
110         
111         
112     }
113 
114     /**
115      * Test of hashCode method, of class DependencyVersion.
116      */
117     @Test
118     public void testHashCode() {
119         DependencyVersion instance = new DependencyVersion("3.2.1");
120         int expResult = 80756;
121         int result = instance.hashCode();
122         assertEquals(expResult, result);
123     }
124 
125     /**
126      * Test of matchesAtLeastThreeLevels method, of class DependencyVersion.
127      */
128     @Test
129     public void testMatchesAtLeastThreeLevels() {
130 
131         DependencyVersion instance = new DependencyVersion("2.3.16.3");
132         DependencyVersion version = new DependencyVersion("2.3.16.4");
133         //true tests
134         assertEquals(true, instance.matchesAtLeastThreeLevels(version));
135         version = new DependencyVersion("2.3");
136         assertEquals(true, instance.matchesAtLeastThreeLevels(version));
137         //false tests
138         version = new DependencyVersion("2.3.16.1");
139         assertEquals(false, instance.matchesAtLeastThreeLevels(version));
140         version = new DependencyVersion("2");
141         assertEquals(false, instance.matchesAtLeastThreeLevels(version));
142     }
143 
144     /**
145      * Test of compareTo method, of class DependencyVersion.
146      */
147     @Test
148     public void testCompareTo() {
149         DependencyVersion instance = new DependencyVersion("1.2.3");
150         DependencyVersion version = new DependencyVersion("1.2.3");
151         assertEquals(0, instance.compareTo(version));
152         version = new DependencyVersion("1.1");
153         assertEquals(1, instance.compareTo(version));
154         version = new DependencyVersion("1.2");
155         assertEquals(1, instance.compareTo(version));
156         version = new DependencyVersion("1.3");
157         assertEquals(-1, instance.compareTo(version));
158         version = new DependencyVersion("1.2.3.1");
159         assertEquals(-1, instance.compareTo(version));
160 
161         instance = new DependencyVersion("1.0.1n");
162         version = new DependencyVersion("1.0.1m");
163         assertEquals(1, instance.compareTo(version));
164         version = new DependencyVersion("1.0.1n");
165         assertEquals(0, instance.compareTo(version));
166         version = new DependencyVersion("1.0.1o");
167         assertEquals(-1, instance.compareTo(version));
168 
169         DependencyVersion[] dv = new DependencyVersion[7];
170         dv[0] = new DependencyVersion("2.1.3");
171         dv[1] = new DependencyVersion("2.1.3.r2");
172         dv[2] = new DependencyVersion("2.1.3.r1");
173         dv[3] = new DependencyVersion("1.2.3.1");
174         dv[4] = new DependencyVersion("1.2.3");
175         dv[5] = new DependencyVersion("2");
176         dv[6] = new DependencyVersion("-");
177 
178         DependencyVersion[] expected = new DependencyVersion[7];
179         expected[0] = new DependencyVersion("-");
180         expected[1] = new DependencyVersion("1.2.3");
181         expected[2] = new DependencyVersion("1.2.3.1");
182         expected[3] = new DependencyVersion("2");
183         expected[4] = new DependencyVersion("2.1.3");
184         expected[5] = new DependencyVersion("2.1.3.r1");
185         expected[6] = new DependencyVersion("2.1.3.r2");
186         Arrays.sort(dv);
187 
188         assertArrayEquals(expected, dv);
189     }
190 
191     /**
192      * Test of getVersionParts method, of class DependencyVersion.
193      */
194     @Test
195     public void testGetVersionParts() {
196         DependencyVersion instance = new DependencyVersion();
197         List<String> versionParts = Arrays.asList("1", "1", "1");
198         instance.setVersionParts(versionParts);
199         List<String> expResult = Arrays.asList("1", "1", "1");
200         List<String> result = instance.getVersionParts();
201         assertEquals(expResult, result);
202     }
203 
204     /**
205      * Test of setVersionParts method, of class DependencyVersion.
206      */
207     @Test
208     public void testSetVersionParts() {
209         List<String> versionParts = Arrays.asList("1", "1", "1");
210         DependencyVersion instance = new DependencyVersion();
211         instance.setVersionParts(versionParts);
212         List<String> results = instance.getVersionParts();
213         assertEquals(versionParts, results);
214     }
215 }