-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #887 from stuartwdouglas/uri-injection
Add support for directly injecting the HTTP URL into tests
- Loading branch information
Showing
20 changed files
with
236 additions
and
10 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
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
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
16 changes: 16 additions & 0 deletions
16
...mon/src/main/java/org/jboss/shamrock/test/common/http/StringTestHTTPResourceProvider.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,16 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.URI; | ||
|
||
public class StringTestHTTPResourceProvider implements TestHTTPResourceProvider<String> { | ||
@Override | ||
public Class<String> getProvidedType() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public String provide(URI testUri, Field field) { | ||
return testUri.toASCIIString(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...-framework/common/src/main/java/org/jboss/shamrock/test/common/http/TestHTTPResource.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,25 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Indicates that a field should be injected with a resource that is pre-configured | ||
* to use the correct test URL. | ||
* | ||
* This could be a String or URL object, or some other HTTP/Websocket based client. | ||
* | ||
* This mechanism is plugable, via {@link TestHTTPResourceProvider} | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface TestHTTPResource { | ||
|
||
/** | ||
* | ||
* @return The path part of the URL | ||
*/ | ||
String value() default ""; | ||
} |
18 changes: 18 additions & 0 deletions
18
...rk/common/src/main/java/org/jboss/shamrock/test/common/http/TestHTTPResourceProvider.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,18 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.URI; | ||
|
||
public interface TestHTTPResourceProvider<T> { | ||
|
||
Class<T> getProvidedType(); | ||
|
||
/** | ||
* Create the resource to be injected into the field. | ||
* <p> | ||
* Note that there is no need to directly call set() on the field, it is only provided | ||
* to allow you to examine the generic type and any additional annotations. | ||
*/ | ||
T provide(URI testUri, Field field); | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...ork/common/src/main/java/org/jboss/shamrock/test/common/http/TestHttpResourceManager.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,64 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
|
||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
public class TestHttpResourceManager { | ||
|
||
static final String uri; | ||
static final Map<Class<?>, TestHTTPResourceProvider<?>> providers; | ||
|
||
static { | ||
Map<Class<?>, TestHTTPResourceProvider<?>> map = new HashMap<>(); | ||
for (TestHTTPResourceProvider i : ServiceLoader.load(TestHTTPResourceProvider.class)) { | ||
map.put(i.getProvidedType(), i); | ||
} | ||
providers = Collections.unmodifiableMap(map); | ||
Config config = ConfigProvider.getConfig(); | ||
String host = config.getOptionalValue("shamrock.http.host", String.class).orElse("localhost"); | ||
String port = config.getOptionalValue("shamrock.http.test-port", String.class).orElse("8081"); | ||
uri = "http://" + host + ":" + port; | ||
System.setProperty("test.url", uri); | ||
} | ||
|
||
public static String getUri() { | ||
return uri; | ||
} | ||
|
||
public static void inject(Object testCase) { | ||
Class<?> c = testCase.getClass(); | ||
while (c != Object.class) { | ||
for(Field f : c.getDeclaredFields()) { | ||
TestHTTPResource resource = f.getAnnotation(TestHTTPResource.class); | ||
if(resource != null) { | ||
TestHTTPResourceProvider provider = providers.get(f.getType()); | ||
if(provider == null) { | ||
throw new RuntimeException("Unable to inject TestHTTPResource field " + f + " as no provider exists for the type"); | ||
} | ||
String path = resource.value(); | ||
String val; | ||
if(path.startsWith("/")) { | ||
val = uri + path; | ||
} else { | ||
val = uri + "/" + path; | ||
} | ||
f.setAccessible(true); | ||
try { | ||
f.set(testCase, provider.provide(new URI(val), f)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
c = c.getSuperclass(); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...common/src/main/java/org/jboss/shamrock/test/common/http/URITestHTTPResourceProvider.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,16 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.URI; | ||
|
||
public class URITestHTTPResourceProvider implements TestHTTPResourceProvider<URI> { | ||
@Override | ||
public Class<URI> getProvidedType() { | ||
return URI.class; | ||
} | ||
|
||
@Override | ||
public URI provide(URI testUri, Field field) { | ||
return testUri; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...common/src/main/java/org/jboss/shamrock/test/common/http/URLTestHTTPResourceProvider.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,22 @@ | ||
package org.jboss.shamrock.test.common.http; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
|
||
public class URLTestHTTPResourceProvider implements TestHTTPResourceProvider<URL> { | ||
@Override | ||
public Class<URL> getProvidedType() { | ||
return URL.class; | ||
} | ||
|
||
@Override | ||
public URL provide(URI testUri, Field field) { | ||
try { | ||
return testUri.toURL(); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../resources/META-INF/services/org.jboss.shamrock.test.common.http.TestHTTPResourceProvider
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,3 @@ | ||
org.jboss.shamrock.test.common.http.URLTestHTTPResourceProvider | ||
org.jboss.shamrock.test.common.http.URITestHTTPResourceProvider | ||
org.jboss.shamrock.test.common.http.StringTestHTTPResourceProvider |
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.