Skip to content

Commit

Permalink
When System property is empty use def value.
Browse files Browse the repository at this point in the history
Motivation:

When system property is empty, the default value should be used.

Modification:

- Correctly use the default value in all cases
- Add unit tests

Result:

Correct behaviour
  • Loading branch information
Soner Kaya authored and normanmaurer committed Nov 23, 2017
1 parent c1b1d62 commit f9cadc0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static boolean getBoolean(String key, boolean def) {

value = value.trim().toLowerCase();
if (value.isEmpty()) {
return true;
return def;
}

if ("true".equals(value) || "yes".equals(value) || "1".equals(value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2017 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class SystemPropertyUtilTest {

@Before
public void clearSystemPropertyBeforeEach() {
System.clearProperty("key");
}

@Test(expected = NullPointerException.class)
public void testGetWithKeyNull() {
SystemPropertyUtil.get(null, null);
}

@Test(expected = IllegalArgumentException.class)
public void testGetWithKeyEmpty() {
SystemPropertyUtil.get("", null);
}

@Test
public void testGetDefaultValueWithPropertyNull() {
assertEquals("default", SystemPropertyUtil.get("key", "default"));
}

@Test
public void testGetPropertyValue() {
System.setProperty("key", "value");
assertEquals("value", SystemPropertyUtil.get("key"));
}

@Test
public void testGetBooleanDefaultValueWithPropertyNull() {
assertTrue(SystemPropertyUtil.getBoolean("key", true));
assertFalse(SystemPropertyUtil.getBoolean("key", false));
}

@Test
public void testGetBooleanDefaultValueWithEmptyString() {
System.setProperty("key", "");
assertTrue(SystemPropertyUtil.getBoolean("key", true));
assertFalse(SystemPropertyUtil.getBoolean("key", false));
}

@Test
public void testGetBooleanWithTrueValue() {
System.setProperty("key", "true");
assertTrue(SystemPropertyUtil.getBoolean("key", false));
System.setProperty("key", "yes");
assertTrue(SystemPropertyUtil.getBoolean("key", false));
System.setProperty("key", "1");
assertTrue(SystemPropertyUtil.getBoolean("key", true));
}

@Test
public void testGetBooleanWithFalseValue() {
System.setProperty("key", "false");
assertFalse(SystemPropertyUtil.getBoolean("key", true));
System.setProperty("key", "no");
assertFalse(SystemPropertyUtil.getBoolean("key", false));
System.setProperty("key", "0");
assertFalse(SystemPropertyUtil.getBoolean("key", true));
}

@Test
public void testGetBooleanDefaultValueWithWrongValue() {
System.setProperty("key", "abc");
assertTrue(SystemPropertyUtil.getBoolean("key", true));
System.setProperty("key", "123");
assertFalse(SystemPropertyUtil.getBoolean("key", false));
}

@Test
public void getIntDefaultValueWithPropertyNull() {
assertEquals(1, SystemPropertyUtil.getInt("key", 1));
}

@Test
public void getIntWithPropertValueIsInt() {
System.setProperty("key", "123");
assertEquals(123, SystemPropertyUtil.getInt("key", 1));
}

@Test
public void getIntDefaultValueWithPropertValueIsNotInt() {
System.setProperty("key", "NotInt");
assertEquals(1, SystemPropertyUtil.getInt("key", 1));
}

@Test
public void getLongDefaultValueWithPropertyNull() {
assertEquals(1, SystemPropertyUtil.getLong("key", 1));
}

@Test
public void getLongWithPropertValueIsLong() {
System.setProperty("key", "123");
assertEquals(123, SystemPropertyUtil.getLong("key", 1));
}

@Test
public void getLongDefaultValueWithPropertValueIsNotLong() {
System.setProperty("key", "NotInt");
assertEquals(1, SystemPropertyUtil.getLong("key", 1));
}

}

0 comments on commit f9cadc0

Please sign in to comment.