From fb294e8bea47d0d4caaff0c3f4ca078ff2504aaf Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Tue, 23 Apr 2013 07:06:30 -0400 Subject: [PATCH] added tests for DependencyVersionUtil Former-commit-id: fcbc3b4f9d619be77d78d011926751d528cb1eb1 --- .../utils/DependencyVersionUtilTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/test/java/org/owasp/dependencycheck/utils/DependencyVersionUtilTest.java diff --git a/src/test/java/org/owasp/dependencycheck/utils/DependencyVersionUtilTest.java b/src/test/java/org/owasp/dependencycheck/utils/DependencyVersionUtilTest.java new file mode 100644 index 000000000..c875bb2cb --- /dev/null +++ b/src/test/java/org/owasp/dependencycheck/utils/DependencyVersionUtilTest.java @@ -0,0 +1,80 @@ +/* + * This file is part of DependencyCheck. + * + * DependencyCheck is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * DependencyCheck is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * DependencyCheck. If not, see http://www.gnu.org/licenses/. + * + * Copyright (c) 2012 Jeremy Long. All Rights Reserved. + */ +package org.owasp.dependencycheck.utils; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Jeremy Long (jeremy.long@gmail.com) + */ +public class DependencyVersionUtilTest { + + public DependencyVersionUtilTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of parseVersionFromFileName method, of class DependencyVersionUtil. + */ + @Test + public void testParseVersionFromFileName() { + final String[] fileName = {"something-0.9.5.jar", "lib2-1.1.jar", "lib1.5r4-someflag-R26.jar", + "lib-1.2.5-dev-20050313.jar", "testlib_V4.4.0.jar", "lib-core-2.0.0-RC1-SNAPSHOT.jar", + "lib-jsp-2.0.1_R114940.jar", "dev-api-2.3.11_R121413.jar", "lib-api-3.7-SNAPSHOT.jar"}; + final String[] expResult = {"0.9.5", "1.1", "1.5.r4", "1.2.5", "4.4.0", "2.0.0.rc1", + "2.0.1.r114940", "2.3.11.r121413", "3.7"}; + + for (int i = 0; i < fileName.length; i++) { + final DependencyVersion version = DependencyVersionUtil.parseVersionFromFileName(fileName[i]); + String result = null; + if (version != null) { + result = version.toString(); + } + assertEquals("Failed extraction on \"" + fileName[i] + "\".", expResult[i], result); + } + + String[] failingNames = { "no-version-identified.jar", "somelib-04aug2000r7-dev.jar", "no.version15.jar", + "lib_1.0_spec-1.1.jar", "lib-api_1.0_spec-1.0.1.jar" }; + for (int i = 0; i < failingNames.length; i++) { + final DependencyVersion version = DependencyVersionUtil.parseVersionFromFileName(failingNames[i]); + assertNull("Found version in name that should have failed \"" + failingNames[i] + "\".", version); + } + } +}