16
16
17
17
package org .springframework .boot .autoconfigure .data .cassandra ;
18
18
19
+ import java .util .Collections ;
19
20
import java .util .Set ;
20
21
21
22
import com .datastax .driver .core .Session ;
22
23
import org .junit .After ;
23
24
import org .junit .Test ;
24
25
25
26
import org .springframework .boot .autoconfigure .cassandra .CassandraAutoConfiguration ;
26
- import org .springframework .boot .autoconfigure .context .PropertyPlaceholderAutoConfiguration ;
27
27
import org .springframework .boot .autoconfigure .data .cassandra .city .City ;
28
28
import org .springframework .boot .autoconfigure .domain .EntityScan ;
29
29
import org .springframework .boot .test .util .TestPropertyValues ;
32
32
import org .springframework .context .annotation .ComponentScan ;
33
33
import org .springframework .context .annotation .Configuration ;
34
34
import org .springframework .context .annotation .FilterType ;
35
+ import org .springframework .core .convert .converter .Converter ;
35
36
import org .springframework .data .cassandra .core .CassandraTemplate ;
37
+ import org .springframework .data .cassandra .core .convert .CassandraCustomConversions ;
36
38
import org .springframework .data .cassandra .core .mapping .CassandraMappingContext ;
37
39
import org .springframework .data .cassandra .core .mapping .SimpleUserTypeResolver ;
38
40
import org .springframework .test .util .ReflectionTestUtils ;
41
+ import org .springframework .util .ObjectUtils ;
39
42
40
43
import static org .assertj .core .api .Assertions .assertThat ;
41
44
import static org .mockito .Mockito .mock ;
42
45
43
46
/**
44
- * Tests for {@link CassandraDataAutoConfiguration}
47
+ * Tests for {@link CassandraDataAutoConfiguration}.
45
48
*
46
49
* @author Eddú Meléndez
47
50
* @author Mark Paluch
51
+ * @author Stephane Nicoll
48
52
*/
49
53
public class CassandraDataAutoConfigurationTests {
50
54
@@ -59,26 +63,15 @@ public void close() {
59
63
60
64
@ Test
61
65
public void templateExists () {
62
- this .context = new AnnotationConfigApplicationContext ();
63
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
64
- .applyTo (this .context );
65
- this .context .register (TestExcludeConfiguration .class , TestConfiguration .class ,
66
- PropertyPlaceholderAutoConfiguration .class ,
67
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
68
- this .context .refresh ();
66
+ load (TestExcludeConfiguration .class );
69
67
assertThat (this .context .getBeanNamesForType (CassandraTemplate .class ).length )
70
68
.isEqualTo (1 );
71
69
}
72
70
73
71
@ Test
74
72
@ SuppressWarnings ("unchecked" )
75
73
public void entityScanShouldSetInitialEntitySet () throws Exception {
76
- this .context = new AnnotationConfigApplicationContext ();
77
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" );
78
- this .context .register (TestConfiguration .class , EntityScanConfig .class ,
79
- PropertyPlaceholderAutoConfiguration .class ,
80
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
81
- this .context .refresh ();
74
+ load (EntityScanConfig .class );
82
75
CassandraMappingContext mappingContext = this .context
83
76
.getBean (CassandraMappingContext .class );
84
77
Set <Class <?>> initialEntitySet = (Set <Class <?>>) ReflectionTestUtils
@@ -88,19 +81,43 @@ public void entityScanShouldSetInitialEntitySet() throws Exception {
88
81
89
82
@ Test
90
83
public void userTypeResolverShouldBeSet () throws Exception {
91
- this .context = new AnnotationConfigApplicationContext ();
92
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
93
- .applyTo (this .context );
94
- this .context .register (TestConfiguration .class ,
95
- PropertyPlaceholderAutoConfiguration .class ,
96
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
97
- this .context .refresh ();
84
+ load ();
98
85
CassandraMappingContext mappingContext = this .context
99
86
.getBean (CassandraMappingContext .class );
100
87
assertThat (ReflectionTestUtils .getField (mappingContext , "userTypeResolver" ))
101
88
.isInstanceOf (SimpleUserTypeResolver .class );
102
89
}
103
90
91
+ @ Test
92
+ public void defaultConversions () {
93
+ load ();
94
+ CassandraTemplate template = this .context .getBean (CassandraTemplate .class );
95
+ assertThat (template .getConverter ().getConversionService ().canConvert (Person .class ,
96
+ String .class )).isFalse ();
97
+ }
98
+
99
+ @ Test
100
+ public void customConversions () {
101
+ load (CustomConversionConfig .class );
102
+ CassandraTemplate template = this .context .getBean (CassandraTemplate .class );
103
+ assertThat (template .getConverter ().getConversionService ().canConvert (Person .class ,
104
+ String .class )).isTrue ();
105
+
106
+ }
107
+
108
+ public void load (Class <?>... config ) {
109
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
110
+ TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
111
+ .applyTo (ctx );
112
+ if (!ObjectUtils .isEmpty (config )) {
113
+ ctx .register (config );
114
+ }
115
+ ctx .register (TestConfiguration .class , CassandraAutoConfiguration .class ,
116
+ CassandraDataAutoConfiguration .class );
117
+ ctx .refresh ();
118
+ this .context = ctx ;
119
+ }
120
+
104
121
@ Configuration
105
122
@ ComponentScan (excludeFilters = @ ComponentScan .Filter (classes = {
106
123
Session .class }, type = FilterType .ASSIGNABLE_TYPE ))
@@ -124,4 +141,27 @@ static class EntityScanConfig {
124
141
125
142
}
126
143
144
+ @ Configuration
145
+ static class CustomConversionConfig {
146
+
147
+ @ Bean
148
+ public CassandraCustomConversions myCassandraCustomConversions () {
149
+ return new CassandraCustomConversions (Collections .singletonList (
150
+ new MyConverter ()));
151
+ }
152
+
153
+ }
154
+
155
+ private static class MyConverter implements Converter <Person , String > {
156
+
157
+ @ Override
158
+ public String convert (Person o ) {
159
+ return null ;
160
+ }
161
+ }
162
+
163
+ private static class Person {
164
+
165
+ }
166
+
127
167
}
0 commit comments