Polish CronSequenceGenerator and tests

This commit is contained in:
Sam Brannen 2016-04-23 18:05:54 +02:00
parent 151530b93b
commit 4a4cd5bde8
2 changed files with 29 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -95,7 +95,6 @@ public class CronSequenceGenerator {
parse(expression); parse(expression);
} }
/** /**
* Return the cron pattern that this sequence generator has been built for. * Return the cron pattern that this sequence generator has been built for.
*/ */
@ -149,22 +148,6 @@ public class CronSequenceGenerator {
return calendar.getTime(); return calendar.getTime();
} }
/**
* Indicates whether the specified cron expression can be parsed into a
* valid cron sequence generator
* @param cronExpression the expression to evaluate
* @return a boolean indicating whether the given expression is a valid cron
* expression
*/
public static boolean isValidExpression(String cronExpression) {
String[] fields = StringUtils.tokenizeToStringArray(cronExpression, " ");
return validateCronFields(fields);
}
private static boolean validateCronFields(String[] fields) {
return fields != null && fields.length == 6;
}
private void doNext(Calendar calendar, int dot) { private void doNext(Calendar calendar, int dot) {
List<Integer> resets = new ArrayList<Integer>(); List<Integer> resets = new ArrayList<Integer>();
@ -278,7 +261,7 @@ public class CronSequenceGenerator {
*/ */
private void parse(String expression) throws IllegalArgumentException { private void parse(String expression) throws IllegalArgumentException {
String[] fields = StringUtils.tokenizeToStringArray(expression, " "); String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
if (!validateCronFields(fields)) { if (!areValidCronFields(fields)) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
"Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression)); "Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression));
} }
@ -399,6 +382,23 @@ public class CronSequenceGenerator {
} }
/**
* Determine whether the specified expression represents a valid cron pattern.
* <p>Specifically, this method verifies that the expression contains six
* fields separated by single spaces.
* @param expression the expression to evaluate
* @return {@code true} if the given expression is a valid cron expression
*/
public static boolean isValidExpression(String expression) {
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
return areValidCronFields(fields);
}
private static boolean areValidCronFields(String[] fields) {
return (fields != null && fields.length == 6);
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) { if (this == other) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,45 +29,46 @@ import static org.junit.Assert.*;
public class CronSequenceGeneratorTests { public class CronSequenceGeneratorTests {
@Test @Test
public void testAt50Seconds() { public void at50Seconds() {
assertEquals(new Date(2012, 6, 2, 1, 0), assertEquals(new Date(2012, 6, 2, 1, 0),
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50))); new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53, 50)));
} }
@Test @Test
public void testAt0Seconds() { public void at0Seconds() {
assertEquals(new Date(2012, 6, 2, 1, 0), assertEquals(new Date(2012, 6, 2, 1, 0),
new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53))); new CronSequenceGenerator("*/15 * 1-4 * * *").next(new Date(2012, 6, 1, 9, 53)));
} }
@Test @Test
public void testAt0Minutes() { public void at0Minutes() {
assertEquals(new Date(2012, 6, 2, 1, 0), assertEquals(new Date(2012, 6, 2, 1, 0),
new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0))); new CronSequenceGenerator("0 */2 1-4 * * *").next(new Date(2012, 6, 1, 9, 0)));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWith0Increment() { public void with0Increment() {
new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0)); new CronSequenceGenerator("*/0 * * * * *").next(new Date(2012, 6, 1, 9, 0));
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testWithNegativeIncrement() { public void withNegativeIncrement() {
new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0)); new CronSequenceGenerator("*/-1 * * * * *").next(new Date(2012, 6, 1, 9, 0));
} }
@Test @Test
public void testValidExpression() { public void validExpression() {
assertTrue(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * *")); assertTrue(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * *"));
} }
@Test @Test
public void testNotValidExpression() { public void invalidExpression() {
assertFalse(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * * *")); assertFalse(CronSequenceGenerator.isValidExpression("0 */2 1-4 * * * *"));
} }
@Test @Test
public void testNullExpression() { public void nullExpression() {
assertFalse(CronSequenceGenerator.isValidExpression(null)); assertFalse(CronSequenceGenerator.isValidExpression(null));
} }
} }