Unverified 提交 01397c72 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #818 from katzyn/misc

Fixes for remaining issues with boolean parameters
......@@ -333,7 +333,7 @@ public class ConnectionInfo implements Cloneable {
* @param defaultValue the default value
* @return the value
*/
boolean getProperty(String key, boolean defaultValue) {
public boolean getProperty(String key, boolean defaultValue) {
return Utils.parseBoolean(getProperty(key, null), defaultValue, false);
}
......@@ -345,8 +345,7 @@ public class ConnectionInfo implements Cloneable {
* @return the value
*/
public boolean removeProperty(String key, boolean defaultValue) {
String x = removeProperty(key, null);
return x == null ? defaultValue : Boolean.parseBoolean(x);
return Utils.parseBoolean(removeProperty(key, null), defaultValue, false);
}
/**
......
......@@ -335,8 +335,7 @@ public class SessionRemote extends SessionWithState implements DataHandler {
}
// create the session using reflection,
// so that the JDBC layer can be compiled without it
boolean autoServerMode = Boolean.parseBoolean(
ci.getProperty("AUTO_SERVER", "false"));
boolean autoServerMode = ci.getProperty("AUTO_SERVER", false);
ConnectionInfo backup = null;
try {
if (autoServerMode) {
......@@ -413,11 +412,9 @@ public class SessionRemote extends SessionWithState implements DataHandler {
serverList = StringUtils.quoteStringSQL(server);
ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED);
}
autoReconnect = Boolean.parseBoolean(ci.getProperty(
"AUTO_RECONNECT", "false"));
autoReconnect = ci.getProperty("AUTO_RECONNECT", false);
// AUTO_SERVER implies AUTO_RECONNECT
boolean autoServer = Boolean.parseBoolean(ci.getProperty(
"AUTO_SERVER", "false"));
boolean autoServer = ci.getProperty("AUTO_SERVER", false);
if (autoServer && serverList != null) {
throw DbException
.getUnsupportedException("autoServer && serverList != null");
......
......@@ -351,12 +351,10 @@ public class WebApp {
int port = Integer.decode((String) attributes.get("port"));
prop.setProperty("webPort", String.valueOf(port));
server.setPort(port);
boolean allowOthers = Boolean.parseBoolean(
(String) attributes.get("allowOthers"));
boolean allowOthers = Utils.parseBoolean((String) attributes.get("allowOthers"), false, false);
prop.setProperty("webAllowOthers", String.valueOf(allowOthers));
server.setAllowOthers(allowOthers);
boolean ssl = Boolean.parseBoolean(
(String) attributes.get("ssl"));
boolean ssl = Utils.parseBoolean((String) attributes.get("ssl"), false, false);
prop.setProperty("webSSL", String.valueOf(ssl));
server.setSSL(ssl);
server.saveProperties(prop);
......
......@@ -32,6 +32,7 @@ import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils;
import org.h2.util.New;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
/**
* A facility to read from and write to CSV (comma separated values) files. When
......@@ -857,11 +858,11 @@ public class Csv implements SimpleRowSource {
} else if (isParam(key, "charset", "characterSet")) {
charset = value;
} else if (isParam(key, "preserveWhitespace")) {
setPreserveWhitespace(Boolean.parseBoolean(value));
setPreserveWhitespace(Utils.parseBoolean(value, false, false));
} else if (isParam(key, "writeColumnHeader")) {
setWriteColumnHeader(Boolean.parseBoolean(value));
setWriteColumnHeader(Utils.parseBoolean(value, true, false));
} else if (isParam(key, "caseSensitiveColumnNames")) {
setCaseSensitiveColumnNames(Boolean.parseBoolean(value));
setCaseSensitiveColumnNames(Utils.parseBoolean(value, false, false));
} else {
throw DbException.getUnsupportedException(key);
}
......
......@@ -38,6 +38,7 @@ import org.h2.util.Bits;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.New;
import org.h2.util.Utils;
import org.h2.value.DataType;
/**
......@@ -498,7 +499,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
}
return n.longValue() != 0;
}
return Boolean.parseBoolean(o.toString());
return Utils.parseBoolean(o.toString(), false, true);
}
/**
......
......@@ -659,12 +659,35 @@ public class Utils {
if (value == null) {
return defaultValue;
}
if (value.equalsIgnoreCase("true") || value.equals("1")) {
switch (value.length()) {
case 1:
if (value.equals("1") || value.equalsIgnoreCase("t") || value.equalsIgnoreCase("y")) {
return true;
}
if (value.equalsIgnoreCase("false") || value.equals("0")) {
if (value.equals("0") || value.equalsIgnoreCase("f") || value.equalsIgnoreCase("n")) {
return false;
}
break;
case 2:
if (value.equalsIgnoreCase("no")) {
return false;
}
break;
case 3:
if (value.equalsIgnoreCase("yes")) {
return true;
}
break;
case 4:
if (value.equalsIgnoreCase("true")) {
return true;
}
break;
case 5:
if (value.equalsIgnoreCase("false")) {
return false;
}
}
if (throwException) {
throw new IllegalArgumentException(value);
}
......
......@@ -52,6 +52,7 @@ public class TestUtils extends TestBase {
testGetNonPrimitiveClass();
testGetNonPrimitiveClass();
testReflectionUtils();
testParseBoolean();
}
private void testIOUtils() throws IOException {
......@@ -221,4 +222,64 @@ public class TestUtils extends TestBase {
ArrayList.class));
}
private void testParseBooleanCheckFalse(String value) {
assertFalse(Utils.parseBoolean(value, false, false));
assertFalse(Utils.parseBoolean(value, false, true));
assertFalse(Utils.parseBoolean(value, true, false));
assertFalse(Utils.parseBoolean(value, true, true));
}
private void testParseBooleanCheckTrue(String value) {
assertTrue(Utils.parseBoolean(value, false, false));
assertTrue(Utils.parseBoolean(value, false, true));
assertTrue(Utils.parseBoolean(value, true, false));
assertTrue(Utils.parseBoolean(value, true, true));
}
private void testParseBoolean() {
// Test for default value in case of null
assertFalse(Utils.parseBoolean(null, false, false));
assertFalse(Utils.parseBoolean(null, false, true));
assertTrue(Utils.parseBoolean(null, true, false));
assertTrue(Utils.parseBoolean(null, true, true));
// Test assorted valid strings
testParseBooleanCheckFalse("0");
testParseBooleanCheckFalse("f");
testParseBooleanCheckFalse("F");
testParseBooleanCheckFalse("n");
testParseBooleanCheckFalse("N");
testParseBooleanCheckFalse("no");
testParseBooleanCheckFalse("No");
testParseBooleanCheckFalse("NO");
testParseBooleanCheckFalse("false");
testParseBooleanCheckFalse("False");
testParseBooleanCheckFalse("FALSE");
testParseBooleanCheckTrue("1");
testParseBooleanCheckTrue("t");
testParseBooleanCheckTrue("T");
testParseBooleanCheckTrue("y");
testParseBooleanCheckTrue("Y");
testParseBooleanCheckTrue("yes");
testParseBooleanCheckTrue("Yes");
testParseBooleanCheckTrue("YES");
testParseBooleanCheckTrue("true");
testParseBooleanCheckTrue("True");
testParseBooleanCheckTrue("TRUE");
// Test other values
assertFalse(Utils.parseBoolean("BAD", false, false));
assertTrue(Utils.parseBoolean("BAD", true, false));
try {
Utils.parseBoolean("BAD", false, true);
fail();
} catch (IllegalArgumentException e) {
// OK
}
try {
Utils.parseBoolean("BAD", true, true);
fail();
} catch (IllegalArgumentException e) {
// OK
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论