1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.process;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@RunWith(DataProviderRunner.class)
public class PropsTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void constructor_trims_key_and_values_from_Properties_argument(String blankBefore, String blankAfter) {
Properties properties = new Properties();
String key = RandomStringUtils.randomAlphanumeric(3);
String value = RandomStringUtils.randomAlphanumeric(3);
properties.put(blankBefore + key + blankAfter, blankBefore + value + blankAfter);
Props underTest = new Props(properties);
if (!blankBefore.isEmpty() || !blankAfter.isEmpty()) {
assertThat(underTest.contains(blankBefore + key + blankAfter)).isFalse();
}
assertThat(underTest.value(key)).isEqualTo(value);
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void value(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty(blankBefore + "foo" + blankAfter, blankBefore + "bar" + blankAfter);
p.setProperty("blank", blankBefore + blankAfter);
Props props = new Props(p);
assertThat(props.value("foo")).isEqualTo("bar");
assertThat(props.value("foo", "default value")).isEqualTo("bar");
assertThat(props.value("blank")).isEmpty();
assertThat(props.value("blank", "default value")).isEmpty();
assertThat(props.value("unknown")).isNull();
assertThat(props.value("unknown", "default value")).isEqualTo("default value");
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void nonNullValue(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty("foo", blankBefore + "bar" + blankAfter);
Props props = new Props(p);
assertThat(props.nonNullValue("foo")).isEqualTo("bar");
}
@Test
public void nonNullValue_throws_IAE_on_non_existing_key() {
Props props = new Props(new Properties());
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Missing property: other");
props.nonNullValue("other");
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void nonNullValue_return_empty_string_IAE_on_existing_key_with_blank_value(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty("blank", blankBefore + blankAfter);
Props props = new Props(p);
assertThat(props.nonNullValue("blank")).isEmpty();
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void valueAsInt(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty("foo", blankBefore + "33" + blankAfter);
p.setProperty("blank", blankBefore + blankAfter);
Props props = new Props(p);
assertThat(props.valueAsInt("foo")).isEqualTo(33);
assertThat(props.valueAsInt("foo", 44)).isEqualTo(33);
assertThat(props.valueAsInt("blank")).isNull();
assertThat(props.valueAsInt("blank", 55)).isEqualTo(55);
assertThat(props.valueAsInt("unknown")).isNull();
assertThat(props.valueAsInt("unknown", 44)).isEqualTo(44);
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void valueAsInt_not_integer(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty("foo", blankBefore + "bar" + blankAfter);
Props props = new Props(p);
try {
props.valueAsInt("foo");
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Value of property foo is not an integer: bar");
}
}
@Test
@UseDataProvider("beforeAndAfterBlanks")
public void booleanOf(String blankBefore, String blankAfter) {
Properties p = new Properties();
p.setProperty("foo", blankBefore + "True" + blankAfter);
p.setProperty("bar", blankBefore + "false" + blankAfter);
Props props = new Props(p);
assertThat(props.valueAsBoolean("foo")).isTrue();
assertThat(props.valueAsBoolean("bar")).isFalse();
assertThat(props.valueAsBoolean("foo", false)).isTrue();
assertThat(props.valueAsBoolean("bar", true)).isFalse();
assertThat(props.valueAsBoolean("unknown")).isFalse();
assertThat(props.valueAsBoolean("unset", false)).isFalse();
assertThat(props.valueAsBoolean("unset", true)).isTrue();
}
@Test
public void setDefault() {
Properties p = new Properties();
p.setProperty("foo", "foo_value");
Props props = new Props(p);
props.setDefault("foo", "foo_def");
props.setDefault("bar", "bar_def");
assertThat(props.value("foo")).isEqualTo("foo_value");
assertThat(props.value("bar")).isEqualTo("bar_def");
assertThat(props.value("other")).isNull();
}
@Test
public void set() {
Properties p = new Properties();
p.setProperty("foo", "old_foo");
Props props = new Props(p);
props.set("foo", "new_foo");
props.set("bar", "new_bar");
assertThat(props.value("foo")).isEqualTo("new_foo");
assertThat(props.value("bar")).isEqualTo("new_bar");
}
@Test
public void raw_properties() {
Properties p = new Properties();
p.setProperty("encrypted_prop", "{aes}abcde");
p.setProperty("clear_prop", "foo");
Props props = new Props(p);
assertThat(props.rawProperties()).hasSize(2);
// do not decrypt
assertThat(props.rawProperties().get("encrypted_prop")).isEqualTo("{aes}abcde");
assertThat(props.rawProperties().get("clear_prop")).isEqualTo("foo");
}
@Test
public void nonNullValueAsFile() throws IOException {
File file = temp.newFile();
Props props = new Props(new Properties());
props.set("path", file.getAbsolutePath());
assertThat(props.nonNullValueAsFile("path")).isEqualTo(file);
try {
props.nonNullValueAsFile("other_path");
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Property other_path is not set");
}
}
@DataProvider
public static Object[][] beforeAndAfterBlanks() {
return new Object[][] {
{"", ""},
{" ", ""},
{"", " "},
{" ", " "},
};
}
}
|