@@ -392,4 +392,220 @@ public void testCreatePersistentError() {
392392 }
393393 }
394394
395+ @ Test
396+ public void testCheckExists () throws Exception {
397+ client .create (testNodeFullPath , false );
398+ boolean exists = Whitebox .invokeMethod (client , "checkExists" , testNodeFullPath );
399+ Assert .assertTrue (exists );
400+
401+ boolean notExists = Whitebox .invokeMethod (client , "checkExists" , testNodeFullPath + "/notexist" );
402+ Assert .assertFalse (notExists );
403+ }
404+
405+ @ Test
406+ public void testCheckExistsError () throws Exception {
407+ ZookeeperClient client = new CuratorZookeeperClient (buildConfig ());
408+ ((CuratorZookeeperClient ) client ).setClient (null );
409+
410+ boolean exists = Whitebox .invokeMethod (client , "checkExists" , testNodeFullPath );
411+ Assert .assertFalse (exists );
412+ }
413+
414+ @ Test
415+ public void testCreatePersistentNoData () throws Exception {
416+ Whitebox .invokeMethod (client , "createPersistent" , testRootPath );
417+ List <String > children = client .getChildren ("/" );
418+ Assert .assertTrue (children .contains (testRootName ));
419+ }
420+
421+ @ Test
422+ public void testCreatePersistentNoDataError () throws Exception {
423+ ZookeeperClient client = new CuratorZookeeperClient (buildConfig ());
424+ ((CuratorZookeeperClient ) client ).setClient (null );
425+
426+ try {
427+ Whitebox .invokeMethod (client , "createPersistent" , testRootPath );
428+ } catch (Exception e ) {
429+ LOGGER .info ("testCreatePersistentNoDataError success" );
430+ }
431+ }
432+
433+ @ Test
434+ public void testCreateEphemeralNodeExists () throws Exception {
435+ client .create (testNodeFullPath , false );
436+ String providerPath = testNodeFullPath + "/provider1" ;
437+ client .create (providerPath , true );
438+
439+ Whitebox .invokeMethod (client , "createEphemeral" , providerPath );
440+ List <String > children = client .getChildren (testNodeFullPath );
441+ Assert .assertEquals (1 , children .size ());
442+ }
443+
444+ @ Test
445+ public void testCreateEphemeralWithDataNodeExists () throws Exception {
446+ client .create (testNodeFullPath , false );
447+ String providerPath = testNodeFullPath + "/provider1" ;
448+ client .create (providerPath , "data1" , true );
449+
450+ Whitebox .invokeMethod (client , "createEphemeral" , providerPath , "data2" );
451+ List <String > children = client .getChildren (testNodeFullPath );
452+ Assert .assertEquals (1 , children .size ());
453+ }
454+
455+ @ Test
456+ public void testCreatePersistentWithDataNodeExists () throws Exception {
457+ client .create (testNodeFullPath , false );
458+ String providerPath = testNodeFullPath + "/provider1" ;
459+ client .create (providerPath , "data1" , false );
460+
461+ Whitebox .invokeMethod (client , "createPersistent" , providerPath , "data2" );
462+ List <String > children = client .getChildren (testNodeFullPath );
463+ Assert .assertEquals (1 , children .size ());
464+ }
465+
466+ @ Test
467+ public void testStateListener () throws Exception {
468+ final Map <String , Integer > stateMap = new HashMap <>();
469+ client .addStateListener (state -> {
470+ stateMap .put (state .name (), stateMap .getOrDefault (state .name (), 0 ) + 1 );
471+ });
472+
473+ client .create (testRootPath , false );
474+ Thread .sleep (500 );
475+ Assert .assertTrue (client .isConnected ());
476+ }
477+
478+ @ Test
479+ public void testGetCuratorConnectStringError () {
480+ try {
481+ RegistryCenterConfig config = new RegistryCenterConfig ();
482+ config .setAddresses ("" );
483+ ZookeeperClient client = new CuratorZookeeperClient (config );
484+ } catch (Exception e ) {
485+ LOGGER .info ("testGetCuratorConnectStringError success" );
486+ }
487+ }
488+
489+ @ Test
490+ public void testCuratorChildWatcherProcess () throws Exception {
491+ client .create (testNodeFullPath , false );
492+
493+ final Map <String , List <String >> childrenMap = new HashMap <>();
494+ ChildListener childListener = (path , children ) -> {
495+ childrenMap .put (path , children );
496+ };
497+
498+ client .addChildListener (testNodeFullPath , childListener );
499+
500+ String providerPath = testNodeFullPath + "/provider1" ;
501+ client .create (providerPath , true );
502+
503+ Thread .sleep (1000 );
504+ Assert .assertTrue (childrenMap .containsKey (testNodeFullPath ));
505+ Assert .assertEquals (1 , childrenMap .get (testNodeFullPath ).size ());
506+ }
507+
508+ @ Test
509+ public void testCuratorChildWatcherUnwatch () throws Exception {
510+ client .create (testNodeFullPath , false );
511+
512+ final Map <String , List <String >> childrenMap = new HashMap <>();
513+ ChildListener childListener = (path , children ) -> {
514+ childrenMap .put (path , children );
515+ };
516+
517+ client .addChildListener (testNodeFullPath , childListener );
518+ client .removeChildListener (testNodeFullPath , childListener );
519+
520+ String providerPath = testNodeFullPath + "/provider1" ;
521+ client .create (providerPath , true );
522+
523+ Thread .sleep (1000 );
524+ Assert .assertFalse (childrenMap .containsKey (testNodeFullPath ));
525+ }
526+
527+ @ Test
528+ public void testCuratorDataCacheGetterSetter () throws Exception {
529+ DataListener dataListener1 = (type , oldData , data ) -> {};
530+ DataListener dataListener2 = (type , oldData , data ) -> {};
531+
532+ CuratorZookeeperClient .CuratorDataCacheImpl cache =
533+ new CuratorZookeeperClient .CuratorDataCacheImpl (dataListener1 );
534+
535+ Assert .assertEquals (dataListener1 , cache .getDataListener ());
536+
537+ cache .setDataListener (dataListener2 );
538+ Assert .assertEquals (dataListener2 , cache .getDataListener ());
539+ }
540+
541+ @ Test
542+ public void testRemoveDataListenerWithNullCache () throws Exception {
543+ client .create (testNodeFullPath , false );
544+ String providerPath = testNodeFullPath + "/provider1" ;
545+
546+ DataListener dataListener = (type , oldData , data ) -> {};
547+ client .removeDataListener (providerPath , dataListener );
548+ }
549+
550+ @ Test
551+ public void testCreateWithContent () throws Exception {
552+ client .create (testNodeFullPath , false );
553+ String providerPath = testNodeFullPath + "/provider1" ;
554+ client .create (providerPath , "testContent" , false );
555+
556+ List <String > children = client .getChildren (testNodeFullPath );
557+ Assert .assertEquals (1 , children .size ());
558+ Assert .assertEquals ("provider1" , children .get (0 ));
559+ }
560+
561+ @ Test
562+ public void testCreateEphemeralWithContent () throws Exception {
563+ client .create (testNodeFullPath , false );
564+ String providerPath = testNodeFullPath + "/provider1" ;
565+ client .create (providerPath , "testContent" , true );
566+
567+ List <String > children = client .getChildren (testNodeFullPath );
568+ Assert .assertEquals (1 , children .size ());
569+ Assert .assertEquals ("provider1" , children .get (0 ));
570+ }
571+
572+ @ Test
573+ public void testDoClose () throws Exception {
574+ ZookeeperClient newClient = curatorZookeeperFactory .connect (buildConfig ());
575+ newClient .create (testRootPath , false );
576+ Assert .assertTrue (newClient .isConnected ());
577+ newClient .close ();
578+ newClient .close ();
579+ }
580+
581+ @ Test
582+ public void testGetState () throws Exception {
583+ client .create (testRootPath , false );
584+ Thread .sleep (500 );
585+ }
586+
587+ @ Test
588+ public void testAddStateListener () throws Exception {
589+ final Map <String , Integer > stateMap = new HashMap <>();
590+ client .addStateListener (state -> {
591+ stateMap .put (state .name (), stateMap .getOrDefault (state .name (), 0 ) + 1 );
592+ });
593+
594+ client .removeStateListener (state -> {});
595+ }
596+
597+ @ Test
598+ public void testGetContent () throws Exception {
599+ client .create (testNodeFullPath , "testContent" , false );
600+ String content = client .getContent (testNodeFullPath );
601+ Assert .assertNull (content );
602+ }
603+
604+ @ Test
605+ public void testGetContentNotExist () throws Exception {
606+ String content = client .getContent (testNodeFullPath + "/notexist" );
607+ Assert .assertNull (content );
608+ }
609+
395610}
611+
0 commit comments