|
3 | 3 | import static org.junit.Assert.assertNull; |
4 | 4 | import static org.junit.Assert.assertTrue; |
5 | 5 |
|
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.math.BigInteger; |
6 | 8 | import java.util.ArrayList; |
7 | 9 | import java.util.Collection; |
8 | 10 | import java.util.Collections; |
@@ -414,6 +416,20 @@ public void opt() { |
414 | 416 | assertTrue("Array opt string default implicit", |
415 | 417 | "".equals(jsonArray.optString(-1))); |
416 | 418 | } |
| 419 | + |
| 420 | + /** |
| 421 | + * Verifies that the opt methods properly convert string values. |
| 422 | + */ |
| 423 | + @Test |
| 424 | + public void optStringConversion(){ |
| 425 | + JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]"); |
| 426 | + assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true); |
| 427 | + assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false); |
| 428 | + assertTrue("unexpected optInt value",ja.optInt(0,0)==123); |
| 429 | + assertTrue("unexpected optLong value",ja.optLong(0,0)==123); |
| 430 | + assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0); |
| 431 | + assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0); |
| 432 | + assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); } |
417 | 433 |
|
418 | 434 | /** |
419 | 435 | * Exercise the JSONArray.put(value) method with various parameters |
|
0 commit comments