View Javadoc

    1   /*
    2    * To change this template, choose Tools | Templates
    3    * and open the template in the editor.
    4    */
    5   package org.owasp.dependencycheck.utils;
    6
    7   import java.util.ArrayList;
    8   import java.util.Iterator;
    9   import java.util.List;
    10  import org.junit.After;
    11  import org.junit.AfterClass;
    12  import org.junit.Before;
    13  import org.junit.BeforeClass;
    14  import org.junit.Test;
    15  import static org.junit.Assert.*;
    16
    17  /**
    18   *
    19   * @author Jeremy Long (jeremy.long@owasp.org)
    20   */
    21  public class DependencyVersionTest {
    22
    23      public DependencyVersionTest() {
    24      }
    25
    26      @BeforeClass
    27      public static void setUpClass() {
    28      }
    29
    30      @AfterClass
    31      public static void tearDownClass() {
    32      }
    33
    34      @Before
    35      public void setUp() {
    36      }
    37
    38      @After
    39      public void tearDown() {
    40      }
    41
    42      /**
    43       * Test of parseVersion method, of class DependencyVersion.
    44       */
    45      @Test
    46      public void testParseVersion() {
    47          String version = "1.2r1";
    48          DependencyVersion instance = new DependencyVersion();
    49          instance.parseVersion(version);
    50          List<String> parts = instance.getVersionParts();
    51          assertEquals(3, parts.size());
    52          assertEquals("1", parts.get(0));
    53          assertEquals("2", parts.get(1));
    54          assertEquals("r1", parts.get(2));
    55
    56          instance.parseVersion("x6.0");
    57          parts = instance.getVersionParts();
    58          assertEquals(2, parts.size());
    59          assertEquals("x6", parts.get(0));
    60          assertEquals("0", parts.get(1));
    61          //assertEquals("0", parts.get(2));
    62
    63
    64      }
    65
    66      /**
    67       * Test of iterator method, of class DependencyVersion.
    68       */
    69      @Test
    70      public void testIterator() {
    71          DependencyVersion instance = new DependencyVersion("1.2.3");
    72          Iterator result = instance.iterator();
    73          int count = 1;
    74          while (result.hasNext()) {
    75              String v = (String) result.next();
    76              assertTrue(String.valueOf(count++).equals(v));
    77          }
    78      }
    79
    80      /**
    81       * Test of toString method, of class DependencyVersion.
    82       */
    83      @Test
    84      public void testToString() {
    85          DependencyVersion instance = new DependencyVersion("1.2.3r1");
    86          String expResult = "1.2.3.r1";
    87          String result = instance.toString();
    88          assertEquals(expResult, result);
    89      }
    90
    91      /**
    92       * Test of equals method, of class DependencyVersion.
    93       */
    94      @Test
    95      public void testEquals() {
    96          DependencyVersion obj = new DependencyVersion("1.2.3.r1");
    97          DependencyVersion instance = new DependencyVersion("1.2.3");
    98          boolean expResult = false;
    99          boolean result = instance.equals(obj);
    100         assertEquals(expResult, result);
    101         obj = new DependencyVersion("1.2.3");
    102         expResult = true;
    103         result = instance.equals(obj);
    104         assertEquals(expResult, result);
    105     }
    106
    107     /**
    108      * Test of hashCode method, of class DependencyVersion.
    109      */
    110     @Test
    111     public void testHashCode() {
    112         DependencyVersion instance = new DependencyVersion("3.2.1");
    113         int expResult = 80756;
    114         int result = instance.hashCode();
    115         assertEquals(expResult, result);
    116     }
    117
    118     /**
    119      * Test of matchesAtLeastThreeLevels method, of class DependencyVersion.
    120      */
    121     @Test
    122     public void testMatchesAtLeastThreeLevels() {
    123
    124         DependencyVersion instance = new DependencyVersion("1.2.3.4");
    125         DependencyVersion version = new DependencyVersion("1.2.3.5");
    126         //true tests
    127         assertEquals(true, instance.matchesAtLeastThreeLevels(version));
    128         version = new DependencyVersion("1.2");
    129         assertEquals(true, instance.matchesAtLeastThreeLevels(version));
    130         //false tests
    131         version = new DependencyVersion("1.2.2.5");
    132         assertEquals(false, instance.matchesAtLeastThreeLevels(version));
    133         version = new DependencyVersion("2");
    134         assertEquals(false, instance.matchesAtLeastThreeLevels(version));
    135     }
    136
    137     /**
    138      * Test of compareTo method, of class DependencyVersion.
    139      */
    140     @Test
    141     public void testCompareTo() {
    142         DependencyVersion instance = new DependencyVersion("1.2.3");
    143         DependencyVersion version = new DependencyVersion("1.2.3");
    144         int expResult = 0;
    145         assertEquals(0, instance.compareTo(version));
    146         version = new DependencyVersion("1.1");
    147         assertEquals(1, instance.compareTo(version));
    148         version = new DependencyVersion("1.2");
    149         assertEquals(1, instance.compareTo(version));
    150         version = new DependencyVersion("1.3");
    151         assertEquals(-1, instance.compareTo(version));
    152         version = new DependencyVersion("1.2.3.1");
    153         assertEquals(-1, instance.compareTo(version));
    154
    155         DependencyVersion[] dv = new DependencyVersion[7];
    156         dv[0] = new DependencyVersion("2.1.3");
    157         dv[1] = new DependencyVersion("2.1.3.r2");
    158         dv[2] = new DependencyVersion("2.1.3.r1");
    159         dv[3] = new DependencyVersion("1.2.3.1");
    160         dv[4] = new DependencyVersion("1.2.3");
    161         dv[5] = new DependencyVersion("2");
    162         dv[6] = new DependencyVersion("-");
    163
    164         DependencyVersion[] expected = new DependencyVersion[7];
    165         expected[0] = new DependencyVersion("-");
    166         expected[1] = new DependencyVersion("1.2.3");
    167         expected[2] = new DependencyVersion("1.2.3.1");
    168         expected[3] = new DependencyVersion("2");
    169         expected[4] = new DependencyVersion("2.1.3");
    170         expected[5] = new DependencyVersion("2.1.3.r1");
    171         expected[6] = new DependencyVersion("2.1.3.r2");
    172         java.util.Arrays.sort(dv);
    173
    174         assertArrayEquals(expected, dv);
    175     }
    176 }