-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesTest.java
57 lines (49 loc) · 1.73 KB
/
PropertiesTest.java
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
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
/**
*
*/
public class PropertiesTest extends Assert {
@Test
public void test() {
// Получение несуществующего свойства
assertNull(System.getProperty("NOT-EXISTING-PROPERTY"));
String value = System.getProperty("NOT-EXISTING-PROPERTY");
if (value != null && !value.isEmpty()) {
fail("Нет такого свойства :)");
}
System.out.println("Все системные свойства:");
Properties properties = System.getProperties();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
System.out.println(" " + entry.getKey() + " = " + entry.getValue());
}
}
/**
* Свойства из файла настроек
*
* @throws IOException
*/
@Test
public void testConfigProperties() throws IOException {
String filename = "config.properties";
InputStream input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return;
}
Properties properties = new Properties();
properties.load(new InputStreamReader(input, "UTF-8"));
Enumeration<?> e = properties.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = properties.getProperty(key);
System.out.println(key + " = " + value);
}
}
}