Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle both custom port and host for RestAssured #893

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
* <p>
* TODO: do we actually want this here, or should it be in a different module?
*/
public class RestAssuredPortManager {
public class RestAssuredURLManager {

private static final Field portField;
private static final Field baseURIField;
private static int oldPort;
private static String oldBaseURI;

static {
Field p;
Field base;
try {
Class<?> restAssured = Class.forName("io.restassured.RestAssured");
p = restAssured.getField("port");
p.setAccessible(true);
base = restAssured.getField("baseURI");
base.setAccessible(true);
} catch (Exception e) {
p = null;
base = null;
}
portField = p;
baseURIField = base;
}

public static void setPort() {
public static void setURL() {
if (portField != null) {
try {
oldPort = (Integer) portField.get(null);
Expand All @@ -38,15 +45,31 @@ public static void setPort() {
e.printStackTrace();
}
}
if (baseURIField != null) {
try {
oldBaseURI = (String) baseURIField.get(null);
String baseURI = "http://" + ConfigProvider.getConfig().getOptionalValue("shamrock.http.host", String.class).orElse("localhost");
baseURIField.set(null, baseURI);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

public static void clearPort() {
public static void clearURL() {
if (portField != null) {
try {
portField.set(null, oldPort);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (baseURIField != null) {
try {
baseURIField.set(null, oldBaseURI);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jboss.shamrock.test.common.RestAssuredPortManager;
import org.jboss.shamrock.test.common.RestAssuredURLManager;
import org.jboss.shamrock.test.common.TestResourceManager;
import org.junit.runner.Description;
import org.junit.runner.Result;
Expand All @@ -46,7 +46,7 @@ protected AbstractShamrockRunListener(Class<?> testClass, RunNotifier runNotifie

@Override
public void testStarted(Description description) throws Exception {
RestAssuredPortManager.setPort();
RestAssuredURLManager.setURL();
if (!started) {
List<RunListener> stopListeners = new ArrayList<>();

Expand Down Expand Up @@ -92,7 +92,7 @@ public void testRunFinished(Result result) throws Exception {
@Override
public void testFinished(Description description) throws Exception {
super.testFinished(description);
RestAssuredPortManager.clearPort();
RestAssuredURLManager.clearURL();
}

protected abstract void startShamrock() throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.jboss.shamrock.runtime.InjectionInstance;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.test.common.PathTestHelper;
import org.jboss.shamrock.test.common.RestAssuredPortManager;
import org.jboss.shamrock.test.common.RestAssuredURLManager;
import org.jboss.shamrock.test.common.TestResourceManager;
import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
Expand Down Expand Up @@ -286,11 +286,11 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx

@Override
public void afterEach(ExtensionContext context) throws Exception {
RestAssuredPortManager.clearPort();
RestAssuredURLManager.clearURL();
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
RestAssuredPortManager.setPort();
RestAssuredURLManager.setURL();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
import static org.jboss.shamrock.test.common.PathTestHelper.getTestClassesLocation;

import java.io.Closeable;
import java.util.Collections;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.shamrock.runner.RuntimeRunner;
import org.jboss.shamrock.runtime.LaunchMode;
import org.jboss.shamrock.test.common.NativeImageLauncher;
import org.jboss.shamrock.test.common.RestAssuredPortManager;
import org.jboss.shamrock.test.common.RestAssuredURLManager;
import org.jboss.shamrock.test.common.TestResourceManager;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand Down Expand Up @@ -74,12 +72,12 @@ private ExtensionState doJavaStart(ExtensionContext context, TestResourceManager

@Override
public void afterEach(ExtensionContext context) throws Exception {
RestAssuredPortManager.clearPort();
RestAssuredURLManager.clearURL();
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
RestAssuredPortManager.setPort();
RestAssuredURLManager.setURL();
}


Expand Down