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 testGetString_String_String() {
85 String key = "key That Doesn't Exist";
86 String defaultValue = "blue bunny";
87 String expResult = "blue bunny";
88 String result = Settings.getString(key);
89 Assert.assertTrue(result == null);
90 result = Settings.getString(key, defaultValue);
91 Assert.assertEquals(expResult, result);
92 }
93
94
95
96
97 @Test
98 public void testGetString_String() {
99 String key = Settings.KEYS.CONNECTION_TIMEOUT;
100 String result = Settings.getString(key);
101 Assert.assertTrue(result == null);
102 }
103
104
105
106
107 @Test
108 public void testGetInt() throws InvalidSettingException {
109 String key = "SomeNumber";
110 int expResult = 85;
111 Settings.setString(key, "85");
112 int result = Settings.getInt(key);
113 Assert.assertEquals(expResult, result);
114 }
115
116
117
118
119 @Test
120 public void testGetLong() throws InvalidSettingException {
121 String key = "SomeNumber";
122 long expResult = 300L;
123 Settings.setString(key, "300");
124 long result = Settings.getLong(key);
125 Assert.assertEquals(expResult, result);
126 }
127
128
129
130
131 @Test
132 public void testGetBoolean() throws InvalidSettingException {
133 String key = "SomeBoolean";
134 Settings.setString(key, "false");
135 boolean expResult = false;
136 boolean result = Settings.getBoolean(key);
137 Assert.assertEquals(expResult, result);
138
139 key = "something that does not exist";
140 expResult = true;
141 result = Settings.getBoolean(key, true);
142 Assert.assertEquals(expResult, result);
143 }
144
145
146
147
148 @Test
149 public void testRemoveProperty() {
150 String key = "SomeKey";
151 String value = "value";
152 String dfault = "default";
153 Settings.setString(key, value);
154 String ret = Settings.getString(key);
155 Assert.assertEquals(value, ret);
156 Settings.removeProperty(key);
157 ret = Settings.getString(key, dfault);
158 Assert.assertEquals(dfault, ret);
159 }
160
161
162
163
164 @Test
165 public void testGetConnectionString() throws Exception {
166 String value = Settings.getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME, Settings.KEYS.DB_VERSION);
167 Assert.assertNotNull(value);
168 String msg = null;
169 try {
170 value = Settings.getConnectionString(Settings.KEYS.DB_CONNECTION_STRING, Settings.KEYS.DB_FILE_NAME, null);
171 } catch (InvalidSettingException e) {
172 msg = e.getMessage();
173 }
174 Assert.assertNotNull(msg, msg);
175 try {
176 value = Settings.getConnectionString("invalidKey", null, null);
177 } catch (InvalidSettingException e) {
178 msg = e.getMessage();
179 }
180 Assert.assertNotNull(msg, msg);
181 }
182
183
184
185
186 @Test
187 public void testGetTempDirectory() throws Exception {
188 File tmp = Settings.getTempDirectory();
189 Assert.assertTrue(tmp.exists());
190 }
191 }