Skip to content

Commit e941a39

Browse files
authored
Merge pull request #32 from ligangty/quarkus
Add quarkus based support
2 parents 0189d59 + 4c7c06c commit e941a39

File tree

12 files changed

+540
-2
lines changed

12 files changed

+540
-2
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is a test fixture, which provides a very basic servlet that registers expec
44

55
Usage is pretty simple:
66

7-
####For junit 4 Rule based
7+
#### For junit 4 Rule based
88

99
@Rule
1010
public ExpectationServerRule serverRule = new ExpectationServerRule( "repos" );
@@ -23,7 +23,7 @@ Usage is pretty simple:
2323
}
2424

2525

26-
####For junit 5 Extension Based:
26+
#### For junit 5 Extension Based:
2727

2828
@ExtendWith(ExpectationServerExtension.class)
2929
public class ExpectaionTest{
@@ -62,4 +62,29 @@ or:
6262
// Do any assertions....
6363
.......
6464
}
65+
}
66+
67+
#### Quarkus Based Test
68+
69+
There are some limitations to let junit5 @ExtendWith work together with @QuarkusTest, see https://github.com/quarkusio/quarkus/issues/24911#issuecomment-1098935690
70+
So to make it work, here brings the new annotation to make it work.
71+
72+
73+
@QuarkusTest
74+
public class ExpectaionTest{
75+
76+
@InjectExpected("repos")
77+
ExpectationServer server;
78+
79+
@Test
80+
public void run()
81+
throws Exception
82+
{
83+
final String pathParts = "/repos/pathParts/to/something.txt";
84+
final String content = "this is the content";
85+
final String url = server.formatUrl( pathParts );
86+
server.expect( url, 200, content );
87+
// Do any assertions....
88+
.......
89+
}
6590
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<module>core</module>
7878
<module>junit4</module>
7979
<module>junit5</module>
80+
<module>quarkus</module>
8081
</modules>
8182

8283
</project>

quarkus/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver)
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<groupId>org.commonjava.util</groupId>
24+
<artifactId>http-testserver-parent</artifactId>
25+
<version>2.1.1-SNAPSHOT</version>
26+
<relativePath>../pom.xml</relativePath>
27+
</parent>
28+
<modelVersion>4.0.0</modelVersion>
29+
30+
<artifactId>http-testserver-quarkus</artifactId>
31+
<properties>
32+
<quarkus.version>2.16.11.Final</quarkus.version>
33+
</properties>
34+
35+
<dependencyManagement>
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.quarkus</groupId>
39+
<artifactId>quarkus-bom</artifactId>
40+
<version>${quarkus.version}</version>
41+
<type>pom</type>
42+
<scope>import</scope>
43+
</dependency>
44+
</dependencies>
45+
</dependencyManagement>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.commonjava.util</groupId>
50+
<artifactId>http-testserver-core</artifactId>
51+
<version>2.1.1-SNAPSHOT</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter</artifactId>
56+
<version>5.9.2</version>
57+
<scope>compile</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.quarkus</groupId>
61+
<artifactId>quarkus-arc-deployment</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>io.quarkus</groupId>
65+
<artifactId>quarkus-mutiny</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>io.quarkus</groupId>
69+
<artifactId>quarkus-junit5</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.hamcrest</groupId>
73+
<artifactId>hamcrest</artifactId>
74+
<version>2.2</version>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>io.quarkus</groupId>
83+
<artifactId>quarkus-maven-plugin</artifactId>
84+
<version>${quarkus.version}</version>
85+
<executions>
86+
<execution>
87+
<goals>
88+
<goal>build</goal>
89+
<goal>generate-code</goal>
90+
<goal>generate-code-tests</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
98+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.test.http.quarkus;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* We use this new annotation to let the {@link org.commonjava.test.http.expect.ExpectationServer} have ability to be work like the
25+
* old junit Rule based way under Quarkus Based Test.
26+
* Note: This annotation should work with @QuarkusTest under a Quarkus based project, like below: <br />
27+
*
28+
* <pre>
29+
* {@literal @}QuarkusTest
30+
* public class TestClass {
31+
* {@literal @}InjectExpected(base="base")
32+
* ExpectationServer server;
33+
* ......
34+
* }
35+
* </pre>
36+
*/
37+
@Target( ElementType.FIELD)
38+
@Retention( RetentionPolicy.RUNTIME)
39+
public @interface InjectExpected
40+
{
41+
/**
42+
* The base path of the server
43+
*
44+
* @return
45+
*/
46+
String base() default "";
47+
48+
/**
49+
* The default port of the server. Note that if the port is occupied, the server will randomly find a new port to start
50+
* @return
51+
*/
52+
int port() default 0;
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.test.http.quarkus;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* We use this new annotation to let the {@link org.commonjava.test.http.stream.StreamServer} have ability to be work like the
25+
* old junit Rule based way under Quarkus Based Test.
26+
* Note: This annotation should work with @QuarkusTest under a Quarkus based project
27+
*/
28+
@Target( ElementType.FIELD )
29+
@Retention( RetentionPolicy.RUNTIME )
30+
public @interface InjectStream
31+
{
32+
/**
33+
* The base path of the server
34+
*
35+
* @return
36+
*/
37+
String base() default "";
38+
39+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.test.http.quarkus.internal;
17+
18+
import io.quarkus.test.junit.callback.QuarkusTestAfterConstructCallback;
19+
import io.quarkus.test.junit.callback.QuarkusTestAfterEachCallback;
20+
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
21+
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
22+
import org.commonjava.test.http.expect.ExpectationServer;
23+
import org.commonjava.test.http.quarkus.InjectExpected;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.lang.reflect.Field;
28+
import java.util.Map;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
31+
public class ExpectationServerCallBack
32+
implements QuarkusTestAfterConstructCallback, QuarkusTestAfterEachCallback, QuarkusTestBeforeEachCallback
33+
{
34+
private final Logger logger = LoggerFactory.getLogger( this.getClass() );
35+
36+
private static final Map<Object, ExpectationServer> injectServers = new ConcurrentHashMap<>();
37+
38+
@Override
39+
public void afterConstruct( Object testInstance )
40+
{
41+
Class<?> current = testInstance.getClass();
42+
while ( current.getSuperclass() != null )
43+
{
44+
for ( Field field : current.getDeclaredFields() )
45+
{
46+
InjectExpected injectMockAnnotation = field.getAnnotation( InjectExpected.class );
47+
if ( injectMockAnnotation != null && field.getType().equals( ExpectationServer.class ) )
48+
{
49+
final String baseResource = injectMockAnnotation.base();
50+
final int port = injectMockAnnotation.port();
51+
logger.trace( "Found field with @InjectExpected annotation, base resource is {}, port is {}",
52+
baseResource, port );
53+
54+
ExpectationServer server = new ExpectationServer( baseResource, port );
55+
try
56+
{
57+
logger.trace( "Injecting the field {} with server instance", field.getName() );
58+
field.setAccessible( true );
59+
field.set( testInstance, server );
60+
injectServers.put( testInstance, server );
61+
}
62+
catch ( IllegalAccessException e )
63+
{
64+
throw new RuntimeException( e );
65+
}
66+
return;
67+
}
68+
}
69+
current = current.getSuperclass();
70+
}
71+
}
72+
73+
@Override
74+
public void beforeEach( QuarkusTestMethodContext context )
75+
{
76+
ExpectationServer server = injectServers.get( context.getTestInstance() );
77+
if ( server != null )
78+
{
79+
server.start();
80+
}
81+
}
82+
83+
@Override
84+
public void afterEach( QuarkusTestMethodContext context )
85+
{
86+
ExpectationServer server = injectServers.get( context.getTestInstance() );
87+
if ( server != null )
88+
{
89+
server.stop();
90+
}
91+
}
92+
93+
}

0 commit comments

Comments
 (0)