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 { ...@@ -333,7 +333,7 @@ public class ConnectionInfo implements Cloneable {
* @param defaultValue the default value * @param defaultValue the default value
* @return the 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); return Utils.parseBoolean(getProperty(key, null), defaultValue, false);
} }
...@@ -345,8 +345,7 @@ public class ConnectionInfo implements Cloneable { ...@@ -345,8 +345,7 @@ public class ConnectionInfo implements Cloneable {
* @return the value * @return the value
*/ */
public boolean removeProperty(String key, boolean defaultValue) { public boolean removeProperty(String key, boolean defaultValue) {
String x = removeProperty(key, null); return Utils.parseBoolean(removeProperty(key, null), defaultValue, false);
return x == null ? defaultValue : Boolean.parseBoolean(x);
} }
/** /**
......
...@@ -335,8 +335,7 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -335,8 +335,7 @@ public class SessionRemote extends SessionWithState implements DataHandler {
} }
// create the session using reflection, // create the session using reflection,
// so that the JDBC layer can be compiled without it // so that the JDBC layer can be compiled without it
boolean autoServerMode = Boolean.parseBoolean( boolean autoServerMode = ci.getProperty("AUTO_SERVER", false);
ci.getProperty("AUTO_SERVER", "false"));
ConnectionInfo backup = null; ConnectionInfo backup = null;
try { try {
if (autoServerMode) { if (autoServerMode) {
...@@ -413,11 +412,9 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -413,11 +412,9 @@ public class SessionRemote extends SessionWithState implements DataHandler {
serverList = StringUtils.quoteStringSQL(server); serverList = StringUtils.quoteStringSQL(server);
ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED); ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED);
} }
autoReconnect = Boolean.parseBoolean(ci.getProperty( autoReconnect = ci.getProperty("AUTO_RECONNECT", false);
"AUTO_RECONNECT", "false"));
// AUTO_SERVER implies AUTO_RECONNECT // AUTO_SERVER implies AUTO_RECONNECT
boolean autoServer = Boolean.parseBoolean(ci.getProperty( boolean autoServer = ci.getProperty("AUTO_SERVER", false);
"AUTO_SERVER", "false"));
if (autoServer && serverList != null) { if (autoServer && serverList != null) {
throw DbException throw DbException
.getUnsupportedException("autoServer && serverList != null"); .getUnsupportedException("autoServer && serverList != null");
......
...@@ -351,12 +351,10 @@ public class WebApp { ...@@ -351,12 +351,10 @@ public class WebApp {
int port = Integer.decode((String) attributes.get("port")); int port = Integer.decode((String) attributes.get("port"));
prop.setProperty("webPort", String.valueOf(port)); prop.setProperty("webPort", String.valueOf(port));
server.setPort(port); server.setPort(port);
boolean allowOthers = Boolean.parseBoolean( boolean allowOthers = Utils.parseBoolean((String) attributes.get("allowOthers"), false, false);
(String) attributes.get("allowOthers"));
prop.setProperty("webAllowOthers", String.valueOf(allowOthers)); prop.setProperty("webAllowOthers", String.valueOf(allowOthers));
server.setAllowOthers(allowOthers); server.setAllowOthers(allowOthers);
boolean ssl = Boolean.parseBoolean( boolean ssl = Utils.parseBoolean((String) attributes.get("ssl"), false, false);
(String) attributes.get("ssl"));
prop.setProperty("webSSL", String.valueOf(ssl)); prop.setProperty("webSSL", String.valueOf(ssl));
server.setSSL(ssl); server.setSSL(ssl);
server.saveProperties(prop); server.saveProperties(prop);
......
...@@ -32,6 +32,7 @@ import org.h2.util.IOUtils; ...@@ -32,6 +32,7 @@ import org.h2.util.IOUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils;
/** /**
* A facility to read from and write to CSV (comma separated values) files. When * A facility to read from and write to CSV (comma separated values) files. When
...@@ -857,11 +858,11 @@ public class Csv implements SimpleRowSource { ...@@ -857,11 +858,11 @@ public class Csv implements SimpleRowSource {
} else if (isParam(key, "charset", "characterSet")) { } else if (isParam(key, "charset", "characterSet")) {
charset = value; charset = value;
} else if (isParam(key, "preserveWhitespace")) { } else if (isParam(key, "preserveWhitespace")) {
setPreserveWhitespace(Boolean.parseBoolean(value)); setPreserveWhitespace(Utils.parseBoolean(value, false, false));
} else if (isParam(key, "writeColumnHeader")) { } else if (isParam(key, "writeColumnHeader")) {
setWriteColumnHeader(Boolean.parseBoolean(value)); setWriteColumnHeader(Utils.parseBoolean(value, true, false));
} else if (isParam(key, "caseSensitiveColumnNames")) { } else if (isParam(key, "caseSensitiveColumnNames")) {
setCaseSensitiveColumnNames(Boolean.parseBoolean(value)); setCaseSensitiveColumnNames(Utils.parseBoolean(value, false, false));
} else { } else {
throw DbException.getUnsupportedException(key); throw DbException.getUnsupportedException(key);
} }
......
...@@ -38,6 +38,7 @@ import org.h2.util.Bits; ...@@ -38,6 +38,7 @@ import org.h2.util.Bits;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
/** /**
...@@ -498,7 +499,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData, ...@@ -498,7 +499,7 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
} }
return n.longValue() != 0; return n.longValue() != 0;
} }
return Boolean.parseBoolean(o.toString()); return Utils.parseBoolean(o.toString(), false, true);
} }
/** /**
......
...@@ -659,12 +659,35 @@ public class Utils { ...@@ -659,12 +659,35 @@ public class Utils {
if (value == null) { if (value == null) {
return defaultValue; 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; return true;
} }
if (value.equalsIgnoreCase("false") || value.equals("0")) { if (value.equals("0") || value.equalsIgnoreCase("f") || value.equalsIgnoreCase("n")) {
return false; 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) { if (throwException) {
throw new IllegalArgumentException(value); throw new IllegalArgumentException(value);
} }
......
...@@ -52,6 +52,7 @@ public class TestUtils extends TestBase { ...@@ -52,6 +52,7 @@ public class TestUtils extends TestBase {
testGetNonPrimitiveClass(); testGetNonPrimitiveClass();
testGetNonPrimitiveClass(); testGetNonPrimitiveClass();
testReflectionUtils(); testReflectionUtils();
testParseBoolean();
} }
private void testIOUtils() throws IOException { private void testIOUtils() throws IOException {
...@@ -221,4 +222,64 @@ public class TestUtils extends TestBase { ...@@ -221,4 +222,64 @@ public class TestUtils extends TestBase {
ArrayList.class)); 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论