1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import java .util .Properties ;
29
29
import java .util .TimeZone ;
30
30
31
+ import org .junit .After ;
31
32
import org .junit .Test ;
32
33
33
34
import org .springframework .beans .DirectFieldAccessor ;
46
47
import org .springframework .scheduling .support .SimpleTriggerContext ;
47
48
import org .springframework .tests .Assume ;
48
49
import org .springframework .tests .TestGroup ;
50
+ import org .springframework .validation .annotation .Validated ;
51
+ import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
49
52
50
53
import static org .junit .Assert .*;
51
54
@@ -60,6 +63,13 @@ public class ScheduledAnnotationBeanPostProcessorTests {
60
63
61
64
private final StaticApplicationContext context = new StaticApplicationContext ();
62
65
66
+
67
+ @ After
68
+ public void closeContextAfterTest () {
69
+ context .close ();
70
+ }
71
+
72
+
63
73
@ Test
64
74
public void fixedDelayTask () {
65
75
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
@@ -140,7 +150,8 @@ public void fixedRateTaskWithInitialDelay() {
140
150
public void severalFixedRatesWithRepeatedScheduledAnnotation () {
141
151
BeanDefinition processorDefinition = new
142
152
RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
143
- BeanDefinition targetDefinition = new RootBeanDefinition (SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean .class );
153
+ BeanDefinition targetDefinition = new RootBeanDefinition (
154
+ SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean .class );
144
155
severalFixedRates (context , processorDefinition , targetDefinition );
145
156
}
146
157
@@ -155,6 +166,7 @@ public void severalFixedRatesWithSchedulesContainerAnnotation() {
155
166
156
167
private void severalFixedRates (StaticApplicationContext context ,
157
168
BeanDefinition processorDefinition , BeanDefinition targetDefinition ) {
169
+
158
170
context .registerBeanDefinition ("postProcessor" , processorDefinition );
159
171
context .registerBeanDefinition ("target" , targetDefinition );
160
172
context .refresh ();
@@ -248,7 +260,8 @@ public void cronTaskWithZone() throws InterruptedException {
248
260
Date lastActualExecutionTime = cal .getTime ();
249
261
cal .add (Calendar .MINUTE , 30 ); // 4:30
250
262
Date lastCompletionTime = cal .getTime ();
251
- TriggerContext triggerContext = new SimpleTriggerContext (lastScheduledExecutionTime , lastActualExecutionTime , lastCompletionTime );
263
+ TriggerContext triggerContext = new SimpleTriggerContext (
264
+ lastScheduledExecutionTime , lastActualExecutionTime , lastCompletionTime );
252
265
cal .add (Calendar .MINUTE , 30 );
253
266
cal .add (Calendar .HOUR_OF_DAY , 1 ); // 6:00
254
267
Date nextExecutionTime = cronTrigger .nextExecutionTime (triggerContext );
@@ -269,6 +282,18 @@ public void cronTaskWithInvalidZone() throws InterruptedException {
269
282
Thread .sleep (10000 );
270
283
}
271
284
285
+ @ Test (expected = BeanCreationException .class )
286
+ public void cronTaskWithMethodValidation () throws InterruptedException {
287
+ BeanDefinition validationDefinition = new RootBeanDefinition (MethodValidationPostProcessor .class );
288
+ BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
289
+ BeanDefinition targetDefinition = new RootBeanDefinition (
290
+ ScheduledAnnotationBeanPostProcessorTests .CronTestBean .class );
291
+ context .registerBeanDefinition ("methodValidation" , validationDefinition );
292
+ context .registerBeanDefinition ("postProcessor" , processorDefinition );
293
+ context .registerBeanDefinition ("target" , targetDefinition );
294
+ context .refresh ();
295
+ }
296
+
272
297
@ Test
273
298
public void metaAnnotationWithFixedRate () {
274
299
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
@@ -527,10 +552,11 @@ public void fixedRate() {
527
552
}
528
553
529
554
555
+ @ Validated
530
556
static class CronTestBean {
531
557
532
558
@ Scheduled (cron ="*/7 * * * * ?" )
533
- public void cron () throws IOException {
559
+ private void cron () throws IOException {
534
560
throw new IOException ("no no no" );
535
561
}
536
562
}
@@ -539,7 +565,7 @@ public void cron() throws IOException {
539
565
static class CronWithTimezoneTestBean {
540
566
541
567
@ Scheduled (cron ="0 0 0-4,6-23 * * ?" , zone = "GMT+10" )
542
- public void cron () throws IOException {
568
+ protected void cron () throws IOException {
543
569
throw new IOException ("no no no" );
544
570
}
545
571
}
@@ -642,7 +668,8 @@ public void fixedRate() {
642
668
@ Scheduled (cron ="${schedules.businessHours}" )
643
669
@ Target (ElementType .METHOD )
644
670
@ Retention (RetentionPolicy .RUNTIME )
645
- private static @interface BusinessHours {}
671
+ private static @interface BusinessHours {
672
+ }
646
673
647
674
648
675
static class PropertyPlaceholderMetaAnnotationTestBean {
0 commit comments