22
22
import org .junit .Test ;
23
23
24
24
import org .springframework .boot .test .util .TestPropertyValues ;
25
- import org .springframework .context .ConfigurableApplicationContext ;
26
25
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
27
26
import org .springframework .data .domain .PageRequest ;
28
27
import org .springframework .data .web .PageableHandlerMethodArgumentResolver ;
38
37
*
39
38
* @author Andy Wilkinson
40
39
* @author Vedran Pavic
40
+ * @author Stephane Nicoll
41
41
*/
42
42
public class SpringDataWebAutoConfigurationTests {
43
43
44
- private ConfigurableApplicationContext context ;
44
+ private AnnotationConfigWebApplicationContext context ;
45
45
46
46
@ After
47
47
public void after () {
@@ -52,39 +52,33 @@ public void after() {
52
52
53
53
@ Test
54
54
public void webSupportIsAutoConfiguredInWebApplicationContexts () {
55
- this .context = new AnnotationConfigWebApplicationContext ();
56
- ((AnnotationConfigWebApplicationContext ) this .context )
57
- .register (SpringDataWebAutoConfiguration .class );
58
- this .context .refresh ();
59
- ((AnnotationConfigWebApplicationContext ) this .context )
60
- .setServletContext (new MockServletContext ());
55
+ load ();
56
+ this .context .setServletContext (new MockServletContext ());
61
57
Map <String , PageableHandlerMethodArgumentResolver > beans = this .context
62
58
.getBeansOfType (PageableHandlerMethodArgumentResolver .class );
63
59
assertThat (beans ).hasSize (1 );
64
60
}
65
61
66
62
@ Test
67
63
public void autoConfigurationBacksOffInNonWebApplicationContexts () {
68
- this .context = new AnnotationConfigApplicationContext ();
69
- ((AnnotationConfigApplicationContext ) this .context )
70
- .register (SpringDataWebAutoConfiguration .class );
71
- this .context .refresh ();
72
- Map <String , PageableHandlerMethodArgumentResolver > beans = this .context
73
- .getBeansOfType (PageableHandlerMethodArgumentResolver .class );
74
- assertThat (beans ).isEmpty ();
64
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
65
+ ctx .register (SpringDataWebAutoConfiguration .class );
66
+ try {
67
+ ctx .refresh ();
68
+ Map <String , PageableHandlerMethodArgumentResolver > beans = ctx
69
+ .getBeansOfType (PageableHandlerMethodArgumentResolver .class );
70
+ assertThat (beans ).isEmpty ();
71
+ }
72
+ finally {
73
+ ctx .close ();
74
+ }
75
75
}
76
76
77
77
@ Test
78
- public void pageable () {
79
- this .context = new AnnotationConfigWebApplicationContext ();
80
- TestPropertyValues
81
- .of ("spring.data.web.pageable.page-parameter=p" ,
82
- "spring.data.web.pageable.size-parameter=s" ,
83
- "spring.data.web.pageable.default-page-size=10" )
84
- .applyTo (this .context );
85
- ((AnnotationConfigWebApplicationContext ) this .context )
86
- .register (SpringDataWebAutoConfiguration .class );
87
- this .context .refresh ();
78
+ public void customizePageable () {
79
+ load ("spring.data.web.pageable.page-parameter=p" ,
80
+ "spring.data.web.pageable.size-parameter=s" ,
81
+ "spring.data.web.pageable.default-page-size=10" );
88
82
PageableHandlerMethodArgumentResolver argumentResolver = this .context
89
83
.getBean (PageableHandlerMethodArgumentResolver .class );
90
84
assertThat (ReflectionTestUtils .getField (argumentResolver , "pageParameterName" ))
@@ -96,17 +90,21 @@ public void pageable() {
96
90
}
97
91
98
92
@ Test
99
- public void sort () {
100
- this .context = new AnnotationConfigWebApplicationContext ();
101
- TestPropertyValues .of ("spring.data.web.sort.sort-parameter=s" )
102
- .applyTo (this .context );
103
- ((AnnotationConfigWebApplicationContext ) this .context )
104
- .register (SpringDataWebAutoConfiguration .class );
105
- this .context .refresh ();
93
+ public void customizeSort () {
94
+ load ("spring.data.web.sort.sort-parameter=s" );
106
95
SortHandlerMethodArgumentResolver argumentResolver = this .context
107
96
.getBean (SortHandlerMethodArgumentResolver .class );
108
97
assertThat (ReflectionTestUtils .getField (argumentResolver , "sortParameter" ))
109
98
.isEqualTo ("s" );
110
99
}
111
100
101
+ private void load (String ... environment ) {
102
+ AnnotationConfigWebApplicationContext ctx =
103
+ new AnnotationConfigWebApplicationContext ();
104
+ TestPropertyValues .of (environment ).applyTo (ctx );
105
+ ctx .register (SpringDataWebAutoConfiguration .class );
106
+ ctx .refresh ();
107
+ this .context = ctx ;
108
+ }
109
+
112
110
}
0 commit comments