View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.net.URISyntaxException;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /**
27   *
28   * @author Jeremy Long
29   */
30  public class SettingsTest extends BaseTest {
31  
32      /**
33       * Test of getString method, of class Settings.
34       */
35      @Test
36      public void testGetString() {
37          String key = Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS;
38          String expResult = "7";
39          String result = Settings.getString(key);
40          Assert.assertTrue(result.endsWith(expResult));
41      }
42  
43      /**
44       * Test of getDataFile method, of class Settings.
45       */
46      @Test
47      public void testGetDataFile() throws IOException {
48          String key = Settings.KEYS.DATA_DIRECTORY;
49          String expResult = "data";
50          File result = Settings.getDataFile(key);
51          Assert.assertTrue(result.getAbsolutePath().endsWith(expResult));
52      }
53  
54      /**
55       * Test of mergeProperties method, of class Settings.
56       */
57      @Test
58      public void testMergeProperties_String() throws IOException, URISyntaxException {
59          String key = Settings.KEYS.PROXY_PORT;
60          String expResult = Settings.getString(key);
61          File f = new File(this.getClass().getClassLoader().getResource("test.properties").toURI());
62          //InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.properties");
63          Settings.mergeProperties(f.getAbsolutePath());
64          String result = Settings.getString(key);
65          Assert.assertTrue("setting didn't change?", (expResult == null && result != null) || !expResult.equals(result));
66      }
67  
68      /**
69       * Test of setString method, of class Settings.
70       */
71      @Test
72      public void testSetString() {
73          String key = "newProperty";
74          String value = "someValue";
75          Settings.setString(key, value);
76          String expResults = Settings.getString(key);
77          Assert.assertEquals(expResults, value);
78      }
79  
80      /**
81       * Test of setStringIfNotNull method, of class Settings.
82       */
83      @Test
84      public void testSetStringIfNotNull() {
85          String key = "nullableProperty";
86          String value = "someValue";
87          Settings.setString(key, value);
88          Settings.setStringIfNotNull(key, null); // NO-OP
89          String expResults = Settings.getString(key);
90          Assert.assertEquals(expResults, value);
91      }
92  
93      /**
94       * Test of setStringIfNotNull method, of class Settings.
95       */
96      @Test
97      public void testSetStringIfNotEmpty() {
98          String key = "optionalProperty";
99          String value = "someValue";
100         Settings.setString(key, value);
101         Settings.setStringIfNotEmpty(key, ""); // NO-OP
102         String expResults = Settings.getString(key);
103         Assert.assertEquals(expResults, value);
104     }
105 
106     /**
107      * Test of getString method, of class Settings.
108      */
109     @Test
110     public void testGetString_String_String() {
111         String key = "key That Doesn't Exist";
112         String defaultValue = "blue bunny";
113         String expResult = "blue bunny";
114         String result = Settings.getString(key);
115         Assert.assertTrue(result == null);
116         result = Settings.getString(key, defaultValue);
117         Assert.assertEquals(expResult, result);
118     }
119 
120     /**
121      * Test of getString method, of class Settings.
122      */
123     @Test
124     public void testGetString_String() {
125         String key = Settings.KEYS.CONNECTION_TIMEOUT;
126         String result = Settings.getString(key);
127         Assert.assertTrue(result == null);
128     }
129 
130     /**
131      * Test of getInt method, of class Settings.
132      */
133     @Test
134     public void testGetInt() throws InvalidSettingException {
135         String key = "SomeNumber";
136         int expResult = 85;
137         Settings.setString(key, "85");
138         int result = Settings.getInt(key);
139         Assert.assertEquals(expResult, result);
140     }
141 
142     /**
143      * Test of getLong method, of class Settings.
144      */
145     @Test
146     public void testGetLong() throws InvalidSettingException {
147         String key = "SomeNumber";
148         long expResult = 300L;
149         Settings.setString(key, "300");
150         long result = Settings.getLong(key);
151         Assert.assertEquals(expResult, result);
152     }
153 
154     /**
155      * Test of getBoolean method, of class Settings.
156      */
157     @Test
158     public void testGetBoolean() throws InvalidSettingException {
159         String key = "SomeBoolean";
160         Settings.setString(key, "false");
161         boolean expResult = false;
162         boolean result = Settings.getBoolean(key);
163         Assert.assertEquals(expResult, result);
164 
165         key = "something that does not exist";
166         expResult = true;
167         result = Settings.getBoolean(key, true);
168         Assert.assertEquals(expResult, result);
169     }
170 
171     /**
172      * Test of removeProperty method, of class Settings.
173      */
174     @Test
175     public void testRemoveProperty() {
176         String key = "SomeKey";
177         String value = "value";
178         String dfault = "default";
179         Settings.setString(key, value);
180         String ret = Settings.getString(key);
181         Assert.assertEquals(value, ret);
182         Settings.removeProperty(key);
183         ret = Settings.getString(key, dfault);
184         Assert.assertEquals(dfault, ret);
185     }
186 
187     /**
188      * Test of getConnectionString.
189      */
190     @Test
191     public void testGetConnectionString() throws Exception {
192         String value = Settings.getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME);
193         Assert.assertNotNull(value);
194         String msg = null;
195         try {
196             value = Settings.getConnectionString("invalidKey", null);
197         } catch (InvalidSettingException e) {
198             msg = e.getMessage();
199         }
200         Assert.assertNotNull(msg);
201     }
202 
203     /**
204      * Test of getTempDirectory.
205      */
206     @Test
207     public void testGetTempDirectory() throws Exception {
208         File tmp = Settings.getTempDirectory();
209         Assert.assertTrue(tmp.exists());
210     }
211 }