22
33import static org .easymock .EasyMock .expect ;
44import static org .easymock .EasyMock .expectLastCall ;
5+ import static org .junit .Assert .assertEquals ;
6+
7+ import java .util .HashSet ;
8+ import java .util .Set ;
59
610import org .jooby .Env ;
711import org .jooby .Env .ServiceKey ;
1721import com .datastax .driver .core .Cluster .Builder ;
1822import com .datastax .driver .core .CodecRegistry ;
1923import com .datastax .driver .core .Configuration ;
24+ import com .datastax .driver .core .DelegatingCluster ;
2025import com .datastax .driver .core .Host .StateListener ;
2126import com .datastax .driver .core .Session ;
27+ import com .datastax .driver .dse .DseCluster ;
28+ import com .datastax .driver .dse .DseSession ;
2229import com .datastax .driver .extras .codecs .jdk8 .InstantCodec ;
2330import com .datastax .driver .extras .codecs .jdk8 .LocalDateCodec ;
2431import com .datastax .driver .extras .codecs .jdk8 .LocalTimeCodec ;
2532import com .datastax .driver .mapping .MappingManager ;
33+ import com .google .common .collect .ImmutableSet ;
2634import com .google .inject .Binder ;
2735import com .google .inject .Key ;
2836import com .google .inject .binder .AnnotatedBindingBuilder ;
3644 MappingManager .class , Datastore .class , CassandraMapper .class })
3745public class CassandraTest {
3846
47+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
3948 private Block clusterBuilder = unit -> {
49+ String cname = Cluster .class .getName ();
4050 unit .mockStatic (Cluster .class );
51+ expect (Cluster .class .getName ()).andReturn (cname );
52+ expect (Cluster .class .getInterfaces ()).andReturn (new Class [0 ]);
53+ expect (Cluster .class .isInterface ()).andReturn (false );
54+ Class obj = Object .class ;
55+ expect (Cluster .class .getSuperclass ()).andReturn (obj );
4156
4257 Cluster cluster = unit .get (Cluster .class );
4358 expect (cluster .getConfiguration ()).andReturn (unit .get (Configuration .class ));
@@ -47,6 +62,23 @@ public class CassandraTest {
4762 expect (builder .build ()).andReturn (cluster );
4863 };
4964
65+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
66+ private Block clusterBuilderProvider = unit -> {
67+ String cname = Cluster .class .getName ();
68+ unit .mockStatic (Cluster .class );
69+ expect (Cluster .class .getName ()).andReturn (cname );
70+ expect (Cluster .class .getInterfaces ()).andReturn (new Class [0 ]);
71+ expect (Cluster .class .isInterface ()).andReturn (false );
72+ Class obj = Object .class ;
73+ expect (Cluster .class .getSuperclass ()).andReturn (obj );
74+
75+ Cluster cluster = unit .get (Cluster .class );
76+ expect (cluster .getConfiguration ()).andReturn (unit .get (Configuration .class ));
77+
78+ Builder builder = unit .get (Cluster .Builder .class );
79+ expect (builder .build ()).andReturn (cluster );
80+ };
81+
5082 private Block codecRegistry = unit -> {
5183 CodecRegistry codecRegistry = unit .powerMock (CodecRegistry .class );
5284
@@ -124,6 +156,37 @@ public void connectViaProperty() throws Exception {
124156 });
125157 }
126158
159+ @ Test
160+ public void connectViaPropertySupplier () throws Exception {
161+ new MockUnit (Env .class , Config .class , Binder .class , Cluster .class , Cluster .Builder .class ,
162+ Configuration .class , Session .class )
163+ .expect (unit -> {
164+ Config conf = unit .get (Config .class );
165+ expect (conf .getString ("db" )).andReturn ("cassandra://localhost/beers" );
166+ })
167+ .expect (serviceKey (new Env .ServiceKey ()))
168+ .expect (clusterBuilderProvider )
169+ .expect (contactPoints ("localhost" ))
170+ .expect (port (9042 ))
171+ .expect (codecRegistry )
172+ .expect (bind ("beers" , Cluster .class ))
173+ .expect (bind (null , Cluster .class ))
174+ .expect (bind ("beers" , Session .class ))
175+ .expect (bind (null , Session .class ))
176+ .expect (connect ("beers" ))
177+ .expect (mapper )
178+ .expect (bind ("beers" , MappingManager .class ))
179+ .expect (bind (null , MappingManager .class ))
180+ .expect (datastore )
181+ .expect (bind ("beers" , Datastore .class ))
182+ .expect (bind (null , Datastore .class ))
183+ .expect (routeMapper ).expect (onStop )
184+ .run (unit -> {
185+ new Cassandra (() -> unit .get (Cluster .Builder .class ))
186+ .configure (unit .get (Env .class ), unit .get (Config .class ), unit .get (Binder .class ));
187+ });
188+ }
189+
127190 private Block serviceKey (final ServiceKey serviceKey ) {
128191 return unit -> {
129192 Env env = unit .get (Env .class );
@@ -158,6 +221,33 @@ public void connectViaConnectionString() throws Exception {
158221 });
159222 }
160223
224+ @ Test
225+ public void connectViaConnectionStringSupplier () throws Exception {
226+ new MockUnit (Env .class , Config .class , Binder .class , Cluster .class , Cluster .Builder .class ,
227+ Configuration .class , Session .class )
228+ .expect (clusterBuilderProvider )
229+ .expect (serviceKey (new Env .ServiceKey ()))
230+ .expect (contactPoints ("localhost" ))
231+ .expect (port (9042 ))
232+ .expect (codecRegistry )
233+ .expect (bind ("beers" , Cluster .class ))
234+ .expect (bind (null , Cluster .class ))
235+ .expect (bind ("beers" , Session .class ))
236+ .expect (bind (null , Session .class ))
237+ .expect (connect ("beers" ))
238+ .expect (mapper )
239+ .expect (bind ("beers" , MappingManager .class ))
240+ .expect (bind (null , MappingManager .class ))
241+ .expect (datastore )
242+ .expect (bind ("beers" , Datastore .class ))
243+ .expect (bind (null , Datastore .class ))
244+ .expect (routeMapper ).expect (onStop )
245+ .run (unit -> {
246+ new Cassandra ("cassandra://localhost/beers" , () -> unit .get (Cluster .Builder .class ))
247+ .configure (unit .get (Env .class ), unit .get (Config .class ), unit .get (Binder .class ));
248+ });
249+ }
250+
161251 @ Test
162252 public void onStop () throws Exception {
163253 new MockUnit (Env .class , Config .class , Binder .class , Cluster .class , Cluster .Builder .class ,
@@ -350,16 +440,41 @@ private Block bind(final String name, final Class type) {
350440 Binder binder = unit .get (Binder .class );
351441 if (name == null ) {
352442 AnnotatedBindingBuilder aab = unit .mock (AnnotatedBindingBuilder .class );
353- aab .toInstance (unit .get (type ));
443+ Object val = unit .get (type );
444+ aab .toInstance (val );
445+ expectLastCall ().times (1 , 2 );
354446 expect (binder .bind (Key .get (type ))).andReturn (aab );
447+ expect (binder .bind (Key .get (val .getClass ()))).andReturn (aab ).times (0 , 1 );
355448 } else {
356449 AnnotatedBindingBuilder aab = unit .mock (AnnotatedBindingBuilder .class );
357- aab .toInstance (unit .get (type ));
450+ Object val = unit .get (type );
451+ aab .toInstance (val );
452+ expectLastCall ().times (1 , 2 );
358453 expect (binder .bind (Key .get (type , Names .named (name )))).andReturn (aab );
454+ expect (binder .bind (Key .get (val .getClass (), Names .named (name )))).andReturn (aab ).times (0 , 1 );
359455 }
360456 };
361457 }
362458
459+ @ Test
460+ public void hierarchy () {
461+ assertEquals (ImmutableSet .of (), hierarchy (Object .class ));
462+ // normal
463+ assertEquals (ImmutableSet .of (Cluster .class ), hierarchy (Cluster .class ));
464+ assertEquals (ImmutableSet .of (Session .class ), hierarchy (Session .class ));
465+ // dse
466+ assertEquals (ImmutableSet .of (DseCluster .class , DelegatingCluster .class , Cluster .class ),
467+ hierarchy (DseCluster .class ));
468+ assertEquals (ImmutableSet .of (DseSession .class , Session .class ), hierarchy (DseSession .class ));
469+ }
470+
471+ @ SuppressWarnings ("rawtypes" )
472+ private Set <Class > hierarchy (final Class type ) {
473+ Set <Class > types = new HashSet <>();
474+ Cassandra .hierarchy (type , types ::add );
475+ return types ;
476+ }
477+
363478 private Block port (final int port ) {
364479 return unit -> {
365480 Builder builder = unit .get (Cluster .Builder .class );
0 commit comments