24
24
import org .junit .Test ;
25
25
26
26
import org .springframework .boot .autoconfigure .cassandra .CassandraAutoConfiguration ;
27
- import org .springframework .boot .autoconfigure .context .PropertyPlaceholderAutoConfiguration ;
28
27
import org .springframework .boot .autoconfigure .data .cassandra .city .City ;
29
28
import org .springframework .boot .autoconfigure .domain .EntityScan ;
30
29
import org .springframework .boot .test .util .TestPropertyValues ;
39
38
import org .springframework .data .cassandra .core .mapping .CassandraMappingContext ;
40
39
import org .springframework .data .cassandra .core .mapping .SimpleUserTypeResolver ;
41
40
import org .springframework .test .util .ReflectionTestUtils ;
41
+ import org .springframework .util .ObjectUtils ;
42
42
43
43
import static org .assertj .core .api .Assertions .assertThat ;
44
44
import static org .mockito .Mockito .mock ;
45
45
46
46
/**
47
- * Tests for {@link CassandraDataAutoConfiguration}
47
+ * Tests for {@link CassandraDataAutoConfiguration}.
48
48
*
49
49
* @author Eddú Meléndez
50
50
* @author Mark Paluch
51
+ * @author Stephane Nicoll
51
52
*/
52
53
public class CassandraDataAutoConfigurationTests {
53
54
@@ -62,26 +63,15 @@ public void close() {
62
63
63
64
@ Test
64
65
public void templateExists () {
65
- this .context = new AnnotationConfigApplicationContext ();
66
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
67
- .applyTo (this .context );
68
- this .context .register (TestExcludeConfiguration .class , TestConfiguration .class ,
69
- PropertyPlaceholderAutoConfiguration .class ,
70
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
71
- this .context .refresh ();
66
+ load (TestExcludeConfiguration .class );
72
67
assertThat (this .context .getBeanNamesForType (CassandraTemplate .class ).length )
73
68
.isEqualTo (1 );
74
69
}
75
70
76
71
@ Test
77
72
@ SuppressWarnings ("unchecked" )
78
73
public void entityScanShouldSetInitialEntitySet () throws Exception {
79
- this .context = new AnnotationConfigApplicationContext ();
80
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" );
81
- this .context .register (TestConfiguration .class , EntityScanConfig .class ,
82
- PropertyPlaceholderAutoConfiguration .class ,
83
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
84
- this .context .refresh ();
74
+ load (EntityScanConfig .class );
85
75
CassandraMappingContext mappingContext = this .context
86
76
.getBean (CassandraMappingContext .class );
87
77
Set <Class <?>> initialEntitySet = (Set <Class <?>>) ReflectionTestUtils
@@ -91,13 +81,7 @@ public void entityScanShouldSetInitialEntitySet() throws Exception {
91
81
92
82
@ Test
93
83
public void userTypeResolverShouldBeSet () throws Exception {
94
- this .context = new AnnotationConfigApplicationContext ();
95
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
96
- .applyTo (this .context );
97
- this .context .register (TestConfiguration .class ,
98
- PropertyPlaceholderAutoConfiguration .class ,
99
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
100
- this .context .refresh ();
84
+ load ();
101
85
CassandraMappingContext mappingContext = this .context
102
86
.getBean (CassandraMappingContext .class );
103
87
assertThat (ReflectionTestUtils .getField (mappingContext , "userTypeResolver" ))
@@ -106,34 +90,34 @@ public void userTypeResolverShouldBeSet() throws Exception {
106
90
107
91
@ Test
108
92
public void defaultConversions () {
109
- this .context = new AnnotationConfigApplicationContext ();
110
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
111
- .applyTo (this .context );
112
- this .context .register (TestConfiguration .class ,
113
- PropertyPlaceholderAutoConfiguration .class ,
114
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
115
- this .context .refresh ();
93
+ load ();
116
94
CassandraTemplate template = this .context .getBean (CassandraTemplate .class );
117
95
assertThat (template .getConverter ().getConversionService ().canConvert (Person .class ,
118
96
String .class )).isFalse ();
119
97
}
120
98
121
99
@ Test
122
100
public void customConversions () {
123
- this .context = new AnnotationConfigApplicationContext ();
124
- TestPropertyValues .of ("spring.data.cassandra.keyspaceName:boot_test" )
125
- .applyTo (this .context );
126
- this .context .register (CustomConversionConfig .class ,
127
- TestConfiguration .class ,
128
- PropertyPlaceholderAutoConfiguration .class ,
129
- CassandraAutoConfiguration .class , CassandraDataAutoConfiguration .class );
130
- this .context .refresh ();
101
+ load (CustomConversionConfig .class );
131
102
CassandraTemplate template = this .context .getBean (CassandraTemplate .class );
132
103
assertThat (template .getConverter ().getConversionService ().canConvert (Person .class ,
133
104
String .class )).isTrue ();
134
105
135
106
}
136
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
+
137
121
@ Configuration
138
122
@ ComponentScan (excludeFilters = @ ComponentScan .Filter (classes = {
139
123
Session .class }, type = FilterType .ASSIGNABLE_TYPE ))
0 commit comments