-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CommandLineJobRunnerTests.java
598 lines (504 loc) · 18.8 KB
/
CommandLineJobRunnerTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* Copyright 2006-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.launch.support;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Lucas Ward
* @author Mahmoud Ben Hassine
*
*/
public class CommandLineJobRunnerTests {
private String jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class,
"launcher-with-environment.xml");
private String jobName = "test-job";
private String jobKey = "job.Key=myKey";
private String scheduleDate = "schedule.Date=01/23/2008";
private String vendorId = "vendor.id=33243243";
private String[] args = new String[] { jobPath, jobName, jobKey, scheduleDate, vendorId };
private InputStream stdin;
@Before
public void setUp() throws Exception {
JobExecution jobExecution = new JobExecution(null, 1L, null, null);
ExitStatus exitStatus = ExitStatus.COMPLETED;
jobExecution.setExitStatus(exitStatus);
StubJobLauncher.jobExecution = jobExecution;
stdin = System.in;
System.setIn(new InputStream() {
@Override
public int read() {
return -1;
}
});
}
@After
public void tearDown() throws Exception {
System.setIn(stdin);
StubJobLauncher.tearDown();
}
@Test
public void testMain() throws Exception {
CommandLineJobRunner.main(args);
assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called);
assertEquals(0, StubSystemExiter.getStatus());
}
@Test
public void testWithJobLocator() throws Exception {
jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class, "launcher-with-locator.xml");
CommandLineJobRunner.main(new String[] { jobPath, jobName, jobKey });
assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called);
assertEquals(0, StubSystemExiter.getStatus());
}
@Test
public void testJobAlreadyRunning() throws Throwable {
StubJobLauncher.throwExecutionRunningException = true;
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testInvalidArgs() throws Exception {
String[] args = new String[] {};
CommandLineJobRunner.presetSystemExiter(new StubSystemExiter());
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage, errorMessage.contains("At least 2 arguments are required: JobPath/JobClass and jobIdentifier."));
}
@Test
public void testWrongJobName() throws Exception {
String[] args = new String[] { jobPath, "no-such-job" };
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage, (errorMessage
.contains("No bean named 'no-such-job' is defined") || (errorMessage
.contains("No bean named 'no-such-job' available"))));
}
@Test
public void testWithNoParameters() throws Throwable {
String[] args = new String[] { jobPath, jobName };
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
assertEquals(new JobParameters(), StubJobLauncher.jobParameters);
}
@Test
public void testWithInvalidStdin() throws Throwable {
System.setIn(new InputStream() {
@Override
public int available() throws IOException {
throw new IOException("Planned");
}
@Override
public int read() {
return -1;
}
});
CommandLineJobRunner.main(new String[] { jobPath, jobName });
assertEquals(0, StubSystemExiter.status);
assertEquals(0, StubJobLauncher.jobParameters.getParameters().size());
}
@Test
public void testWithStdinCommandLine() throws Throwable {
System.setIn(new InputStream() {
char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\nspam=bucket").toCharArray();
int index = 0;
@Override
public int available() {
return input.length - index;
}
@Override
public int read() {
return index<input.length-1 ? (int) input[index++] : -1;
}
});
CommandLineJobRunner.main(new String[0]);
assertEquals(0, StubSystemExiter.status);
assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}
@Test
public void testWithStdinCommandLineWithEmptyLines() throws Throwable {
System.setIn(new InputStream() {
char[] input = (jobPath+"\n"+jobName+"\nfoo=bar\n\nspam=bucket\n\n").toCharArray();
int index = 0;
@Override
public int available() {
return input.length - index;
}
@Override
public int read() {
return index<input.length-1 ? (int) input[index++] : -1;
}
});
CommandLineJobRunner.main(new String[0]);
assertEquals(0, StubSystemExiter.status);
assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}
@Test
public void testWithStdinParameters() throws Throwable {
String[] args = new String[] { jobPath, jobName };
System.setIn(new InputStream() {
char[] input = ("foo=bar\nspam=bucket").toCharArray();
int index = 0;
@Override
public int available() {
return input.length - index;
}
@Override
public int read() {
return index<input.length-1 ? (int) input[index++] : -1;
}
});
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
}
@Test
public void testWithInvalidParameters() throws Throwable {
String[] args = new String[] { jobPath, jobName, "foo" };
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage, errorMessage.contains("in the form name=value"));
}
@Test
public void testStop() throws Throwable {
String[] args = new String[] { jobPath, "-stop", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(3L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testStopFailed() throws Throwable {
String[] args = new String[] { jobPath, "-stop", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(0L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testStopFailedAndRestarted() throws Throwable {
String[] args = new String[] { jobPath, "-stop", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(5L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testStopRestarted() throws Throwable {
String[] args = new String[] { jobPath, "-stop", jobName };
JobInstance jobInstance = new JobInstance(3L, jobName);
StubJobExplorer.jobInstances = Arrays.asList(jobInstance);
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testAbandon() throws Throwable {
String[] args = new String[] { jobPath, "-abandon", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(2L, jobName));
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
}
@Test
public void testAbandonRunning() throws Throwable {
String[] args = new String[] { jobPath, "-abandon", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(3L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testAbandonAbandoned() throws Throwable {
String[] args = new String[] { jobPath, "-abandon", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(4L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
}
@Test
public void testRestart() throws Throwable {
String[] args = new String[] { jobPath, "-restart", jobName };
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
JobInstance jobInstance = new JobInstance(0L, jobName);
StubJobExplorer.jobInstances = Arrays.asList(jobInstance);
StubJobExplorer.jobParameters = jobParameters;
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
assertEquals(jobParameters, StubJobLauncher.jobParameters);
StubJobExplorer.jobParameters = new JobParameters();
}
@Test
public void testRestartExecution() throws Throwable {
String[] args = new String[] { jobPath, "-restart", "11" };
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
JobExecution jobExecution = new JobExecution(new JobInstance(0L, jobName), 11L, jobParameters, null);
jobExecution.setStatus(BatchStatus.FAILED);
StubJobExplorer.jobExecution = jobExecution;
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
assertEquals(jobParameters, StubJobLauncher.jobParameters);
}
@Test
public void testRestartExecutionNotFailed() throws Throwable {
String[] args = new String[] { jobPath, "-restart", "11" };
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters();
JobExecution jobExecution = new JobExecution(new JobInstance(0L, jobName), 11L, jobParameters, null);
jobExecution.setStatus(BatchStatus.COMPLETED);
StubJobExplorer.jobExecution = jobExecution;
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
assertEquals(null, StubJobLauncher.jobParameters);
}
@Test
public void testRestartNotFailed() throws Throwable {
String[] args = new String[] { jobPath, "-restart", jobName };
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(123L, jobName));
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage, errorMessage
.contains("No failed or stopped execution found"));
}
@Test
public void testNext() throws Throwable {
String[] args = new String[] { jobPath, "-next", jobName, "bar=foo" };
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").addString("bar", "foo")
.toJobParameters();
StubJobExplorer.jobInstances = Arrays.asList(new JobInstance(2L, jobName));
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
jobParameters = new JobParametersBuilder().addString("foo", "spam").addString("bar", "foo").toJobParameters();
assertEquals(jobParameters, StubJobLauncher.jobParameters);
}
@Test
public void testNextFirstInSequence() throws Throwable {
String[] args = new String[] { jobPath, "-next", jobName };
StubJobExplorer.jobInstances = new ArrayList<>();
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.status);
JobParameters jobParameters = new JobParametersBuilder().addString("foo", "spam").toJobParameters();
assertEquals(jobParameters, StubJobLauncher.jobParameters);
}
@Test
public void testNextWithNoParameters() throws Exception {
jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class, "launcher-with-locator.xml");
CommandLineJobRunner.main(new String[] { jobPath, "-next", "test-job2", jobKey });
assertEquals(1, StubSystemExiter.getStatus());
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage, errorMessage
.contains(" No job parameters incrementer found"));
}
@Test
public void testDestroyCallback() throws Throwable {
String[] args = new String[] { jobPath, jobName };
CommandLineJobRunner.main(args);
assertTrue(StubJobLauncher.destroyed);
}
@Test
public void testJavaConfig() throws Exception {
String[] args =
new String[] { "org.springframework.batch.core.launch.support.CommandLineJobRunnerTests$Configuration1",
"invalidJobName"};
CommandLineJobRunner.presetSystemExiter(new StubSystemExiter());
CommandLineJobRunner.main(args);
assertEquals(1, StubSystemExiter.status);
String errorMessage = CommandLineJobRunner.getErrorMessage();
assertTrue("Wrong error message: " + errorMessage,
errorMessage.contains("A JobLauncher must be provided. Please add one to the configuration."));
}
public static class StubSystemExiter implements SystemExiter {
private static int status;
@Override
public void exit(int status) {
StubSystemExiter.status = status;
}
public static int getStatus() {
return status;
}
}
public static class StubJobLauncher implements JobLauncher {
public static JobExecution jobExecution;
public static boolean throwExecutionRunningException = false;
public static JobParameters jobParameters;
private static boolean destroyed = false;
@Override
public JobExecution run(Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException {
StubJobLauncher.jobParameters = jobParameters;
if (throwExecutionRunningException) {
throw new JobExecutionAlreadyRunningException("");
}
return jobExecution;
}
public void destroy() {
destroyed = true;
}
public static void tearDown() {
jobExecution = null;
throwExecutionRunningException = false;
jobParameters = null;
destroyed = false;
}
}
public static class StubJobRepository extends JobRepositorySupport {
}
public static class StubJobExplorer implements JobExplorer {
static List<JobInstance> jobInstances = new ArrayList<>();
static JobExecution jobExecution;
static JobParameters jobParameters = new JobParameters();
@Override
public Set<JobExecution> findRunningJobExecutions(@Nullable String jobName) {
return new HashSet<>();
}
@Nullable
@Override
public JobExecution getJobExecution(@Nullable Long executionId) {
if (jobExecution != null) {
return jobExecution;
}
throw new UnsupportedOperationException();
}
@Override
public List<JobExecution> getJobExecutions(JobInstance jobInstance) {
if (jobInstance.getId() == 0) {
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.FAILED));
}
if (jobInstance.getId() == 1) {
return null;
}
if (jobInstance.getId() == 2) {
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.STOPPED));
}
if (jobInstance.getId() == 3) {
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.STARTED));
}
if (jobInstance.getId() == 4) {
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.ABANDONED));
}
if (jobInstance.getId() == 5) {
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.STARTED), createJobExecution(
jobInstance, BatchStatus.FAILED));
}
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.COMPLETED));
}
private JobExecution createJobExecution(JobInstance jobInstance, BatchStatus status) {
JobExecution jobExecution = new JobExecution(jobInstance, 1L, jobParameters, null);
jobExecution.setStatus(status);
jobExecution.setStartTime(new Date());
if (status != BatchStatus.STARTED) {
jobExecution.setEndTime(new Date());
}
return jobExecution;
}
@Nullable
@Override
public JobInstance getJobInstance(@Nullable Long instanceId) {
throw new UnsupportedOperationException();
}
@Nullable
@Override
public JobInstance getLastJobInstance(String jobName) {
return null;
}
@Nullable
@Override
public JobExecution getLastJobExecution(JobInstance jobInstance) {
return null;
}
@Override
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
if (jobInstances == null) {
return new ArrayList<>();
}
List<JobInstance> result = jobInstances;
jobInstances = null;
return result;
}
@Nullable
@Override
public StepExecution getStepExecution(@Nullable Long jobExecutionId, @Nullable Long stepExecutionId) {
throw new UnsupportedOperationException();
}
@Override
public List<String> getJobNames() {
throw new UnsupportedOperationException();
}
@Override
public List<JobInstance> findJobInstancesByJobName(String jobName, int start, int count) {
throw new UnsupportedOperationException();
}
@Override
public int getJobInstanceCount(@Nullable String jobName)
throws NoSuchJobException {
int count = 0;
for (JobInstance jobInstance : jobInstances) {
if(jobInstance.getJobName().equals(jobName)) {
count++;
}
}
if(count == 0) {
throw new NoSuchJobException("Unable to find job instances for " + jobName);
} else {
return count;
}
}
}
public static class StubJobParametersConverter implements JobParametersConverter {
JobParametersConverter delegate = new DefaultJobParametersConverter();
static boolean called = false;
@Override
public JobParameters getJobParameters(@Nullable Properties properties) {
called = true;
return delegate.getJobParameters(properties);
}
@Override
public Properties getProperties(@Nullable JobParameters params) {
throw new UnsupportedOperationException();
}
}
@Configuration
public static class Configuration1 {
@Bean
public String bean1() {
return "bean1";
}
}
}