1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
29
30 public class SettingsTest extends BaseTest {
31
32
33
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
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
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
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
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
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);
89 String expResults = Settings.getString(key);
90 Assert.assertEquals(expResults, value);
91 }
92
93
94
95
96 @Test
97 public void testSetStringIfNotEmpty() {
98 String key = "optionalProperty";
99 String value = "someValue";
100 Settings.setString(key, value);
101 Settings.setStringIfNotEmpty(key, "");
102 String expResults = Settings.getString(key);
103 Assert.assertEquals(expResults, value);
104 }
105
106
107
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
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
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
144
145 @Test
146 public void testGetIntDefault() throws InvalidSettingException {
147 String key = "SomeKey";
148 int expResult = 85;
149 Settings.setString(key, "blue");
150 int result = Settings.getInt(key, expResult);
151 Assert.assertEquals(expResult, result);
152 }
153
154
155
156
157 @Test
158 public void testGetLong() throws InvalidSettingException {
159 String key = "SomeNumber";
160 long expResult = 300L;
161 Settings.setString(key, "300");
162 long result = Settings.getLong(key);
163 Assert.assertEquals(expResult, result);
164 }
165
166
167
168
169 @Test
170 public void testGetBoolean() throws InvalidSettingException {
171 String key = "SomeBoolean";
172 Settings.setString(key, "false");
173 boolean expResult = false;
174 boolean result = Settings.getBoolean(key);
175 Assert.assertEquals(expResult, result);
176
177 key = "something that does not exist";
178 expResult = true;
179 result = Settings.getBoolean(key, true);
180 Assert.assertEquals(expResult, result);
181 }
182
183
184
185
186 @Test
187 public void testRemoveProperty() {
188 String key = "SomeKey";
189 String value = "value";
190 String dfault = "default";
191 Settings.setString(key, value);
192 String ret = Settings.getString(key);
193 Assert.assertEquals(value, ret);
194 Settings.removeProperty(key);
195 ret = Settings.getString(key, dfault);
196 Assert.assertEquals(dfault, ret);
197 }
198
199
200
201
202 @Test
203 public void testGetConnectionString() throws Exception {
204 String value = Settings.getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME);
205 Assert.assertNotNull(value);
206 String msg = null;
207 try {
208 value = Settings.getConnectionString("invalidKey", null);
209 } catch (InvalidSettingException e) {
210 msg = e.getMessage();
211 }
212 Assert.assertNotNull(msg);
213 }
214
215
216
217
218 @Test
219 public void testGetTempDirectory() throws Exception {
220 File tmp = Settings.getTempDirectory();
221 Assert.assertTrue(tmp.exists());
222 }
223 }