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) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.data.update;
    20
    21  import java.io.File;
    22  import java.io.IOException;
    23  import java.net.MalformedURLException;
    24  import org.junit.After;
    25  import org.junit.AfterClass;
    26  import org.junit.Before;
    27  import org.junit.BeforeClass;
    28  import org.junit.Test;
    29  import static org.junit.Assert.*;
    30  import org.owasp.dependencycheck.utils.DownloadFailedException;
    31
    32  /**
    33   *
    34   * @author Jeremy Long (jeremy.long@owasp.org)
    35   */
    36  public class UpdateableTest {
    37
    38      public UpdateableTest() {
    39      }
    40
    41      @BeforeClass
    42      public static void setUpClass() {
    43      }
    44
    45      @AfterClass
    46      public static void tearDownClass() {
    47      }
    48
    49      @Before
    50      public void setUp() {
    51      }
    52
    53      @After
    54      public void tearDown() {
    55      }
    56
    57      /**
    58       * Test of isUpdateNeeded method, of class Updateable.
    59       */
    60      @Test
    61      public void testIsUpdateNeeded() throws MalformedURLException, DownloadFailedException, IOException {
    62          String id = "key";
    63          //use a local file as this test will load the result and check the timestamp
    64          File f = new File("target/test-classes/nvdcve-2.0-2012.xml");
    65          String url = "file:///" + f.getCanonicalPath();
    66          Updateable instance = new Updateable();
    67          instance.add(id, url, url, false);
    68
    69          boolean expResult = false;
    70          boolean result = instance.isUpdateNeeded();
    71          assertEquals(expResult, result);
    72
    73          instance.add("nextId", url, url, true);
    74
    75          expResult = true;
    76          result = instance.isUpdateNeeded();
    77          assertEquals(expResult, result);
    78      }
    79
    80      /**
    81       * Test of add method, of class Updateable.
    82       */
    83      @Test
    84      public void testAdd_3args() throws Exception {
    85          String id = "key";
    86          File f = new File("target/test-classes/nvdcve-2.0-2012.xml");
    87          //use a local file as this test will load the result and check the timestamp
    88          String url = "file:///" + f.getCanonicalPath();
    89          Updateable instance = new Updateable();
    90          instance.add(id, url, url);
    91          NvdCveInfo results = instance.get(id);
    92          assertEquals(id, results.getId());
    93          assertEquals(url, results.getUrl());
    94          assertEquals(url, results.getOldSchemaVersionUrl());
    95      }
    96
    97      /**
    98       * Test of add method, of class Updateable.
    99       */
    100     @Test
    101     public void testAdd_4args() throws Exception {
    102         String id = "key";
    103         //use a local file as this test will load the result and check the timestamp
    104         File f = new File("target/test-classes/nvdcve-2.0-2012.xml");
    105         String url = "file:///" + f.getCanonicalPath();
    106         Updateable instance = new Updateable();
    107         instance.add(id, url, url, false);
    108
    109         boolean expResult = false;
    110         boolean result = instance.isUpdateNeeded();
    111         assertEquals(expResult, result);
    112
    113         instance.add("nextId", url, url, false);
    114         NvdCveInfo results = instance.get(id);
    115
    116         assertEquals(id, results.getId());
    117         assertEquals(url, results.getUrl());
    118         assertEquals(url, results.getOldSchemaVersionUrl());
    119
    120     }
    121
    122     /**
    123      * Test of clear method, of class Updateable.
    124      */
    125     @Test
    126     public void testClear() throws MalformedURLException, DownloadFailedException, IOException {
    127         String id = "key";
    128         //use a local file as this test will load the result and check the timestamp
    129         File f = new File("target/test-classes/nvdcve-2.0-2012.xml");
    130         String url = "file:///" + f.getCanonicalPath();
    131         Updateable instance = new Updateable();
    132         instance.add(id, url, url, false);
    133         assertFalse(instance.getCollection().isEmpty());
    134         instance.clear();
    135         assertTrue(instance.getCollection().isEmpty());
    136     }
    137
    138     /**
    139      * Test of iterator method, of class Updateable.
    140      */
    141     @Test
    142     public void testIterator() throws IOException {
    143         //use a local file as this test will load the result and check the timestamp
    144         File f = new File("target/test-classes/nvdcve-2.0-2012.xml");
    145         String url = "file:///" + f.getCanonicalPath();
    146         Updateable instance = new Updateable();
    147         instance.add("one", url, url, false);
    148         instance.add("two", url, url, false);
    149         instance.add("three", url, url, false);
    150         int itemsProcessed = 0;
    151         for (NvdCveInfo item : instance) {
    152             if ("one".equals(item.getId())) {
    153                 instance.remove();
    154             }
    155             itemsProcessed += 1;
    156         }
    157         assertEquals(3, itemsProcessed);
    158         assertEquals(2, instance.getCollection().size());
    159     }
    160 }