You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/08/19 12:24:27 UTC
[camel-k-runtime] branch master updated: java: cleanup loader and
tests
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push:
new 33fcf5c java: cleanup loader and tests
33fcf5c is described below
commit 33fcf5c9e1b99f1ddb920d10fce2c54d8302081e
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Aug 19 13:54:28 2020 +0200
java: cleanup loader and tests
---
camel-k-loader-groovy/pom.xml | 23 ---
camel-k-loader-java/pom.xml | 18 ++
.../k/loader/java/JavaSourceLoaderTest.groovy | 98 +++++++++++
.../camel/k/loader/java/model/EmployeeDTO.groovy} | 35 +---
.../camel/k/loader/java/support/TestRuntime.groovy | 75 ++++++++
.../camel/k/loader/java/RoutesLoaderTest.java | 191 ---------------------
.../src/main/java/org/apache/camel/k/Runtime.java | 7 +-
tooling/camel-k-test/pom.xml | 16 ++
8 files changed, 220 insertions(+), 243 deletions(-)
diff --git a/camel-k-loader-groovy/pom.xml b/camel-k-loader-groovy/pom.xml
index 5fe3c55..41a1daa 100644
--- a/camel-k-loader-groovy/pom.xml
+++ b/camel-k-loader-groovy/pom.xml
@@ -76,12 +76,6 @@
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-test</artifactId>
<scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>*</artifactId>
- </exclusion>
- </exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
@@ -120,23 +114,6 @@
</dependency>
<dependency>
- <groupId>org.spockframework</groupId>
- <artifactId>spock-core</artifactId>
- <version>${spock.version}</version>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>*</artifactId>
- </exclusion>
- <exclusion>
- <groupId>junit</groupId>
- <artifactId>*</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
-
- <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${commons-dbcp2.version}</version>
diff --git a/camel-k-loader-java/pom.xml b/camel-k-loader-java/pom.xml
index c280a7c..fe2fda1 100644
--- a/camel-k-loader-java/pom.xml
+++ b/camel-k-loader-java/pom.xml
@@ -110,6 +110,24 @@
<build>
<plugins>
<plugin>
+ <groupId>org.codehaus.gmavenplus</groupId>
+ <artifactId>gmavenplus-plugin</artifactId>
+ <version>${gmavenplus-plugin.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>addSources</goal>
+ <goal>addTestSources</goal>
+ <goal>compile</goal>
+ <goal>compileTests</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <invokeDynamic>true</invokeDynamic>
+ </configuration>
+ </plugin>
+ <plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
diff --git a/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/JavaSourceLoaderTest.groovy b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/JavaSourceLoaderTest.groovy
new file mode 100644
index 0000000..2b868f8
--- /dev/null
+++ b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/JavaSourceLoaderTest.groovy
@@ -0,0 +1,98 @@
+/*
+ * 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.k.loader.java
+
+
+import org.apache.camel.k.loader.java.model.EmployeeDTO
+import org.apache.camel.k.loader.java.support.TestRuntime
+import org.apache.camel.model.ProcessDefinition
+import org.apache.camel.model.SetBodyDefinition
+import org.apache.camel.model.ToDefinition
+import spock.lang.AutoCleanup
+import spock.lang.Specification
+
+class JavaSourceLoaderTest extends Specification {
+ @AutoCleanup
+ def runtime = new TestRuntime()
+
+ def "load routes with nested class"() {
+ when:
+ runtime.loadRoutes("classpath:MyRoutesWithNestedClass.java")
+ then:
+ with(runtime.context.routeDefinitions) {
+ it.size() == 1
+
+ it[0].outputs[0] instanceof SetBodyDefinition
+ it[0].outputs[1] instanceof ProcessDefinition
+ it[0].outputs[2] instanceof ToDefinition
+
+ it[0].input.endpointUri == 'timer:tick'
+ }
+ }
+
+ def "load routes with nested type"() {
+ when:
+ runtime.loadRoutes("classpath:MyRoutesWithNestedTypes.java")
+ runtime.context.applicationContextClassLoader.loadClass('MyRoutesWithNestedTypes$MyModel')
+ then:
+ noExceptionThrown()
+ }
+
+ def "load routes with rest configuration"() {
+ when:
+ runtime.loadRoutes("classpath:MyRoutesWithRestConfiguration.java")
+ then:
+ runtime.context.restConfiguration.component == 'restlet'
+ }
+
+ def "load routes with model"() {
+ when:
+ runtime.loadRoutes("classpath:MyRoutesWithModel.java")
+ then:
+ runtime.context.restDefinitions.any {
+ it.verbs.first().outType == EmployeeDTO.class.name
+ }
+ }
+
+ def "load configuration"() {
+ when:
+ runtime.loadRoutes("classpath:MyRoutesConfig.java")
+ then:
+ runtime.configurations.size() == 1
+ }
+
+
+ def "load"(location) {
+ expect:
+ runtime.loadRoutes(location)
+
+ with(runtime.context.routeDefinitions) {
+ it[0].input.endpointUri ==~ /timer:.*tick/
+ it[0].outputs[0] instanceof ToDefinition
+ }
+ where:
+ location << [
+ "classpath:MyRoutes.java",
+ "classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java",
+ "classpath:MyRoutesWithPackage.java",
+ "classpath:MyRoutesWithPackageAndComment.java",
+ "classpath:MyRoutesWithPackageAndLineComment.java",
+ "classpath:MyRoutesWithEndpointDsl.java"
+ ]
+
+ }
+}
diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/model/EmployeeDTO.java b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/model/EmployeeDTO.groovy
similarity index 61%
rename from camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/model/EmployeeDTO.java
rename to camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/model/EmployeeDTO.groovy
index 65c4918..62b34d7 100644
--- a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/model/EmployeeDTO.java
+++ b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/model/EmployeeDTO.groovy
@@ -14,34 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.k.loader.java.model;
+package org.apache.camel.k.loader.java.model
-public class EmployeeDTO {
- public int id;
- public String name;
- public String org;
+import groovy.transform.CompileStatic
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getOrg() {
- return org;
- }
-
- public void setOrg(String org) {
- this.org = org;
- }
+@CompileStatic
+class EmployeeDTO {
+ int id
+ String name
+ String org
}
diff --git a/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/support/TestRuntime.groovy b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/support/TestRuntime.groovy
new file mode 100644
index 0000000..21f16e1
--- /dev/null
+++ b/camel-k-loader-java/src/test/groovy/org/apache/camel/k/loader/java/support/TestRuntime.groovy
@@ -0,0 +1,75 @@
+/*
+ * 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.k.loader.java.support
+
+import org.apache.camel.CamelContext
+import org.apache.camel.RoutesBuilder
+import org.apache.camel.impl.DefaultCamelContext
+import org.apache.camel.k.CompositeClassloader
+import org.apache.camel.k.Runtime
+import org.apache.camel.model.ModelCamelContext
+
+import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes
+
+class TestRuntime implements Runtime, AutoCloseable {
+ final ModelCamelContext context
+ final List<RoutesBuilder> builders
+ final List<Object> configurations
+
+ TestRuntime() {
+ this.context = new DefaultCamelContext()
+ this.context.setApplicationContextClassLoader(new CompositeClassloader())
+ this.builders = []
+ this.configurations = []
+ }
+
+ @Override
+ CamelContext getCamelContext() {
+ return this.context
+ }
+
+ @Override
+ void addRoutes(RoutesBuilder builder) {
+ this.builders << builder
+ this.context.addRoutes(builder)
+ }
+
+ @Override
+ void addConfiguration(Object configuration) {
+ this.configurations.add(configuration)
+ }
+
+ void loadRoutes(String... routes) {
+ routes.each {
+ forRoutes(it).accept(Phase.ConfigureRoutes, this)
+ }
+ }
+
+ void start() {
+ context.start()
+ }
+
+ @Override
+ void stop() {
+ context.stop()
+ }
+
+ @Override
+ void close() {
+ stop()
+ }
+}
diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java
deleted file mode 100644
index 2519bab..0000000
--- a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java
+++ /dev/null
@@ -1,191 +0,0 @@
-/*
- * 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.k.loader.java;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.stream.Stream;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.RoutesBuilder;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.k.CompositeClassloader;
-import org.apache.camel.k.Runtime;
-import org.apache.camel.k.Source;
-import org.apache.camel.k.SourceLoader;
-import org.apache.camel.k.Sources;
-import org.apache.camel.k.listener.RoutesConfigurer;
-import org.apache.camel.model.ProcessDefinition;
-import org.apache.camel.model.RouteDefinition;
-import org.apache.camel.model.SetBodyDefinition;
-import org.apache.camel.model.ToDefinition;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RoutesLoaderTest {
- @Test
- public void testLoadJavaWithNestedClass() throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI("classpath:MyRoutesWithNestedClass.java");
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(JavaSourceLoader.class);
- assertThat(runtime.builders).hasSize(1);
- assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);
-
- RouteBuilder builder = (RouteBuilder)runtime.builders.get(0);
- builder.setContext(runtime.getCamelContext());
- builder.configure();
-
- List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
- assertThat(routes).hasSize(1);
- assertThat(routes.get(0).getInput().getEndpointUri()).isEqualTo("timer:tick");
- assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(SetBodyDefinition.class);
- assertThat(routes.get(0).getOutputs().get(1)).isInstanceOf(ProcessDefinition.class);
- assertThat(routes.get(0).getOutputs().get(2)).isInstanceOf(ToDefinition.class);
- }
-
- @Test
- public void testLoadJavaWithRestConfiguration() throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI("classpath:MyRoutesWithRestConfiguration.java");
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(JavaSourceLoader.class);
- assertThat(runtime.builders).hasSize(1);
- assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);
-
- runtime.getCamelContext().addRoutes(runtime.builders.get(0));
-
- assertThat(runtime.getCamelContext().getRestConfiguration()).hasFieldOrPropertyWithValue("component", "restlet");
- }
-
- @Test
- public void testLoadJavaConfiguration() throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI("classpath:MyRoutesConfig.java");
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(JavaSourceLoader.class);
- assertThat(runtime.builders).isEmpty();
- assertThat(runtime.configurations).hasSize(1);
- }
-
- @Test
- public void testLoadJavaWithModel() throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI("classpath:MyRoutesWithModel.java");
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(JavaSourceLoader.class);
- assertThat(runtime.builders).hasSize(1);
- assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);
-
- runtime.getCamelContext().addRoutes(runtime.builders.get(0));
-
- assertThat(runtime.camelContext.getRestDefinitions()).first().satisfies(definition -> {
- assertThat(definition.getVerbs()).first().satisfies(verb -> {
- assertThat(verb).hasFieldOrPropertyWithValue("outType", "org.apache.camel.k.loader.java.model.EmployeeDTO");
- });
- });
- }
-
- @Test
- public void testLoadJavaWithNestedType() throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI("classpath:MyRoutesWithNestedTypes.java");
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(JavaSourceLoader.class);
- assertThat(runtime.builders).hasSize(1);
- assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);
-
- runtime.getCamelContext().addRoutes(runtime.builders.get(0));
- runtime.getCamelContext().getApplicationContextClassLoader().loadClass("MyRoutesWithNestedTypes$MyModel");
- }
-
- @ParameterizedTest
- @MethodSource("parameters")
- public void testLoaders(String location, Class<? extends SourceLoader> type) throws Exception {
- TestRuntime runtime = new TestRuntime();
- Source source = Sources.fromURI(location);
- SourceLoader loader = RoutesConfigurer.load(runtime, source);
-
- assertThat(loader).isInstanceOf(type);
- assertThat(runtime.builders).hasSize(1);
- assertThat(runtime.builders).first().isInstanceOf(RouteBuilder.class);
-
- RouteBuilder builder = (RouteBuilder)runtime.builders.get(0);
- builder.setContext(runtime.getCamelContext());
- builder.configure();
-
- List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
- assertThat(routes).hasSize(1);
- assertThat(routes.get(0).getInput().getEndpointUri()).matches("timer:/*tick");
- assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
- }
-
- static Stream<Arguments> parameters() {
- return Stream.of(
- Arguments.arguments("classpath:MyRoutes.java", JavaSourceLoader.class),
- Arguments.arguments("classpath:MyRoutesWithNameOverride.java?name=MyRoutes.java", JavaSourceLoader.class),
- Arguments.arguments("classpath:MyRoutesWithPackage.java", JavaSourceLoader.class),
- Arguments.arguments("classpath:MyRoutesWithPackageAndComment.java", JavaSourceLoader.class),
- Arguments.arguments("classpath:MyRoutesWithPackageAndLineComment.java", JavaSourceLoader.class),
- Arguments.arguments("classpath:MyRoutesWithEndpointDsl.java", JavaSourceLoader.class)
- );
- }
-
- static class TestRuntime implements Runtime {
- private final DefaultCamelContext camelContext;
- private final List<RoutesBuilder> builders;
- private final List<Object> configurations;
-
- public TestRuntime() {
- this.camelContext = new DefaultCamelContext();
- this.camelContext.setApplicationContextClassLoader(new CompositeClassloader());
- this.builders = new ArrayList<>();
- this.configurations = new ArrayList<>();
- }
-
- @Override
- public CamelContext getCamelContext() {
- return this.camelContext;
- }
-
- @Override
- public void addRoutes(RoutesBuilder builder) {
- this.builders.add(builder);
- }
-
- @Override
- public void addConfiguration(Object configuration) {
- this.configurations.add(configuration);
- }
-
- @Override
- public void setPropertiesLocations(Collection<String> locations) {
- }
- }
-}
-
diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java
index 980f9e0..d0979a0 100644
--- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java
@@ -32,7 +32,7 @@ import org.apache.camel.spi.Registry;
import static org.apache.camel.util.CollectionHelper.mapOf;
-public interface Runtime extends HasCamelContext {
+public interface Runtime extends HasCamelContext, AutoCloseable {
default <T extends CamelContext> T getCamelContext(Class<T> type) {
return getCamelContext().adapt(type);
@@ -113,6 +113,11 @@ public interface Runtime extends HasCamelContext {
getCamelContext().stop();
}
+ @Override
+ default void close() throws Exception {
+ stop();
+ }
+
enum Phase {
Starting,
ConfigureProperties,
diff --git a/tooling/camel-k-test/pom.xml b/tooling/camel-k-test/pom.xml
index 364c095..2ddb5fa 100644
--- a/tooling/camel-k-test/pom.xml
+++ b/tooling/camel-k-test/pom.xml
@@ -60,6 +60,22 @@
</dependency>
<dependency>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-core</artifactId>
+ <version>${spock.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>junit</groupId>
+ <artifactId>*</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>