1 /*
2  * Copyright (C) 2012 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.base;
18 
19 import static com.google.common.base.StandardSystemProperty.JAVA_COMPILER;
20 import static com.google.common.base.StandardSystemProperty.JAVA_EXT_DIRS;
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import junit.framework.TestCase;
24 
25 /**
26  * Tests for {@link StandardSystemProperty}.
27  *
28  * @author Kurt Alfred Kluever
29  */
30 public class StandardSystemPropertyTest extends TestCase {
31 
testGetKeyMatchesString()32   public void testGetKeyMatchesString() {
33     for (StandardSystemProperty property : StandardSystemProperty.values()) {
34       String fieldName = property.name();
35       String expected = Ascii.toLowerCase(fieldName).replaceAll("_", ".");
36       assertEquals(expected, property.key());
37     }
38   }
39 
testGetValue()40   public void testGetValue() {
41     for (StandardSystemProperty property : StandardSystemProperty.values()) {
42       assertEquals(System.getProperty(property.key()), property.value());
43     }
44   }
45 
testToString()46   public void testToString() {
47     for (StandardSystemProperty property : StandardSystemProperty.values()) {
48       assertEquals(property.key() + "=" + property.value(), property.toString());
49     }
50   }
51 
testNoNullValues()52   public void testNoNullValues() {
53     for (StandardSystemProperty property : StandardSystemProperty.values()) {
54       // Even though the contract in System.getProperties() specifies that a value will exist for
55       // all of the listed keys, for some reason the "java.compiler" key returns null in some JVMs.
56       if (property == JAVA_COMPILER) {
57         continue;
58       }
59       // Removed in Java 9:
60       // https://docs.oracle.com/javase/9/migrate/toc.htm#JSMIG-GUID-2C896CA8-927C-4381-A737-B1D81D964B7B
61       if (property == JAVA_EXT_DIRS) {
62         continue;
63       }
64       assertWithMessage(property.toString()).that(property.value()).isNotNull();
65     }
66   }
67 }
68