This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new 8563442 Fix problem with early property translation in tests
8563442 is described below
commit 856344252cb6997e15205b7cff1b2f3f9afea074
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Mar 22 09:35:13 2021 +0100
Fix problem with early property translation in tests
---
.../test/spring/junit5/CamelSpringTestSupport.java | 35 ++++++++++-----
.../test/spring/CamelSpringTestSupportTest.java | 50 ++++++++++++++++++++++
2 files changed, 75 insertions(+), 10 deletions(-)
diff --git
a/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestSupport.java
b/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestSupport.java
index a50ddf2..be6d235 100644
---
a/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestSupport.java
+++
b/components/camel-test/camel-test-spring-junit5/src/main/java/org/apache/camel/test/spring/junit5/CamelSpringTestSupport.java
@@ -27,6 +27,9 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
import org.apache.camel.CamelContext;
import org.apache.camel.component.properties.DefaultPropertiesParser;
@@ -297,18 +300,30 @@ public abstract class CamelSpringTestSupport extends
CamelTestSupport {
@Override
public InputStream getInputStream() throws IOException {
- StringWriter sw = new StringWriter();
- try (InputStreamReader r = new
InputStreamReader(delegate.getInputStream(), StandardCharsets.UTF_8)) {
- char[] buf = new char[32768];
- int l;
- while ((l = r.read(buf)) > 0) {
- sw.write(buf, 0, l);
+ if (properties.size() > 0) {
+ StringWriter sw = new StringWriter();
+ try (InputStreamReader r = new
InputStreamReader(delegate.getInputStream(), StandardCharsets.UTF_8)) {
+ char[] buf = new char[32768];
+ int l;
+ while ((l = r.read(buf)) > 0) {
+ sw.write(buf, 0, l);
+ }
+ }
+ PropertiesParser parser = new DefaultPropertiesParser();
+ String before = sw.toString();
+ String p = properties.keySet().stream().map(Pattern::quote)
+ .collect(Collectors.joining("|", Pattern.quote("{{") +
"(", ")" + Pattern.quote("}}")));
+ Matcher m = Pattern.compile(p).matcher(before);
+ StringBuffer sb = new StringBuffer(before.length());
+ while (m.find()) {
+ m.appendReplacement(sb, properties.get(m.group(1)));
}
+ m.appendTail(sb);
+ String after = sb.toString();
+ return new
ByteArrayInputStream(after.getBytes(StandardCharsets.UTF_8));
+ } else {
+ return delegate.getInputStream();
}
- PropertiesParser parser = new DefaultPropertiesParser();
- String before = sw.toString();
- String after = parser.parseUri(before, properties::get, false,
true);
- return new
ByteArrayInputStream(after.getBytes(StandardCharsets.UTF_8));
}
}
}
diff --git
a/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/spring/CamelSpringTestSupportTest.java
b/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/spring/CamelSpringTestSupportTest.java
new file mode 100644
index 0000000..128eb30
--- /dev/null
+++
b/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/spring/CamelSpringTestSupportTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.spring;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.test.spring.junit5.CamelSpringTestSupport;
+import org.junit.jupiter.api.Test;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.Resource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CamelSpringTestSupportTest {
+
+ @Test
+ public void testReplacement() throws IOException {
+ String input = "<camel id='{{myCamelContext}}'>\n" +
+ " <bean class='{{fooClass}}'/>\n" +
+ "</camel>\n";
+ Resource io = new
ByteArrayResource(input.getBytes(StandardCharsets.UTF_8));
+ Map<String, String> props = new HashMap<>();
+ props.put("myCamelContext", "camel-context-id");
+ Resource tr = new CamelSpringTestSupport.TranslatedResource(io, props);
+ byte[] buf = new byte[1024];
+ int l = tr.getInputStream().read(buf);
+ String output = new String(buf, 0, l, StandardCharsets.UTF_8);
+ assertEquals("<camel id='camel-context-id'>\n" +
+ " <bean class='{{fooClass}}'/>\n" +
+ "</camel>\n",
+ output);
+ }
+}