Skip to content

Commit

Permalink
CamelTestSupport style of testing apache#3511
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Jun 13, 2022
1 parent 4c86d42 commit beac8e4
Show file tree
Hide file tree
Showing 39 changed files with 2,306 additions and 87 deletions.
8 changes: 8 additions & 0 deletions extensions-core/core/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus.arc</groupId>
<artifactId>arc-processor</artifactId>
</dependency>

<!-- camel -->
<dependency>
Expand All @@ -59,6 +63,10 @@
<groupId>org.jboss.spec.javax.xml.bind</groupId>
<artifactId>jboss-jaxb-api_2.3_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support-jvm</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.core.deployment;

import javax.inject.Inject;

import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.TestAnnotationBuildItem;
import io.quarkus.test.junit.TestProfile;
import org.apache.camel.Produce;
import org.apache.camel.quarkus.test.CamelQuarkusTest;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CamelTestProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelTestProcessor.class);

@BuildStep
void produceTestAnnotationBuildItem(BuildProducer<TestAnnotationBuildItem> testAnnotationBuildItems) {
testAnnotationBuildItems.produce(new TestAnnotationBuildItem(CamelQuarkusTest.class.getName()));
}

@BuildStep
void annotationTransformerBuildItem(BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformerBuildItems) {
annotationsTransformerBuildItems.produce(removeProduces(null));
annotationsTransformerBuildItems.produce(createAnnotationTransformer(null));
}

private AnnotationsTransformerBuildItem createAnnotationTransformer(DotName className) {
return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
return kind == AnnotationTarget.Kind.CLASS;
}

public void transform(TransformationContext context) {

if (context.getAnnotations().contains(
AnnotationInstance.create(
DotName.createSimple(CamelQuarkusTest.class.getName()),
context.getTarget(),
new AnnotationValue[] {}))) {
context.transform().add(AnnotationInstance.create(
DotName.createSimple(TestProfile.class.getName()),
context.getTarget(),
new AnnotationValue[] { AnnotationValue.createClassValue("value",
Type.create(context.getTarget().asClass().name(), Type.Kind.CLASS)) }))
.done();
}
}
});
}

private AnnotationsTransformerBuildItem removeProduces(DotName className) {
return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
public boolean appliesTo(org.jboss.jandex.AnnotationTarget.Kind kind) {
return kind == AnnotationTarget.Kind.FIELD;
}

public void transform(TransformationContext context) {
if (context.getTarget().asField().name().equals("template1")) {
AnnotationInstance ai = AnnotationInstance.create(
DotName.createSimple(Produce.class.getName()),
null,
new AnnotationValue[] { AnnotationValue.createStringValue("value", "direct:start") });
context.transform().remove(p -> p.name().toString().contains(Produce.class.getName())).done();
context.transform().add(AnnotationInstance.create(DotName.createSimple(Inject.class.getName()), //todo valiue direxct:start
context.getTarget(), new AnnotationValue[] {})).done();
}
}
});
}

// @BuildStep
// void doSomethingWithNamedBeans(SynthesisFinishedBuildItem synthesisFinished) {
// List<BeanInfo> namedBeans = synthesisFinished.beanStream().withName().collect(Collectors.toList());
// System.out.println(namedBeans);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.camel.quarkus.core.CamelRecorder;
import org.apache.camel.quarkus.core.InjectionPointsRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem;
import org.apache.camel.quarkus.test.CamelQuarkusTest;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
Expand Down Expand Up @@ -226,12 +227,16 @@ void syntheticBeans(
BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions) {

Set<String> alreadyCreated = new HashSet<>();

for (AnnotationInstance annot : index.getIndex().getAnnotations(ENDPOINT_INJECT_ANNOTATION)) {
final AnnotationTarget target = annot.target();
switch (target.kind()) {
case FIELD: {
final FieldInfo field = target.asField();
endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name());
if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass())) {
endpointInjectBeans(recorder, syntheticBeans, index.getIndex(), annot, field.type().name());
}
break;
}
case METHOD: {
Expand All @@ -251,8 +256,10 @@ void syntheticBeans(
switch (target.kind()) {
case FIELD: {
final FieldInfo field = target.asField();
produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable,
index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name());
if (!excludeTestSyntheticBeanDuplicities(annot, alreadyCreated, field.declaringClass())) {
produceBeans(recorder, capabilities, syntheticBeans, proxyDefinitions, beanCapabilityAvailable,
index.getIndex(), annot, field.type().name(), field.name(), field.declaringClass().name());
}
break;
}
case METHOD: {
Expand All @@ -266,6 +273,17 @@ void syntheticBeans(
}
}

private boolean excludeTestSyntheticBeanDuplicities(AnnotationInstance annot, Set<String> alreadyCreated,
ClassInfo declaringClass) {
if (declaringClass.annotations().containsKey(DotName.createSimple(CamelQuarkusTest.class.getName()))) {
if (!alreadyCreated.contains(annot.toString())) {
alreadyCreated.add(annot.toString());
return false;
}
}
return true;
}

void produceBeans(CamelRecorder recorder, List<CapabilityBuildItem> capabilities,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions,
Expand Down
7 changes: 7 additions & 0 deletions integration-tests-jvm/stub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support-jvm</artifactId>
<!-- <version>2.10.0-SNAPSHOT</version>-->
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
1 change: 1 addition & 0 deletions integration-tests-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<module>mongodb</module>
<module>process-executor-support</module>
<module>test-support</module>
<module>test-support-jvm</module>
<module>mock-backend</module>
<module>wiremock</module>
<module>xslt-support</module>
Expand Down
62 changes: 62 additions & 0 deletions integration-tests-support/test-support-jvm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-tests-support</artifactId>
<version>2.10.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>camel-quarkus-integration-test-support-jvm</artifactId>
<name>Camel Quarkus :: Integration Tests :: Support</name>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-junit5</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.apache.camel.quarkus.test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;

import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.engine.execution.ExtensionValuesStore;
import org.junit.jupiter.engine.execution.NamespaceAwareStore;

public class BeforeEachCallback implements QuarkusTestBeforeEachCallback {

@Override
public void beforeEach(QuarkusTestMethodContext context) {
CamelQuarkusTestSupport testInstance = (CamelQuarkusTestSupport) context.getTestInstance();

testInstance.setCurrentTestName(getDisplayName(context.getTestMethod()));
testInstance.setGlobalStore(new NamespaceAwareStore(new ExtensionValuesStore(null), ExtensionContext.Namespace.GLOBAL));
}

private String getDisplayName(Method method) {
StringBuilder sb = new StringBuilder(method.getName());
sb.append("(");
sb.append(Arrays.stream(method.getParameterTypes()).map(c -> c.getSimpleName()).collect(Collectors.joining(", ")));
sb.append(")");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.camel.quarkus.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import io.quarkus.test.junit.QuarkusTest;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@QuarkusTest
public @interface CamelQuarkusTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.apache.camel.quarkus.test;

import io.quarkus.test.junit.QuarkusTestProfile;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.extension.ExtensionContext;

public class CamelQuarkusTestSupport extends CamelTestSupport
implements QuarkusTestProfile {

@Override
public void beforeAll(ExtensionContext context) {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

@Override
public void afterAll(ExtensionContext context) {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
//in camel-quarkus, junit5 uses different classloader, necessaryu code was moved into quarkus's callback
}

void setCurrentTestName(String currentTestName) {
this.currentTestName = currentTestName;
}

void setGlobalStore(ExtensionContext.Store store) {
this.globalStore = store;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.camel.quarkus.test.BeforeEachCallback
Loading

0 comments on commit beac8e4

Please sign in to comment.