Unverified 提交 4de13b24 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #811 from katzyn/misc

Issues with Boolean.parseBoolean()
...@@ -30,10 +30,10 @@ public class SettingsBase { ...@@ -30,10 +30,10 @@ public class SettingsBase {
* @return the setting * @return the setting
*/ */
protected boolean get(String key, boolean defaultValue) { protected boolean get(String key, boolean defaultValue) {
String s = get(key, "" + defaultValue); String s = get(key, Boolean.toString(defaultValue));
try { try {
return Boolean.parseBoolean(s); return Utils.parseBoolean(s, defaultValue, true);
} catch (NumberFormatException e) { } catch (IllegalArgumentException e) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1,
e, "key:" + key + " value:" + s); e, "key:" + key + " value:" + s);
} }
......
...@@ -8,6 +8,7 @@ package org.h2.tools; ...@@ -8,6 +8,7 @@ package org.h2.tools;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL; import java.net.URL;
import java.sql.Array; import java.sql.Array;
import java.sql.Blob; import java.sql.Blob;
...@@ -478,10 +479,26 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData, ...@@ -478,10 +479,26 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
@Override @Override
public boolean getBoolean(int columnIndex) throws SQLException { public boolean getBoolean(int columnIndex) throws SQLException {
Object o = get(columnIndex); Object o = get(columnIndex);
if (o != null && !(o instanceof Boolean)) { if (o == null) {
o = Boolean.valueOf(o.toString()); return false;
}
if (o instanceof Boolean) {
return (Boolean) o;
}
if (o instanceof Number) {
Number n = (Number) o;
if (n instanceof Double || n instanceof Float) {
return n.doubleValue() != 0;
}
if (n instanceof BigDecimal) {
return ((BigDecimal) n).signum() != 0;
}
if (n instanceof BigInteger) {
return ((BigInteger) n).signum() != 0;
}
return n.longValue() != 0;
} }
return o == null ? false : ((Boolean) o).booleanValue(); return Boolean.parseBoolean(o.toString());
} }
/** /**
......
...@@ -52,10 +52,9 @@ public class SortedProperties extends Properties { ...@@ -52,10 +52,9 @@ public class SortedProperties extends Properties {
*/ */
public static boolean getBooleanProperty(Properties prop, String key, public static boolean getBooleanProperty(Properties prop, String key,
boolean def) { boolean def) {
String value = prop.getProperty(key, "" + def);
try { try {
return Boolean.parseBoolean(value); return Utils.parseBoolean(prop.getProperty(key, null), def, true);
} catch (Exception e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
return def; return def;
} }
......
...@@ -642,6 +642,35 @@ public class Utils { ...@@ -642,6 +642,35 @@ public class Utils {
return clazz; return clazz;
} }
/**
* Parses the specified string to boolean value.
*
* @param value
* string to parse
* @param defaultValue
* value to return if value is null or on parsing error
* @param throwException
* throw exception on parsing error or return default value instead
* @return parsed or default value
* @throws IllegalArgumentException
* on parsing error if {@code throwException} is true
*/
public static boolean parseBoolean(String value, boolean defaultValue, boolean throwException) {
if (value == null) {
return defaultValue;
}
if (value.equalsIgnoreCase("true")) {
return true;
}
if (value.equalsIgnoreCase("false")) {
return false;
}
if (throwException) {
throw new IllegalArgumentException(value);
}
return defaultValue;
}
/** /**
* Get the system property. If the system property is not set, or if a * Get the system property. If the system property is not set, or if a
* security exception occurs, the default value is returned. * security exception occurs, the default value is returned.
...@@ -687,15 +716,7 @@ public class Utils { ...@@ -687,15 +716,7 @@ public class Utils {
* @return the value * @return the value
*/ */
public static boolean getProperty(String key, boolean defaultValue) { public static boolean getProperty(String key, boolean defaultValue) {
String s = getProperty(key, null); return parseBoolean(getProperty(key, null), defaultValue, false);
if (s != null) {
try {
return Boolean.parseBoolean(s);
} catch (NumberFormatException e) {
// ignore
}
}
return defaultValue;
} }
/** /**
......
...@@ -216,7 +216,7 @@ public class ValueTimestamp extends Value { ...@@ -216,7 +216,7 @@ public class ValueTimestamp extends Value {
tz, year, month, day, hour, minute, (int) second, (int) ms); tz, year, month, day, hour, minute, (int) second, (int) ms);
ms = DateTimeUtils.convertToLocal( ms = DateTimeUtils.convertToLocal(
new Date(millis), new Date(millis),
DateTimeUtils.createGregorianCalendar(TimeZone.getTimeZone("UTC"))); DateTimeUtils.createGregorianCalendar(DateTimeUtils.UTC));
long md = DateTimeUtils.MILLIS_PER_DAY; long md = DateTimeUtils.MILLIS_PER_DAY;
long absoluteDay = (ms >= 0 ? ms : ms - md + 1) / md; long absoluteDay = (ms >= 0 ? ms : ms - md + 1) / md;
dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay); dateValue = DateTimeUtils.dateValueFromAbsoluteDay(absoluteDay);
......
...@@ -18,6 +18,7 @@ import java.io.Writer; ...@@ -18,6 +18,7 @@ import java.io.Writer;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
...@@ -257,6 +258,8 @@ public class TestTools extends TestBase { ...@@ -257,6 +258,8 @@ public class TestTools extends TestBase {
Clob clob = new SimpleClob("Hello World"); Clob clob = new SimpleClob("Hello World");
Blob blob = new SimpleBlob(new byte[]{(byte) 1, (byte) 2}); Blob blob = new SimpleBlob(new byte[]{(byte) 1, (byte) 2});
rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob); rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob);
rs.addRow(BigInteger.ONE, null, true, null, BigDecimal.ONE, 1d, null, null, null, null, null);
rs.addRow(BigInteger.ZERO, null, false, null, BigDecimal.ZERO, 0d, null, null, null, null, null);
rs.addRow(null, null, null, null, null, null, null, null, null, null, null); rs.addRow(null, null, null, null, null, null, null, null, null, null, null);
rs.next(); rs.next();
...@@ -270,6 +273,7 @@ public class TestTools extends TestBase { ...@@ -270,6 +273,7 @@ public class TestTools extends TestBase {
assertEquals((short) 1, rs.getShort("a")); assertEquals((short) 1, rs.getShort("a"));
assertTrue(rs.getObject(1).getClass() == Integer.class); assertTrue(rs.getObject(1).getClass() == Integer.class);
assertTrue(rs.getObject("a").getClass() == Integer.class); assertTrue(rs.getObject("a").getClass() == Integer.class);
assertTrue(rs.getBoolean(1));
assertEquals(b, rs.getBytes(2)); assertEquals(b, rs.getBytes(2));
assertEquals(b, rs.getBytes("b")); assertEquals(b, rs.getBytes("b"));
...@@ -288,6 +292,7 @@ public class TestTools extends TestBase { ...@@ -288,6 +292,7 @@ public class TestTools extends TestBase {
assertTrue(Math.PI == rs.getDouble("f")); assertTrue(Math.PI == rs.getDouble("f"));
assertTrue((float) Math.PI == rs.getFloat(6)); assertTrue((float) Math.PI == rs.getFloat(6));
assertTrue((float) Math.PI == rs.getFloat("f")); assertTrue((float) Math.PI == rs.getFloat("f"));
assertTrue(rs.getBoolean(6));
assertEquals(-3, rs.getInt(7)); assertEquals(-3, rs.getInt(7));
assertEquals(-3, rs.getByte(7)); assertEquals(-3, rs.getByte(7));
...@@ -326,6 +331,20 @@ public class TestTools extends TestBase { ...@@ -326,6 +331,20 @@ public class TestTools extends TestBase {
rs.next(); rs.next();
assertTrue(rs.getBoolean(1));
assertTrue(rs.getBoolean(3));
assertTrue(rs.getBoolean(5));
assertTrue(rs.getBoolean(6));
rs.next();
assertFalse(rs.getBoolean(1));
assertFalse(rs.getBoolean(3));
assertFalse(rs.getBoolean(5));
assertFalse(rs.getBoolean(6));
rs.next();
assertEquals(0, rs.getLong(1)); assertEquals(0, rs.getLong(1));
assertTrue(rs.wasNull()); assertTrue(rs.wasNull());
assertEquals(null, rs.getBytes(2)); assertEquals(null, rs.getBytes(2));
...@@ -454,6 +473,8 @@ public class TestTools extends TestBase { ...@@ -454,6 +473,8 @@ public class TestTools extends TestBase {
assertFalse(rs.isClosed()); assertFalse(rs.isClosed());
assertEquals(1, rs.getRow()); assertEquals(1, rs.getRow());
assertTrue(rs.next()); assertTrue(rs.next());
assertTrue(rs.next());
assertTrue(rs.next());
assertFalse(rs.next()); assertFalse(rs.next());
assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs). assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs).
getInt(1); getInt(1);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论