|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 | import static org.junit.Assert.assertFalse; |
5 | 5 | import static org.junit.Assert.assertNotEquals; |
| 6 | +import static org.junit.Assert.assertNotNull; |
6 | 7 | import static org.junit.Assert.assertNull; |
7 | 8 | import static org.junit.Assert.assertTrue; |
8 | 9 | import static org.junit.Assert.fail; |
|
30 | 31 | import org.json.JSONObject; |
31 | 32 | import org.json.JSONPointerException; |
32 | 33 | import org.json.XML; |
| 34 | +import org.json.junit.data.BrokenToString; |
| 35 | +import org.json.junit.data.ExceptionalBean; |
| 36 | +import org.json.junit.data.Fraction; |
| 37 | +import org.json.junit.data.GenericBean; |
| 38 | +import org.json.junit.data.GenericBeanInt; |
| 39 | +import org.json.junit.data.MyBean; |
| 40 | +import org.json.junit.data.MyBigNumberBean; |
| 41 | +import org.json.junit.data.MyEnum; |
| 42 | +import org.json.junit.data.MyEnumField; |
| 43 | +import org.json.junit.data.MyJsonString; |
| 44 | +import org.json.junit.data.MyNumber; |
| 45 | +import org.json.junit.data.MyNumberContainer; |
| 46 | +import org.json.junit.data.MyPublicClass; |
| 47 | +import org.json.junit.data.Singleton; |
| 48 | +import org.json.junit.data.SingletonEnum; |
| 49 | +import org.json.junit.data.WeirdList; |
33 | 50 | import org.junit.Test; |
34 | 51 |
|
35 | 52 | import com.jayway.jsonpath.Configuration; |
@@ -484,7 +501,7 @@ public void jsonObjectByObjectAndNames() { |
484 | 501 | @Test |
485 | 502 | public void jsonObjectByResourceBundle() { |
486 | 503 | JSONObject jsonObject = new |
487 | | - JSONObject("org.json.junit.StringsResourceBundle", |
| 504 | + JSONObject("org.json.junit.data.StringsResourceBundle", |
488 | 505 | Locale.getDefault()); |
489 | 506 |
|
490 | 507 | // validate JSON |
@@ -2575,18 +2592,109 @@ public void toMap() { |
2575 | 2592 | // assert that the new map is mutable |
2576 | 2593 | assertTrue("Removing a key should succeed", map.remove("key3") != null); |
2577 | 2594 | assertTrue("Map should have 2 elements", map.size() == 2); |
| 2595 | + } |
| 2596 | + |
| 2597 | + /** |
| 2598 | + * test that validates a singleton can be serialized as a bean. |
| 2599 | + */ |
| 2600 | + @Test |
| 2601 | + public void testSingletonBean() { |
| 2602 | + final JSONObject jo = new JSONObject(Singleton.getInstance()); |
| 2603 | + assertEquals(jo.keySet().toString(), 1, jo.length()); |
| 2604 | + assertEquals(0, jo.get("someInt")); |
| 2605 | + assertEquals(null, jo.opt("someString")); |
| 2606 | + |
| 2607 | + // Update the singleton values |
| 2608 | + Singleton.getInstance().setSomeInt(42); |
| 2609 | + Singleton.getInstance().setSomeString("Something"); |
| 2610 | + final JSONObject jo2 = new JSONObject(Singleton.getInstance()); |
| 2611 | + assertEquals(2, jo2.length()); |
| 2612 | + assertEquals(42, jo2.get("someInt")); |
| 2613 | + assertEquals("Something", jo2.get("someString")); |
2578 | 2614 |
|
| 2615 | + // ensure our original jo hasn't changed. |
| 2616 | + assertEquals(0, jo.get("someInt")); |
| 2617 | + assertEquals(null, jo.opt("someString")); |
| 2618 | + } |
| 2619 | + |
| 2620 | + /** |
| 2621 | + * test that validates a singleton can be serialized as a bean. |
| 2622 | + */ |
| 2623 | + @Test |
| 2624 | + public void testSingletonEnumBean() { |
| 2625 | + final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); |
| 2626 | + assertEquals(jo.keySet().toString(), 1, jo.length()); |
| 2627 | + assertEquals(0, jo.get("someInt")); |
| 2628 | + assertEquals(null, jo.opt("someString")); |
| 2629 | + |
| 2630 | + // Update the singleton values |
| 2631 | + SingletonEnum.getInstance().setSomeInt(42); |
| 2632 | + SingletonEnum.getInstance().setSomeString("Something"); |
| 2633 | + final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance()); |
| 2634 | + assertEquals(2, jo2.length()); |
| 2635 | + assertEquals(42, jo2.get("someInt")); |
| 2636 | + assertEquals("Something", jo2.get("someString")); |
| 2637 | + |
| 2638 | + // ensure our original jo hasn't changed. |
| 2639 | + assertEquals(0, jo.get("someInt")); |
| 2640 | + assertEquals(null, jo.opt("someString")); |
2579 | 2641 | } |
2580 | 2642 |
|
2581 | 2643 | /** |
2582 | | - * test class for verifying write errors. |
2583 | | - * @author John Aylward |
2584 | | - * |
| 2644 | + * Test to validate that a generic class can be serialized as a bean. |
2585 | 2645 | */ |
2586 | | - private static class BrokenToString { |
2587 | | - @Override |
2588 | | - public String toString() { |
2589 | | - throw new IllegalStateException("Something went horribly wrong!"); |
2590 | | - } |
| 2646 | + @Test |
| 2647 | + public void testGenericBean() { |
| 2648 | + GenericBean<Integer> bean = new GenericBean<>(42); |
| 2649 | + final JSONObject jo = new JSONObject(bean); |
| 2650 | + assertEquals(jo.keySet().toString(), 8, jo.length()); |
| 2651 | + assertEquals(42, jo.get("genericValue")); |
| 2652 | + assertEquals("Expected the getter to only be called once", |
| 2653 | + 1, bean.genericGetCounter); |
| 2654 | + assertEquals(0, bean.genericSetCounter); |
| 2655 | + } |
| 2656 | + |
| 2657 | + /** |
| 2658 | + * Test to validate that a generic class can be serialized as a bean. |
| 2659 | + */ |
| 2660 | + @Test |
| 2661 | + public void testGenericIntBean() { |
| 2662 | + GenericBeanInt bean = new GenericBeanInt(42); |
| 2663 | + final JSONObject jo = new JSONObject(bean); |
| 2664 | + assertEquals(jo.keySet().toString(), 9, jo.length()); |
| 2665 | + assertEquals(42, jo.get("genericValue")); |
| 2666 | + assertEquals("Expected the getter to only be called once", |
| 2667 | + 1, bean.genericGetCounter); |
| 2668 | + assertEquals(0, bean.genericSetCounter); |
| 2669 | + } |
| 2670 | + |
| 2671 | + /** |
| 2672 | + * Test to verify <code>key</code> limitations in the JSONObject bean serializer. |
| 2673 | + */ |
| 2674 | + @Test |
| 2675 | + public void testWierdListBean() { |
| 2676 | + WeirdList bean = new WeirdList(42, 43, 44); |
| 2677 | + final JSONObject jo = new JSONObject(bean); |
| 2678 | + // get() should have a key of 0 length |
| 2679 | + // get(int) should be ignored base on parameter count |
| 2680 | + // getInt(int) should also be ignored based on parameter count |
| 2681 | + // add(Integer) should be ignore as it doesn't start with get/is and also has a parameter |
| 2682 | + // getALL should be mapped |
| 2683 | + assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(), |
| 2684 | + 1, jo.length()); |
| 2685 | + assertNotNull(jo.get("ALL")); |
| 2686 | + } |
| 2687 | + |
| 2688 | + /** |
| 2689 | + * Tests the exception portions of populateMap. |
| 2690 | + */ |
| 2691 | + @Test |
| 2692 | + public void testExceptionalBean() { |
| 2693 | + ExceptionalBean bean = new ExceptionalBean(); |
| 2694 | + final JSONObject jo = new JSONObject(bean); |
| 2695 | + assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(), |
| 2696 | + 1, jo.length()); |
| 2697 | + assertTrue(jo.get("closeable") instanceof JSONObject); |
| 2698 | + assertTrue(jo.getJSONObject("closeable").has("string")); |
2591 | 2699 | } |
2592 | 2700 | } |
0 commit comments