View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assumptions.assumeTrue;
25  
26  import org.junit.jupiter.api.Test;
27  
28  public class SystemPropertiesTest {
29  
30      private boolean isJava11OrGreater() {
31          return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_11);
32      }
33  
34      @Test
35      public void testGetAwtToolkit() {
36          assertDoesNotThrow(SystemProperties::getAwtToolkit);
37      }
38  
39      @Test
40      public void testGetBoolean() {
41          final String key = RandomStringUtils.random(10);
42          final String absentKey = RandomStringUtils.random(10);
43          assertNull(System.getProperty(absentKey));
44          try {
45              System.setProperty(key, Boolean.toString(Boolean.TRUE));
46              assertEquals(Boolean.TRUE, SystemProperties.getBoolean(key, () -> false));
47              assertEquals(Boolean.TRUE, SystemProperties.getBoolean(absentKey, () -> Boolean.TRUE));
48              assertEquals(false, SystemProperties.getBoolean(absentKey, () -> false));
49              assertEquals(false, SystemProperties.getBoolean(absentKey, null));
50          } finally {
51              System.clearProperty(key);
52          }
53      }
54  
55      @Test
56      public void testGetFileEncoding() {
57          assertNotNull(SystemProperties.getFileEncoding());
58      }
59  
60      @Test
61      public void testGetFileSeparator() {
62          assertNotNull(SystemProperties.getFileSeparator());
63      }
64  
65      @Test
66      public void testGetInt() {
67          final String key = RandomStringUtils.random(10);
68          final String absentKey = RandomStringUtils.random(10);
69          assertNull(System.getProperty(absentKey));
70          try {
71              System.setProperty(key, Integer.toString(Integer.MAX_VALUE));
72              assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(key, () -> 0));
73              assertEquals(Integer.MAX_VALUE, SystemProperties.getInt(absentKey, () -> Integer.MAX_VALUE));
74              assertEquals(0, SystemProperties.getInt(absentKey, () -> 0));
75              assertEquals(0, SystemProperties.getInt(absentKey, null));
76          } finally {
77              System.clearProperty(key);
78          }
79      }
80  
81      @Test
82      public void testGetJavaAwtFonts() {
83          assertNull(SystemProperties.getJavaAwtFonts());
84      }
85  
86      @Test
87      public void testGetJavaAwtGraphicsenv() {
88          assertDoesNotThrow(SystemProperties::getJavaAwtGraphicsenv);
89      }
90  
91      @Test
92      public void testGetJavaAwtHeadless() {
93          assertNull(SystemProperties.getJavaAwtHeadless());
94      }
95  
96      @Test
97      public void testGetJavaAwtPrinterjob() {
98          assertDoesNotThrow(SystemProperties::getJavaAwtPrinterjob);
99      }
100 
101     @Test
102     public void testGetJavaClassPath() {
103         assertNotNull(SystemProperties.getJavaClassPath());
104     }
105 
106     @Test
107     public void testGetJavaClassVersion() {
108         assertNotNull(SystemProperties.getJavaClassVersion());
109     }
110 
111     @Test
112     public void testGetJavaCompiler() {
113         if (SystemUtils.IS_JAVA_14) {
114             // Not in Java 11
115             assertNotNull(SystemProperties.getJavaCompiler());
116         }
117     }
118 
119     @Test
120     public void testGetJavaEndorsedDirs() {
121         if (isJava11OrGreater()) {
122             // Not in Java 11
123             assertNull(SystemProperties.getJavaEndorsedDirs());
124         } else {
125             assertNotNull(SystemProperties.getJavaExtDirs());
126         }
127     }
128 
129     @Test
130     public void testGetJavaExtDirs() {
131         if (isJava11OrGreater()) {
132             // Not in Java 11
133             assertNull(SystemProperties.getJavaExtDirs());
134         } else {
135             assertNotNull(SystemProperties.getJavaExtDirs());
136         }
137     }
138 
139     @Test
140     public void testGetJavaHome() {
141         assertNotNull(SystemProperties.getJavaHome());
142     }
143 
144     @Test
145     public void testGetJavaIoTmpdir() {
146         assertNotNull(SystemProperties.getJavaIoTmpdir());
147     }
148 
149     @Test
150     public void testGetJavaLibraryPath() {
151         assertNotNull(SystemProperties.getJavaLibraryPath());
152     }
153 
154     @Test
155     public void testGetJavaLocaleProviders() {
156         assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
157         // default is null
158         assertNull(SystemProperties.getJavaLocaleProviders(), SystemProperties.getJavaVersion());
159     }
160 
161     @Test
162     public void testGetJavaRuntimeName() {
163         assertNotNull(SystemProperties.getJavaRuntimeName());
164     }
165 
166     @Test
167     public void testGetJavaRuntimeVersion() {
168         assertNotNull(SystemProperties.getJavaRuntimeVersion());
169     }
170 
171     @Test
172     public void testGetJavaSpecificationName() {
173         assertNotNull(SystemProperties.getJavaSpecificationName());
174     }
175 
176     @Test
177     public void testGetJavaSpecificationVendor() {
178         assertNotNull(SystemProperties.getJavaSpecificationVendor());
179     }
180 
181     @Test
182     public void testGetJavaSpecificationVersion() {
183         assertNotNull(SystemProperties.getJavaSpecificationVersion());
184     }
185 
186     @Test
187     public void testGetJavaUtilPrefsPreferencesFactory() {
188         assertNull(SystemProperties.getJavaUtilPrefsPreferencesFactory());
189     }
190 
191     @Test
192     public void testGetJavaVendor() {
193         assertNotNull(SystemProperties.getJavaVendor());
194     }
195 
196     @Test
197     public void testGetJavaVendorUrl() {
198         assertNotNull(SystemProperties.getJavaVendorUrl());
199     }
200 
201     @Test
202     public void testGetJavaVersion() {
203         assertNotNull(SystemProperties.getJavaVersion());
204     }
205 
206     @Test
207     public void testGetJavaVmInfo() {
208         assertNotNull(SystemProperties.getJavaVmInfo());
209     }
210 
211     @Test
212     public void testGetJavaVmName() {
213         assertNotNull(SystemProperties.getJavaVmName());
214     }
215 
216     @Test
217     public void testGetJavaVmSpecificationName() {
218         assertNotNull(SystemProperties.getJavaVmSpecificationName());
219     }
220 
221     @Test
222     public void testGetJavaVmSpecificationVendor() {
223         assertNotNull(SystemProperties.getJavaVmSpecificationVendor());
224     }
225 
226     @Test
227     public void testGetJavaVmSpecificationVersion() {
228         assertNotNull(SystemProperties.getJavaVmSpecificationVersion());
229     }
230 
231     @Test
232     public void testGetJavaVmVendor() {
233         assertNotNull(SystemProperties.getJavaVmVendor());
234     }
235 
236     @Test
237     public void testGetJavaVmVersion() {
238         assertNotNull(SystemProperties.getJavaVmVersion());
239     }
240 
241     @Test
242     public void testGetLineSeparator() {
243         assertNotNull(SystemProperties.getLineSeparator());
244     }
245 
246     @Test
247     public void testGetLong() {
248         final String key = RandomStringUtils.random(10);
249         final String absentKey = RandomStringUtils.random(10);
250         assertNull(System.getProperty(absentKey));
251         try {
252             System.setProperty(key, Long.toString(Long.MAX_VALUE));
253             assertEquals(Long.MAX_VALUE, SystemProperties.getLong(key, () -> 0));
254             assertEquals(Long.MAX_VALUE, SystemProperties.getLong(absentKey, () -> Long.MAX_VALUE));
255             assertEquals(0, SystemProperties.getLong(absentKey, () -> 0));
256             assertEquals(0, SystemProperties.getLong(absentKey, null));
257         } finally {
258             System.clearProperty(key);
259         }
260     }
261 
262     @Test
263     public void testGetOsArch() {
264         assertNotNull(SystemProperties.getOsArch());
265     }
266 
267     @Test
268     public void testGetOsName() {
269         assertNotNull(SystemProperties.getOsName());
270     }
271 
272 
273     @Test
274     public void testGetOsVersion() {
275         assertNotNull(SystemProperties.getOsVersion());
276     }
277 
278     @Test
279     public void testGetPathSeparator() {
280         assertNotNull(SystemProperties.getPathSeparator());
281     }
282 
283     @Test
284     public void testGetUserCountry() {
285         assertDoesNotThrow(SystemProperties::getUserCountry);
286     }
287 
288     @Test
289     public void testGetUserDir() {
290         assertNotNull(SystemProperties.getUserDir());
291     }
292 
293     @Test
294     public void testGetUserHome() {
295         assertNotNull(SystemProperties.getUserHome());
296     }
297 
298     @Test
299     public void testGetUserLanguage() {
300         assertNotNull(SystemProperties.getUserLanguage());
301     }
302 
303     @Test
304     public void testGetUserName() {
305         assertNotNull(SystemProperties.getUserName());
306     }
307 
308     @Test
309     public void testGetUserTimezone() {
310         assertDoesNotThrow(SystemProperties::getUserTimezone);
311     }
312 
313 }