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 org.junit.After;
21  import org.junit.AfterClass;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  import org.junit.Before;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  
28  /**
29   *
30   * @author Jeremy Long
31   */
32  public class DependencyVersionUtilTest {
33  
34      public DependencyVersionUtilTest() {
35      }
36  
37      @BeforeClass
38      public static void setUpClass() throws Exception {
39      }
40  
41      @AfterClass
42      public static void tearDownClass() throws Exception {
43      }
44  
45      @Before
46      public void setUp() {
47      }
48  
49      @After
50      public void tearDown() {
51      }
52  
53      /**
54       * Test of parseVersion method, of class DependencyVersionUtil.
55       */
56      @Test
57      public void testParseVersion() {
58          final String[] fileName = {"something-0.9.5.jar", "lib2-1.1.jar", "lib1.5r4-someflag-R26.jar",
59              "lib-1.2.5-dev-20050313.jar", "testlib_V4.4.0.jar", "lib-core-2.0.0-RC1-SNAPSHOT.jar",
60              "lib-jsp-2.0.1_R114940.jar", "dev-api-2.3.11_R121413.jar", "lib-api-3.7-SNAPSHOT.jar",
61              "-", "", "1.3-beta", "6", "openssl1.0.1c", "jsf-impl-2.2.8-02.jar",
62              "plone.rfc822-1.1.1-py2-none-any.whl"};
63          final String[] expResult = {"0.9.5", "1.1", "1.5.r4", "1.2.5", "4.4.0", "2.0.0.rc1",
64              "2.0.1.r114940", "2.3.11.r121413", "3.7", "-", null, "1.3.beta", "6", "1.0.1c",
65              "2.2.8.02", "1.1.1"};
66  
67          for (int i = 0; i < fileName.length; i++) {
68              final DependencyVersion version = DependencyVersionUtil.parseVersion(fileName[i]);
69              String result = null;
70              if (version != null) {
71                  result = version.toString();
72              }
73              assertEquals("Failed extraction on \"" + fileName[i] + "\".", expResult[i], result);
74          }
75  
76          String[] failingNames = {"no-version-identified.jar", "somelib-04aug2000r7-dev.jar", /*"no.version15.jar",*/
77              "lib_1.0_spec-1.1.jar", "lib-api_1.0_spec-1.0.1.jar"};
78          for (String failingName : failingNames) {
79              final DependencyVersion version = DependencyVersionUtil.parseVersion(failingName);
80              assertNull("Found version in name that should have failed \"" + failingName + "\".", version);
81          }
82      }
83  }