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.net.MalformedURLException;
    23  import org.apache.commons.io.FileUtils;
    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.data.UpdateException;
    31  import org.owasp.dependencycheck.utils.DownloadFailedException;
    32  import org.owasp.dependencycheck.utils.Settings;
    33
    34  /**
    35   *
    36   * @author Jeremy Long (jeremy.long@owasp.org)
    37   */
    38  public class BatchUpdateTaskTest {
    39
    40      public BatchUpdateTaskTest() {
    41      }
    42
    43      @BeforeClass
    44      public static void setUpClass() {
    45      }
    46
    47      @AfterClass
    48      public static void tearDownClass() {
    49      }
    50      private String old12;
    51      private String old20;
    52
    53      @Before
    54      public void setUp() throws Exception {
    55          old12 = Settings.getString(Settings.KEYS.CVE_MODIFIED_12_URL);
    56          old20 = Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL);
    57
    58          File tmp = Settings.getTempDirectory();
    59          if (!tmp.exists()) {
    60              tmp.mkdirs();
    61          }
    62
    63          File dest = new File(tmp, "data.zip");
    64          File file = new File(this.getClass().getClassLoader().getResource("data.zip").toURI());
    65          FileUtils.copyFile(file, dest);
    66          String path = "file:///" + dest.getCanonicalPath();
    67          Settings.setString(Settings.KEYS.BATCH_UPDATE_URL, path);
    68
    69          dest = new File(tmp, "nvdcve-2012.xml");
    70          file = new File(this.getClass().getClassLoader().getResource("nvdcve-2012.xml").toURI());
    71          FileUtils.copyFile(file, dest);
    72          path = "file:///" + dest.getCanonicalPath();
    73          Settings.setString(Settings.KEYS.CVE_MODIFIED_12_URL, path);
    74
    75          dest = new File(tmp, "nvdcve-2.0-2012.xml");
    76          file = new File(this.getClass().getClassLoader().getResource("nvdcve-2.0-2012.xml").toURI());
    77          FileUtils.copyFile(file, dest);
    78          path = "file:///" + dest.getCanonicalPath();
    79          Settings.setString(Settings.KEYS.CVE_MODIFIED_20_URL, path);
    80      }
    81
    82      @After
    83      public void tearDown() {
    84          Settings.setString(Settings.KEYS.CVE_MODIFIED_12_URL, old12);
    85          Settings.setString(Settings.KEYS.CVE_MODIFIED_20_URL, old20);
    86          Settings.setString(Settings.KEYS.BATCH_UPDATE_URL, "");
    87      }
    88
    89      public BatchUpdateTask getBatchUpdateTask() throws MalformedURLException, DownloadFailedException, UpdateException {
    90          DataStoreMetaInfo props = new DataStoreMetaInfo();
    91          BatchUpdateTask instance = new BatchUpdateTask(props);
    92          return instance;
    93      }
    94
    95      /**
    96       * Test of setDoBatchUpdate method, of class BatchUpdateTask.
    97       */
    98      @Test
    99      public void testSetDoBatchUpdate() throws DownloadFailedException, MalformedURLException, UpdateException {
    100         boolean expected = false;
    101         BatchUpdateTask instance = getBatchUpdateTask();
    102         instance.setDoBatchUpdate(expected);
    103         boolean results = instance.isDoBatchUpdate();
    104         assertEquals(results, expected);
    105     }
    106
    107     /**
    108      * Test of update method, of class BatchUpdateTask.
    109      */
    110     @Test
    111     public void testUpdate() throws Exception {
    112         BatchUpdateTask instance = getBatchUpdateTask();
    113
    114         //do some setup
    115         instance.setDoBatchUpdate(true);
    116         instance.deleteExistingData();
    117
    118         instance.update(); //no exceptions it worked?
    119         //todo add some actual asserts to check things.
    120     }
    121 }