Skip to content

Commit

Permalink
Merge pull request #32 from ligangty/quarkus
Browse files Browse the repository at this point in the history
Add quarkus based support
  • Loading branch information
ligangty authored Sep 26, 2023
2 parents 0189d59 + 4c7c06c commit e941a39
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 2 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a test fixture, which provides a very basic servlet that registers expec

Usage is pretty simple:

####For junit 4 Rule based
#### For junit 4 Rule based

@Rule
public ExpectationServerRule serverRule = new ExpectationServerRule( "repos" );
Expand All @@ -23,7 +23,7 @@ Usage is pretty simple:
}


####For junit 5 Extension Based:
#### For junit 5 Extension Based:

@ExtendWith(ExpectationServerExtension.class)
public class ExpectaionTest{
Expand Down Expand Up @@ -62,4 +62,29 @@ or:
// Do any assertions....
.......
}
}

#### Quarkus Based Test

There are some limitations to let junit5 @ExtendWith work together with @QuarkusTest, see https://github.com/quarkusio/quarkus/issues/24911#issuecomment-1098935690
So to make it work, here brings the new annotation to make it work.


@QuarkusTest
public class ExpectaionTest{

@InjectExpected("repos")
ExpectationServer server;
@Test
public void run()
throws Exception
{
final String pathParts = "/repos/pathParts/to/something.txt";
final String content = "this is the content";
final String url = server.formatUrl( pathParts );
server.expect( url, 200, content );
// Do any assertions....
.......
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<module>core</module>
<module>junit4</module>
<module>junit5</module>
<module>quarkus</module>
</modules>

</project>
98 changes: 98 additions & 0 deletions quarkus/pom.xml
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>
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;
}
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 "";

}
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();
}
}

}
Loading

0 comments on commit e941a39

Please sign in to comment.