Fix Boolean.getBoolean(String). Fix various minor issues with system properties

See #306
This commit is contained in:
Alexey Andreev 2017-10-13 16:06:10 +03:00
parent 0b985b95d1
commit daf0f03634
5 changed files with 41 additions and 6 deletions

View File

@ -87,7 +87,8 @@ public class TBoolean extends TObject implements TSerializable, TComparable<TBoo
return obj instanceof TBoolean && ((TBoolean) obj).value == value; return obj instanceof TBoolean && ((TBoolean) obj).value == value;
} }
public boolean getBoolean(TString key) { public static boolean getBoolean(TString key) {
return valueOf(TString.wrap(TSystem.getProperty(key.toString()))).booleanValue(); String stringValue = key != null ? TSystem.getProperty((String) (Object) key) : null;
return stringValue != null && valueOf(TString.wrap(stringValue)).booleanValue();
} }
} }

View File

@ -172,8 +172,12 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
} }
public static TInteger getInteger(TString nm, TInteger val) { public static TInteger getInteger(TString nm, TInteger val) {
TString result = TString.wrap(TSystem.getProperty(nm.toString())); TString result = nm != null ? TString.wrap(TSystem.getProperty(nm.toString())) : null;
return result != null ? TInteger.valueOf(result) : val; try {
return result != null ? TInteger.valueOf(result) : val;
} catch (NumberFormatException e) {
return null;
}
} }
public static TInteger decode(TString nm) throws TNumberFormatException { public static TInteger decode(TString nm) throws TNumberFormatException {

View File

@ -225,8 +225,12 @@ public class TLong extends TNumber implements TComparable<TLong> {
} }
public static TLong getLong(TString nm, TLong val) { public static TLong getLong(TString nm, TLong val) {
TString result = TString.wrap(TSystem.getProperty(nm.toString())); TString result = nm != null ? TString.wrap(TSystem.getProperty(nm.toString())) : null;
return result != null ? TLong.valueOf(result) : val; try {
return result != null ? TLong.valueOf(result) : val;
} catch (NumberFormatException e) {
return null;
}
} }
public static long highestOneBit(long i) { public static long highestOneBit(long i) {

View File

@ -16,6 +16,8 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@ -28,4 +30,15 @@ public class BooleanTest {
assertEquals(false, new Boolean("False")); assertEquals(false, new Boolean("False"));
assertEquals(false, new Boolean("True15")); 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));
}
} }

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.MAX_VALUE, Integer.MIN_VALUE) > 0);
assertTrue(Integer.compare(Integer.MIN_VALUE, Integer.MAX_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));
}
} }