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) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.utils;
    20
    21  import org.owasp.dependencycheck.utils.InvalidSettingException;
    22  import org.owasp.dependencycheck.utils.Settings;
    23  import java.io.File;
    24  import java.io.IOException;
    25  import java.net.URISyntaxException;
    26  import org.junit.After;
    27  import org.junit.AfterClass;
    28  import org.junit.Assert;
    29  import org.junit.Before;
    30  import org.junit.BeforeClass;
    31  import org.junit.Test;
    32
    33  /**
    34   *
    35   * @author Jeremy Long (jeremy.long@owasp.org)
    36   */
    37  public class SettingsTest {
    38
    39      @BeforeClass
    40      public static void setUpClass() throws Exception {
    41      }
    42
    43      @AfterClass
    44      public static void tearDownClass() throws Exception {
    45      }
    46
    47      @Before
    48      public void setUp() throws Exception {
    49      }
    50
    51      @After
    52      public void tearDown() throws Exception {
    53      }
    54
    55      /**
    56       * Test of getString method, of class Settings.
    57       */
    58      @Test
    59      public void testGetString() {
    60          String key = Settings.KEYS.CPE_DATA_DIRECTORY;
    61          String expResult = "cpe";
    62          String result = Settings.getString(key);
    63          Assert.assertTrue(result.endsWith(expResult));
    64      }
    65
    66      /**
    67       * Test of getFile method, of class Settings.
    68       */
    69      @Test
    70      public void testGetFile() throws IOException {
    71          String key = Settings.KEYS.CPE_DATA_DIRECTORY;
    72          String expResult = "data" + File.separator + "cpe";
    73          File result = Settings.getFile(key);
    74          Assert.assertTrue(result.getAbsolutePath().endsWith(expResult));
    75
    76          result = Settings.getFile(Settings.KEYS.DATA_DIRECTORY);
    77          String path = result.getPath();
    78          Assert.assertTrue(path.endsWith("data") || path.endsWith("data" + File.separator));
    79      }
    80
    81      /**
    82       * Test of mergeProperties method, of class Settings.
    83       */
    84      @Test
    85      public void testMergeProperties_String() throws IOException, URISyntaxException {
    86          String key = Settings.KEYS.PROXY_PORT;
    87          String expResult = Settings.getString(key);
    88          File f = new File(this.getClass().getClassLoader().getResource("test.properties").toURI());
    89          //InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.properties");
    90          Settings.mergeProperties(f.getAbsolutePath());
    91          String result = Settings.getString(key);
    92          Assert.assertTrue("setting didn't change?", (expResult == null && result != null) || !expResult.equals(result));
    93      }
    94
    95      /**
    96       * Test of setString method, of class Settings.
    97       */
    98      @Test
    99      public void testSetString() {
    100         String key = "newProperty";
    101         String value = "someValue";
    102         Settings.setString(key, value);
    103         String expResults = Settings.getString(key);
    104         Assert.assertEquals(expResults, value);
    105     }
    106
    107     /**
    108      * Test of getString method, of class Settings.
    109      */
    110     @Test
    111     public void testGetString_String_String() {
    112         String key = "key That Doesn't Exist";
    113         String defaultValue = "blue bunny";
    114         String expResult = "blue bunny";
    115         String result = Settings.getString(key);
    116         Assert.assertTrue(result == null);
    117         result = Settings.getString(key, defaultValue);
    118         Assert.assertEquals(expResult, result);
    119     }
    120
    121     /**
    122      * Test of getString method, of class Settings.
    123      */
    124     @Test
    125     public void testGetString_String() {
    126         String key = Settings.KEYS.CONNECTION_TIMEOUT;
    127         String result = Settings.getString(key);
    128         Assert.assertTrue(result == null);
    129     }
    130
    131     /**
    132      * Test of getInt method, of class Settings.
    133      */
    134     @Test
    135     public void testGetInt() throws InvalidSettingException {
    136         String key = "SomeNumber";
    137         int expResult = 85;
    138         Settings.setString(key, "85");
    139         int result = Settings.getInt(key);
    140         Assert.assertEquals(expResult, result);
    141     }
    142
    143     /**
    144      * Test of getLong method, of class Settings.
    145      */
    146     @Test
    147     public void testGetLong() throws InvalidSettingException {
    148         String key = "SomeNumber";
    149         long expResult = 300L;
    150         Settings.setString(key, "300");
    151         long result = Settings.getLong(key);
    152         Assert.assertEquals(expResult, result);
    153     }
    154
    155     /**
    156      * Test of getBoolean method, of class Settings.
    157      */
    158     @Test
    159     public void testGetBoolean() throws InvalidSettingException {
    160         String key = "SomeBoolean";
    161         Settings.setString(key, "false");
    162         boolean expResult = false;
    163         boolean result = Settings.getBoolean(key);
    164         Assert.assertEquals(expResult, result);
    165     }
    166
    167     /**
    168      * Test of removeProperty method, of class Settings.
    169      */
    170     @Test
    171     public void testRemoveProperty() {
    172         String key = "SomeKey";
    173         String value = "value";
    174         String dfault = "default";
    175         Settings.setString(key, value);
    176         String ret = Settings.getString(key);
    177         Assert.assertEquals(value, ret);
    178         Settings.removeProperty(key);
    179         ret = Settings.getString(key, dfault);
    180         Assert.assertEquals(dfault, ret);
    181     }
    182 }