-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from ligangty/quarkus
Add quarkus based support
- Loading branch information
Showing
12 changed files
with
540 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver) | ||
Licensed 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.commonjava.util</groupId> | ||
<artifactId>http-testserver-parent</artifactId> | ||
<version>2.1.1-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>http-testserver-quarkus</artifactId> | ||
<properties> | ||
<quarkus.version>2.16.11.Final</quarkus.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-bom</artifactId> | ||
<version>${quarkus.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.commonjava.util</groupId> | ||
<artifactId>http-testserver-core</artifactId> | ||
<version>2.1.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.9.2</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-arc-deployment</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-mutiny</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest</artifactId> | ||
<version>2.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<version>${quarkus.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
<goal>generate-code</goal> | ||
<goal>generate-code-tests</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
53 changes: 53 additions & 0 deletions
53
quarkus/src/main/java/org/commonjava/test/http/quarkus/InjectExpected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver) | ||
* | ||
* Licensed 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.commonjava.test.http.quarkus; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* We use this new annotation to let the {@link org.commonjava.test.http.expect.ExpectationServer} have ability to be work like the | ||
* old junit Rule based way under Quarkus Based Test. | ||
* Note: This annotation should work with @QuarkusTest under a Quarkus based project, like below: <br /> | ||
* | ||
* <pre> | ||
* {@literal @}QuarkusTest | ||
* public class TestClass { | ||
* {@literal @}InjectExpected(base="base") | ||
* ExpectationServer server; | ||
* ...... | ||
* } | ||
* </pre> | ||
*/ | ||
@Target( ElementType.FIELD) | ||
@Retention( RetentionPolicy.RUNTIME) | ||
public @interface InjectExpected | ||
{ | ||
/** | ||
* The base path of the server | ||
* | ||
* @return | ||
*/ | ||
String base() default ""; | ||
|
||
/** | ||
* The default port of the server. Note that if the port is occupied, the server will randomly find a new port to start | ||
* @return | ||
*/ | ||
int port() default 0; | ||
} |
39 changes: 39 additions & 0 deletions
39
quarkus/src/main/java/org/commonjava/test/http/quarkus/InjectStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver) | ||
* | ||
* Licensed 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.commonjava.test.http.quarkus; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* We use this new annotation to let the {@link org.commonjava.test.http.stream.StreamServer} have ability to be work like the | ||
* old junit Rule based way under Quarkus Based Test. | ||
* Note: This annotation should work with @QuarkusTest under a Quarkus based project | ||
*/ | ||
@Target( ElementType.FIELD ) | ||
@Retention( RetentionPolicy.RUNTIME ) | ||
public @interface InjectStream | ||
{ | ||
/** | ||
* The base path of the server | ||
* | ||
* @return | ||
*/ | ||
String base() default ""; | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...us/src/main/java/org/commonjava/test/http/quarkus/internal/ExpectationServerCallBack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver) | ||
* | ||
* Licensed 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.commonjava.test.http.quarkus.internal; | ||
|
||
import io.quarkus.test.junit.callback.QuarkusTestAfterConstructCallback; | ||
import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback; | ||
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback; | ||
import io.quarkus.test.junit.callback.QuarkusTestMethodContext; | ||
import org.commonjava.test.http.expect.ExpectationServer; | ||
import org.commonjava.test.http.quarkus.InjectExpected; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class ExpectationServerCallBack | ||
implements QuarkusTestAfterConstructCallback, QuarkusTestAfterEachCallback, QuarkusTestBeforeEachCallback | ||
{ | ||
private final Logger logger = LoggerFactory.getLogger( this.getClass() ); | ||
|
||
private static final Map<Object, ExpectationServer> injectServers = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void afterConstruct( Object testInstance ) | ||
{ | ||
Class<?> current = testInstance.getClass(); | ||
while ( current.getSuperclass() != null ) | ||
{ | ||
for ( Field field : current.getDeclaredFields() ) | ||
{ | ||
InjectExpected injectMockAnnotation = field.getAnnotation( InjectExpected.class ); | ||
if ( injectMockAnnotation != null && field.getType().equals( ExpectationServer.class ) ) | ||
{ | ||
final String baseResource = injectMockAnnotation.base(); | ||
final int port = injectMockAnnotation.port(); | ||
logger.trace( "Found field with @InjectExpected annotation, base resource is {}, port is {}", | ||
baseResource, port ); | ||
|
||
ExpectationServer server = new ExpectationServer( baseResource, port ); | ||
try | ||
{ | ||
logger.trace( "Injecting the field {} with server instance", field.getName() ); | ||
field.setAccessible( true ); | ||
field.set( testInstance, server ); | ||
injectServers.put( testInstance, server ); | ||
} | ||
catch ( IllegalAccessException e ) | ||
{ | ||
throw new RuntimeException( e ); | ||
} | ||
return; | ||
} | ||
} | ||
current = current.getSuperclass(); | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeEach( QuarkusTestMethodContext context ) | ||
{ | ||
ExpectationServer server = injectServers.get( context.getTestInstance() ); | ||
if ( server != null ) | ||
{ | ||
server.start(); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterEach( QuarkusTestMethodContext context ) | ||
{ | ||
ExpectationServer server = injectServers.get( context.getTestInstance() ); | ||
if ( server != null ) | ||
{ | ||
server.stop(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.