34
34
import org .junit .Test ;
35
35
36
36
import org .springframework .aop .framework .ProxyFactory ;
37
+ import org .springframework .aop .scope .ScopedProxyUtils ;
37
38
import org .springframework .beans .DirectFieldAccessor ;
38
39
import org .springframework .beans .factory .BeanCreationException ;
39
40
import org .springframework .beans .factory .config .BeanDefinition ;
40
41
import org .springframework .beans .factory .config .PropertyPlaceholderConfigurer ;
41
42
import org .springframework .beans .factory .support .RootBeanDefinition ;
43
+ import org .springframework .context .annotation .AnnotatedBeanDefinitionReader ;
44
+ import org .springframework .context .annotation .Scope ;
45
+ import org .springframework .context .annotation .ScopedProxyMode ;
42
46
import org .springframework .context .support .StaticApplicationContext ;
43
47
import org .springframework .core .annotation .AliasFor ;
44
48
import org .springframework .scheduling .Trigger ;
49
53
import org .springframework .scheduling .support .CronTrigger ;
50
54
import org .springframework .scheduling .support .ScheduledMethodRunnable ;
51
55
import org .springframework .scheduling .support .SimpleTriggerContext ;
52
- import org .springframework .tests .Assume ;
53
- import org .springframework .tests .TestGroup ;
56
+ import org .springframework .stereotype .Component ;
54
57
import org .springframework .validation .annotation .Validated ;
55
58
import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
56
59
@@ -222,9 +225,7 @@ private void severalFixedRates(StaticApplicationContext context,
222
225
}
223
226
224
227
@ Test
225
- public void cronTask () throws InterruptedException {
226
- Assume .group (TestGroup .LONG_RUNNING );
227
-
228
+ public void cronTask () {
228
229
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
229
230
BeanDefinition targetDefinition = new RootBeanDefinition (CronTestBean .class );
230
231
context .registerBeanDefinition ("postProcessor" , processorDefinition );
@@ -246,13 +247,10 @@ public void cronTask() throws InterruptedException {
246
247
assertEquals (target , targetObject );
247
248
assertEquals ("cron" , targetMethod .getName ());
248
249
assertEquals ("*/7 * * * * ?" , task .getExpression ());
249
- Thread .sleep (10000 );
250
250
}
251
251
252
252
@ Test
253
- public void cronTaskWithZone () throws InterruptedException {
254
- Assume .group (TestGroup .LONG_RUNNING );
255
-
253
+ public void cronTaskWithZone () {
256
254
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
257
255
BeanDefinition targetDefinition = new RootBeanDefinition (CronWithTimezoneTestBean .class );
258
256
context .registerBeanDefinition ("postProcessor" , processorDefinition );
@@ -280,34 +278,30 @@ public void cronTaskWithZone() throws InterruptedException {
280
278
CronTrigger cronTrigger = (CronTrigger ) trigger ;
281
279
Calendar cal = Calendar .getInstance (TimeZone .getTimeZone ("GMT+10" ));
282
280
cal .clear ();
283
- cal .set (2013 , 3 , 15 , 4 , 0 ); // 15-04-2013 4:00 GMT+10
281
+ cal .set (2013 , 3 , 15 , 4 , 0 ); // 15-04-2013 4:00 GMT+10
284
282
Date lastScheduledExecutionTime = cal .getTime ();
285
283
Date lastActualExecutionTime = cal .getTime ();
286
- cal .add (Calendar .MINUTE , 30 ); // 4:30
284
+ cal .add (Calendar .MINUTE , 30 ); // 4:30
287
285
Date lastCompletionTime = cal .getTime ();
288
286
TriggerContext triggerContext = new SimpleTriggerContext (
289
287
lastScheduledExecutionTime , lastActualExecutionTime , lastCompletionTime );
290
288
cal .add (Calendar .MINUTE , 30 );
291
- cal .add (Calendar .HOUR_OF_DAY , 1 ); // 6:00
289
+ cal .add (Calendar .HOUR_OF_DAY , 1 ); // 6:00
292
290
Date nextExecutionTime = cronTrigger .nextExecutionTime (triggerContext );
293
- assertEquals (cal .getTime (), nextExecutionTime ); // assert that 6:00 is next execution time
294
- Thread .sleep (10000 );
291
+ assertEquals (cal .getTime (), nextExecutionTime ); // assert that 6:00 is next execution time
295
292
}
296
293
297
294
@ Test (expected = BeanCreationException .class )
298
- public void cronTaskWithInvalidZone () throws InterruptedException {
299
- Assume .group (TestGroup .LONG_RUNNING );
300
-
295
+ public void cronTaskWithInvalidZone () {
301
296
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
302
297
BeanDefinition targetDefinition = new RootBeanDefinition (CronWithInvalidTimezoneTestBean .class );
303
298
context .registerBeanDefinition ("postProcessor" , processorDefinition );
304
299
context .registerBeanDefinition ("target" , targetDefinition );
305
300
context .refresh ();
306
- Thread .sleep (10000 );
307
301
}
308
302
309
303
@ Test (expected = BeanCreationException .class )
310
- public void cronTaskWithMethodValidation () throws InterruptedException {
304
+ public void cronTaskWithMethodValidation () {
311
305
BeanDefinition validationDefinition = new RootBeanDefinition (MethodValidationPostProcessor .class );
312
306
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
313
307
BeanDefinition targetDefinition = new RootBeanDefinition (CronTestBean .class );
@@ -317,6 +311,29 @@ public void cronTaskWithMethodValidation() throws InterruptedException {
317
311
context .refresh ();
318
312
}
319
313
314
+ @ Test
315
+ public void cronTaskWithScopedProxy () {
316
+ BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
317
+ context .registerBeanDefinition ("postProcessor" , processorDefinition );
318
+ new AnnotatedBeanDefinitionReader (context ).register (ProxiedCronTestBean .class , ProxiedCronTestBeanDependent .class );
319
+ context .refresh ();
320
+
321
+ Object postProcessor = context .getBean ("postProcessor" );
322
+ ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar )
323
+ new DirectFieldAccessor (postProcessor ).getPropertyValue ("registrar" );
324
+ @ SuppressWarnings ("unchecked" )
325
+ List <CronTask > cronTasks = (List <CronTask >)
326
+ new DirectFieldAccessor (registrar ).getPropertyValue ("cronTasks" );
327
+ assertEquals (1 , cronTasks .size ());
328
+ CronTask task = cronTasks .get (0 );
329
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) task .getRunnable ();
330
+ Object targetObject = runnable .getTarget ();
331
+ Method targetMethod = runnable .getMethod ();
332
+ assertEquals (context .getBean (ScopedProxyUtils .getTargetBeanName ("target" )), targetObject );
333
+ assertEquals ("cron" , targetMethod .getName ());
334
+ assertEquals ("*/7 * * * * ?" , task .getExpression ());
335
+ }
336
+
320
337
@ Test
321
338
public void metaAnnotationWithFixedRate () {
322
339
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
@@ -352,11 +369,11 @@ public void composedAnnotationWithInitialDelayAndFixedRate() {
352
369
353
370
Object postProcessor = context .getBean ("postProcessor" );
354
371
Object target = context .getBean ("target" );
355
- ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar ) new DirectFieldAccessor (
356
- postProcessor ).getPropertyValue ("registrar" );
372
+ ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar )
373
+ new DirectFieldAccessor ( postProcessor ).getPropertyValue ("registrar" );
357
374
@ SuppressWarnings ("unchecked" )
358
- List <IntervalTask > fixedRateTasks = (List <IntervalTask >) new DirectFieldAccessor ( registrar ). getPropertyValue (
359
- "fixedRateTasks" );
375
+ List <IntervalTask > fixedRateTasks = (List <IntervalTask >)
376
+ new DirectFieldAccessor ( registrar ). getPropertyValue ( "fixedRateTasks" );
360
377
assertEquals (1 , fixedRateTasks .size ());
361
378
IntervalTask task = fixedRateTasks .get (0 );
362
379
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) task .getRunnable ();
@@ -627,8 +644,7 @@ public void fixedRate() {
627
644
628
645
static class SeveralFixedRatesWithSchedulesContainerAnnotationTestBean {
629
646
630
- @ Schedules ({ @ Scheduled (fixedRate = 4000 ),
631
- @ Scheduled (fixedRate = 4000 , initialDelay = 2000 ) })
647
+ @ Schedules ({@ Scheduled (fixedRate = 4000 ), @ Scheduled (fixedRate = 4000 , initialDelay = 2000 )})
632
648
public void fixedRate () {
633
649
}
634
650
}
@@ -705,6 +721,24 @@ public void cron() throws IOException {
705
721
}
706
722
707
723
724
+ @ Component ("target" )
725
+ @ Scope (proxyMode = ScopedProxyMode .TARGET_CLASS )
726
+ static class ProxiedCronTestBean {
727
+
728
+ @ Scheduled (cron = "*/7 * * * * ?" )
729
+ public void cron () throws IOException {
730
+ throw new IOException ("no no no" );
731
+ }
732
+ }
733
+
734
+
735
+ static class ProxiedCronTestBeanDependent {
736
+
737
+ public ProxiedCronTestBeanDependent (ProxiedCronTestBean testBean ) {
738
+ }
739
+ }
740
+
741
+
708
742
static class NonVoidReturnTypeTestBean {
709
743
710
744
@ Scheduled (cron = "0 0 9-17 * * MON-FRI" )
@@ -785,6 +819,7 @@ public void generateReport() {
785
819
}
786
820
}
787
821
822
+
788
823
static class PropertyPlaceholderWithCronTestBean {
789
824
790
825
@ Scheduled (cron = "${schedules.businessHours}" )
0 commit comments