View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.utils;
    20
    21  import org.junit.After;
    22  import org.junit.AfterClass;
    23  import org.junit.Before;
    24  import org.junit.BeforeClass;
    25  import org.junit.Test;
    26  import static org.junit.Assert.*;
    27
    28  /**
    29   *
    30   * @author Jeremy Long (jeremy.long@owasp.org)
    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 testParseVersionFromFileName() {
    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"};
    62          final String[] expResult = {"0.9.5", "1.1", "1.5.r4", "1.2.5", "4.4.0", "2.0.0.rc1",
    63              "2.0.1.r114940", "2.3.11.r121413", "3.7", "-", null, "1.3.beta", "6"};
    64
    65          for (int i = 0; i < fileName.length; i++) {
    66              final DependencyVersion version = DependencyVersionUtil.parseVersion(fileName[i]);
    67              String result = null;
    68              if (version != null) {
    69                  result = version.toString();
    70              }
    71              assertEquals("Failed extraction on \"" + fileName[i] + "\".", expResult[i], result);
    72          }
    73
    74          String[] failingNames = {"no-version-identified.jar", "somelib-04aug2000r7-dev.jar", /*"no.version15.jar",*/
    75              "lib_1.0_spec-1.1.jar", "lib-api_1.0_spec-1.0.1.jar"};
    76          for (String failingName : failingNames) {
    77              final DependencyVersion version = DependencyVersionUtil.parseVersion(failingName);
    78              assertNull("Found version in name that should have failed \"" + failingName + "\".", version);
    79          }
    80      }
    81  }