Skip to content

Commit 4a4cd5b

Browse files
committed
Polish CronSequenceGenerator and tests
1 parent 151530b commit 4a4cd5b

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,7 +95,6 @@ public CronSequenceGenerator(String expression, TimeZone timeZone) {
9595
parse(expression);
9696
}
9797

98-
9998
/**
10099
* Return the cron pattern that this sequence generator has been built for.
101100
*/
@@ -149,22 +148,6 @@ public Date next(Date date) {
149148
return calendar.getTime();
150149
}
151150

152-
/**
153-
* Indicates whether the specified cron expression can be parsed into a
154-
* valid cron sequence generator
155-
* @param cronExpression the expression to evaluate
156-
* @return a boolean indicating whether the given expression is a valid cron
157-
* expression
158-
*/
159-
public static boolean isValidExpression(String cronExpression) {
160-
String[] fields = StringUtils.tokenizeToStringArray(cronExpression, " ");
161-
return validateCronFields(fields);
162-
}
163-
164-
private static boolean validateCronFields(String[] fields) {
165-
return fields != null && fields.length == 6;
166-
}
167-
168151
private void doNext(Calendar calendar, int dot) {
169152
List<Integer> resets = new ArrayList<Integer>();
170153

@@ -278,7 +261,7 @@ private void reset(Calendar calendar, List<Integer> fields) {
278261
*/
279262
private void parse(String expression) throws IllegalArgumentException {
280263
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
281-
if (!validateCronFields(fields)) {
264+
if (!areValidCronFields(fields)) {
282265
throw new IllegalArgumentException(String.format(
283266
"Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression));
284267
}
@@ -399,6 +382,23 @@ private int[] getRange(String field, int min, int max) {
399382
}
400383

401384

385+
/**
386+
* Determine whether the specified expression represents a valid cron pattern.
387+
* <p>Specifically, this method verifies that the expression contains six
388+
* fields separated by single spaces.
389+
* @param expression the expression to evaluate
390+
* @return {@code true} if the given expression is a valid cron expression
391+
*/
392+
public static boolean isValidExpression(String expression) {
393+
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
394+
return areValidCronFields(fields);
395+
}
396+
397+
private static boolean areValidCronFields(String[] fields) {
398+
return (fields != null && fields.length == 6);
399+
}
400+
401+
402402
@Override
403403
public boolean equals(Object other) {
404404
if (this == other) {

spring-context/src/test/java/org/springframework/scheduling/support/CronSequenceGeneratorTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,45 +29,46 @@
2929
public class CronSequenceGeneratorTests {
3030

3131
@Test
32-
public void testAt50Seconds() {
32+
public void at50Seconds() {
3333
assertEquals(new Date(2012, 6, 2, 1, 0),
3434
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50)));
3535
}
3636

3737
@Test
38-
public void testAt0Seconds() {
38+
public void at0Seconds() {
3939
assertEquals(new Date(2012, 6, 2, 1, 0),
4040
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53)));
4141
}
4242

4343
@Test
44-
public void testAt0Minutes() {
44+
public void at0Minutes() {
4545
assertEquals(new Date(2012, 6, 2, 1, 0),
4646
new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0)));
4747
}
4848

4949
@Test(expected = IllegalArgumentException.class)
50-
public void testWith0Increment() {
50+
public void with0Increment() {
5151
new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5252
}
5353

5454
@Test(expected = IllegalArgumentException.class)
55-
public void testWithNegativeIncrement() {
55+
public void withNegativeIncrement() {
5656
new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0));
5757
}
5858

5959
@Test
60-
public void testValidExpression() {
60+
public void validExpression() {
6161
assertTrue(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * *"));
6262
}
6363

6464
@Test
65-
public void testNotValidExpression() {
65+
public void invalidExpression() {
6666
assertFalse(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * * *"));
6767
}
6868

6969
@Test
70-
public void testNullExpression() {
70+
public void nullExpression() {
7171
assertFalse(CronSequenceGenerator.isValidExpression(null));
7272
}
73+
7374
}

0 commit comments

Comments
 (0)