Skip to content

Commit

Permalink
Merge pull request #893 from rsvoboda/restAssuredURLManager
Browse files Browse the repository at this point in the history
Handle both custom port and host for RestAssured
  • Loading branch information
Sanne authored Feb 14, 2019
2 parents 580d545 + de513c4 commit ecd3139
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
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();
}


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.shamrock.test.common.http.TestHttpResourceManager;
import org.jboss.shrinkwrap.api.exporter.ExplodedExporter;
Expand Down Expand Up @@ -289,11 +289,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.jboss.shamrock.test.common.http.TestHttpResourceManager;
import org.junit.jupiter.api.TestFactory;
Expand Down Expand Up @@ -79,12 +77,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();
}

@Override
Expand Down

0 comments on commit ecd3139

Please sign in to comment.