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