|
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; |
|
32 | 33 | import org.json.XML; |
33 | 34 | import org.json.junit.data.BrokenToString; |
34 | 35 | import org.json.junit.data.Fraction; |
| 36 | +import org.json.junit.data.GenericBean; |
| 37 | +import org.json.junit.data.GenericBeanInt; |
35 | 38 | import org.json.junit.data.MyBean; |
36 | 39 | import org.json.junit.data.MyBigNumberBean; |
37 | 40 | import org.json.junit.data.MyEnum; |
|
40 | 43 | import org.json.junit.data.MyNumber; |
41 | 44 | import org.json.junit.data.MyNumberContainer; |
42 | 45 | import org.json.junit.data.MyPublicClass; |
| 46 | +import org.json.junit.data.Singleton; |
| 47 | +import org.json.junit.data.SingletonEnum; |
| 48 | +import org.json.junit.data.WeirdList; |
43 | 49 | import org.junit.Test; |
44 | 50 |
|
45 | 51 | import com.jayway.jsonpath.Configuration; |
@@ -2583,4 +2589,78 @@ public void toMap() { |
2583 | 2589 | assertTrue("Removing a key should succeed", map.remove("key3") != null); |
2584 | 2590 | assertTrue("Map should have 2 elements", map.size() == 2); |
2585 | 2591 | } |
| 2592 | + |
| 2593 | + @Test |
| 2594 | + public void testSingletonBean() { |
| 2595 | + final JSONObject jo = new JSONObject(Singleton.getInstance()); |
| 2596 | + assertEquals(jo.keySet().toString(), 1, jo.length()); |
| 2597 | + assertEquals(0, jo.get("someInt")); |
| 2598 | + assertEquals(null, jo.opt("someString")); |
| 2599 | + |
| 2600 | + // Update the singleton values |
| 2601 | + Singleton.getInstance().setSomeInt(42); |
| 2602 | + Singleton.getInstance().setSomeString("Something"); |
| 2603 | + final JSONObject jo2 = new JSONObject(Singleton.getInstance()); |
| 2604 | + assertEquals(2, jo2.length()); |
| 2605 | + assertEquals(42, jo2.get("someInt")); |
| 2606 | + assertEquals("Something", jo2.get("someString")); |
| 2607 | + |
| 2608 | + // ensure our original jo hasn't changed. |
| 2609 | + assertEquals(0, jo.get("someInt")); |
| 2610 | + assertEquals(null, jo.opt("someString")); |
| 2611 | + } |
| 2612 | + @Test |
| 2613 | + public void testSingletonEnumBean() { |
| 2614 | + final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); |
| 2615 | + assertEquals(jo.keySet().toString(), 1, jo.length()); |
| 2616 | + assertEquals(0, jo.get("someInt")); |
| 2617 | + assertEquals(null, jo.opt("someString")); |
| 2618 | + |
| 2619 | + // Update the singleton values |
| 2620 | + SingletonEnum.getInstance().setSomeInt(42); |
| 2621 | + SingletonEnum.getInstance().setSomeString("Something"); |
| 2622 | + final JSONObject jo2 = new JSONObject(SingletonEnum.getInstance()); |
| 2623 | + assertEquals(2, jo2.length()); |
| 2624 | + assertEquals(42, jo2.get("someInt")); |
| 2625 | + assertEquals("Something", jo2.get("someString")); |
| 2626 | + |
| 2627 | + // ensure our original jo hasn't changed. |
| 2628 | + assertEquals(0, jo.get("someInt")); |
| 2629 | + assertEquals(null, jo.opt("someString")); |
| 2630 | + } |
| 2631 | + |
| 2632 | + @Test |
| 2633 | + public void testGenericBean() { |
| 2634 | + GenericBean<Integer> bean = new GenericBean<>(42); |
| 2635 | + final JSONObject jo = new JSONObject(bean); |
| 2636 | + assertEquals(jo.keySet().toString(), 8, jo.length()); |
| 2637 | + assertEquals(42, jo.get("genericValue")); |
| 2638 | + assertEquals("Expected the getter to only be called once", |
| 2639 | + 1, bean.genericGetCounter); |
| 2640 | + assertEquals(0, bean.genericSetCounter); |
| 2641 | + } |
| 2642 | + |
| 2643 | + @Test |
| 2644 | + public void testGenericIntBean() { |
| 2645 | + GenericBeanInt bean = new GenericBeanInt(42); |
| 2646 | + final JSONObject jo = new JSONObject(bean); |
| 2647 | + assertEquals(jo.keySet().toString(), 9, jo.length()); |
| 2648 | + assertEquals(42, jo.get("genericValue")); |
| 2649 | + assertEquals("Expected the getter to only be called once", |
| 2650 | + 1, bean.genericGetCounter); |
| 2651 | + assertEquals(0, bean.genericSetCounter); |
| 2652 | + } |
| 2653 | + |
| 2654 | + @Test |
| 2655 | + public void testWierdListBean() { |
| 2656 | + WeirdList bean = new WeirdList(42, 43, 44); |
| 2657 | + final JSONObject jo = new JSONObject(bean); |
| 2658 | + // get() should have a key of 0 length |
| 2659 | + // get(int) should be ignored base on parameter count |
| 2660 | + // getInt(int) should also be ignored based on parameter count |
| 2661 | + // add(Integer) should be ignore as it doesn't start with get/is and also has a parameter |
| 2662 | + // getALL should be mapped |
| 2663 | + assertEquals("Expected 1 key to mapped "+jo.keySet().toString(), 1, jo.length()); |
| 2664 | + assertNotNull(jo.get("ALL")); |
| 2665 | + } |
2586 | 2666 | } |
0 commit comments