This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push:
new 131a476 CAMEL-15214: camel-catalog - Validate should support the new
duration type, so it works again.
131a476 is described below
commit 131a476dce42469a30a7d109b6cdd1f80328eb47
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jun 19 07:29:28 2020 +0200
CAMEL-15214: camel-catalog - Validate should support the new duration type,
so it works again.
---
.../org/apache/camel/catalog/CamelCatalogTest.java | 12 +++++++
.../camel/catalog/PropertiesValidationResult.java | 18 +++++++++-
.../apache/camel/converter/DurationConverter.java | 2 +-
.../camel/catalog/impl/AbstractCamelCatalog.java | 41 ++++++++++++++++++++--
4 files changed, 68 insertions(+), 5 deletions(-)
diff --git
a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
index a71f635..b091f46 100644
---
a/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
+++
b/catalog/camel-catalog/src/test/java/org/apache/camel/catalog/CamelCatalogTest.java
@@ -1161,6 +1161,18 @@ public class CamelCatalogTest {
}
@Test
+ public void testValidateEndpointTimerDuration() throws Exception {
+ String uri = "timer:foo?period=5s";
+ EndpointValidationResult result =
catalog.validateEndpointProperties(uri);
+ assertTrue(result.isSuccess());
+
+ uri = "timer:foo?period=5p";
+ result = catalog.validateEndpointProperties(uri);
+ assertFalse(result.isSuccess());
+ assertEquals("5p", result.getInvalidDuration().get("period"));
+ }
+
+ @Test
public void testValidateEndpointHttpPropertyPlaceholder() throws Exception
{
String uri =
"http://api.openweathermap.org/data/2.5/weather?{{property.weatherUri}}";
EndpointValidationResult result =
catalog.validateEndpointProperties(uri);
diff --git
a/core/camel-api/src/main/java/org/apache/camel/catalog/PropertiesValidationResult.java
b/core/camel-api/src/main/java/org/apache/camel/catalog/PropertiesValidationResult.java
index a9b050e..2b3d53b 100644
---
a/core/camel-api/src/main/java/org/apache/camel/catalog/PropertiesValidationResult.java
+++
b/core/camel-api/src/main/java/org/apache/camel/catalog/PropertiesValidationResult.java
@@ -50,6 +50,7 @@ abstract class PropertiesValidationResult implements
Serializable {
Map<String, String> invalidBoolean;
Map<String, String> invalidInteger;
Map<String, String> invalidNumber;
+ Map<String, String> invalidDuration;
Map<String, String> defaultValues;
public boolean hasErrors() {
@@ -72,7 +73,8 @@ abstract class PropertiesValidationResult implements
Serializable {
boolean ok = syntaxError == null && unknown == null && required ==
null;
if (ok) {
ok = invalidEnum == null && invalidEnumChoices == null &&
invalidReference == null
- && invalidBoolean == null && invalidInteger == null &&
invalidNumber == null;
+ && invalidBoolean == null && invalidInteger == null &&
invalidNumber == null
+ && invalidDuration == null;
}
if (ok) {
ok = invalidMap == null && invalidArray == null;
@@ -215,6 +217,16 @@ abstract class PropertiesValidationResult implements
Serializable {
}
}
+ public void addInvalidDuration(String name, String value) {
+ if (invalidDuration == null) {
+ invalidDuration = new LinkedHashMap<>();
+ }
+ if (!invalidDuration.containsKey(name)) {
+ invalidDuration.put(name, value);
+ errors++;
+ }
+ }
+
public void addDefaultValue(String name, String value) {
if (defaultValues == null) {
defaultValues = new LinkedHashMap<>();
@@ -293,6 +305,10 @@ abstract class PropertiesValidationResult implements
Serializable {
return invalidNumber;
}
+ public Map<String, String> getInvalidDuration() {
+ return invalidDuration;
+ }
+
public Map<String, String> getDefaultValues() {
return defaultValues;
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/converter/DurationConverter.java
b/core/camel-base/src/main/java/org/apache/camel/converter/DurationConverter.java
index 0b2197f..492432b 100644
---
a/core/camel-base/src/main/java/org/apache/camel/converter/DurationConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/converter/DurationConverter.java
@@ -45,7 +45,7 @@ public final class DurationConverter {
@Converter
public static Duration toDuration(String source) {
- if (source.startsWith("P") || source.startsWith("-P")) {
+ if (source.startsWith("P") || source.startsWith("-P") ||
source.startsWith("p") || source.startsWith("-p")) {
return Duration.parse(source);
} else {
return Duration.ofMillis(TimeUtils.toMilliSeconds(source));
diff --git
a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
index e0d746f..3a57294 100644
---
a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
+++
b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -142,7 +143,7 @@ public abstract class AbstractCamelCatalog {
}
public boolean validateTimePattern(String pattern) {
- return validateInteger(pattern);
+ return validateDuration(pattern);
}
public EndpointValidationResult validateEndpointProperties(String uri) {
@@ -298,6 +299,15 @@ public abstract class AbstractCamelCatalog {
}
}
+ // is duration
+ if (!multiValue && !valuePlaceholder && !lookup &&
"duration".equals(row.getType())) {
+ // value must be convertable to a duration
+ boolean valid = validateDuration(value);
+ if (!valid) {
+ result.addInvalidDuration(name, value);
+ }
+ }
+
// is integer
if (!multiValue && !valuePlaceholder && !lookup &&
"integer".equals(row.getType())) {
// value must be an integer
@@ -1000,6 +1010,15 @@ public abstract class AbstractCamelCatalog {
}
}
+ // is duration
+ if (!optionPlaceholder && !lookup &&
"duration".equals(row.getType())) {
+ // value must be convertable to a duration
+ boolean valid = validateDuration(value);
+ if (!valid) {
+ result.addInvalidDuration(longKey, value);
+ }
+ }
+
// is integer
if (!optionPlaceholder && !lookup &&
"integer".equals(row.getType())) {
// value must be an integer
@@ -1273,10 +1292,26 @@ public abstract class AbstractCamelCatalog {
} catch (Exception e) {
// ignore
}
+ return valid;
+ }
+
+ private static boolean validateDuration(String value) {
+ boolean valid = false;
+ try {
+ Long.parseLong(value);
+ valid = true;
+ } catch (Exception e) {
+ // ignore
+ }
if (!valid) {
- // it may be a time pattern, such as 5s for 5 seconds = 5000
try {
- TimePatternConverter.toMilliSeconds(value);
+ if (value.startsWith("P") || value.startsWith("-P") ||
value.startsWith("p") || value.startsWith("-p")) {
+ // its a duration
+ Duration.parse(value);
+ } else {
+ // it may be a time pattern, such as 5s for 5 seconds =
5000
+ TimePatternConverter.toMilliSeconds(value);
+ }
valid = true;
} catch (Exception e) {
// ignore