diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java index 7f96bc5b6..fbd361ee9 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java @@ -87,7 +87,8 @@ public class TBoolean extends TObject implements TSerializable, TComparable { } public static TInteger getInteger(TString nm, TInteger val) { - TString result = TString.wrap(TSystem.getProperty(nm.toString())); - return result != null ? TInteger.valueOf(result) : val; + TString result = nm != null ? TString.wrap(TSystem.getProperty(nm.toString())) : null; + try { + return result != null ? TInteger.valueOf(result) : val; + } catch (NumberFormatException e) { + return null; + } } public static TInteger decode(TString nm) throws TNumberFormatException { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index 0f66dad19..fd92e87bc 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -225,8 +225,12 @@ public class TLong extends TNumber implements TComparable { } public static TLong getLong(TString nm, TLong val) { - TString result = TString.wrap(TSystem.getProperty(nm.toString())); - return result != null ? TLong.valueOf(result) : val; + TString result = nm != null ? TString.wrap(TSystem.getProperty(nm.toString())) : null; + try { + return result != null ? TLong.valueOf(result) : val; + } catch (NumberFormatException e) { + return null; + } } public static long highestOneBit(long i) { diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java index d63f95da5..c9410f1f5 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/BooleanTest.java @@ -16,6 +16,8 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.TeaVMTestRunner; @@ -28,4 +30,15 @@ public class BooleanTest { assertEquals(false, new Boolean("False")); assertEquals(false, new Boolean("True15")); } + + @Test + public void getFromSystemProperty() { + System.setProperty("test.foo", "true"); + System.setProperty("test.bar", "false"); + + assertTrue(Boolean.getBoolean("test.foo")); + assertFalse(Boolean.getBoolean("test.bar")); + assertFalse(Boolean.getBoolean("test.baz")); + assertFalse(Boolean.getBoolean(null)); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java index e542819dd..5f92545b6 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java @@ -16,6 +16,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; @@ -137,4 +138,16 @@ public class IntegerTest { assertTrue(Integer.compare(Integer.MAX_VALUE, Integer.MIN_VALUE) > 0); assertTrue(Integer.compare(Integer.MIN_VALUE, Integer.MAX_VALUE) < 0); } + + + @Test + public void getFromSystemProperty() { + System.setProperty("test.foo", "23"); + System.setProperty("test.bar", "q"); + + assertEquals((Object) 23, Integer.getInteger("test.foo")); + assertNull(Integer.getInteger("test.bar")); + assertNull(Integer.getInteger("test.baz")); + assertNull(Integer.getInteger(null)); + } }