11package org .json .junit ;
22
33import static org .junit .Assert .assertEquals ;
4- import static org .junit .Assert .assertFalse ;
4+ import static org .junit .Assert .assertNotEquals ;
5+ import static org .junit .Assert .assertNotNull ;
6+ import static org .junit .Assert .assertNull ;
7+ import static org .junit .Assert .assertThrows ;
58import static org .junit .Assert .assertTrue ;
69import static org .junit .Assert .fail ;
710
8- import java .math .BigDecimal ;
9- import java .math .BigInteger ;
1011import java .util .Collections ;
1112import java .util .HashMap ;
1213import java .util .Map ;
13- import java .util .concurrent .atomic .AtomicBoolean ;
14+ import java .util .concurrent .atomic .AtomicInteger ;
1415
16+ import org .json .JSONException ;
1517import org .json .JSONObject ;
1618import org .junit .Test ;
1719
@@ -397,14 +399,14 @@ public void testPutFloat() {
397399
398400 @ Test
399401 public void lastTest () {
400- final Byte a_Byte /* = new JSONObject().optByte("a") */ ;
401- final Double a_Double = new JSONObject ().optDouble ("a" ); //testNullDouble
402- final Float a_Float = new JSONObject ().optFloat ("a" ); //testNullFloat
403- final Integer a_Integer = new JSONObject ().optInteger ("a" ); //testNullInteger
404- final Long a_Long = new JSONObject ().optLong ("a" ); //testNullLong
405- final Short a_Short /* = new JSONObject().getShort("a") */ ;
406- final BigDecimal a_BigDecimal /* = new JSONObject().getBigDecimal("a") */ ;
407- final BigInteger a_BigInteger /* = new JSONObject().getBigInteger("a") */ ;
402+ // final Byte a_Byte /* = new JSONObject().optByte("a") */;
403+ // final Double a_Double = new JSONObject().optDouble("a"); //testNullDouble
404+ // final Float a_Float = new JSONObject().optFloat("a"); //testNullFloat
405+ // final Integer a_Integer = new JSONObject().optInteger("a"); //testNullInteger
406+ // final Long a_Long = new JSONObject().optLong("a"); //testNullLong
407+ // final Short a_Short /* = new JSONObject().getShort("a") */;
408+ // final BigDecimal a_BigDecimal /* = new JSONObject().getBigDecimal("a") */;
409+ // final BigInteger a_BigInteger /* = new JSONObject().getBigInteger("a") */;
408410 }
409411
410412 @ Test
@@ -420,20 +422,145 @@ public void computeTest() {
420422 assertEquals (r2 , "myNull" );
421423 }
422424
423-
424425 @ Test
425- public void updateTest () {
426+ public void updateNotEqualsTest () {
426427 final JSONObject j = new JSONObject ();
427- final AtomicBoolean atomicBoolean = new AtomicBoolean ();
428- assertFalse (atomicBoolean .get ());
429- j .addUpdateListener (evt -> {
428+
429+ assertThrows (JSONException .class , () -> j .update ("myMapListener" , "propertyChange" ));
430+
431+ j .addUpdateListenerGlobal (evt -> {
430432 final Object oldValue = evt .getOldValue ();
431433 assertEquals (oldValue , null );
434+ });
435+
436+ j .addUpdateListenerGlobal (evt -> {
437+ final Object newValue = evt .getNewValue ();
438+ assertTrue (newValue .toString ().startsWith ("propertyChange" ));
439+ });
440+
441+ j .addUpdateListener ("myMapListener" , evt -> {
442+ final Object oldValue = evt .getOldValue ();
432443 final Object newValue = evt .getNewValue ();
433- assertEquals ( newValue , "propertyChange" );
434- atomicBoolean . set ( true );
444+
445+ assertNotEquals ( oldValue , newValue );
435446 });
447+
436448 j .update ("myMapListener" , "propertyChange" );
437- assertTrue (atomicBoolean .get ());
449+ }
450+
451+ @ Test
452+ public void updateListenerGlobalTest () {
453+ final JSONObject j = new JSONObject ();
454+
455+ final AtomicInteger counter = new AtomicInteger ();
456+ final AtomicInteger globalExecutions = new AtomicInteger ();
457+ assertEquals (counter .get (), globalExecutions .get ());
458+
459+ j .addUpdateListenerGlobal (evt -> {
460+ globalExecutions .incrementAndGet ();
461+ });
462+
463+ j .addUpdateListenerGlobal (evt -> {
464+ assertEquals (counter .incrementAndGet (), globalExecutions .get () * 3 - 2 );
465+ });
466+
467+ j .addUpdateListenerGlobal (evt -> {
468+ assertEquals (counter .incrementAndGet (), globalExecutions .get () * 3 - 1 );
469+ });
470+
471+ j .addUpdateListener ("myMapListener" , evt -> {
472+ assertEquals (counter .incrementAndGet (), globalExecutions .get () * 3 - 0 );
473+ });
474+
475+ j .update ("myMapListener" , "propertyChange123" );
476+ j .update ("myMapListener" , "propertyChange456" );
477+ j .update ("myMapListener" , "propertyChange789" );
478+
479+ assertEquals (counter .get (), globalExecutions .get () * 3 );
480+ }
481+
482+ @ Test
483+ public void updateListenerTest () {
484+ final JSONObject j = new JSONObject ();
485+
486+ j .put ("myMapListener" , "unchangedProperty" );
487+
488+ j .addUpdateListener ("myMapListener" , evt -> {
489+ fail ("They are the same" );
490+ });
491+
492+ j .update ("myMapListener" , "unchangedProperty" );
493+
494+ j .addUpdateListener ("otherMapListener" , evt -> {
495+ final Object oldValue = evt .getOldValue ();
496+ assertEquals (oldValue , null );
497+
498+ final Object newValue = evt .getNewValue ();
499+ assertEquals (newValue , "otherOtherPropertyChange" );
500+ });
501+
502+ j .update ("otherMapListener" , "otherOtherPropertyChange" );
503+ }
504+
505+ @ Test
506+ public void updateListener2Test () {
507+ final JSONObject jsonObject1 = new JSONObject ();
508+ final JSONObject jsonObject2 = new JSONObject ();
509+
510+ jsonObject1
511+ .put ("trueKey" , Boolean .valueOf (true ))
512+ .put ("falseKey" , Boolean .valueOf (false ))
513+ .put ("stringKey" , "CHANGE ME!!!" );
514+ //.put("nullKey", "NOT NULL");
515+
516+ final JSONObject oldJsonObject1 = new JSONObject (jsonObject1 .toString ());
517+
518+ jsonObject2
519+ .put ("stringKey" , "hello world!" )
520+ .put ("nullKey" , null )
521+ .put ("escapeStringKey" , "h\b e\t llo w\u1234 orld!" )
522+ .put ("intKey" , Long .valueOf (42 ));
523+ //.put("doubleKey", Double.valueOf(-23.45e67)); PROBLEM WITH DOUBLE CONVERTING TO BIGDECIMAL AFTER JSONOBJECT.TOSTRING
524+
525+ final JSONObject oldJsonObject2 = new JSONObject (jsonObject2 .toString ());
526+
527+ jsonObject1 .addUpdateListenerGlobal (evt -> {
528+ final Object oldValue = evt .getOldValue ();
529+ final Object newValue = evt .getNewValue ();
530+
531+ assertNotEquals (oldValue , newValue );
532+ });
533+
534+ jsonObject1 .addUpdateListener ("trueKey" , evt -> {
535+ assertNull (evt .getOldValue ());
536+ assertTrue ("expected \" trueKey\" :true" , Boolean .TRUE .equals (evt .getNewValue ()));
537+ });
538+ jsonObject1 .addUpdateListener ("falseKey" , evt -> {
539+ assertNull (evt .getOldValue ());
540+ assertTrue ("expected \" falseKey\" :false" , Boolean .FALSE .equals (evt .getNewValue ()));
541+ });
542+ jsonObject1 .addUpdateListener ("stringKey" , evt -> {
543+ assertNotNull (evt .getOldValue ());
544+ assertTrue ("expected \" stringKey\" :\" hello world!\" " , "hello world!" .equals (evt .getNewValue ()));
545+ });
546+ jsonObject1 .addUpdateListener ("nullKey" , evt -> {
547+ fail ("They are the same" );
548+ });
549+ jsonObject1 .addUpdateListener ("escapeStringKey" , evt -> {
550+ assertNotNull (evt .getOldValue ());
551+ assertTrue ("expected \" escapeStringKey\" :\" h\b e\t llo w\u1234 orld!\" " , "h\b e\t llo w\u1234 orld!" .equals (evt .getNewValue ()));
552+ });
553+ jsonObject1 .addUpdateListener ("intKey" , evt -> {
554+ assertNotNull (evt .getOldValue ());
555+ assertTrue ("expected \" intKey\" :42" , Long .valueOf ("42" ).equals (evt .getNewValue ()));
556+ });
557+
558+ assertEquals (jsonObject1 .toString (), oldJsonObject1 .toString ());
559+ assertEquals (jsonObject2 .toString (), oldJsonObject2 .toString ());
560+
561+ jsonObject1 .update (jsonObject2 );
562+
563+ assertNotEquals (jsonObject1 .toString (), oldJsonObject1 .toString ());
564+ assertEquals (jsonObject2 .toString (), oldJsonObject2 .toString ());
438565 }
439566}
0 commit comments