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