Skip to content

Commit 5918c1a

Browse files
authored
Merge pull request #419 from ShaneLee/bug/412-hash
Check for case where hash is the last value of cron expression #412
2 parents 50e158c + 41abc73 commit 5918c1a

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/main/java/com/cronutils/parser/CronParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ public Cron parse(final String expression) {
121121
String.format("Cron expression contains %s parts but we expect one of %s", expressionLength, expressions.keySet()));
122122
}
123123
try {
124-
final int size = fields.size();
124+
125+
final int size = expressionParts.length;
125126
final List<CronField> results = new ArrayList<>(size + 1);
126127
for (int j = 0; j < size; j++) {
127128
results.add(fields.get(j).parse(expressionParts[j]));
@@ -133,3 +134,4 @@ public Cron parse(final String expression) {
133134
}
134135
}
135136
}
137+

src/main/java/com/cronutils/parser/FieldParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ protected On parseOnWithHash(final String exp) {
182182
}
183183
final SpecialCharFieldValue specialChar = new SpecialCharFieldValue(HASH);
184184
final String[] array = exp.split(HASH_TAG);
185+
if (array.length == 0) {
186+
throw new IllegalArgumentException("Invalid Position of # Character!");
187+
}
185188
final IntegerFieldValue nth = mapToIntegerFieldValue(array[1]);
186189
if (array[0].isEmpty()) {
187190
throw new IllegalArgumentException("Time should be specified!");

src/test/java/com/cronutils/parser/CronParserTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public void testTrailingCommaListUnix(){
101101
validateExpression(CronType.UNIX, "1, * * * *");
102102
}
103103

104+
@Test(expected = IllegalArgumentException.class)
105+
public void testHashListUnix(){
106+
validateExpression(CronType.UNIX, "0 0 0 ? * #");
107+
}
108+
104109
@Test // issue #369
105110
public void testParseIncompleteRangeNoValues() {
106111
parseIncompleteExpression("-", "Missing values for range: -");
@@ -217,4 +222,10 @@ public void testParseMulticron(){
217222
Cron cron = parser.parse(multicron);
218223
assertEquals(multicron, cron.asString());
219224
}
225+
226+
@Test(expected = IllegalArgumentException.class)
227+
public void testParseQuartzCronWithHash() {
228+
parser = new CronParser(TestCronDefinitionsFactory.withDayOfYearDefinitionWhereYearAndDoYOptionals());
229+
parser.parse("0 0 0 ? * #");
230+
}
220231
}

0 commit comments

Comments
 (0)