1
2
3
4
5 package oshi.util;
6
7 import static java.lang.String.format;
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.Matchers.closeTo;
10 import static org.hamcrest.Matchers.is;
11 import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
12 import static oshi.util.GlobalConfig.clear;
13 import static oshi.util.GlobalConfig.get;
14 import static oshi.util.GlobalConfig.load;
15 import static oshi.util.GlobalConfig.remove;
16 import static oshi.util.GlobalConfig.set;
17 import static oshi.util.GlobalConfigTest.GlobalConfigAsserter.asserter;
18
19 import java.util.Locale;
20 import java.util.Properties;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.parallel.Execution;
25
26 import oshi.util.GlobalConfig.PropertyException;
27
28 @Execution(SAME_THREAD)
29 class GlobalConfigTest {
30 private static final String PROPERTY = "oshi.test.property";
31 private static final double EPSILON = Double.MIN_VALUE;
32
33 @BeforeEach
34 void setUp() {
35 clear();
36 }
37
38 @Test
39 void testGetString() {
40 asserter(PROPERTY).assertDefaultThat(null, null);
41 set(PROPERTY, "test");
42 asserter(PROPERTY).assertThat("test", null);
43 set(PROPERTY, 123);
44
45 asserter(PROPERTY).assertThat("123", null);
46 }
47
48 @Test
49 void testGetInteger() {
50 asserter(PROPERTY).assertDefaultThat(0, 0);
51 set(PROPERTY, 123);
52 asserter(PROPERTY).assertThat(123, 0).assertThat("123", null);
53
54
55 set(PROPERTY, "1.23");
56 asserter(PROPERTY).assertThat(0, 0);
57 }
58
59 @Test
60 void testGetDouble() {
61 asserter(PROPERTY).assertDefaultThat(0.0, 0.0);
62 set(PROPERTY, 1.23d);
63 asserter(PROPERTY).assertThat(1.23, 0.0).assertThat("1.23", null);
64
65
66 set(PROPERTY, "1.2.3");
67 asserter(PROPERTY).assertThat(0.0, 0.0);
68 }
69
70 @Test
71 void testGetBoolean() {
72 asserter(PROPERTY).assertDefaultThat(false, false);
73 set(PROPERTY, true);
74 asserter(PROPERTY).assertThat(true, false).assertThat("true", null);
75 }
76
77 @Test
78 void testSetNull() {
79 set(PROPERTY, "test");
80 set(PROPERTY, null);
81 asserter(PROPERTY).assertThat("123", "123");
82 }
83
84 @Test
85 void testRemove() {
86 String removed = "test";
87 set(PROPERTY, removed);
88 remove(PROPERTY);
89 asserter(PROPERTY).assertThat(format(Locale.ROOT, "Should have removed property %s", removed), "123", "123");
90 }
91
92 @Test
93 void testLoad() {
94 load(propertiesWith("321"));
95
96 asserter(PROPERTY).assertThat("321", null);
97 }
98
99 @Test
100 void testPropertyExceptionMessage() {
101 set(PROPERTY, "test");
102 assertThat(new PropertyException(PROPERTY).getMessage(),
103 is(format(Locale.ROOT, "Invalid property: \"%s\" = test", PROPERTY)));
104 }
105
106 private Properties propertiesWith(String value) {
107 Properties updates = new Properties();
108 updates.setProperty(PROPERTY, value);
109 return updates;
110 }
111
112 static final class GlobalConfigAsserter {
113 private static final String FAILURE_MESSAGE_TEMPLATE = "property: %s value for def: %s should be";
114 private static final String DEFAULT_FAILURE_MESSAGE_TEMPLATE = "Property: %s default value def: %s should be";
115 private final String property;
116
117 private GlobalConfigAsserter(String property) {
118 this.property = property;
119 }
120
121 static GlobalConfigAsserter asserter(String property) {
122 return new GlobalConfigAsserter(property);
123 }
124
125 GlobalConfigAsserter assertThat(Object expected, Object def) {
126 assertThat(failureMessage(def), expected, def);
127 return this;
128 }
129
130 GlobalConfigAsserter assertThat(String message, Object expected, Object def) {
131 if (def instanceof String) {
132 assertThat(message, get(property, (String) def), is(expected));
133 } else if (def instanceof Boolean) {
134 assertThat(message, get(property, (boolean) def), is(expected));
135 } else if (def instanceof Integer) {
136 assertThat(message, get(property, (Integer) def), is(expected));
137 } else if (def instanceof Double) {
138 assertThat(message, get(property, (Double) def), is(closeTo((Double) expected, EPSILON)));
139 }
140 return this;
141 }
142
143 GlobalConfigAsserter assertDefaultThat(Object expected, Object def) {
144 assertThat(defaultFailureMessage(def), expected, def);
145 return this;
146 }
147
148 private String failureMessage(Object def) {
149 return format(Locale.ROOT, FAILURE_MESSAGE_TEMPLATE, property, def);
150 }
151
152 private String defaultFailureMessage(Object def) {
153 return format(Locale.ROOT, DEFAULT_FAILURE_MESSAGE_TEMPLATE, PROPERTY, def);
154 }
155 }
156 }