Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix joor after switching to Java 11 #284

Merged
merged 2 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion camel-k-loader-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>joor-java-8</artifactId>
<artifactId>joor</artifactId>
<version>${joor.version}</version>
</dependency>

Expand Down
32 changes: 32 additions & 0 deletions camel-k-loader-java/src/test/resources/MyRoutesWithBeans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/
import org.apache.camel.BindToRegistry;
import org.apache.camel.builder.RouteBuilder;

public class MyRoutesWithBeans extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start")
.setBody().simple("${header[MyHeader]}")
.to("log:knative");
}

@BindToRegistry("my-bean")
public static String myBean() {
return "my-bean-string";
}
}
3 changes: 2 additions & 1 deletion camel-k-loader-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>0</forkCount>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
Expand Down
5 changes: 5 additions & 0 deletions camel-k-main/camel-k-runtime-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
<artifactId>camel-k-loader-kotlin</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
<artifactId>camel-k-runtime-knative</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@

import org.apache.camel.CamelContext;
import org.apache.camel.Route;
import org.apache.camel.component.knative.KnativeComponent;
import org.apache.camel.component.knative.spi.Knative;
import org.apache.camel.component.knative.spi.KnativeEnvironment;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.http.PlatformHttpServiceContextCustomizer;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.test.AvailablePortFinder;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.util.CollectionHelper;
import org.apache.camel.model.ToDefinition;
import org.apache.camel.util.ObjectHelper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.apache.camel.util.CollectionHelper.mapOf;
import static org.assertj.core.api.Assertions.assertThat;

public class RuntimeTest {
Expand Down Expand Up @@ -82,7 +88,7 @@ void testLoadRouteAndRest() throws Exception {

@Test
void testLoadRouteWithExpression() throws Exception {
runtime.setProperties(CollectionHelper.mapOf(
runtime.setProperties(mapOf(
"the.body", "10"
));

Expand All @@ -105,4 +111,30 @@ public void testLoadJavaSource() throws Exception {
});
}

@Test
public void testLoadJavaSourceWrap() throws Exception {
KnativeComponent component = new KnativeComponent();
component.setEnvironment(KnativeEnvironment.on(
KnativeEnvironment.endpoint(Knative.EndpointKind.sink, "sink", "localhost", AvailablePortFinder.getNextAvailable())
));

PlatformHttpServiceContextCustomizer phsc = new PlatformHttpServiceContextCustomizer();
phsc.setBindPort(AvailablePortFinder.getNextAvailable());
phsc.apply(runtime.getCamelContext());

runtime.getCamelContext().addComponent("knative", component);
runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java?interceptors=knative-source"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.run();

assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name");
});
assertThat(runtime.getCamelContext(ModelCamelContext.class).getRouteDefinition("my-route")).satisfies(definition -> {
assertThat(definition.getOutputs()).last().isInstanceOfSatisfying(ToDefinition.class, to -> {
assertThat(to.getEndpointUri()).isEqualTo("knative://endpoint/sink");
});
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class MyRoutesWithBeans extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start")
.routeId("my-route")
.bean("my-bean", "getName")
.to("log:info");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
import static org.apache.camel.util.CollectionHelper.mapOf;

public interface Runtime extends HasCamelContext {

default <T extends CamelContext> T getCamelContext(Class<T> type) {
return getCamelContext().adapt(type);
}

/**
* Returns the registry associated to this runtime.
*/
Expand Down
8 changes: 2 additions & 6 deletions camel-k-runtime-knative/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@
<artifactId>camel-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-undertow</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cloud</artifactId>
Expand All @@ -138,7 +133,8 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<forkCount>0</forkCount>
<forkCount>1</forkCount>
<resueForks>false</resueForks>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.stream.Stream;

import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.knative.KnativeComponent;
Expand All @@ -37,11 +36,9 @@
import org.apache.camel.k.Sources;
import org.apache.camel.k.http.PlatformHttpServiceContextCustomizer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.loader.java.JavaSourceLoader;
import org.apache.camel.k.test.AvailablePortFinder;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
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;
Expand Down Expand Up @@ -69,15 +66,14 @@ static Stream<Arguments> parameters() {
public void testWrapLoader(String uri) throws Exception {
LOGGER.info("uri: {}", uri);

final int port = AvailablePortFinder.getNextAvailable();
final String data = UUID.randomUUID().toString();
final TestRuntime runtime = new TestRuntime();

KnativeComponent component = new KnativeComponent();
component.setEnvironment(KnativeEnvironment.on(
KnativeEnvironment.endpoint(Knative.EndpointKind.sink, "sink", "localhost", port)
KnativeEnvironment.endpoint(Knative.EndpointKind.sink, "sink", "localhost", runtime.port)
));

TestRuntime runtime = new TestRuntime();

CamelContext context = runtime.getCamelContext();
context.disableJMX();
Expand All @@ -95,7 +91,7 @@ public void testWrapLoader(String uri) throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
fromF("undertow:http://localhost:%d", port)
fromF("platform-http:/")
.routeId("http")
.to("mock:result");
}
Expand Down Expand Up @@ -128,62 +124,25 @@ public void configure() throws Exception {
}
}

@Test
public void testWrapLoaderWithBeanRegistration() throws Exception {
final int port = AvailablePortFinder.getNextAvailable();

KnativeComponent component = new KnativeComponent();
component.setEnvironment(KnativeEnvironment.on(
KnativeEnvironment.endpoint(Knative.EndpointKind.sink, "sink", "localhost", port)
));

TestRuntime runtime = new TestRuntime();

CamelContext context = runtime.getCamelContext();
context.disableJMX();
context.setStreamCaching(true);
context.addComponent("knative", component);

Source source = Sources.fromURI("classpath:sources/routes.java?name=MyRoutes.java&interceptors=knative-source");
SourceLoader loader = RoutesConfigurer.load(runtime, source);

assertThat(loader.getSupportedLanguages()).contains(source.getLanguage());
assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);

try {
RoutesBuilder builder = runtime.builders.get(0);

context.addRoutes(builder);

context.adapt(ExtendedCamelContext.class)
.getBeanPostProcessor()
.postProcessBeforeInitialization(builder, builder.getClass().getName());
context.adapt(ExtendedCamelContext.class)
.getBeanPostProcessor()
.postProcessAfterInitialization(builder, builder.getClass().getName());

context.start();

assertThat(context.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(String.class, "my-bean-string"::equals);
} finally {
context.stop();
}
}

static class TestRuntime implements Runtime {
private final CamelContext camelContext;
private final List<RoutesBuilder> builders;
private final int port;

public TestRuntime() {
this.camelContext = new DefaultCamelContext();
this.builders = new ArrayList<>();
this.port = AvailablePortFinder.getNextAvailable();

PlatformHttpServiceContextCustomizer httpService = new PlatformHttpServiceContextCustomizer();
httpService.setBindPort(AvailablePortFinder.getNextAvailable());
httpService.setBindPort(this.port);
httpService.apply(this.camelContext);
}

public int getPort() {
return port;
}

@Override
public CamelContext getCamelContext() {
return this.camelContext;
Expand Down