forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
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 quarkusio#11080 from stuartwdouglas/test-resources
Add ability to directly specify endpoints in tests
- Loading branch information
Showing
20 changed files
with
465 additions
and
17 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
core/runtime/src/main/java/io/quarkus/runtime/test/TestHttpEndpointProvider.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,24 @@ | ||
package io.quarkus.runtime.test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Interface that can be used to integrate with the TestHTTPEndpoint infrastructure | ||
*/ | ||
public interface TestHttpEndpointProvider { | ||
|
||
Function<Class<?>, String> endpointProvider(); | ||
|
||
static List<Function<Class<?>, String>> load() { | ||
List<Function<Class<?>, String>> ret = new ArrayList<>(); | ||
for (TestHttpEndpointProvider i : ServiceLoader.load(TestHttpEndpointProvider.class, | ||
Thread.currentThread().getContextClassLoader())) { | ||
ret.add(i.endpointProvider()); | ||
} | ||
return ret; | ||
} | ||
|
||
} |
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
70 changes: 70 additions & 0 deletions
70
...ime/src/main/java/io/quarkus/resteasy/server/common/runtime/RESTEasyTestHttpProvider.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,70 @@ | ||
package io.quarkus.resteasy.server.common.runtime; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import javax.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.quarkus.runtime.test.TestHttpEndpointProvider; | ||
|
||
public class RESTEasyTestHttpProvider implements TestHttpEndpointProvider { | ||
@Override | ||
public Function<Class<?>, String> endpointProvider() { | ||
return new Function<Class<?>, String>() { | ||
@Override | ||
public String apply(Class<?> aClass) { | ||
String value = getPath(aClass); | ||
if (value == null) { | ||
return null; | ||
} | ||
if (value.startsWith("/")) { | ||
value = value.substring(1); | ||
} | ||
//TODO: there is not really any way to handle @ApplicationPath, we could do something for @QuarkusTest apps but we can't for | ||
//native apps, so we just have to document the limitation | ||
String path = "/"; | ||
Optional<String> appPath = ConfigProvider.getConfig().getOptionalValue("quarkus.resteasy.path", String.class); | ||
if (appPath.isPresent()) { | ||
path = appPath.get(); | ||
} | ||
if (!path.endsWith("/")) { | ||
path = path + "/"; | ||
} | ||
value = path + value; | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
private String getPath(Class<?> aClass) { | ||
String value = null; | ||
for (Annotation annotation : aClass.getAnnotations()) { | ||
if (annotation.annotationType().getName().equals(Path.class.getName())) { | ||
try { | ||
value = (String) annotation.annotationType().getMethod("value").invoke(annotation); | ||
break; | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
if (value == null) { | ||
for (Class<?> i : aClass.getInterfaces()) { | ||
value = getPath(i); | ||
if (value != null) { | ||
break; | ||
} | ||
} | ||
} | ||
if (value == null) { | ||
if (aClass.getSuperclass() != Object.class) { | ||
value = getPath(aClass.getSuperclass()); | ||
} | ||
} | ||
return value; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ime/src/main/resources/META-INF/services/io.quarkus.runtime.test.TestHttpEndpointProvider
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 @@ | ||
io.quarkus.resteasy.server.common.runtime.RESTEasyTestHttpProvider |
57 changes: 57 additions & 0 deletions
57
.../undertow/runtime/src/main/java/io/quarkus/undertow/runtime/UndertowTestHttpProvider.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,57 @@ | ||
package io.quarkus.undertow.runtime; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import javax.servlet.annotation.WebServlet; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.quarkus.runtime.test.TestHttpEndpointProvider; | ||
|
||
public class UndertowTestHttpProvider implements TestHttpEndpointProvider { | ||
@Override | ||
public Function<Class<?>, String> endpointProvider() { | ||
return new Function<Class<?>, String>() { | ||
@Override | ||
public String apply(Class<?> aClass) { | ||
String value = null; | ||
for (Annotation annotation : aClass.getAnnotations()) { | ||
if (annotation.annotationType().getName().equals(WebServlet.class.getName())) { | ||
try { | ||
String[] patterns = (String[]) annotation.annotationType().getMethod("urlPatterns") | ||
.invoke(annotation); | ||
if (patterns.length > 0) { | ||
value = patterns[0]; | ||
} | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
if (value == null) { | ||
return null; | ||
} | ||
if (value.endsWith("/*")) { | ||
value = value.substring(0, value.length() - 1); | ||
} | ||
if (value.startsWith("/")) { | ||
value = value.substring(1); | ||
} | ||
String path = "/"; | ||
Optional<String> appPath = ConfigProvider.getConfig().getOptionalValue("quarkus.servlet.context-path", | ||
String.class); | ||
if (appPath.isPresent()) { | ||
path = appPath.get(); | ||
} | ||
if (!path.endsWith("/")) { | ||
path = path + "/"; | ||
} | ||
value = path + value; | ||
return value; | ||
} | ||
}; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ime/src/main/resources/META-INF/services/io.quarkus.runtime.test.TestHttpEndpointProvider
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 @@ | ||
io.quarkus.undertow.runtime.UndertowTestHttpProvider |
36 changes: 36 additions & 0 deletions
36
...eb/runtime/src/main/java/io/quarkus/vertx/web/runtime/ReactiveRoutesTestHttpProvider.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,36 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.function.Function; | ||
|
||
import io.quarkus.runtime.test.TestHttpEndpointProvider; | ||
import io.quarkus.vertx.web.RouteBase; | ||
|
||
public class ReactiveRoutesTestHttpProvider implements TestHttpEndpointProvider { | ||
@Override | ||
public Function<Class<?>, String> endpointProvider() { | ||
return new Function<Class<?>, String>() { | ||
@Override | ||
public String apply(Class<?> aClass) { | ||
String value = null; | ||
for (Annotation annotation : aClass.getAnnotations()) { | ||
if (annotation.annotationType().getName().equals(RouteBase.class.getName())) { | ||
try { | ||
value = (String) annotation.annotationType().getMethod("path").invoke(annotation); | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
if ((value == null) || value.isEmpty()) { | ||
return null; | ||
} | ||
if (!value.startsWith("/")) { | ||
value = "/" + value; | ||
} | ||
return value; | ||
} | ||
}; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ime/src/main/resources/META-INF/services/io.quarkus.runtime.test.TestHttpEndpointProvider
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 @@ | ||
io.quarkus.vertx.web.runtime.ReactiveRoutesTestHttpProvider |
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
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
Oops, something went wrong.