1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2011 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.
16
16
17
17
package org .springframework .scheduling .annotation ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
-
19
+ import java .io .IOException ;
21
20
import java .lang .annotation .ElementType ;
22
21
import java .lang .annotation .Retention ;
23
22
import java .lang .annotation .RetentionPolicy ;
24
23
import java .lang .annotation .Target ;
24
+ import java .lang .reflect .Method ;
25
25
import java .util .Map ;
26
26
import java .util .Properties ;
27
27
28
+ import static org .junit .Assert .*;
28
29
import org .junit .Test ;
29
30
30
31
import org .springframework .beans .DirectFieldAccessor ;
34
35
import org .springframework .beans .factory .support .RootBeanDefinition ;
35
36
import org .springframework .context .support .StaticApplicationContext ;
36
37
import org .springframework .scheduling .config .ScheduledTaskRegistrar ;
37
- import org .springframework .scheduling .support .MethodInvokingRunnable ;
38
+ import org .springframework .scheduling .support .ScheduledMethodRunnable ;
38
39
39
40
/**
40
41
* @author Mark Fisher
42
+ * @author Juergen Hoeller
41
43
*/
42
- @ SuppressWarnings ({"unchecked" , "unused" })
43
44
public class ScheduledAnnotationBeanPostProcessorTests {
44
45
45
46
@ Test
@@ -58,11 +59,11 @@ public void fixedDelayTask() {
58
59
Map <Runnable , Long > fixedDelayTasks = (Map <Runnable , Long >)
59
60
new DirectFieldAccessor (registrar ).getPropertyValue ("fixedDelayTasks" );
60
61
assertEquals (1 , fixedDelayTasks .size ());
61
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) fixedDelayTasks .keySet ().iterator ().next ();
62
- Object targetObject = runnable .getTargetObject ();
63
- String targetMethod = runnable .getTargetMethod ();
62
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) fixedDelayTasks .keySet ().iterator ().next ();
63
+ Object targetObject = runnable .getTarget ();
64
+ Method targetMethod = runnable .getMethod ();
64
65
assertEquals (target , targetObject );
65
- assertEquals ("fixedDelay" , targetMethod );
66
+ assertEquals ("fixedDelay" , targetMethod . getName () );
66
67
assertEquals (new Long (5000 ), fixedDelayTasks .values ().iterator ().next ());
67
68
}
68
69
@@ -82,16 +83,16 @@ public void fixedRateTask() {
82
83
Map <Runnable , Long > fixedRateTasks = (Map <Runnable , Long >)
83
84
new DirectFieldAccessor (registrar ).getPropertyValue ("fixedRateTasks" );
84
85
assertEquals (1 , fixedRateTasks .size ());
85
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) fixedRateTasks .keySet ().iterator ().next ();
86
- Object targetObject = runnable .getTargetObject ();
87
- String targetMethod = runnable .getTargetMethod ();
86
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) fixedRateTasks .keySet ().iterator ().next ();
87
+ Object targetObject = runnable .getTarget ();
88
+ Method targetMethod = runnable .getMethod ();
88
89
assertEquals (target , targetObject );
89
- assertEquals ("fixedRate" , targetMethod );
90
+ assertEquals ("fixedRate" , targetMethod . getName () );
90
91
assertEquals (new Long (3000 ), fixedRateTasks .values ().iterator ().next ());
91
92
}
92
93
93
94
@ Test
94
- public void cronTask () {
95
+ public void cronTask () throws InterruptedException {
95
96
StaticApplicationContext context = new StaticApplicationContext ();
96
97
BeanDefinition processorDefinition = new RootBeanDefinition (ScheduledAnnotationBeanPostProcessor .class );
97
98
BeanDefinition targetDefinition = new RootBeanDefinition (
@@ -106,12 +107,13 @@ public void cronTask() {
106
107
Map <Runnable , String > cronTasks = (Map <Runnable , String >)
107
108
new DirectFieldAccessor (registrar ).getPropertyValue ("cronTasks" );
108
109
assertEquals (1 , cronTasks .size ());
109
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) cronTasks .keySet ().iterator ().next ();
110
- Object targetObject = runnable .getTargetObject ();
111
- String targetMethod = runnable .getTargetMethod ();
110
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) cronTasks .keySet ().iterator ().next ();
111
+ Object targetObject = runnable .getTarget ();
112
+ Method targetMethod = runnable .getMethod ();
112
113
assertEquals (target , targetObject );
113
- assertEquals ("cron" , targetMethod );
114
+ assertEquals ("cron" , targetMethod . getName () );
114
115
assertEquals ("*/7 * * * * ?" , cronTasks .values ().iterator ().next ());
116
+ Thread .sleep (10000 );
115
117
}
116
118
117
119
@ Test
@@ -130,11 +132,11 @@ public void metaAnnotationWithFixedRate() {
130
132
Map <Runnable , Long > fixedRateTasks = (Map <Runnable , Long >)
131
133
new DirectFieldAccessor (registrar ).getPropertyValue ("fixedRateTasks" );
132
134
assertEquals (1 , fixedRateTasks .size ());
133
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) fixedRateTasks .keySet ().iterator ().next ();
134
- Object targetObject = runnable .getTargetObject ();
135
- String targetMethod = runnable .getTargetMethod ();
135
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) fixedRateTasks .keySet ().iterator ().next ();
136
+ Object targetObject = runnable .getTarget ();
137
+ Method targetMethod = runnable .getMethod ();
136
138
assertEquals (target , targetObject );
137
- assertEquals ("checkForUpdates" , targetMethod );
139
+ assertEquals ("checkForUpdates" , targetMethod . getName () );
138
140
assertEquals (new Long (5000 ), fixedRateTasks .values ().iterator ().next ());
139
141
}
140
142
@@ -154,11 +156,11 @@ public void metaAnnotationWithCronExpression() {
154
156
Map <Runnable , String > cronTasks = (Map <Runnable , String >)
155
157
new DirectFieldAccessor (registrar ).getPropertyValue ("cronTasks" );
156
158
assertEquals (1 , cronTasks .size ());
157
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) cronTasks .keySet ().iterator ().next ();
158
- Object targetObject = runnable .getTargetObject ();
159
- String targetMethod = runnable .getTargetMethod ();
159
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) cronTasks .keySet ().iterator ().next ();
160
+ Object targetObject = runnable .getTarget ();
161
+ Method targetMethod = runnable .getMethod ();
160
162
assertEquals (target , targetObject );
161
- assertEquals ("generateReport" , targetMethod );
163
+ assertEquals ("generateReport" , targetMethod . getName () );
162
164
assertEquals ("0 0 * * * ?" , cronTasks .values ().iterator ().next ());
163
165
}
164
166
@@ -184,11 +186,11 @@ public void propertyPlaceholderWithCronExpression() {
184
186
Map <Runnable , String > cronTasks = (Map <Runnable , String >)
185
187
new DirectFieldAccessor (registrar ).getPropertyValue ("cronTasks" );
186
188
assertEquals (1 , cronTasks .size ());
187
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) cronTasks .keySet ().iterator ().next ();
188
- Object targetObject = runnable .getTargetObject ();
189
- String targetMethod = runnable .getTargetMethod ();
189
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) cronTasks .keySet ().iterator ().next ();
190
+ Object targetObject = runnable .getTarget ();
191
+ Method targetMethod = runnable .getMethod ();
190
192
assertEquals (target , targetObject );
191
- assertEquals ("x" , targetMethod );
193
+ assertEquals ("x" , targetMethod . getName () );
192
194
assertEquals (businessHoursCronExpression , cronTasks .values ().iterator ().next ());
193
195
}
194
196
@@ -214,11 +216,11 @@ public void propertyPlaceholderForMetaAnnotation() {
214
216
Map <Runnable , String > cronTasks = (Map <Runnable , String >)
215
217
new DirectFieldAccessor (registrar ).getPropertyValue ("cronTasks" );
216
218
assertEquals (1 , cronTasks .size ());
217
- MethodInvokingRunnable runnable = (MethodInvokingRunnable ) cronTasks .keySet ().iterator ().next ();
218
- Object targetObject = runnable .getTargetObject ();
219
- String targetMethod = runnable .getTargetMethod ();
219
+ ScheduledMethodRunnable runnable = (ScheduledMethodRunnable ) cronTasks .keySet ().iterator ().next ();
220
+ Object targetObject = runnable .getTarget ();
221
+ Method targetMethod = runnable .getMethod ();
220
222
assertEquals (target , targetObject );
221
- assertEquals ("y" , targetMethod );
223
+ assertEquals ("y" , targetMethod . getName () );
222
224
assertEquals (businessHoursCronExpression , cronTasks .values ().iterator ().next ());
223
225
}
224
226
@@ -267,28 +269,27 @@ public void nonEmptyParamList() {
267
269
}
268
270
269
271
270
- private static class FixedDelayTestBean {
272
+ public static class FixedDelayTestBean {
271
273
272
274
@ Scheduled (fixedDelay =5000 )
273
275
public void fixedDelay () {
274
276
}
275
-
276
277
}
277
278
278
279
279
- private static class FixedRateTestBean {
280
+ public static class FixedRateTestBean {
280
281
281
282
@ Scheduled (fixedRate =3000 )
282
283
public void fixedRate () {
283
284
}
284
-
285
285
}
286
286
287
287
288
- private static class CronTestBean {
288
+ public static class CronTestBean {
289
289
290
290
@ Scheduled (cron ="*/7 * * * * ?" )
291
- public void cron () {
291
+ public void cron () throws IOException {
292
+ throw new IOException ("no no no" );
292
293
}
293
294
294
295
}
0 commit comments