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 java.text.DateFormat;
    25  import java.util.Calendar;
    26  import java.util.logging.Level;
    27  import java.util.logging.Logger;
    28  import org.junit.After;
    29  import org.junit.AfterClass;
    30  import org.junit.Before;
    31  import org.junit.BeforeClass;
    32  import org.junit.Test;
    33  import static org.junit.Assert.*;
    34  import org.owasp.dependencycheck.data.UpdateException;
    35  import org.owasp.dependencycheck.utils.DownloadFailedException;
    36
    37  /**
    38   *
    39   * @author Jeremy Long (jeremy.long@owasp.org)
    40   */
    41  public class AbstractUpdateTaskTest {
    42
    43      public AbstractUpdateTaskTest() {
    44      }
    45
    46      @BeforeClass
    47      public static void setUpClass() {
    48      }
    49
    50      @AfterClass
    51      public static void tearDownClass() {
    52      }
    53
    54      @Before
    55      public void setUp() {
    56      }
    57
    58      @After
    59      public void tearDown() {
    60      }
    61
    62      public AbstractUpdateTask getAbstractUpdateImpl() throws Exception {
    63          DataStoreMetaInfo props = new DataStoreMetaInfo();
    64          AbstractUpdateTask instance = new AbstractUpdateImpl(props);
    65          return instance;
    66      }
    67
    68      /**
    69       * Test of setDeleteAndRecreate method, of class AbstractUpdateTask.
    70       */
    71      @Test
    72      public void testSetDeleteAndRecreate() throws Exception {
    73          boolean deleteAndRecreate = false;
    74          boolean expResult = false;
    75          AbstractUpdateTask instance = getAbstractUpdateImpl();
    76          instance.setDeleteAndRecreate(deleteAndRecreate);
    77          boolean result = instance.shouldDeleteAndRecreate();
    78          assertEquals(expResult, result);
    79      }
    80
    81      /**
    82       * Test of deleteExistingData method, of class AbstractUpdateTask.
    83       */
    84      @Test
    85      public void testDeleteExistingData() throws Exception {
    86          AbstractUpdateTask instance = getAbstractUpdateImpl();
    87          Exception result = null;
    88          try {
    89              instance.deleteExistingData();
    90          } catch (IOException ex) {
    91              result = ex;
    92          }
    93          assertNull(result);
    94      }
    95
    96      /**
    97       * Test of openDataStores method, of class AbstractUpdateTask.
    98       */
    99      @Test
    100     public void testOpenDataStores() throws Exception {
    101         AbstractUpdateTask instance = getAbstractUpdateImpl();
    102         instance.openDataStores();
    103         instance.closeDataStores();
    104     }
    105
    106     /**
    107      * Test of withinRange method, of class AbstractUpdateTask.
    108      */
    109     @Test
    110     public void testWithinRange() throws Exception {
    111         Calendar c = Calendar.getInstance();
    112
    113         long current = c.getTimeInMillis();
    114         long lastRun = c.getTimeInMillis() - (3 * (1000 * 60 * 60 * 24));
    115         int range = 7; // 7 days
    116         AbstractUpdateTask instance = getAbstractUpdateImpl();
    117         boolean expResult = true;
    118         boolean result = instance.withinRange(lastRun, current, range);
    119         assertEquals(expResult, result);
    120
    121         lastRun = c.getTimeInMillis() - (8 * (1000 * 60 * 60 * 24));
    122         expResult = false;
    123         result = instance.withinRange(lastRun, current, range);
    124         assertEquals(expResult, result);
    125     }
    126
    127     public class AbstractUpdateImpl extends AbstractUpdateTask {
    128
    129         public AbstractUpdateImpl(DataStoreMetaInfo props) throws Exception {
    130             super(props);
    131         }
    132
    133         public Updateable updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException {
    134             return null;
    135         }
    136
    137         public void update() throws UpdateException {
    138         }
    139     }
    140 }