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 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
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
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
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
205
206 @Test
207 public void testGetTempDirectory() throws Exception {
208 File tmp = Settings.getTempDirectory();
209 Assert.assertTrue(tmp.exists());
210 }
211 }