|
37 | 37 | import org.json.junit.data.GenericBean;
|
38 | 38 | import org.json.junit.data.GenericBeanInt;
|
39 | 39 | import org.json.junit.data.MyBean;
|
| 40 | +import org.json.junit.data.MyBeanCustomName; |
| 41 | +import org.json.junit.data.MyBeanCustomNameSubClass; |
40 | 42 | import org.json.junit.data.MyBigNumberBean;
|
41 | 43 | import org.json.junit.data.MyEnum;
|
42 | 44 | import org.json.junit.data.MyEnumField;
|
@@ -371,7 +373,7 @@ public void verifyPutCollection() {
|
371 | 373 |
|
372 | 374 |
|
373 | 375 | /**
|
374 |
| - * Verifies that the put Map has backwards compatability with RAW types pre-java5. |
| 376 | + * Verifies that the put Map has backwards compatibility with RAW types pre-java5. |
375 | 377 | */
|
376 | 378 | @Test
|
377 | 379 | public void verifyPutMap() {
|
@@ -467,7 +469,7 @@ public void jsonObjectByMapWithNullValue() {
|
467 | 469 | */
|
468 | 470 | @SuppressWarnings("boxing")
|
469 | 471 | @Test
|
470 |
| - public void jsonObjectByBean() { |
| 472 | + public void jsonObjectByBean1() { |
471 | 473 | /**
|
472 | 474 | * Default access classes have to be mocked since JSONObject, which is
|
473 | 475 | * not in the same package, cannot call MyBean methods by reflection.
|
@@ -501,6 +503,73 @@ public void jsonObjectByBean() {
|
501 | 503 | assertTrue("expected 0 callbacks[1] items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0);
|
502 | 504 | }
|
503 | 505 |
|
| 506 | + /** |
| 507 | + * JSONObject built from a bean that has custom field names. |
| 508 | + */ |
| 509 | + @Test |
| 510 | + public void jsonObjectByBean2() { |
| 511 | + JSONObject jsonObject = new JSONObject(new MyBeanCustomName()); |
| 512 | + assertNotNull(jsonObject); |
| 513 | + assertEquals("Wrong number of keys found:", |
| 514 | + 5, |
| 515 | + jsonObject.keySet().size()); |
| 516 | + assertFalse("Normal field name (someString) processing did not work", |
| 517 | + jsonObject.has("someString")); |
| 518 | + assertFalse("Normal field name (myDouble) processing did not work", |
| 519 | + jsonObject.has("myDouble")); |
| 520 | + assertFalse("Normal field name (someFloat) found", |
| 521 | + jsonObject.has("someFloat")); |
| 522 | + assertFalse("Ignored field found!", |
| 523 | + jsonObject.has("ignoredInt")); |
| 524 | + assertTrue("Normal field name (someInt) processing did not work", |
| 525 | + jsonObject.has("someInt")); |
| 526 | + assertTrue("Normal field name (someLong) processing did not work", |
| 527 | + jsonObject.has("someLong")); |
| 528 | + assertTrue("Overridden String field name (myStringField) not found", |
| 529 | + jsonObject.has("myStringField")); |
| 530 | + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found", |
| 531 | + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); |
| 532 | + assertTrue("Overridden String field name (InterfaceField) not found", |
| 533 | + jsonObject.has("InterfaceField")); |
| 534 | + } |
| 535 | + |
| 536 | + /** |
| 537 | + * JSONObject built from a bean that has custom field names inherited from a parent class. |
| 538 | + */ |
| 539 | + @Test |
| 540 | + public void jsonObjectByBean3() { |
| 541 | + JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass()); |
| 542 | + assertNotNull(jsonObject); |
| 543 | + assertEquals("Wrong number of keys found:", |
| 544 | + 7, |
| 545 | + jsonObject.keySet().size()); |
| 546 | + assertFalse("Normal int field name (someInt) found, but was overridden", |
| 547 | + jsonObject.has("someInt")); |
| 548 | + assertFalse("Normal field name (myDouble) processing did not work", |
| 549 | + jsonObject.has("myDouble")); |
| 550 | + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!", |
| 551 | + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); |
| 552 | + assertFalse("Normal field name (someFloat) found", |
| 553 | + jsonObject.has("someFloat")); |
| 554 | + assertFalse("Ignored field found!", |
| 555 | + jsonObject.has("ignoredInt")); |
| 556 | + assertFalse("Ignored field at the same level as forced name found", |
| 557 | + jsonObject.has("ShouldBeIgnored")); |
| 558 | + assertTrue("Overridden int field name (newIntFieldName) not found", |
| 559 | + jsonObject.has("newIntFieldName")); |
| 560 | + assertTrue("Normal field name (someLong) processing did not work", |
| 561 | + jsonObject.has("someLong")); |
| 562 | + assertTrue("Overridden String field name (myStringField) not found", |
| 563 | + jsonObject.has("myStringField")); |
| 564 | + assertTrue(jsonObject.has("AMoreNormalName")); |
| 565 | + assertTrue("Overridden String field name (InterfaceField) not found", |
| 566 | + jsonObject.has("InterfaceField")); |
| 567 | + assertTrue("Forced field not found!", |
| 568 | + jsonObject.has("forcedInt")); |
| 569 | + assertTrue("Normally ignored field (getable) with explicit property name not found", |
| 570 | + jsonObject.has("Getable")); |
| 571 | + } |
| 572 | + |
504 | 573 | /**
|
505 | 574 | * A bean is also an object. But in order to test the JSONObject
|
506 | 575 | * ctor that takes an object and a list of names,
|
@@ -541,7 +610,7 @@ public void jsonObjectByResourceBundle() {
|
541 | 610 | assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
|
542 | 611 | assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
|
543 | 612 | }
|
544 |
| - |
| 613 | + |
545 | 614 | /**
|
546 | 615 | * Exercise the JSONObject.accumulate() method
|
547 | 616 | */
|
@@ -2855,7 +2924,7 @@ public void testGenericBean() {
|
2855 | 2924 | public void testGenericIntBean() {
|
2856 | 2925 | GenericBeanInt bean = new GenericBeanInt(42);
|
2857 | 2926 | final JSONObject jo = new JSONObject(bean);
|
2858 |
| - assertEquals(jo.keySet().toString(), 9, jo.length()); |
| 2927 | + assertEquals(jo.keySet().toString(), 10, jo.length()); |
2859 | 2928 | assertEquals(42, jo.get("genericValue"));
|
2860 | 2929 | assertEquals("Expected the getter to only be called once",
|
2861 | 2930 | 1, bean.genericGetCounter);
|
|
0 commit comments