File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
main/java/org/springframework/scheduling/support
test/java/org/springframework/scheduling/support Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 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.
@@ -202,6 +202,25 @@ public static CronExpression parse(String expression) {
202
202
}
203
203
}
204
204
205
+ /**
206
+ * Determine whether the given string represents a valid cron expression.
207
+ * @param expression the expression to evaluate
208
+ * @return {@code true} if the given expression is a valid cron expression
209
+ * @since 5.3.8
210
+ */
211
+ public static boolean isValidExpression (@ Nullable String expression ) {
212
+ if (expression == null ) {
213
+ return false ;
214
+ }
215
+ try {
216
+ parse (expression );
217
+ return true ;
218
+ }
219
+ catch (IllegalArgumentException ex ) {
220
+ return false ;
221
+ }
222
+ }
223
+
205
224
206
225
private static String resolveMacros (String expression ) {
207
226
expression = expression .trim ();
Original file line number Diff line number Diff line change @@ -51,6 +51,16 @@ public boolean matches(Temporal value) {
51
51
}
52
52
};
53
53
54
+ @ Test
55
+ public void isValidExpression () {
56
+ assertThat (CronExpression .isValidExpression (null )).isFalse ();
57
+ assertThat (CronExpression .isValidExpression ("" )).isFalse ();
58
+ assertThat (CronExpression .isValidExpression ("*" )).isFalse ();
59
+ assertThat (CronExpression .isValidExpression ("* * * * *" )).isFalse ();
60
+ assertThat (CronExpression .isValidExpression ("* * * * * * *" )).isFalse ();
61
+
62
+ assertThat (CronExpression .isValidExpression ("* * * * * *" )).isTrue ();
63
+ }
54
64
55
65
@ Test
56
66
void matchAll () {
You can’t perform that action at this time.
0 commit comments