Skip to content

Commit

Permalink
Fixed classpath and no prefix resources that were ignored in dev mode a…
Browse files Browse the repository at this point in the history
  • Loading branch information
aldettinger committed Apr 26, 2021
1 parent c24998c commit c6393c8
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class CamelMainHelper {
private CamelMainHelper() {
}

public static Stream<String> routesIncludePatter() {
public static Stream<String> routesIncludePattern() {
final String[] i1 = CamelSupport.getOptionalConfigValue(
"camel.main.routes-include-pattern", String[].class, EMPTY_STRING_ARRAY);
final String[] i2 = CamelSupport.getOptionalConfigValue(
Expand All @@ -37,7 +37,7 @@ public static Stream<String> routesIncludePatter() {
: Stream.concat(Stream.of(i1), Stream.of(i2)).filter(location -> !"false".equals(location));
}

public static Stream<String> routesExcludePatter() {
public static Stream<String> routesExcludePattern() {
final String[] i1 = CamelSupport.getOptionalConfigValue(
"camel.main.routes-exclude-pattern", String[].class, EMPTY_STRING_ARRAY);
final String[] i2 = CamelSupport.getOptionalConfigValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@
class CamelMainHotDeploymentProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelMainHotDeploymentProcessor.class);
private static final String FILE_PREFIX = "file:";
private static final String CLASSPATH_PREFIX = "classpath:";

@BuildStep(onlyIf = CamelMainEnabled.class)
List<HotDeploymentWatchedFileBuildItem> locations() {
List<HotDeploymentWatchedFileBuildItem> items = CamelMainHelper.routesIncludePatter()
.filter(location -> location.startsWith(FILE_PREFIX))
.map(location -> location.substring(FILE_PREFIX.length()))
List<HotDeploymentWatchedFileBuildItem> items = CamelMainHelper.routesIncludePattern()
.map(CamelMainHotDeploymentProcessor::routesIncludePatternToLocation)
.filter(location -> location != null)
.distinct()
.map(Paths::get)
.filter(Files::exists)
.map(Path::toAbsolutePath)
.map(Path::toString)
.map(HotDeploymentWatchedFileBuildItem::new)
.collect(Collectors.toList());

Expand All @@ -54,4 +51,19 @@ List<HotDeploymentWatchedFileBuildItem> locations() {

return items;
}

private static String routesIncludePatternToLocation(String pattern) {
if (pattern.startsWith(CLASSPATH_PREFIX)) {
return pattern.substring(CLASSPATH_PREFIX.length());
} else if (pattern.startsWith(FILE_PREFIX)) {
String filePattern = pattern.substring(FILE_PREFIX.length());
Path filePatternPath = Paths.get(filePattern);
if (Files.exists(filePatternPath)) {
return filePatternPath.toAbsolutePath().toString();
}
} else if (pattern.length() > 0) {
return pattern;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void camelNativeImageResources(
Capabilities capabilities,
BuildProducer<NativeImageResourceBuildItem> nativeResource) {

for (String path : CamelMainHelper.routesIncludePatter().collect(Collectors.toList())) {
for (String path : CamelMainHelper.routesIncludePattern().collect(Collectors.toList())) {
String scheme = ResourceHelper.getScheme(path);

// Null scheme is equivalent to classpath scheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

public class CamelDevModeTest {
public class CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest {
static Path BASE;

@RegisterExtension
Expand Down Expand Up @@ -87,7 +87,7 @@ public static Asset applicationProperties() {

public static void copy(String source, String target) throws IOException {
Files.copy(
CamelDevModeTest.class.getResourceAsStream("/" + source),
CamelMainRoutesIncludePatternWithAbsoluteFilePrefixDevModeTest.class.getResourceAsStream("/" + source),
BASE.resolve(target),
StandardCopyOption.REPLACE_EXISTING);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.quarkus.main;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

public class CamelMainRoutesIncludePatternWithClasspathPrefixDevModeTest {

@RegisterExtension
static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(CamelSupportResource.class)
.addAsResource(initialRoutesXml(), "routes.xml")
.addAsResource(applicationProperties(), "application.properties"));

public static Asset initialRoutesXml() {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><routes xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns=\"http://camel.apache.org/schema/spring\" xsi:schemaLocation=\"http://camel.apache.org/schema/spring "
+ "http://camel.apache.org/schema/spring/camel-spring.xsd\"><route id=\"r1-classpath-prefix\">"
+ "<from uri=\"direct:start\"/>"
+ "<to uri=\"direct:end\"/></route></routes>";

return new StringAsset(xml);
}

public static Asset applicationProperties() {
Writer writer = new StringWriter();

Properties props = new Properties();
props.setProperty("quarkus.banner.enabled", "false");
props.setProperty("camel.main.routes-include-pattern", "classpath:routes.xml");

try {
props.store(writer, "");
} catch (IOException e) {
throw new RuntimeException(e);
}

return new StringAsset(writer.toString());
}

@Test
public void testRoutesDiscovery() {
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
Response res = RestAssured.when().get("/test/describe").thenReturn();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().jsonPath().getList("routes", String.class)).containsOnly("r1-classpath-prefix");
});

TEST.modifyResourceFile("routes.xml", xml -> xml.replaceAll("r1", "r2"));

await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
Response res = RestAssured.when().get("/test/describe").thenReturn();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().jsonPath().getList("routes", String.class)).containsOnly("r2-classpath-prefix");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.quarkus.main;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

public class CamelMainRoutesIncludePatternWithNoPrefixDevModeTest {

@RegisterExtension
static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(CamelSupportResource.class)
.addAsResource(initialRoutesXml(), "routes/routes.xml")
.addAsResource(applicationProperties(), "application.properties"));

public static Asset initialRoutesXml() {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><routes xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns=\"http://camel.apache.org/schema/spring\" xsi:schemaLocation=\"http://camel.apache.org/schema/spring "
+ "http://camel.apache.org/schema/spring/camel-spring.xsd\"><route id=\"r1-no-prefix\">"
+ "<from uri=\"direct:start\"/>"
+ "<to uri=\"direct:end\"/></route></routes>";

return new StringAsset(xml);
}

public static Asset applicationProperties() {
Writer writer = new StringWriter();

Properties props = new Properties();
props.setProperty("quarkus.banner.enabled", "false");
props.setProperty("camel.main.routes-include-pattern", "routes/routes.xml");

try {
props.store(writer, "");
} catch (IOException e) {
throw new RuntimeException(e);
}

return new StringAsset(writer.toString());
}

@Test
public void testRoutesDiscovery() {
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
Response res = RestAssured.when().get("/test/describe").thenReturn();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().jsonPath().getList("routes", String.class)).containsOnly("r1-no-prefix");
});

TEST.modifyResourceFile("routes/routes.xml", xml -> xml.replaceAll("r1", "r2"));

await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
Response res = RestAssured.when().get("/test/describe").thenReturn();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().jsonPath().getList("routes", String.class)).containsOnly("r2-no-prefix");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ camel.rest.component = platform-http
#
# Main
#
camel.main.routes-include-pattern = classpath:routes/my-routes.xml,classpath:rests/my-rests.xml,classpath:templates/my-templates.xml
camel.main.routes-include-pattern = routes/my-routes.xml,classpath:rests/my-rests.xml,classpath:templates/my-templates.xml

0 comments on commit c6393c8

Please sign in to comment.