From aab7bb6b64ccc31370c9303321a3b77c2788662a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Rohde=20D=C3=B8ssing?= Date: Thu, 30 May 2019 17:44:54 +0200 Subject: [PATCH] Issue-1325: Let OS assign random ports to servers during testing, and let tests get the port of the running server once the OS has assigned a port. Delete PortUtil and similar utilities. --- .../fhir/jpa/cds/example/CdsExampleTests.java | 33 +-- .../ca/uhn/fhir/jpa/demo/ExampleServerIT.java | 11 +- .../jpa/demo/RandomServerPortProvider.java | 36 --- hapi-fhir-base/pom.xml | 4 + .../main/java/ca/uhn/fhir/util/JettyUtil.java | 40 +++ .../main/java/ca/uhn/fhir/util/PortUtil.java | 233 ------------------ .../java/ca/uhn/fhir/util/PortUtilTest.java | 107 -------- ...ExportConceptMapToCsvCommandDstu3Test.java | 11 +- .../ExportConceptMapToCsvCommandR4Test.java | 11 +- ...ImportCsvToConceptMapCommandDstu3Test.java | 11 +- .../ImportCsvToConceptMapCommandR4Test.java | 11 +- .../okhttp/GenericOkHttpClientDstu2Test.java | 10 +- .../fhir/okhttp/RandomServerPortProvider.java | 36 --- ...ersionedApiConverterInterceptorR4Test.java | 12 +- hapi-fhir-jacoco/pom.xml | 4 - .../client/GenericJaxRsClientDstu2Test.java | 13 +- .../client/GenericJaxRsClientDstu3Test.java | 12 +- ...bstractJaxRsResourceProviderDstu3Test.java | 22 +- .../AbstractJaxRsResourceProviderTest.java | 22 +- .../server/test/RandomServerPortProvider.java | 36 --- .../JaxRsPatientProviderDstu3Test.java | 21 +- .../example/JaxRsPatientProviderTest.java | 20 +- .../example/RandomServerPortProvider.java | 36 --- .../BaseResourceProviderDstu2Test.java | 13 +- .../jpa/provider/SystemProviderDstu2Test.java | 14 +- ...temProviderTransactionSearchDstu2Test.java | 14 +- .../dstu3/BaseResourceProviderDstu3Test.java | 15 +- ...temProviderTransactionSearchDstu3Test.java | 15 +- .../r4/BaseResourceProviderR4Test.java | 15 +- .../jpa/provider/r4/EmptyIndexesR4Test.java | 14 +- .../jpa/provider/r4/SystemProviderR4Test.java | 14 +- ...SystemProviderTransactionSearchR4Test.java | 14 +- .../subscription/BaseSubscriptionsR4Test.java | 15 +- .../email/EmailSubscriptionDstu2Test.java | 5 +- .../email/EmailSubscriptionDstu3Test.java | 5 +- .../email/JavaMailEmailSenderTest.java | 5 +- ...tivatesPreExistingSubscriptionsR4Test.java | 15 +- .../resthook/RestHookTestDstu2Test.java | 13 +- .../resthook/RestHookTestDstu3Test.java | 15 +- ...rceptorRegisteredToDaoConfigDstu2Test.java | 12 +- ...rceptorRegisteredToDaoConfigDstu3Test.java | 12 +- ...nterceptorRegisteredToDaoConfigR4Test.java | 14 +- .../RestHookWithInterceptorR4Test.java | 3 +- .../SubscriptionTriggeringDstu3Test.java | 13 +- .../testutil/RandomServerPortProvider.java | 36 --- .../ca/uhn/fhir/jpa/demo/ExampleServerIT.java | 9 +- .../jpa/demo/RandomServerPortProvider.java | 36 --- .../module/config/TestSubscriptionConfig.java | 9 +- .../config/TestSubscriptionDstu3Config.java | 1 - ...kingQueueSubscribableChannelDstu3Test.java | 12 +- ...TokenServerInterceptorIntegrationTest.java | 2 +- .../rest/server/CreateBinaryDstu2_1Test.java | 10 +- .../fhir/rest/server/CreateDstu2_1Test.java | 10 +- .../rest/server/CustomTypeServerDstu2_1.java | 10 +- .../server/DeleteConditionalDstu2_1Test.java | 10 +- .../server/FormatParameterDstu2_1Test.java | 11 +- .../MetadataConformanceDstu2_1Test.java | 10 +- .../server/OperationServerDstu2_1Test.java | 10 +- ...ServerWithSearchParamTypesDstu2_1Test.java | 8 +- .../fhir/rest/server/PatchDstu2_1Test.java | 11 +- .../uhn/fhir/rest/server/ReadDstu2_1Test.java | 10 +- .../server/SearchCountParamDstu2_1Test.java | 10 +- .../fhir/rest/server/SearchDstu2_1Test.java | 10 +- .../server/SearchHasParamDstu2_1Test.java | 10 +- .../rest/server/SearchPostDstu2_1Test.java | 10 +- .../rest/server/SearchSortDstu2_1Test.java | 10 +- .../SearchWithGenericListDstu2_1Test.java | 10 +- .../server/SearchWithIncludesDstu2_1Test.java | 10 +- ...hWithServerAddressStrategyDstu2_1Test.java | 10 +- .../server/ServerExceptionDstu2_1Test.java | 10 +- .../server/ServerMimetypeDstu2_1Test.java | 10 +- ...nclassifiedServerExceptionDstu2_1Test.java | 10 +- .../fhir/rest/server/UpdateDstu2_1Test.java | 10 +- .../fhir/rest/server/ValidateDstu2_1Test.java | 10 +- .../apache/ApacheClientIntegrationTest.java | 10 +- .../uhn/fhir/rest/server/BinaryDstu2Test.java | 10 +- .../rest/server/BundleTypeInResponseTest.java | 10 +- .../rest/server/CompartmentDstu2Test.java | 10 +- .../rest/server/CreateConditionalTest.java | 10 +- .../uhn/fhir/rest/server/DeleteDstu2Test.java | 10 +- .../uhn/fhir/rest/server/ETagServerTest.java | 10 +- .../IncludeAndRevincludeParameterTest.java | 8 +- .../fhir/rest/server/IncludeDstu2Test.java | 8 +- .../OperationDuplicateServerDstu2Test.java | 10 +- .../rest/server/OperationServerDstu2Test.java | 10 +- ...onServerWithSearchParamTypesDstu2Test.java | 8 +- .../ca/uhn/fhir/rest/server/PreferTest.java | 10 +- .../uhn/fhir/rest/server/ReadDstu2Test.java | 9 +- ...archBundleProviderWithNoSizeDstu2Test.java | 10 +- .../server/SearchCountParamDstu2Test.java | 10 +- .../uhn/fhir/rest/server/SearchDstu2Test.java | 11 +- ...rchReturningProfiledResourceDstu2Test.java | 10 +- .../server/SearchWithDstu2BundleTest.java | 11 +- .../SearchWithGenericListDstu2Test.java | 10 +- .../rest/server/ServerFeaturesDstu2Test.java | 11 +- .../rest/server/ServerSearchDstu2Test.java | 11 +- .../rest/server/SummaryParamDstu2Test.java | 10 +- ...ransactionWithBundleResourceParamTest.java | 10 +- .../uhn/fhir/rest/server/UpdateDstu2Test.java | 10 +- .../fhir/rest/server/ValidateDstu2Test.java | 10 +- .../InterceptorUserDataMapDstu2Test.java | 10 +- .../LoggingInterceptorDstu2Test.java | 11 +- .../ServerActionInterceptorTest.java | 12 +- .../context/ContextScanningDstu3Test.java | 10 +- .../rest/server/CreateBinaryDstu3Test.java | 10 +- .../uhn/fhir/rest/server/CreateDstu3Test.java | 10 +- .../rest/server/CustomTypeServerDstu3.java | 10 +- .../server/DeleteConditionalDstu3Test.java | 10 +- .../rest/server/FormatParameterDstu3Test.java | 11 +- .../rest/server/InterceptorDstu3Test.java | 11 +- .../MetadataCapabilityStatementDstu3Test.java | 11 +- .../server/MetadataConformanceDstu3Test.java | 10 +- .../rest/server/OperationServerDstu3Test.java | 10 +- ...onServerWithSearchParamTypesDstu3Test.java | 8 +- .../uhn/fhir/rest/server/PatchDstu3Test.java | 10 +- .../uhn/fhir/rest/server/ReadDstu3Test.java | 8 +- ...archBundleProviderWithNoSizeDstu3Test.java | 10 +- .../server/SearchCountParamDstu3Test.java | 10 +- .../server/SearchDefaultMethodDstu3Test.java | 10 +- .../uhn/fhir/rest/server/SearchDstu3Test.java | 8 +- .../rest/server/SearchHasParamDstu3Test.java | 10 +- .../fhir/rest/server/SearchPostDstu3Test.java | 10 +- .../fhir/rest/server/SearchSortDstu3Test.java | 10 +- .../SearchWithGenericListDstu3Test.java | 10 +- .../server/SearchWithIncludesDstu3Test.java | 10 +- ...rchWithServerAddressStrategyDstu3Test.java | 10 +- .../rest/server/ServerExceptionDstu3Test.java | 11 +- .../rest/server/ServerMimetypeDstu3Test.java | 10 +- .../UnclassifiedServerExceptionDstu3Test.java | 10 +- .../uhn/fhir/rest/server/UpdateDstu3Test.java | 10 +- .../fhir/rest/server/ValidateDstu3Test.java | 10 +- .../AuthorizationInterceptorDstu3Test.java | 12 +- ...pprtedHttpMethodsInterceptorDstu3Test.java | 10 +- .../interceptor/CorsInterceptorDstu3Test.java | 10 +- .../rest/server/BinaryHl7OrgDstu2Test.java | 10 +- .../BundleTypeInResponseHl7OrgTest.java | 10 +- .../server/CreateConditionalHl7OrgTest.java | 10 +- .../server/DeleteConditionalHl7OrgTest.java | 10 +- ...cludeAndRevincludeParameterHl7OrgTest.java | 10 +- .../rest/server/IncludeHl7OrgDstu2Test.java | 10 +- ...erationDuplicateServerHl7OrgDstu2Test.java | 10 +- .../server/OperationServerHl7OrgTest.java | 10 +- .../rest/server/PreferHl7OrgDstu2Test.java | 10 +- .../fhir/rest/server/ReadHl7OrgDstu2Test.java | 11 +- .../rest/server/SearchHl7OrgDstu2Test.java | 11 +- .../SearchWithGenericListHl7OrgDstu2Test.java | 10 +- .../SearchWithHl7OrgDstu2BundleTest.java | 11 +- ...ithBundleResourceParamHl7OrgDstu2Test.java | 10 +- .../UpdateConditionalHl7OrgDstu2Test.java | 10 +- .../rest/server/ValidateHl7OrgDstu2Test.java | 10 +- .../fhir/rest/client/ClientHeadersR4Test.java | 14 +- .../rest/client/ClientIntegrationTest.java | 12 +- ...ompressOutgoingContentInterceptorTest.java | 11 +- .../uhn/fhir/rest/client/HttpProxyTest.java | 10 +- .../rest/client/LoggingInterceptorTest.java | 11 +- .../fhir/rest/param/DateRangeParamR4Test.java | 11 +- .../fhir/rest/server/BinaryServerR4Test.java | 11 +- .../ca/uhn/fhir/rest/server/CreateR4Test.java | 11 +- .../fhir/rest/server/CustomTypeServerR4.java | 10 +- .../rest/server/DeleteConditionalR4Test.java | 11 +- .../fhir/rest/server/ETagServerR4Test.java | 11 +- .../fhir/rest/server/ElementsParamR4Test.java | 11 +- .../rest/server/GraphQLR4ProviderTest.java | 11 +- .../fhir/rest/server/GraphQLR4RawTest.java | 11 +- .../uhn/fhir/rest/server/HistoryR4Test.java | 11 +- .../ca/uhn/fhir/rest/server/IncludeTest.java | 8 +- .../fhir/rest/server/MultitenancyR4Test.java | 11 +- .../server/OperationGenericServerR4Test.java | 11 +- .../rest/server/OperationServerR4Test.java | 11 +- .../server/PagingUsingNamedPagesR4Test.java | 11 +- .../fhir/rest/server/PlainProviderR4Test.java | 17 +- .../SearchBundleProviderWithNoSizeR4Test.java | 10 +- .../rest/server/SearchHasParamR4Test.java | 10 +- .../ca/uhn/fhir/rest/server/SearchR4Test.java | 11 +- .../server/SearchSearchServerDstu1Test.java | 8 +- .../fhir/rest/server/SearchSortR4Test.java | 10 +- .../rest/server/ServerMimetypeR4Test.java | 10 +- .../fhir/rest/server/SummaryParamR4Test.java | 11 +- .../ExceptionHandlingInterceptorTest.java | 10 +- .../ExceptionInterceptorMethodTest.java | 11 +- .../interceptor/InjectionAttackTest.java | 12 +- .../InterceptorThrowingExceptionR4Test.java | 12 +- .../ResponseHighlightingInterceptorTest.java | 14 +- .../ServeMediaResourceRawInterceptorTest.java | 15 +- ...eHighlightingInterceptorExceptionTest.java | 12 +- .../auth/SearchNarrowingInterceptorTest.java | 11 +- .../provider/HashMapResourceProviderTest.java | 19 +- .../fhir/util/RandomServerPortProvider.java | 36 --- ...RequestValidatingInterceptorDstu3Test.java | 11 +- .../RequestValidatingInterceptorR4Test.java | 10 +- ...esponseValidatingInterceptorDstu3Test.java | 10 +- .../ResponseValidatingInterceptorR4Test.java | 10 +- .../ca/uhn/fhir/testmindeps/ReadTest.java | 9 +- 193 files changed, 1014 insertions(+), 1599 deletions(-) delete mode 100644 example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java create mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/util/JettyUtil.java delete mode 100644 hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java delete mode 100644 hapi-fhir-base/src/test/java/ca/uhn/fhir/util/PortUtilTest.java delete mode 100644 hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/RandomServerPortProvider.java delete mode 100644 hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/test/RandomServerPortProvider.java delete mode 100644 hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/RandomServerPortProvider.java delete mode 100644 hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/testutil/RandomServerPortProvider.java delete mode 100644 hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java delete mode 100644 hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/RandomServerPortProvider.java diff --git a/example-projects/hapi-fhir-jpaserver-cds-example/src/test/java/ca/uhn/fhir/jpa/cds/example/CdsExampleTests.java b/example-projects/hapi-fhir-jpaserver-cds-example/src/test/java/ca/uhn/fhir/jpa/cds/example/CdsExampleTests.java index c1fc5856894..eb3b07fcb5d 100644 --- a/example-projects/hapi-fhir-jpaserver-cds-example/src/test/java/ca/uhn/fhir/jpa/cds/example/CdsExampleTests.java +++ b/example-projects/hapi-fhir-jpaserver-cds-example/src/test/java/ca/uhn/fhir/jpa/cds/example/CdsExampleTests.java @@ -6,6 +6,7 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.util.JettyUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; import org.hl7.fhir.dstu3.model.*; @@ -41,8 +42,7 @@ public class CdsExampleTests { // Configure and spin up server String path = Paths.get("").toAbsolutePath().toString(); - ourPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/hapi-fhir-jpaserver-cds"); @@ -51,7 +51,8 @@ public class CdsExampleTests { webAppContext.setParentLoaderPriority(true); ourServer.setHandler(webAppContext); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); @@ -70,7 +71,7 @@ public class CdsExampleTests { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } private static void putResource(String resourceFileName, String id) { @@ -377,27 +378,3 @@ public class CdsExampleTests { Assert.assertTrue(procedureRequest.getDoNotPerform()); } } - -class RandomServerPortProvider { - - private static List ourPorts = new ArrayList<>(); - - static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException | InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} diff --git a/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java b/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java index 6a792e9687f..8252fb5b658 100644 --- a/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java +++ b/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java @@ -21,6 +21,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; +import ca.uhn.fhir.util.JettyUtil; public class ExampleServerIT { @@ -51,7 +52,7 @@ public class ExampleServerIT { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass @@ -65,9 +66,8 @@ public class ExampleServerIT { path = new File(path).getParent(); ourLog.info("Project base path is: {}", path); - - ourPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(ourPort); + + ourServer = new Server(0); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/"); @@ -76,7 +76,8 @@ public class ExampleServerIT { webAppContext.setParentLoaderPriority(true); ourServer.setHandler(webAppContext); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); diff --git a/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java b/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java deleted file mode 100644 index b97428b6975..00000000000 --- a/example-projects/hapi-fhir-jpaserver-dynamic/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.jpa.demo; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-base/pom.xml b/hapi-fhir-base/pom.xml index 150f8423df7..33566ea0eb8 100644 --- a/hapi-fhir-base/pom.xml +++ b/hapi-fhir-base/pom.xml @@ -85,6 +85,10 @@ com.google.guava guava + + org.eclipse.jetty + jetty-server + diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/JettyUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/JettyUtil.java new file mode 100644 index 00000000000..3d0ab036b11 --- /dev/null +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/JettyUtil.java @@ -0,0 +1,40 @@ + +package ca.uhn.fhir.util; + +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.StatisticsHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JettyUtil { + + /** + * Gets the local port for the given server. The server must be started. + */ + public static int getPortForStartedServer(Server server) { + assert server.isStarted(); + Connector[] connectors = server.getConnectors(); + assert connectors.length == 1; + return ((ServerConnector) (connectors[0])).getLocalPort(); + } + + /** + * Starts the given Jetty server, and configures it for graceful shutdown + */ + public static void startServer(Server server) throws Exception { + //Needed for graceful shutdown, see https://github.com/eclipse/jetty.project/issues/2076#issuecomment-353717761 + server.insertHandler(new StatisticsHandler()); + server.start(); + } + + /** + * Shut down the given Jetty server, and release held resources. + */ + public static void closeServer(Server server) throws Exception { + server.stop(); + server.destroy(); + } + +} diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java deleted file mode 100644 index cd345512dc7..00000000000 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/PortUtil.java +++ /dev/null @@ -1,233 +0,0 @@ -package ca.uhn.fhir.util; - -/* - * #%L - * HAPI FHIR - Core Library - * %% - * Copyright (C) 2014 - 2019 University Health Network - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ - -import com.google.common.annotations.VisibleForTesting; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.DatagramSocket; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Provides server ports that are free, in order for tests to use them - * - *

- * This class is ONLY designed for unit-testing usage, as it holds on to server ports - * for a long time (potentially lots of them!) and will leave your system low on - * ports if you put it into production. - *

- *

- * How it works: - *

- * We have lots of tests that need a free port because they want to open up - * a server, and need the port to be unique and unused so that the tests can - * run multithreaded. This turns out to just be an awful problem to solve for - * lots of reasons: - *

- * 1. You can request a free port from the OS by calling new ServerSocket(0); - * and this seems to work 99% of the time, but occasionally on a heavily loaded - * server if two processes ask at the exact same time they will receive the - * same port assignment, and one will fail. - * 2. Tests run in separate processes, so we can't just rely on keeping a collection - * of assigned ports or anything like that. - *

- * So we solve this like this: - *

- * At random, this class will pick a "control port" and bind it. A control port - * is just a randomly chosen port that is a multiple of 100. If we can bind - * successfully to that port, we now own the range of "n+1 to n+99". If we can't - * bind that port, it means some other process has probably taken it so - * we'll just try again until we find an available control port. - *

- * Assuming we successfully bind a control port, we'll give out any available - * ports in the range "n+1 to n+99" until we've exhausted the whole set, and - * then we'll pick another control port (if we actually get asked for over - * 100 ports.. this should be a rare event). - *

- * This mechanism has the benefit of (fingers crossed) being bulletproof - * in terms of its ability to give out ports that are actually free, thereby - * preventing random test failures. - *

- * This mechanism has the drawback of never giving up a control port once - * it has assigned one. To be clear, this class is deliberately leaking - * resources. Again, no production use! - */ -public class PortUtil { - private static final int SPACE_SIZE = 100; - private static final Logger ourLog = LoggerFactory.getLogger(PortUtil.class); - private static final PortUtil INSTANCE = new PortUtil(); - private static int ourPortDelay = 500; - private List myControlSockets = new ArrayList<>(); - private Integer myCurrentControlSocketPort = null; - private int myCurrentOffset = 0; - /** - * Constructor - - */ - PortUtil() { - // nothing - } - - /** - * Clear and release all control sockets - */ - synchronized void clearInstance() { - for (ServerSocket next : myControlSockets) { - ourLog.info("Releasing control port: {}", next.getLocalPort()); - try { - next.close(); - } catch (IOException theE) { - // ignore - } - } - myControlSockets.clear(); - myCurrentControlSocketPort = null; - } - - /** - * Clear and release all control sockets - */ - synchronized int getNextFreePort() { - - while (true) { - - // Acquire a control port - while (myCurrentControlSocketPort == null) { - int nextCandidate = (int) (Math.random() * 65000.0); - nextCandidate = nextCandidate - (nextCandidate % SPACE_SIZE); - - if (nextCandidate < 10000) { - continue; - } - - try { - ServerSocket server = new ServerSocket(); - server.setReuseAddress(true); - server.bind(new InetSocketAddress("localhost", nextCandidate)); - myControlSockets.add(server); - ourLog.info("Acquired control socket on port {}", nextCandidate); - myCurrentControlSocketPort = nextCandidate; - myCurrentOffset = 0; - } catch (IOException theE) { - ourLog.info("Candidate control socket {} is already taken", nextCandidate); - continue; - } - } - - // Find a free port within the allowable range - while (true) { - myCurrentOffset++; - - if (myCurrentOffset == SPACE_SIZE) { - // Current space is exhausted - myCurrentControlSocketPort = null; - break; - } - - int nextCandidatePort = myCurrentControlSocketPort + myCurrentOffset; - - // Try to open a port on this socket and use it - if (!isAvailable(nextCandidatePort)) { - continue; - } - - // Log who asked for the port, just in case that's useful - StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); - StackTraceElement previousElement = Arrays.stream(stackTraceElements) - .filter(t -> !t.toString().contains("PortUtil.") && !t.toString().contains("getStackTrace")) - .findFirst() - .orElse(stackTraceElements[2]); - ourLog.info("Returned available port {} for: {}", nextCandidatePort, previousElement.toString()); - - try { - Thread.sleep(ourPortDelay); - } catch (InterruptedException theE) { - // ignore - } - - return nextCandidatePort; - - } - - - } - } - - @VisibleForTesting - public static void setPortDelay(Integer thePortDelay) { - if (thePortDelay == null) { - thePortDelay = 500; - } else { - ourPortDelay = thePortDelay; - } - } - - /** - * This method checks if we are able to bind a given port to both - * 0.0.0.0 and localhost in order to be sure it's truly available. - */ - private static boolean isAvailable(int thePort) { - ourLog.info("Testing a bind on thePort {}", thePort); - try (ServerSocket ss = new ServerSocket()) { - ss.setReuseAddress(true); - ss.bind(new InetSocketAddress("0.0.0.0", thePort)); - try (DatagramSocket ds = new DatagramSocket()) { - ds.setReuseAddress(true); - ds.connect(new InetSocketAddress("127.0.0.1", thePort)); - ourLog.info("Successfully bound thePort {}", thePort); - } catch (IOException e) { - ourLog.info("Failed to bind thePort {}: {}", thePort, e.toString()); - return false; - } - } catch (IOException e) { - ourLog.info("Failed to bind thePort {}: {}", thePort, e.toString()); - return false; - } - - try (ServerSocket ss = new ServerSocket()) { - ss.setReuseAddress(true); - ss.bind(new InetSocketAddress("localhost", thePort)); - } catch (IOException e) { - ourLog.info("Failed to bind thePort {}: {}", thePort, e.toString()); - return false; - } - - return true; - } - - /** - * The entire purpose here is to find an available port that can then be - * bound for by server in a unit test without conflicting with other tests. - *

- * This is really only used for unit tests but is included in the library - * so it can be reused across modules. Use with caution. - */ - public static int findFreePort() { - return INSTANCE.getNextFreePort(); - } - -} - diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/PortUtilTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/PortUtilTest.java deleted file mode 100644 index 662789dbce5..00000000000 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/PortUtilTest.java +++ /dev/null @@ -1,107 +0,0 @@ -package ca.uhn.fhir.util; - -import org.junit.After; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import static org.hamcrest.Matchers.empty; -import static org.junit.Assert.*; - -public class PortUtilTest { - - private static final Logger ourLog = LoggerFactory.getLogger(PortUtilTest.class); - - @Test - public void testFindFreePort() throws IOException { - int port = PortUtil.findFreePort(); - - // First bind should succeed, second bind should fail - try (ServerSocket ss = new ServerSocket()) { - ss.bind(new InetSocketAddress("0.0.0.0", port)); - - try (ServerSocket ss2 = new ServerSocket()) { - ss2.bind(new InetSocketAddress("0.0.0.0", port)); - fail(); - } catch (IOException e) { - // good - } - - } - } - - @After - public void after() { - PortUtil.setPortDelay(null); - } - - @Test - public void testPortsAreNotReused() throws InterruptedException { - PortUtil.setPortDelay(0); - - List ports = Collections.synchronizedList(new ArrayList<>()); - List portUtils = Collections.synchronizedList(new ArrayList<>()); - List errors = Collections.synchronizedList(new ArrayList<>()); - - int tasksCount = 20; - ExecutorService pool = Executors.newFixedThreadPool(tasksCount); - int portsPerTaskCount = 151; - for (int i = 0; i < tasksCount; i++) { - pool.submit(() -> { - PortUtil portUtil = new PortUtil(); - portUtils.add(portUtil); - for (int j = 0; j < portsPerTaskCount; j++) { - int nextFreePort = portUtil.getNextFreePort(); - - boolean bound; - try (ServerSocket ss = new ServerSocket()) { - ss.bind(new InetSocketAddress("localhost", nextFreePort)); - bound = true; - } catch (IOException e) { - bound = false; - } - - if (!bound) { - try (ServerSocket ss = new ServerSocket()) { - Thread.sleep(1000); - ss.bind(new InetSocketAddress("localhost", nextFreePort)); - } catch (Exception e) { - String msg = "Failure binding new port (second attempt) " + nextFreePort + ": " + e.toString(); - ourLog.error(msg, e); - errors.add(msg); - } - } - - ports.add(nextFreePort); - } - - }); - } - pool.shutdown(); - pool.awaitTermination(60, TimeUnit.SECONDS); - - assertThat(errors.toString(), errors, empty()); - assertEquals(tasksCount * portsPerTaskCount, ports.size()); - - while (ports.size() > 0) { - Integer nextPort = ports.remove(0); - if (ports.contains(nextPort)) { - fail("Port " + nextPort + " was given out more than once"); - } - } - - for (PortUtil next : portUtils) { - next.clearInstance(); - } - } -} diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandDstu3Test.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandDstu3Test.java index 9b4fdf92caa..cbaf150eaf0 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandDstu3Test.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandDstu3Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; @@ -25,6 +24,8 @@ import java.io.IOException; import static org.junit.Assert.assertEquals; +import ca.uhn.fhir.util.JettyUtil; + public class ExportConceptMapToCsvCommandDstu3Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExportConceptMapToCsvCommandDstu3Test.class); private static final String CM_URL = "http://example.com/conceptmap"; @@ -48,14 +49,13 @@ public class ExportConceptMapToCsvCommandDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler servletHandler = new ServletHandler(); @@ -67,7 +67,8 @@ public class ExportConceptMapToCsvCommandDstu3Test { servletHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(servletHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourBase = "http://localhost:" + ourPort; diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandR4Test.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandR4Test.java index d795db8509c..8c41820ba42 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandR4Test.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ExportConceptMapToCsvCommandR4Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.FileUtils; @@ -25,6 +24,8 @@ import java.io.IOException; import static org.junit.Assert.assertEquals; +import ca.uhn.fhir.util.JettyUtil; + public class ExportConceptMapToCsvCommandR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ExportConceptMapToCsvCommandR4Test.class); private static final String CM_URL = "http://example.com/conceptmap"; @@ -48,14 +49,13 @@ public class ExportConceptMapToCsvCommandR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler servletHandler = new ServletHandler(); @@ -67,7 +67,8 @@ public class ExportConceptMapToCsvCommandR4Test { servletHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(servletHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourBase = "http://localhost:" + ourPort; diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandDstu3Test.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandDstu3Test.java index 5bde7f2036c..7d16a1f4edf 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandDstu3Test.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandDstu3Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; @@ -26,6 +25,8 @@ import java.io.File; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ImportCsvToConceptMapCommandDstu3Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ImportCsvToConceptMapCommandDstu3Test.class); private static final String CM_URL = "http://example.com/conceptmap"; @@ -60,14 +61,13 @@ public class ImportCsvToConceptMapCommandDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler servletHandler = new ServletHandler(); @@ -79,7 +79,8 @@ public class ImportCsvToConceptMapCommandDstu3Test { servletHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(servletHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourBase = "http://localhost:" + ourPort; diff --git a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandR4Test.java b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandR4Test.java index 7c468bb32a2..f967d5ad2dd 100644 --- a/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandR4Test.java +++ b/hapi-fhir-cli/hapi-fhir-cli-api/src/test/java/ca/uhn/fhir/cli/ImportCsvToConceptMapCommandR4Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; @@ -26,6 +25,8 @@ import java.io.File; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ImportCsvToConceptMapCommandR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ImportCsvToConceptMapCommandR4Test.class); private static final String CM_URL = "http://example.com/conceptmap"; @@ -60,14 +61,13 @@ public class ImportCsvToConceptMapCommandR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler servletHandler = new ServletHandler(); @@ -79,7 +79,8 @@ public class ImportCsvToConceptMapCommandR4Test { servletHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(servletHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourBase = "http://localhost:" + ourPort; diff --git a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/GenericOkHttpClientDstu2Test.java b/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/GenericOkHttpClientDstu2Test.java index 32b1f2d838d..11bb7113fde 100644 --- a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/GenericOkHttpClientDstu2Test.java +++ b/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/GenericOkHttpClientDstu2Test.java @@ -38,6 +38,7 @@ import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException; import ca.uhn.fhir.rest.client.impl.RestfulClientFactory; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.util.JettyUtil; public class GenericOkHttpClientDstu2Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(GenericOkHttpClientDstu2Test.class); @@ -63,9 +64,7 @@ public class GenericOkHttpClientDstu2Test { public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(ourPort); - ourLog.info("Starting server on port {}", ourPort); + ourServer = new Server(0); ourServer.setHandler(new AbstractHandler() { @Override @@ -107,12 +106,13 @@ public class GenericOkHttpClientDstu2Test { } }); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); } @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } /** diff --git a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/RandomServerPortProvider.java b/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/RandomServerPortProvider.java deleted file mode 100644 index f079d1ee396..00000000000 --- a/hapi-fhir-client-okhttp/src/test/java/ca/uhn/fhir/okhttp/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.okhttp; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-converter/src/test/java/ca/uhn/hapi/converters/server/VersionedApiConverterInterceptorR4Test.java b/hapi-fhir-converter/src/test/java/ca/uhn/hapi/converters/server/VersionedApiConverterInterceptorR4Test.java index c6c357b2bad..cf7cd912d67 100644 --- a/hapi-fhir-converter/src/test/java/ca/uhn/hapi/converters/server/VersionedApiConverterInterceptorR4Test.java +++ b/hapi-fhir-converter/src/test/java/ca/uhn/hapi/converters/server/VersionedApiConverterInterceptorR4Test.java @@ -3,10 +3,8 @@ package ca.uhn.hapi.converters.server; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.EncodingEnum; -import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; @@ -33,6 +31,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; +import ca.uhn.fhir.util.JettyUtil; + public class VersionedApiConverterInterceptorR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(VersionedApiConverterInterceptorR4Test.class); @@ -76,14 +76,13 @@ public class VersionedApiConverterInterceptorR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -97,7 +96,8 @@ public class VersionedApiConverterInterceptorR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-jacoco/pom.xml b/hapi-fhir-jacoco/pom.xml index 74a64f034b1..8c028a9a6e2 100644 --- a/hapi-fhir-jacoco/pom.xml +++ b/hapi-fhir-jacoco/pom.xml @@ -169,22 +169,18 @@ org.eclipse.jetty jetty-servlets - test org.eclipse.jetty jetty-servlet - test org.eclipse.jetty jetty-server - test org.eclipse.jetty jetty-util - test diff --git a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu2Test.java b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu2Test.java index 31ff4627c24..8efdd3d7804 100644 --- a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu2Test.java +++ b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu2Test.java @@ -6,8 +6,6 @@ import static org.junit.Assert.*; import java.io.IOException; import java.util.*; -import javax.annotation.Priority; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -23,7 +21,6 @@ import org.junit.Test; import com.google.common.collect.*; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jaxrs.server.test.RandomServerPortProvider; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.dstu2.composite.IdentifierDt; @@ -38,6 +35,7 @@ import ca.uhn.fhir.rest.client.api.*; import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.util.JettyUtil; public class GenericJaxRsClientDstu2Test { private static FhirContext ourCtx; @@ -2075,9 +2073,7 @@ public class GenericJaxRsClientDstu2Test { public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(ourPort); - ourLog.info("Starting server on port {}", ourPort); + ourServer = new Server(0); ourServer.setHandler(new AbstractHandler() { @Override @@ -2119,11 +2115,12 @@ public class GenericJaxRsClientDstu2Test { } }); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); } @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } } diff --git a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu3Test.java b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu3Test.java index 2e3d27bb1ef..0e08b05f23d 100644 --- a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu3Test.java +++ b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/client/GenericJaxRsClientDstu3Test.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.util.*; import java.util.Enumeration; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -24,7 +23,6 @@ import org.junit.*; import com.google.common.collect.*; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jaxrs.server.test.RandomServerPortProvider; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.parser.IParser; @@ -34,6 +32,7 @@ import ca.uhn.fhir.rest.client.api.*; import ca.uhn.fhir.rest.client.exceptions.InvalidResponseException; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.util.JettyUtil; public class GenericJaxRsClientDstu3Test { private static FhirContext ourCtx; @@ -2103,9 +2102,7 @@ public class GenericJaxRsClientDstu3Test { public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu3(); - ourPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(ourPort); - ourLog.info("Starting server on port {}", ourPort); + ourServer = new Server(0); ourServer.setHandler(new AbstractHandler() { @Override @@ -2147,11 +2144,12 @@ public class GenericJaxRsClientDstu3Test { } }); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); } @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } } diff --git a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderDstu3Test.java b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderDstu3Test.java index 67edd32385c..4174d915e4f 100644 --- a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderDstu3Test.java +++ b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderDstu3Test.java @@ -3,7 +3,6 @@ package ca.uhn.fhir.jaxrs.server; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jaxrs.client.JaxRsRestfulClientFactory; import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsResponseException; -import ca.uhn.fhir.jaxrs.server.test.RandomServerPortProvider; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsConformanceRestProviderDstu3; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsMockPageProviderDstu3; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsMockPatientRestProviderDstu3; @@ -45,6 +44,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class AbstractJaxRsResourceProviderDstu3Test { @@ -68,7 +69,8 @@ public class AbstractJaxRsResourceProviderDstu3Test { } @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(jettyServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -423,11 +425,9 @@ public class AbstractJaxRsResourceProviderDstu3Test { @BeforeClass public static void setUpClass() throws Exception { - ourPort = RandomServerPortProvider.findFreePort(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); - System.out.println(ourPort); - jettyServer = new Server(ourPort); + jettyServer = new Server(0); jettyServer.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.class, "/*"); jerseyServlet.setInitOrder(0); @@ -442,7 +442,8 @@ public class AbstractJaxRsResourceProviderDstu3Test { ), ",")); //@formatter:on - jettyServer.start(); + JettyUtil.startServer(jettyServer); + ourPort = JettyUtil.getPortForStartedServer(jettyServer); ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx)); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); @@ -453,13 +454,4 @@ public class AbstractJaxRsResourceProviderDstu3Test { client.registerInterceptor(new LoggingInterceptor(true)); } - @AfterClass - public static void tearDownClass() { - try { - jettyServer.destroy(); - } catch (Exception e) { - - } - } - } diff --git a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderTest.java b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderTest.java index 588363f8c79..d66c3bfd985 100644 --- a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderTest.java +++ b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsResourceProviderTest.java @@ -3,7 +3,6 @@ package ca.uhn.fhir.jaxrs.server; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jaxrs.client.JaxRsRestfulClientFactory; import ca.uhn.fhir.jaxrs.server.interceptor.JaxRsResponseException; -import ca.uhn.fhir.jaxrs.server.test.RandomServerPortProvider; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsConformanceRestProvider; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsMockPageProvider; import ca.uhn.fhir.jaxrs.server.test.TestJaxRsMockPatientRestProvider; @@ -46,6 +45,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class AbstractJaxRsResourceProviderTest { @@ -71,7 +72,8 @@ public class AbstractJaxRsResourceProviderTest { } @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(jettyServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -392,11 +394,9 @@ public class AbstractJaxRsResourceProviderTest { @BeforeClass public static void setUpClass() throws Exception { - ourPort = RandomServerPortProvider.findFreePort(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); - System.out.println(ourPort); - jettyServer = new Server(ourPort); + jettyServer = new Server(0); jettyServer.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.class, "/*"); jerseyServlet.setInitOrder(0); @@ -410,7 +410,8 @@ public class AbstractJaxRsResourceProviderTest { ), ",")); //@formatter:on - jettyServer.start(); + JettyUtil.startServer(jettyServer); + ourPort = JettyUtil.getPortForStartedServer(jettyServer); ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx)); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); @@ -421,13 +422,4 @@ public class AbstractJaxRsResourceProviderTest { client.registerInterceptor(new LoggingInterceptor(true)); } - @AfterClass - public static void tearDownClass() { - try { - jettyServer.destroy(); - } catch (Exception e) { - - } - } - } diff --git a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/test/RandomServerPortProvider.java b/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/test/RandomServerPortProvider.java deleted file mode 100644 index e9e137a6b3e..00000000000 --- a/hapi-fhir-jaxrsserver-base/src/test/java/ca/uhn/fhir/jaxrs/server/test/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.jaxrs.server.test; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderDstu3Test.java b/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderDstu3Test.java index 210180ec2e0..0746244910f 100644 --- a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderDstu3Test.java +++ b/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderDstu3Test.java @@ -19,6 +19,7 @@ import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class JaxRsPatientProviderDstu3Test { @@ -30,18 +31,17 @@ public class JaxRsPatientProviderDstu3Test { private static Server jettyServer; @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(jettyServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void setUpClass() throws Exception { - ourPort = RandomServerPortProvider.findFreePort(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); - System.out.println(ourPort); - jettyServer = new Server(ourPort); + jettyServer = new Server(0); jettyServer.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.class, "/*"); jerseyServlet.setInitOrder(0); @@ -53,7 +53,8 @@ public class JaxRsPatientProviderDstu3Test { JaxRsPageProviderDstu3.class.getCanonicalName() ), ",")); //@formatter:on - jettyServer.start(); + JettyUtil.startServer(jettyServer); + ourPort = JettyUtil.getPortForStartedServer(jettyServer); ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx)); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); @@ -63,16 +64,6 @@ public class JaxRsPatientProviderDstu3Test { client.registerInterceptor(new LoggingInterceptor(true)); } - @AfterClass - public static void tearDownClass() - throws Exception { - try { - jettyServer.destroy(); - } - catch (Exception e) { - } - } - /** Search/Query - Type */ @Test public void findUsingGenericClientBySearch() { diff --git a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderTest.java b/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderTest.java index 6900b6ae4dc..62c70d6c6ed 100644 --- a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderTest.java +++ b/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/JaxRsPatientProviderTest.java @@ -23,6 +23,7 @@ import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class JaxRsPatientProviderTest { @@ -34,18 +35,17 @@ public class JaxRsPatientProviderTest { private static Server jettyServer; @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(jettyServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void setUpClass() throws Exception { - ourPort = RandomServerPortProvider.findFreePort(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); - System.out.println(ourPort); - jettyServer = new Server(ourPort); + jettyServer = new Server(0); jettyServer.setHandler(context); ServletHolder jerseyServlet = context.addServlet(org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.class, "/*"); jerseyServlet.setInitOrder(0); @@ -57,7 +57,8 @@ public class JaxRsPatientProviderTest { JaxRsPageProvider.class.getCanonicalName() ), ",")); //@formatter:on - jettyServer.start(); + JettyUtil.startServer(jettyServer); + ourPort = JettyUtil.getPortForStartedServer(jettyServer); ourCtx.setRestfulClientFactory(new JaxRsRestfulClientFactory(ourCtx)); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); @@ -67,15 +68,6 @@ public class JaxRsPatientProviderTest { client.registerInterceptor(new LoggingInterceptor(true)); } - @AfterClass - public static void tearDownClass() - throws Exception { - try { - jettyServer.destroy(); - } catch (Exception e) { - } - } - /** Search/Query - Type */ @Test public void findUsingGenericClientBySearch() { diff --git a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/RandomServerPortProvider.java b/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/RandomServerPortProvider.java deleted file mode 100644 index 4cfb0eb97d4..00000000000 --- a/hapi-fhir-jaxrsserver-example/src/test/java/ca/uhn/fhir/jaxrs/server/example/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.jaxrs.server.example; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java index 5006062b507..fcc53543292 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/BaseResourceProviderDstu2Test.java @@ -3,7 +3,6 @@ package ca.uhn.fhir.jpa.provider; import ca.uhn.fhir.jpa.config.WebsocketDispatcherConfig; import ca.uhn.fhir.jpa.dao.dstu2.BaseJpaDstu2Test; import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry; import ca.uhn.fhir.model.dstu2.resource.Patient; @@ -36,6 +35,8 @@ import java.util.concurrent.TimeUnit; import static org.apache.commons.lang3.StringUtils.isNotBlank; +import ca.uhn.fhir.util.JettyUtil; + public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { protected static IGenericClient ourClient; @@ -66,12 +67,8 @@ public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { myFhirCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); if (ourServer == null) { - ourPort = RandomServerPortProvider.findFreePort(); - ourRestServer = new RestfulServer(myFhirCtx); - ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; - ourRestServer.registerProviders(myResourceProviders.createProviders()); ourRestServer.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); @@ -114,7 +111,9 @@ public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { server.setHandler(proxyHandler); - server.start(); + JettyUtil.startServer(server); + ourPort = JettyUtil.getPortForStartedServer(server); + ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase); ourClient.registerInterceptor(new LoggingInterceptor()); @@ -152,7 +151,7 @@ public abstract class BaseResourceProviderDstu2Test extends BaseJpaDstu2Test { @AfterClass public static void afterClassClearContextBaseResourceProviderDstu3Test() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); ourHttpClient.close(); ourServer = null; ourHttpClient = null; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderDstu2Test.java index 4065b42b41b..8471ed12ba0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderDstu2Test.java @@ -3,7 +3,6 @@ package ca.uhn.fhir.jpa.provider; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.dstu2.BaseJpaDstu2Test; import ca.uhn.fhir.jpa.rp.dstu2.*; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.dstu2.resource.*; import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum; import ca.uhn.fhir.model.dstu2.valueset.HTTPVerbEnum; @@ -46,6 +45,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SystemProviderDstu2Test extends BaseJpaDstu2Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderDstu2Test.class); @@ -91,14 +92,11 @@ public class SystemProviderDstu2Test extends BaseJpaDstu2Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; - ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); @@ -107,7 +105,9 @@ public class SystemProviderDstu2Test extends BaseJpaDstu2Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -492,7 +492,7 @@ public class SystemProviderDstu2Test extends BaseJpaDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java index 6d592904913..cc362a4de21 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.jpa.dao.dstu2.BaseJpaDstu2Test; import ca.uhn.fhir.jpa.rp.dstu2.ObservationResourceProvider; import ca.uhn.fhir.jpa.rp.dstu2.OrganizationResourceProvider; import ca.uhn.fhir.jpa.rp.dstu2.PatientResourceProvider; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry; import ca.uhn.fhir.model.dstu2.resource.Patient; @@ -32,6 +31,8 @@ import java.util.List; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SystemProviderTransactionSearchDstu2Test extends BaseJpaDstu2Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderTransactionSearchDstu2Test.class); @@ -76,14 +77,11 @@ public class SystemProviderTransactionSearchDstu2Test extends BaseJpaDstu2Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; - ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); @@ -92,7 +90,9 @@ public class SystemProviderTransactionSearchDstu2Test extends BaseJpaDstu2Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; ourCtx.getRestfulClientFactory().setSocketTimeout(600 * 1000); ourClient = ourCtx.newRestfulGenericClient(ourServerBase); @@ -297,7 +297,7 @@ public class SystemProviderTransactionSearchDstu2Test extends BaseJpaDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/BaseResourceProviderDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/BaseResourceProviderDstu3Test.java index 6a6728a9cea..58480996a87 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/BaseResourceProviderDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/BaseResourceProviderDstu3Test.java @@ -15,7 +15,6 @@ import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.CorsInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -46,6 +45,8 @@ import java.util.concurrent.TimeUnit; import static org.apache.commons.lang3.StringUtils.isNotBlank; +import ca.uhn.fhir.util.JettyUtil; + public abstract class BaseResourceProviderDstu3Test extends BaseJpaDstu3Test { @@ -84,12 +85,8 @@ public abstract class BaseResourceProviderDstu3Test extends BaseJpaDstu3Test { myFhirCtx.setParserErrorHandler(new StrictErrorHandler()); if (ourServer == null) { - ourPort = PortUtil.findFreePort(); - ourRestServer = new RestfulServer(myFhirCtx); - ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; - ourRestServer.registerProviders(myResourceProviders.createProviders()); ourRestServer.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); @@ -106,7 +103,7 @@ public abstract class BaseResourceProviderDstu3Test extends BaseJpaDstu3Test { ourPagingProvider = myAppCtx.getBean(DatabaseBackedPagingProvider.class); - Server server = new Server(ourPort); + Server server = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -148,7 +145,9 @@ public abstract class BaseResourceProviderDstu3Test extends BaseJpaDstu3Test { ourRestServer.registerInterceptor(corsInterceptor); server.setHandler(proxyHandler); - server.start(); + JettyUtil.startServer(server); + ourPort = JettyUtil.getPortForStartedServer(server); + ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(subsServletHolder.getServlet().getServletConfig().getServletContext()); myValidationSupport = wac.getBean(JpaValidationSupportChainDstu3.class); @@ -193,7 +192,7 @@ public abstract class BaseResourceProviderDstu3Test extends BaseJpaDstu3Test { @AfterClass public static void afterClassClearContextBaseResourceProviderDstu3Test() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); ourHttpClient.close(); ourServer = null; ourHttpClient = null; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/SystemProviderTransactionSearchDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/SystemProviderTransactionSearchDstu3Test.java index 90ef1a65224..90ec4f474a5 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/SystemProviderTransactionSearchDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/dstu3/SystemProviderTransactionSearchDstu3Test.java @@ -24,11 +24,11 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.dao.dstu3.BaseJpaDstu3Test; import ca.uhn.fhir.jpa.rp.dstu3.*; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.SimpleRequestHeaderInterceptor; import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SystemProviderTransactionSearchDstu3Test extends BaseJpaDstu3Test { @@ -77,13 +77,10 @@ public class SystemProviderTransactionSearchDstu3Test extends BaseJpaDstu3Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); - proxyHandler.setContextPath("/"); - - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; + proxyHandler.setContextPath("/"); ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); @@ -93,7 +90,9 @@ public class SystemProviderTransactionSearchDstu3Test extends BaseJpaDstu3Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -273,7 +272,7 @@ public class SystemProviderTransactionSearchDstu3Test extends BaseJpaDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java index cebd5b30458..850afa422d0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/BaseResourceProviderR4Test.java @@ -18,7 +18,6 @@ import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException; import ca.uhn.fhir.rest.server.interceptor.CorsInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -51,6 +50,8 @@ import java.util.concurrent.TimeUnit; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.junit.Assert.fail; +import ca.uhn.fhir.util.JettyUtil; + public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { protected static JpaValidationSupportChainR4 myValidationSupport; @@ -92,12 +93,8 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { myFhirCtx.setParserErrorHandler(new StrictErrorHandler()); if (ourServer == null) { - ourPort = PortUtil.findFreePort(); - ourRestServer = new RestfulServer(myFhirCtx); - ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; - ourRestServer.registerProviders(myResourceProviders.createProviders()); ourRestServer.getFhirContext().setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator()); @@ -114,7 +111,7 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { ourPagingProvider = myAppCtx.getBean(DatabaseBackedPagingProvider.class); ourResourceCountsCache = (ResourceCountCache) myAppCtx.getBean("myResourceCountsCache"); - Server server = new Server(ourPort); + Server server = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -155,7 +152,9 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { ourRestServer.registerInterceptor(corsInterceptor); server.setHandler(proxyHandler); - server.start(); + JettyUtil.startServer(server); + ourPort = JettyUtil.getPortForStartedServer(server); + ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(subsServletHolder.getServlet().getServletConfig().getServletContext()); myValidationSupport = wac.getBean(JpaValidationSupportChainR4.class); @@ -219,7 +218,7 @@ public abstract class BaseResourceProviderR4Test extends BaseJpaR4Test { @AfterClass public static void afterClassClearContextBaseResourceProviderR4Test() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); ourHttpClient.close(); ourServer = null; ourHttpClient = null; diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/EmptyIndexesR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/EmptyIndexesR4Test.java index ec9c1971e3c..8a6cfd2d486 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/EmptyIndexesR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/EmptyIndexesR4Test.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.jpa.provider.SystemProviderDstu2Test; import ca.uhn.fhir.jpa.rp.r4.ObservationResourceProvider; import ca.uhn.fhir.jpa.rp.r4.OrganizationResourceProvider; import ca.uhn.fhir.jpa.rp.r4.PatientResourceProvider; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.client.api.IGenericClient; @@ -50,6 +49,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class EmptyIndexesR4Test extends BaseJpaR4Test { private static RestfulServer myRestServer; private static IGenericClient ourClient; @@ -93,14 +94,11 @@ public class EmptyIndexesR4Test extends BaseJpaR4Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; - ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); @@ -109,7 +107,9 @@ public class EmptyIndexesR4Test extends BaseJpaR4Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -156,7 +156,7 @@ public class EmptyIndexesR4Test extends BaseJpaR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java index f5f960e6f82..ab448e34c40 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderR4Test.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; import ca.uhn.fhir.jpa.provider.SystemProviderDstu2Test; import ca.uhn.fhir.jpa.rp.r4.*; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.server.IBundleProvider; @@ -56,6 +55,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SystemProviderR4Test extends BaseJpaR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderR4Test.class); @@ -112,14 +113,11 @@ public class SystemProviderR4Test extends BaseJpaR4Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; - ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); @@ -128,7 +126,9 @@ public class SystemProviderR4Test extends BaseJpaR4Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -790,7 +790,7 @@ public class SystemProviderR4Test extends BaseJpaR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderTransactionSearchR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderTransactionSearchR4Test.java index dabc3479068..4e67586c7c6 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderTransactionSearchR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/SystemProviderTransactionSearchR4Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; import ca.uhn.fhir.jpa.rp.r4.*; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.SimpleRequestHeaderInterceptor; @@ -36,6 +35,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SystemProviderTransactionSearchR4Test extends BaseJpaR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SystemProviderTransactionSearchR4Test.class); @@ -87,14 +88,11 @@ public class SystemProviderTransactionSearchR4Test extends BaseJpaR4Test { restServer.setPlainProviders(mySystemProvider); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; - ServletHolder servletHolder = new ServletHolder(); servletHolder.setServlet(restServer); proxyHandler.addServlet(servletHolder, "/fhir/context/*"); @@ -103,7 +101,9 @@ public class SystemProviderTransactionSearchR4Test extends BaseJpaR4Test { restServer.setFhirContext(ourCtx); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -377,7 +377,7 @@ public class SystemProviderTransactionSearchR4Test extends BaseJpaR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/BaseSubscriptionsR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/BaseSubscriptionsR4Test.java index 98d545032c7..afb71c756d0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/BaseSubscriptionsR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/BaseSubscriptionsR4Test.java @@ -12,7 +12,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.util.BundleUtil; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import com.google.common.collect.Lists; import net.ttddyy.dsproxy.QueryCount; import net.ttddyy.dsproxy.listener.SingleQueryCountHolder; @@ -37,6 +37,7 @@ public abstract class BaseSubscriptionsR4Test extends BaseResourceProviderR4Test private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseSubscriptionsR4Test.class); private static Server ourListenerServer; + protected static int ourListenerPort; protected static List ourContentTypes = Collections.synchronizedList(new ArrayList<>()); protected static List ourHeaders = Collections.synchronizedList(new ArrayList<>()); private static SingleQueryCountHolder ourCountHolder; @@ -211,14 +212,12 @@ public abstract class BaseSubscriptionsR4Test extends BaseResourceProviderR4Test @BeforeClass public static void startListenerServer() throws Exception { - int ourListenerPort = PortUtil.findFreePort(); RestfulServer ourListenerRestServer = new RestfulServer(FhirContext.forR4()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; - + ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -228,12 +227,14 @@ public abstract class BaseSubscriptionsR4Test extends BaseResourceProviderR4Test proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java index b00b965a8f5..f40923bea6e 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu2Test.java @@ -2,7 +2,6 @@ package ca.uhn.fhir.jpa.subscription.email; import ca.uhn.fhir.jpa.provider.BaseResourceProviderDstu2Test; import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt; import ca.uhn.fhir.model.dstu2.composite.CodingDt; import ca.uhn.fhir.model.dstu2.resource.Observation; @@ -154,11 +153,11 @@ public class EmailSubscriptionDstu2Test extends BaseResourceProviderDstu2Test { @BeforeClass public static void beforeClass() { - ourListenerPort = RandomServerPortProvider.findFreePort(); - ServerSetup smtp = new ServerSetup(ourListenerPort, null, ServerSetup.PROTOCOL_SMTP); + ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP); smtp.setServerStartupTimeout(2000); ourTestSmtp = new GreenMail(smtp); ourTestSmtp.start(); + ourListenerPort = ourTestSmtp.getSmtp().getPort(); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java index dffb9e12f67..c42849bc9a0 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/EmailSubscriptionDstu3Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test; import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil; import ca.uhn.fhir.jpa.subscription.module.cache.SubscriptionConstants; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.rest.api.MethodOutcome; import com.google.common.collect.Lists; import com.icegreen.greenmail.store.FolderException; @@ -254,13 +253,13 @@ public class EmailSubscriptionDstu3Test extends BaseResourceProviderDstu3Test { @BeforeClass public static void beforeClass() { - ourListenerPort = RandomServerPortProvider.findFreePort(); - ServerSetup smtp = new ServerSetup(ourListenerPort, null, ServerSetup.PROTOCOL_SMTP); + ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP); smtp.setServerStartupTimeout(2000); smtp.setReadTimeout(2000); smtp.setConnectionTimeout(2000); ourTestSmtp = new GreenMail(smtp); ourTestSmtp.start(); + ourListenerPort = ourTestSmtp.getSmtp().getPort(); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/JavaMailEmailSenderTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/JavaMailEmailSenderTest.java index 6c6b8a6cd59..e80450b1ca8 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/JavaMailEmailSenderTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/email/JavaMailEmailSenderTest.java @@ -2,7 +2,6 @@ package ca.uhn.fhir.jpa.subscription.email; import ca.uhn.fhir.jpa.subscription.module.subscriber.email.EmailDetails; import ca.uhn.fhir.jpa.subscription.module.subscriber.email.JavaMailEmailSender; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import com.icegreen.greenmail.util.GreenMail; import com.icegreen.greenmail.util.GreenMailUtil; import com.icegreen.greenmail.util.ServerSetup; @@ -66,11 +65,11 @@ public class JavaMailEmailSenderTest { @BeforeClass public static void beforeClass() { - ourPort = RandomServerPortProvider.findFreePort(); - ServerSetup smtp = new ServerSetup(ourPort, null, ServerSetup.PROTOCOL_SMTP); + ServerSetup smtp = new ServerSetup(0, null, ServerSetup.PROTOCOL_SMTP); smtp.setServerStartupTimeout(2000); ourTestSmtp = new GreenMail(smtp); ourTestSmtp.start(); + ourPort = ourTestSmtp.getSmtp().getPort(); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookActivatesPreExistingSubscriptionsR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookActivatesPreExistingSubscriptionsR4Test.java index 36d567328f3..dfca925197f 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookActivatesPreExistingSubscriptionsR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookActivatesPreExistingSubscriptionsR4Test.java @@ -10,7 +10,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -30,6 +29,8 @@ import java.util.List; import static org.junit.Assert.assertEquals; +import ca.uhn.fhir.util.JettyUtil; + public class RestHookActivatesPreExistingSubscriptionsR4Test extends BaseResourceProviderR4Test { private static final Logger ourLog = LoggerFactory.getLogger(RestHookActivatesPreExistingSubscriptionsR4Test.class); @@ -156,14 +157,12 @@ public class RestHookActivatesPreExistingSubscriptionsR4Test extends BaseResourc @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forR4()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; - + ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -173,12 +172,14 @@ public class RestHookActivatesPreExistingSubscriptionsR4Test extends BaseResourc proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu2Test.java index dba9e1588c1..16f78e840f9 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu2Test.java @@ -20,7 +20,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import ca.uhn.fhir.util.PortUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -38,6 +37,8 @@ import static ca.uhn.fhir.jpa.subscription.resthook.RestHookTestDstu3Test.logAll import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import ca.uhn.fhir.util.JettyUtil; + /** * Test the rest-hook subscriptions */ @@ -300,14 +301,12 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu2()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -317,12 +316,14 @@ public class RestHookTestDstu2Test extends BaseResourceProviderDstu2Test { proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } public static class ObservationListener implements IResourceProvider { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java index 71e38b0d7e4..67ff897d26f 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestDstu3Test.java @@ -17,7 +17,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import ca.uhn.fhir.util.PortUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -38,6 +37,8 @@ import java.util.stream.Collectors; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + /** * Test the rest-hook subscriptions */ @@ -571,16 +572,13 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu3()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; - ourNotificationListenerServer = "http://localhost:" + ourListenerPort + "/fhir/subscription"; ObservationListener obsListener = new ObservationListener(); CommunicationRequestListener crListener = new CommunicationRequestListener(); ourListenerRestServer.setResourceProviders(obsListener, crListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ourNotificationServlet = new NotificationServlet(); ServletContextHandler proxyHandler = new ServletContextHandler(); @@ -594,11 +592,14 @@ public class RestHookTestDstu3Test extends BaseResourceProviderDstu3Test { proxyHandler.addServlet(servletHolder, "/fhir/subscription"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; + ourNotificationListenerServer = "http://localhost:" + ourListenerPort + "/fhir/subscription"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test.java index 2dcb5053f13..5ffbd1a91a6 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test.java @@ -19,7 +19,7 @@ import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -286,14 +286,12 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test extends B @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu2()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -303,12 +301,14 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigDstu2Test extends B proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test.java index 889a723d4eb..0af0a8b289f 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test; import ca.uhn.fhir.jpa.subscription.SubscriptionActivatingInterceptor; import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.Create; import ca.uhn.fhir.rest.annotation.ResourceParam; @@ -14,6 +13,7 @@ import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.util.JettyUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -257,14 +257,12 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test extends B @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = RandomServerPortProvider.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu3()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -274,12 +272,14 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigDstu3Test extends B proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } public static class ObservationListener implements IResourceProvider { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigR4Test.java index 56782fbabef..0659a658ec1 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookTestWithInterceptorRegisteredToDaoConfigR4Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.jpa.dao.DaoConfig; import ca.uhn.fhir.jpa.provider.r4.BaseResourceProviderR4Test; import ca.uhn.fhir.jpa.subscription.SubscriptionTestUtil; -import ca.uhn.fhir.jpa.testutil.RandomServerPortProvider; import ca.uhn.fhir.model.dstu2.valueset.ResourceTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.Create; @@ -14,6 +13,7 @@ import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; +import ca.uhn.fhir.util.JettyUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -266,14 +266,12 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigR4Test extends Base @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = RandomServerPortProvider.findFreePort(); - ourListenerRestServer = new RestfulServer(FhirContext.forR4()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; + ourListenerRestServer = new RestfulServer(FhirContext.forR4()); ObservationListener obsListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(obsListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -283,12 +281,14 @@ public class RestHookTestWithInterceptorRegisteredToDaoConfigR4Test extends Base proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } public static class ObservationListener implements IResourceProvider { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java index 3f685522f6a..73474070793 100755 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java @@ -13,7 +13,6 @@ import ca.uhn.fhir.jpa.subscription.module.interceptor.SubscriptionDebugLogInter import ca.uhn.fhir.jpa.subscription.module.subscriber.ResourceDeliveryMessage; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; import org.apache.commons.lang3.Validate; import org.hl7.fhir.r4.model.IdType; import org.hl7.fhir.r4.model.Observation; @@ -184,7 +183,7 @@ public class RestHookWithInterceptorR4Test extends BaseSubscriptionsR4Test { // Create a subscription CountDownLatch registerLatch = registerLatchHookInterceptor(1, Pointcut.SUBSCRIPTION_AFTER_ACTIVE_SUBSCRIPTION_REGISTERED); Subscription subscription = newSubscription("Observation?status=final", "application/fhir+json"); - subscription.getChannel().setEndpoint("http://localhost:" + PortUtil.findFreePort()); // this better not succeed! + subscription.getChannel().setEndpoint("http://localhost:" + ourListenerPort + "/this/url/does/not/exist"); // this better not succeed! MethodOutcome methodOutcome = ourClient.create().resource(subscription).execute(); subscription.setId(methodOutcome.getId().getIdPart()); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/SubscriptionTriggeringDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/SubscriptionTriggeringDstu3Test.java index e1efd7e5659..b732467f219 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/SubscriptionTriggeringDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/SubscriptionTriggeringDstu3Test.java @@ -15,7 +15,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -34,6 +33,8 @@ import java.util.List; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + /** * Test the rest-hook subscriptions */ @@ -436,15 +437,13 @@ public class SubscriptionTriggeringDstu3Test extends BaseResourceProviderDstu3Te @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu3()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; ObservationListener obsListener = new ObservationListener(); PatientListener ptListener = new PatientListener(); ourListenerRestServer.setResourceProviders(obsListener, ptListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -454,12 +453,14 @@ public class SubscriptionTriggeringDstu3Test extends BaseResourceProviderDstu3Te proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; } @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/testutil/RandomServerPortProvider.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/testutil/RandomServerPortProvider.java deleted file mode 100644 index 37e50dd8db8..00000000000 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/testutil/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.jpa.testutil; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(2000); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java b/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java index 2d85019ec3d..a8a1298a44e 100644 --- a/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java +++ b/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/ExampleServerIT.java @@ -15,6 +15,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; +import ca.uhn.fhir.util.JettyUtil; public class ExampleServerIT { @@ -41,7 +42,7 @@ public class ExampleServerIT { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass @@ -56,9 +57,6 @@ public class ExampleServerIT { ourLog.info("Project base path is: {}", path); - if (ourPort == 0) { - ourPort = RandomServerPortProvider.findFreePort(); - } ourServer = new Server(ourPort); WebAppContext webAppContext = new WebAppContext(); @@ -68,7 +66,8 @@ public class ExampleServerIT { webAppContext.setParentLoaderPriority(true); ourServer.setHandler(webAppContext); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000); diff --git a/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java b/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java deleted file mode 100644 index c8f4e49757d..00000000000 --- a/hapi-fhir-jpaserver-example/src/test/java/ca/uhn/fhir/jpa/demo/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.jpa.demo; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionConfig.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionConfig.java index d63ce2861ea..0ac8fe02be5 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionConfig.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionConfig.java @@ -5,7 +5,7 @@ import ca.uhn.fhir.jpa.model.entity.ModelConfig; import ca.uhn.fhir.jpa.subscription.module.matcher.ISubscriptionMatcher; import ca.uhn.fhir.jpa.subscription.module.matcher.InMemorySubscriptionMatcher; import ca.uhn.fhir.rest.client.api.IGenericClient; -import ca.uhn.fhir.util.PortUtil; +import org.mockito.Mockito; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -16,8 +16,6 @@ import org.springframework.test.context.TestPropertySource; "scheduling_disabled=true" }) public class TestSubscriptionConfig { - private static int ourPort; - private static String ourServerBase; @Bean public ModelConfig modelConfig() { @@ -26,10 +24,7 @@ public class TestSubscriptionConfig { @Bean public IGenericClient fhirClient(FhirContext theFhirContext) { - ourPort = PortUtil.findFreePort(); - ourServerBase = "http://localhost:" + ourPort + "/fhir/context"; - - return theFhirContext.newRestfulGenericClient(ourServerBase); + return Mockito.mock(IGenericClient.class); }; @Bean diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionDstu3Config.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionDstu3Config.java index bc118b2eaff..ecb4028f82f 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionDstu3Config.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/config/TestSubscriptionDstu3Config.java @@ -6,7 +6,6 @@ import org.springframework.context.annotation.*; @Configuration @Import(TestSubscriptionConfig.class) -@ComponentScan(basePackages = {"ca.uhn.fhir.jpa.model.interceptor.executor"}) public class TestSubscriptionDstu3Config extends SubscriptionDstu3Config { @Bean @Primary diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/standalone/BaseBlockingQueueSubscribableChannelDstu3Test.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/standalone/BaseBlockingQueueSubscribableChannelDstu3Test.java index 70ac121d01b..bf1520556b1 100644 --- a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/standalone/BaseBlockingQueueSubscribableChannelDstu3Test.java +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/standalone/BaseBlockingQueueSubscribableChannelDstu3Test.java @@ -24,7 +24,7 @@ import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.SimpleBundleProvider; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import com.google.common.collect.Lists; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -137,14 +137,12 @@ public abstract class BaseBlockingQueueSubscribableChannelDstu3Test extends Base @BeforeClass public static void startListenerServer() throws Exception { - ourListenerPort = PortUtil.findFreePort(); ourListenerRestServer = new RestfulServer(FhirContext.forDstu3()); - ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; ourObservationListener = new ObservationListener(); ourListenerRestServer.setResourceProviders(ourObservationListener); - ourListenerServer = new Server(ourListenerPort); + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -154,7 +152,9 @@ public abstract class BaseBlockingQueueSubscribableChannelDstu3Test extends Base proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + ourListenerServerBase = "http://localhost:" + ourListenerPort + "/fhir/context"; FhirContext context = ourListenerRestServer.getFhirContext(); //Preload structure definitions so the load doesn't happen during the test (first load can be a little slow) context.getValidationSupport().fetchAllStructureDefinitions(context); @@ -162,7 +162,7 @@ public abstract class BaseBlockingQueueSubscribableChannelDstu3Test extends Base @AfterClass public static void stopListenerServer() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); } public static class ObservationListener implements IResourceProvider, IPointcutLatch { diff --git a/hapi-fhir-oauth2/src/test/java/ca/uhn/fhir/rest/server/security/OpenIdConnectBearerTokenServerInterceptorIntegrationTest.java b/hapi-fhir-oauth2/src/test/java/ca/uhn/fhir/rest/server/security/OpenIdConnectBearerTokenServerInterceptorIntegrationTest.java index 4807ba7e623..998eefb19ad 100644 --- a/hapi-fhir-oauth2/src/test/java/ca/uhn/fhir/rest/server/security/OpenIdConnectBearerTokenServerInterceptorIntegrationTest.java +++ b/hapi-fhir-oauth2/src/test/java/ca/uhn/fhir/rest/server/security/OpenIdConnectBearerTokenServerInterceptorIntegrationTest.java @@ -127,7 +127,7 @@ public class OpenIdConnectBearerTokenServerInterceptorIntegrationTest { servlet.registerInterceptor(myInterceptor); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu2_1Test.java index f63e443cfd7..b01f4598d8f 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu2_1Test.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.rest.annotation.Create; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CreateBinaryDstu2_1Test { @@ -123,14 +123,13 @@ public class CreateBinaryDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); BinaryProvider binaryProvider = new BinaryProvider(); @@ -140,7 +139,8 @@ public class CreateBinaryDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateDstu2_1Test.java index aadfd385615..f7ea47be98f 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CreateDstu2_1Test.java @@ -44,7 +44,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CreateDstu2_1Test { @@ -212,14 +212,13 @@ public class CreateDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -230,7 +229,8 @@ public class CreateDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu2_1.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu2_1.java index c4012d1a09f..7712f0fdf32 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu2_1.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu2_1.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CustomTypeServerDstu2_1 { @@ -119,14 +119,13 @@ public class CustomTypeServerDstu2_1 { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -136,7 +135,8 @@ public class CustomTypeServerDstu2_1 { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu2_1Test.java index 2a6289ef899..5dafbc0265a 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu2_1Test.java @@ -21,7 +21,7 @@ import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class DeleteConditionalDstu2_1Test { @@ -78,15 +78,14 @@ public class DeleteConditionalDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -96,7 +95,8 @@ public class DeleteConditionalDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu2_1Test.java index 9fc12013a9c..58bb90a7af5 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu2_1Test.java @@ -22,7 +22,7 @@ import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.api.EncodingEnum; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class FormatParameterDstu2_1Test { @@ -200,15 +200,13 @@ public class FormatParameterDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -219,7 +217,8 @@ public class FormatParameterDstu2_1Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu2_1Test.java index a8581c84c97..c62b768cedf 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu2_1Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.StringParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.VersionUtil; @@ -141,14 +141,13 @@ public class MetadataConformanceDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -159,7 +158,8 @@ public class MetadataConformanceDstu2_1Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2_1Test.java index cda8316c866..a0b3bca2205 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2_1Test.java @@ -36,7 +36,7 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class OperationServerDstu2_1Test { @@ -525,15 +525,14 @@ public class OperationServerDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2_1(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -546,7 +545,8 @@ public class OperationServerDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2_1Test.java index 030ef32baa2..95041b67ec8 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2_1Test.java @@ -370,14 +370,13 @@ public class OperationServerWithSearchParamTypesDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2_1(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -389,7 +388,8 @@ public class OperationServerWithSearchParamTypesDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/PatchDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/PatchDstu2_1Test.java index a1544613622..4875489042f 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/PatchDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/PatchDstu2_1Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.rest.annotation.Patch; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.PatchTypeEnum; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; @@ -33,6 +32,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; +import ca.uhn.fhir.util.JettyUtil; + public class PatchDstu2_1Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(PatchDstu2_1Test.class); @@ -47,14 +48,13 @@ public class PatchDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -66,7 +66,8 @@ public class PatchDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2_1Test.java index ac4aadb6229..60dc94235de 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2_1Test.java @@ -24,7 +24,7 @@ import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ReadDstu2_1Test { @@ -68,14 +68,13 @@ public class ReadDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -86,7 +85,8 @@ public class ReadDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2_1Test.java index bc9477995e6..ef24bc8120e 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2_1Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Count; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchCountParamDstu2_1Test { @@ -112,14 +112,13 @@ public class SearchCountParamDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -131,7 +130,8 @@ public class SearchCountParamDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2_1Test.java index 1b2178e374d..364ab505b9f 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2_1Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchDstu2_1Test { @@ -86,14 +86,13 @@ public class SearchDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -105,7 +104,8 @@ public class SearchDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu2_1Test.java index ae37355793f..b25e2bbd848 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu2_1Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.HasAndListParam; import ca.uhn.fhir.rest.param.HasParam; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchHasParamDstu2_1Test { @@ -67,14 +67,13 @@ public class SearchHasParamDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -86,7 +85,8 @@ public class SearchHasParamDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu2_1Test.java index 9b47d04379d..d830ef3d372 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu2_1Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.param.StringAndListParam; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchPostDstu2_1Test { @@ -200,14 +200,13 @@ public class SearchPostDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -220,7 +219,8 @@ public class SearchPostDstu2_1Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu2_1Test.java index 5199fbe98fc..16ebdc70684 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu2_1Test.java @@ -28,7 +28,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Sort; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchSortDstu2_1Test { @@ -77,14 +77,13 @@ public class SearchSortDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -96,7 +95,8 @@ public class SearchSortDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2_1Test.java index 5ff409cdd57..c7df17150dc 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2_1Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithGenericListDstu2_1Test { @@ -65,7 +65,7 @@ public class SearchWithGenericListDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -73,8 +73,7 @@ public class SearchWithGenericListDstu2_1Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -86,7 +85,8 @@ public class SearchWithGenericListDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu2_1Test.java index 076afef9ca3..ca0e14c1a79 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu2_1Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.rest.annotation.IncludeParam; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithIncludesDstu2_1Test { @@ -63,7 +63,7 @@ public class SearchWithIncludesDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -71,8 +71,7 @@ public class SearchWithIncludesDstu2_1Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -84,7 +83,8 @@ public class SearchWithIncludesDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu2_1Test.java index 43baadbdfcc..bcd50a661f7 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu2_1Test.java @@ -26,7 +26,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithServerAddressStrategyDstu2_1Test { @@ -118,7 +118,7 @@ public class SearchWithServerAddressStrategyDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -126,8 +126,7 @@ public class SearchWithServerAddressStrategyDstu2_1Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -139,7 +138,8 @@ public class SearchWithServerAddressStrategyDstu2_1Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu2_1Test.java index 3347cbf9068..a765e92a757 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu2_1Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.server.exceptions.AuthenticationException; import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ServerExceptionDstu2_1Test { @@ -93,14 +93,13 @@ public class ServerExceptionDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -112,7 +111,8 @@ public class ServerExceptionDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu2_1Test.java index e8db9d2d4d4..b3d6c97245e 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu2_1Test.java @@ -47,7 +47,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ServerMimetypeDstu2_1Test { @@ -348,14 +348,13 @@ public class ServerMimetypeDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -366,7 +365,8 @@ public class ServerMimetypeDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu2_1Test.java index fe652c8ac89..2e13579bd1d 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu2_1Test.java @@ -28,7 +28,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class UnclassifiedServerExceptionDstu2_1Test { @@ -64,14 +64,13 @@ public class UnclassifiedServerExceptionDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -83,7 +82,8 @@ public class UnclassifiedServerExceptionDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2_1Test.java index 93ca275123c..2c3a01ede0d 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2_1Test.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class UpdateDstu2_1Test { @@ -138,14 +138,13 @@ public class UpdateDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -153,7 +152,8 @@ public class UpdateDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2_1Test.java index 98c33504e75..bdbef84f210 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2_1Test.java @@ -25,7 +25,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ValidateDstu2_1Test { @@ -236,14 +236,13 @@ public class ValidateDstu2_1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -253,7 +252,8 @@ public class ValidateDstu2_1Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/apache/ApacheClientIntegrationTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/apache/ApacheClientIntegrationTest.java index 6f192024836..9b1c2f6ef32 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/apache/ApacheClientIntegrationTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/client/apache/ApacheClientIntegrationTest.java @@ -21,7 +21,7 @@ import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.VerboseLoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ApacheClientIntegrationTest { @@ -54,14 +54,13 @@ public class ApacheClientIntegrationTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -72,7 +71,8 @@ public class ApacheClientIntegrationTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourBase = "http://localhost:" + ourPort; } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java index a68e9cc2896..62ff7ccd40d 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryDstu2Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -55,14 +55,13 @@ public class BinaryDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ResourceProvider binaryProvider = new ResourceProvider(); @@ -72,7 +71,8 @@ public class BinaryDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseTest.java index f5e8c9ed6dc..7bb5a1d6c31 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseTest.java @@ -24,7 +24,7 @@ import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -58,14 +58,13 @@ public class BundleTypeInResponseTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -77,7 +76,8 @@ public class BundleTypeInResponseTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CompartmentDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CompartmentDstu2Test.java index fb85ef1e736..308b6c85200 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CompartmentDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CompartmentDstu2Test.java @@ -47,7 +47,7 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -125,13 +125,12 @@ public class CompartmentDstu2Test { @After public void after() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); ourClient.close(); } public void init(IResourceProvider... theProviders) throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -139,7 +138,8 @@ public class CompartmentDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalTest.java index 8a17b5a41ec..0443d3255a8 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalTest.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -137,15 +137,14 @@ public class CreateConditionalTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -155,7 +154,8 @@ public class CreateConditionalTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteDstu2Test.java index 806ab9f18aa..e891d39679d 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteDstu2Test.java @@ -22,7 +22,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class DeleteDstu2Test { @@ -78,15 +78,14 @@ public class DeleteDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -96,7 +95,8 @@ public class DeleteDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ETagServerTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ETagServerTest.java index b0a611a0a76..c73cacf9112 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ETagServerTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ETagServerTest.java @@ -32,7 +32,7 @@ import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -186,14 +186,13 @@ public class ETagServerTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -204,7 +203,8 @@ public class ETagServerTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourConnectionManager = new PoolingHttpClientConnectionManager(50000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterTest.java index 2115df9c049..3f68ddfcd13 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterTest.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.rest.annotation.IncludeParam; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -78,7 +78,7 @@ public class IncludeAndRevincludeParameterTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -86,7 +86,6 @@ public class IncludeAndRevincludeParameterTest { public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = PortUtil.findFreePort(); ourServer = new Server(ourPort); ServletHandler proxyHandler = new ServletHandler(); @@ -96,7 +95,8 @@ public class IncludeAndRevincludeParameterTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeDstu2Test.java index 7ab22ea398b..49b65e98e54 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeDstu2Test.java @@ -258,7 +258,7 @@ public class IncludeDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -266,8 +266,7 @@ public class IncludeDstu2Test { public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -278,7 +277,8 @@ public class IncludeDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerDstu2Test.java index c26b8599514..f661c4a366f 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.Operation; import ca.uhn.fhir.rest.annotation.OperationParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class OperationDuplicateServerDstu2Test { @@ -98,15 +98,14 @@ public class OperationDuplicateServerDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -119,7 +118,8 @@ public class OperationDuplicateServerDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2Test.java index 41dd1628483..44f8000209d 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu2Test.java @@ -41,7 +41,7 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class OperationServerDstu2Test { @@ -529,15 +529,14 @@ public class OperationServerDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -550,7 +549,8 @@ public class OperationServerDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2Test.java index 08bd9b6f10a..d33af62244f 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu2Test.java @@ -315,15 +315,14 @@ public class OperationServerWithSearchParamTypesDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -335,7 +334,8 @@ public class OperationServerWithSearchParamTypesDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java index b7f87f0f2c6..11cf9d1b168 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferTest.java @@ -37,7 +37,7 @@ import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class PreferTest { @@ -163,14 +163,13 @@ public class PreferTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -180,7 +179,8 @@ public class PreferTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java index 86fd1e9b0a1..0a3803381c0 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadDstu2Test.java @@ -197,15 +197,13 @@ public class ReadDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -216,7 +214,8 @@ public class ReadDstu2Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu2Test.java index 200cea9fce6..d6e78af2705 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu2Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchBundleProviderWithNoSizeDstu2Test { @@ -141,14 +141,13 @@ public class SearchBundleProviderWithNoSizeDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -160,7 +159,8 @@ public class SearchBundleProviderWithNoSizeDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2Test.java index 7706784e388..6a9c18b6075 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu2Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Count; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchCountParamDstu2Test { @@ -112,14 +112,13 @@ public class SearchCountParamDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -131,7 +130,8 @@ public class SearchCountParamDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java index ba663e29c59..b697f32f53f 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchDstu2Test.java @@ -15,7 +15,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.*; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -49,6 +48,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SearchDstu2Test { private static CloseableHttpClient ourClient; @@ -426,14 +427,13 @@ public class SearchDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -445,7 +445,8 @@ public class SearchDstu2Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java index 6c0afed1017..a134d9024bd 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchReturningProfiledResourceDstu2Test.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.PatientProfileDstu2; @@ -103,14 +103,13 @@ public class SearchReturningProfiledResourceDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -121,7 +120,8 @@ public class SearchReturningProfiledResourceDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithDstu2BundleTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithDstu2BundleTest.java index 868c81df705..727fda448d4 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithDstu2BundleTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithDstu2BundleTest.java @@ -23,7 +23,7 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu2.resource.Bundle; import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithDstu2BundleTest { @@ -65,15 +65,13 @@ public class SearchWithDstu2BundleTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -84,7 +82,8 @@ public class SearchWithDstu2BundleTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2Test.java index 9ff9ef1410d..5f94977c45b 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithGenericListDstu2Test { @@ -66,14 +66,13 @@ public class SearchWithGenericListDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -85,7 +84,8 @@ public class SearchWithGenericListDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesDstu2Test.java index a9cc0796324..548c1efae1d 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesDstu2Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.model.dstu2.resource.Patient; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; /** @@ -164,15 +164,13 @@ public class ServerFeaturesDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -183,7 +181,8 @@ public class ServerFeaturesDstu2Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerSearchDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerSearchDstu2Test.java index 36d9e0aba94..39e6a705bc1 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerSearchDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ServerSearchDstu2Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.ReferenceParam; import ca.uhn.fhir.rest.param.StringParam; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; @@ -30,6 +29,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ServerSearchDstu2Test { private static CloseableHttpClient ourClient; @@ -126,14 +127,13 @@ public class ServerSearchDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -144,7 +144,8 @@ public class ServerSearchDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SummaryParamDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SummaryParamDstu2Test.java index ce88e6a526c..e0862e466cd 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SummaryParamDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/SummaryParamDstu2Test.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.SummaryEnum; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SummaryParamDstu2Test { @@ -253,14 +253,13 @@ public class SummaryParamDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -269,7 +268,8 @@ public class SummaryParamDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamTest.java index df107b0022d..321d4586d65 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamTest.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Transaction; import ca.uhn.fhir.rest.annotation.TransactionParam; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.client.api.IGenericClient; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class TransactionWithBundleResourceParamTest { @@ -216,14 +216,13 @@ public class TransactionWithBundleResourceParamTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyProvider patientProvider = new DummyProvider(); RestfulServer server = new RestfulServer(ourCtx); @@ -237,7 +236,8 @@ public class TransactionWithBundleResourceParamTest { proxyHandler.addServlet(handler, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(500000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java index 2184c41c48b..b4158643b95 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java @@ -44,7 +44,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class UpdateDstu2Test { @@ -181,15 +181,14 @@ public class UpdateDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -199,7 +198,8 @@ public class UpdateDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2Test.java index 53eff8cd38e..0cc3ef622c7 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.*; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ValidateDstu2Test { @@ -149,14 +149,13 @@ public class ValidateDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -166,7 +165,8 @@ public class ValidateDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java index bd7275c374b..2a9fb024f3e 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorUserDataMapDstu2Test.java @@ -43,7 +43,7 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class InterceptorUserDataMapDstu2Test { @@ -116,14 +116,13 @@ public class InterceptorUserDataMapDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); servlet = new RestfulServer(ourCtx); @@ -134,7 +133,8 @@ public class InterceptorUserDataMapDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptorDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptorDstu2Test.java index cfe9c5f1176..dadc21afc9c 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptorDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/LoggingInterceptorDstu2Test.java @@ -16,7 +16,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -49,6 +48,8 @@ import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + public class LoggingInterceptorDstu2Test { private static CloseableHttpClient ourClient; @@ -358,14 +359,13 @@ public class LoggingInterceptorDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); servlet = new RestfulServer(ourCtx); @@ -376,7 +376,8 @@ public class LoggingInterceptorDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerActionInterceptorTest.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerActionInterceptorTest.java index bef509d86bd..3cf47a723a9 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerActionInterceptorTest.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerActionInterceptorTest.java @@ -15,7 +15,6 @@ import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetails; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; @@ -44,6 +43,8 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + public class ServerActionInterceptorTest { private static CloseableHttpClient ourClient; @@ -54,7 +55,8 @@ public class ServerActionInterceptorTest { private static IGenericClient ourFhirClient; @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -178,8 +180,7 @@ public class ServerActionInterceptorTest { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -190,7 +191,8 @@ public class ServerActionInterceptorTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/context/ContextScanningDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/context/ContextScanningDstu3Test.java index 0f5c1298ac0..159dd9f15b4 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/context/ContextScanningDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/context/ContextScanningDstu3Test.java @@ -21,7 +21,7 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ContextScanningDstu3Test { @@ -137,14 +137,13 @@ public class ContextScanningDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -152,7 +151,8 @@ public class ContextScanningDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu3Test.java index 03f3c623277..ca2091c5b18 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateBinaryDstu3Test.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.rest.annotation.Create; import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CreateBinaryDstu3Test { @@ -123,14 +123,13 @@ public class CreateBinaryDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); BinaryProvider binaryProvider = new BinaryProvider(); @@ -140,7 +139,8 @@ public class CreateBinaryDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java index 63913d53572..8d088c26c5d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CreateDstu3Test.java @@ -43,7 +43,7 @@ import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CreateDstu3Test { @@ -211,14 +211,13 @@ public class CreateDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -229,7 +228,8 @@ public class CreateDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu3.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu3.java index b4e83fa8a59..76ada7a3efd 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu3.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerDstu3.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CustomTypeServerDstu3 { @@ -119,14 +119,13 @@ public class CustomTypeServerDstu3 { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -136,7 +135,8 @@ public class CustomTypeServerDstu3 { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu3Test.java index 5faeba9a7a0..7948bf444d6 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalDstu3Test.java @@ -21,7 +21,7 @@ import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class DeleteConditionalDstu3Test { @@ -78,15 +78,14 @@ public class DeleteConditionalDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -96,7 +95,8 @@ public class DeleteConditionalDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu3Test.java index 900d3a4ca6e..078f36b4e93 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/FormatParameterDstu3Test.java @@ -22,7 +22,7 @@ import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.api.EncodingEnum; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class FormatParameterDstu3Test { @@ -200,15 +200,13 @@ public class FormatParameterDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -219,7 +217,8 @@ public class FormatParameterDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/InterceptorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/InterceptorDstu3Test.java index fabfa4cc0ee..e773554c3e6 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/InterceptorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/InterceptorDstu3Test.java @@ -13,7 +13,6 @@ import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetai import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter; import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -46,6 +45,8 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + public class InterceptorDstu3Test { private static CloseableHttpClient ourClient; @@ -285,14 +286,13 @@ public class InterceptorDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -302,7 +302,8 @@ public class InterceptorDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataCapabilityStatementDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataCapabilityStatementDstu3Test.java index 20e24502eba..7d5ca4fafc2 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataCapabilityStatementDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataCapabilityStatementDstu3Test.java @@ -8,7 +8,6 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.StringParam; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.VersionUtil; import org.apache.commons.io.IOUtils; @@ -37,6 +36,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class MetadataCapabilityStatementDstu3Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MetadataCapabilityStatementDstu3Test.class); @@ -183,14 +184,13 @@ public class MetadataCapabilityStatementDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -203,7 +203,8 @@ public class MetadataCapabilityStatementDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu3Test.java index f01ddb5bd4c..bba039d3e0e 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/MetadataConformanceDstu3Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.StringParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.VersionUtil; @@ -140,14 +140,13 @@ public class MetadataConformanceDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -158,7 +157,8 @@ public class MetadataConformanceDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu3Test.java index 295715f40dc..b2dc615efd0 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerDstu3Test.java @@ -36,7 +36,7 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class OperationServerDstu3Test { @@ -525,15 +525,14 @@ public class OperationServerDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu3(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -546,7 +545,8 @@ public class OperationServerDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu3Test.java index 9566989b0a0..bce481ef04e 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/OperationServerWithSearchParamTypesDstu3Test.java @@ -370,14 +370,13 @@ public class OperationServerWithSearchParamTypesDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu3(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -389,7 +388,8 @@ public class OperationServerWithSearchParamTypesDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/PatchDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/PatchDstu3Test.java index dffea5cdc41..35d95bdf5e1 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/PatchDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/PatchDstu3Test.java @@ -24,7 +24,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.PatchTypeEnum; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class PatchDstu3Test { @@ -130,14 +130,13 @@ public class PatchDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -149,7 +148,8 @@ public class PatchDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ReadDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ReadDstu3Test.java index a5fc7228dce..b6499a98bff 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ReadDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ReadDstu3Test.java @@ -109,14 +109,13 @@ public class ReadDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -127,7 +126,8 @@ public class ReadDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu3Test.java index 973db95cccf..893090c61c2 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeDstu3Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchBundleProviderWithNoSizeDstu3Test { @@ -141,14 +141,13 @@ public class SearchBundleProviderWithNoSizeDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -160,7 +159,8 @@ public class SearchBundleProviderWithNoSizeDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu3Test.java index 45dc9bb4f5a..d811c4203ca 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchCountParamDstu3Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Count; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchCountParamDstu3Test { @@ -112,14 +112,13 @@ public class SearchCountParamDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -131,7 +130,8 @@ public class SearchCountParamDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDefaultMethodDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDefaultMethodDstu3Test.java index e2fb8420e1c..0dca58b4561 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDefaultMethodDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDefaultMethodDstu3Test.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.RawParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.StringAndListParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchDefaultMethodDstu3Test { @@ -182,14 +182,13 @@ public class SearchDefaultMethodDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -201,7 +200,8 @@ public class SearchDefaultMethodDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDstu3Test.java index 351328a4c89..7602c2c996d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchDstu3Test.java @@ -309,14 +309,13 @@ public class SearchDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -329,7 +328,8 @@ public class SearchDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu3Test.java index 711fccb3b6f..e949625105d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamDstu3Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.HasAndListParam; import ca.uhn.fhir.rest.param.HasParam; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchHasParamDstu3Test { @@ -68,14 +68,13 @@ public class SearchHasParamDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -87,7 +86,8 @@ public class SearchHasParamDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu3Test.java index a3ebc15b981..77cd202a86f 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchPostDstu3Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.param.StringAndListParam; import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; import ca.uhn.fhir.rest.server.interceptor.InterceptorAdapter; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchPostDstu3Test { @@ -199,14 +199,13 @@ public class SearchPostDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -219,7 +218,8 @@ public class SearchPostDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu3Test.java index 7763d734454..8d44fa83a10 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchSortDstu3Test.java @@ -28,7 +28,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Sort; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchSortDstu3Test { @@ -77,14 +77,13 @@ public class SearchSortDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -96,7 +95,8 @@ public class SearchSortDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu3Test.java index 9b465a41914..8854a8fbc51 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListDstu3Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithGenericListDstu3Test { @@ -65,7 +65,7 @@ public class SearchWithGenericListDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -73,8 +73,7 @@ public class SearchWithGenericListDstu3Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -86,7 +85,8 @@ public class SearchWithGenericListDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu3Test.java index 195f7b8cc98..2b509736994 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithIncludesDstu3Test.java @@ -34,7 +34,7 @@ import ca.uhn.fhir.rest.annotation.IncludeParam; import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithIncludesDstu3Test { @@ -67,7 +67,7 @@ public class SearchWithIncludesDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -75,8 +75,7 @@ public class SearchWithIncludesDstu3Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -88,7 +87,8 @@ public class SearchWithIncludesDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu3Test.java index 0a71d857ed4..3e32b2772d4 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/SearchWithServerAddressStrategyDstu3Test.java @@ -26,7 +26,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithServerAddressStrategyDstu3Test { @@ -118,7 +118,7 @@ public class SearchWithServerAddressStrategyDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -126,8 +126,7 @@ public class SearchWithServerAddressStrategyDstu3Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -139,7 +138,8 @@ public class SearchWithServerAddressStrategyDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu3Test.java index 0383d6cd320..26a33a369c6 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerExceptionDstu3Test.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.server.exceptions.AuthenticationException; import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -42,6 +41,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.contains; +import ca.uhn.fhir.util.JettyUtil; + public class ServerExceptionDstu3Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerExceptionDstu3Test.class); @@ -158,14 +159,13 @@ public class ServerExceptionDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -177,7 +177,8 @@ public class ServerExceptionDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu3Test.java index c1b49d09882..7c392cc1504 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeDstu3Test.java @@ -47,7 +47,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ServerMimetypeDstu3Test { @@ -347,14 +347,13 @@ public class ServerMimetypeDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -365,7 +364,8 @@ public class ServerMimetypeDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu3Test.java index df5272f485c..29bb11de63b 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UnclassifiedServerExceptionDstu3Test.java @@ -28,7 +28,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.server.exceptions.UnclassifiedServerFailureException; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class UnclassifiedServerExceptionDstu3Test { @@ -64,14 +64,13 @@ public class UnclassifiedServerExceptionDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -83,7 +82,8 @@ public class UnclassifiedServerExceptionDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java index f8d14c54dba..c89f64acda2 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class UpdateDstu3Test { @@ -138,14 +138,13 @@ public class UpdateDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -153,7 +152,8 @@ public class UpdateDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu3Test.java index 00027a9c49e..90989bbe7ad 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/ValidateDstu3Test.java @@ -25,7 +25,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ValidateDstu3Test { @@ -233,14 +233,13 @@ public class ValidateDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -250,7 +249,8 @@ public class ValidateDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/AuthorizationInterceptorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/AuthorizationInterceptorDstu3Test.java index 772d9f3fa91..1c118ce8000 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/AuthorizationInterceptorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/AuthorizationInterceptorDstu3Test.java @@ -20,7 +20,6 @@ import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor.ActionRequestDetai import ca.uhn.fhir.rest.server.interceptor.auth.*; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; import ca.uhn.fhir.rest.server.tenant.UrlBaseTenantIdentificationStrategy; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -55,6 +54,8 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class AuthorizationInterceptorDstu3Test { private static final String ERR403 = "{\"resourceType\":\"OperationOutcome\",\"issue\":[{\"severity\":\"error\",\"code\":\"processing\",\"diagnostics\":\"Access denied by default policy (no applicable rules)\"}]}"; @@ -3649,15 +3650,13 @@ public class AuthorizationInterceptorDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patProvider = new DummyPatientResourceProvider(); DummyObservationResourceProvider obsProv = new DummyObservationResourceProvider(); @@ -3677,7 +3676,8 @@ public class AuthorizationInterceptorDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/BanUnsupprtedHttpMethodsInterceptorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/BanUnsupprtedHttpMethodsInterceptorDstu3Test.java index 9e6a300ea69..a6a026df4de 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/BanUnsupprtedHttpMethodsInterceptorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/BanUnsupprtedHttpMethodsInterceptorDstu3Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class BanUnsupprtedHttpMethodsInterceptorDstu3Test { @@ -127,14 +127,13 @@ public class BanUnsupprtedHttpMethodsInterceptorDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); servlet = new RestfulServer(ourCtx); @@ -145,7 +144,8 @@ public class BanUnsupprtedHttpMethodsInterceptorDstu3Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/CorsInterceptorDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/CorsInterceptorDstu3Test.java index 8612897dfe7..d5992e21e3c 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/CorsInterceptorDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/interceptor/CorsInterceptorDstu3Test.java @@ -36,7 +36,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CorsInterceptorDstu3Test { @@ -139,7 +139,7 @@ public class CorsInterceptorDstu3Test { } public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); ourClient.close(); } @@ -156,8 +156,7 @@ public class CorsInterceptorDstu3Test { builder.setConnectionManager(connectionManager); ourClient = builder.build(); - int port = PortUtil.findFreePort(); - ourServer = new Server(port); + ourServer = new Server(0); RestfulServer restServer = new RestfulServer(ourCtx); restServer.setResourceProviders(new DummyPatientResourceProvider()); @@ -189,7 +188,8 @@ public class CorsInterceptorDstu3Test { ourServer.setHandler(contexts); ourServer.setHandler(ch); - ourServer.start(); + JettyUtil.startServer(ourServer); + int port = JettyUtil.getPortForStartedServer(ourServer); ourBaseUri = "http://localhost:" + port + "/rootctx/rcp2/fhirctx/fcp2"; } diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryHl7OrgDstu2Test.java index 249613807c5..e1bc85bf77a 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BinaryHl7OrgDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; /** * Created by dsotnikov on 2/25/2014. @@ -149,13 +149,12 @@ public class BinaryHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ResourceProvider patientProvider = new ResourceProvider(); @@ -165,7 +164,8 @@ public class BinaryHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseHl7OrgTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseHl7OrgTest.java index eb4cb0ddee5..82ac6c0757a 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseHl7OrgTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/BundleTypeInResponseHl7OrgTest.java @@ -24,7 +24,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class BundleTypeInResponseHl7OrgTest { @@ -52,13 +52,12 @@ public class BundleTypeInResponseHl7OrgTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -70,7 +69,8 @@ public class BundleTypeInResponseHl7OrgTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalHl7OrgTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalHl7OrgTest.java index 9180d03aaee..a9f35e63c29 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalHl7OrgTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/CreateConditionalHl7OrgTest.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; /** * Created by dsotnikov on 2/25/2014. @@ -135,14 +135,13 @@ public class CreateConditionalHl7OrgTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -152,7 +151,8 @@ public class CreateConditionalHl7OrgTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalHl7OrgTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalHl7OrgTest.java index c874aa8762b..562ee1d1b23 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalHl7OrgTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalHl7OrgTest.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.rest.annotation.ConditionalUrlParam; import ca.uhn.fhir.rest.annotation.Delete; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; /** * Created by dsotnikov on 2/25/2014. @@ -80,14 +80,13 @@ public class DeleteConditionalHl7OrgTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -97,7 +96,8 @@ public class DeleteConditionalHl7OrgTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterHl7OrgTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterHl7OrgTest.java index b3315b76cce..2a7ac8f9f54 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterHl7OrgTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeAndRevincludeParameterHl7OrgTest.java @@ -25,7 +25,7 @@ import ca.uhn.fhir.context.api.BundleInclusionRule; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.rest.annotation.IncludeParam; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class IncludeAndRevincludeParameterHl7OrgTest { @@ -70,15 +70,14 @@ public class IncludeAndRevincludeParameterHl7OrgTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2Hl7Org(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -87,7 +86,8 @@ public class IncludeAndRevincludeParameterHl7OrgTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeHl7OrgDstu2Test.java index 1f8187af980..33d87ca2efa 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/IncludeHl7OrgDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.util.ElementUtil; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class IncludeHl7OrgDstu2Test { @@ -234,15 +234,14 @@ public class IncludeHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2Hl7Org(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -253,7 +252,8 @@ public class IncludeHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerHl7OrgDstu2Test.java index 11b998fef9e..565faf05f5b 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationDuplicateServerHl7OrgDstu2Test.java @@ -30,7 +30,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Operation; import ca.uhn.fhir.rest.annotation.OperationParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class OperationDuplicateServerHl7OrgDstu2Test { private static CloseableHttpClient ourClient; @@ -79,14 +79,13 @@ public class OperationDuplicateServerHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2Hl7Org(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -99,7 +98,8 @@ public class OperationDuplicateServerHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerHl7OrgTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerHl7OrgTest.java index d397d67c1d2..04e683722c3 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerHl7OrgTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/OperationServerHl7OrgTest.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class OperationServerHl7OrgTest { private static CloseableHttpClient ourClient; @@ -312,14 +312,13 @@ public class OperationServerHl7OrgTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forDstu2Hl7Org(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -332,7 +331,8 @@ public class OperationServerHl7OrgTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferHl7OrgDstu2Test.java index 95763dc2325..bbccfa89930 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/PreferHl7OrgDstu2Test.java @@ -23,7 +23,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; /** * Created by dsotnikov on 2/25/2014. @@ -64,14 +64,13 @@ public class PreferHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -81,7 +80,8 @@ public class PreferHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadHl7OrgDstu2Test.java index 18e1cea8606..cdc2a167199 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ReadHl7OrgDstu2Test.java @@ -23,7 +23,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Read; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class ReadHl7OrgDstu2Test { @@ -64,14 +64,12 @@ public class ReadHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -82,7 +80,8 @@ public class ReadHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchHl7OrgDstu2Test.java index 7eb68475f53..ae23fa4c330 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchHl7OrgDstu2Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; -import ca.uhn.fhir.util.PortUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -28,6 +27,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SearchHl7OrgDstu2Test { private static CloseableHttpClient ourClient; @@ -101,13 +102,12 @@ public class SearchHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -118,7 +118,8 @@ public class SearchHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListHl7OrgDstu2Test.java index 6d85e8c89c7..c1cdc9050fa 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithGenericListHl7OrgDstu2Test.java @@ -29,7 +29,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.RequiredParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchWithGenericListHl7OrgDstu2Test { @@ -65,7 +65,7 @@ public class SearchWithGenericListHl7OrgDstu2Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -73,8 +73,7 @@ public class SearchWithGenericListHl7OrgDstu2Test { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -86,7 +85,8 @@ public class SearchWithGenericListHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithHl7OrgDstu2BundleTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithHl7OrgDstu2BundleTest.java index e4bfbe03297..38db8fb816f 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithHl7OrgDstu2BundleTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/SearchWithHl7OrgDstu2BundleTest.java @@ -22,7 +22,7 @@ import org.junit.Test; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class SearchWithHl7OrgDstu2BundleTest { @@ -62,14 +62,12 @@ public class SearchWithHl7OrgDstu2BundleTest { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -80,7 +78,8 @@ public class SearchWithHl7OrgDstu2BundleTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamHl7OrgDstu2Test.java index fd9022eacc2..946e3c2353e 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/TransactionWithBundleResourceParamHl7OrgDstu2Test.java @@ -28,7 +28,7 @@ import ca.uhn.fhir.rest.annotation.TransactionParam; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class TransactionWithBundleResourceParamHl7OrgDstu2Test { @@ -214,13 +214,12 @@ public class TransactionWithBundleResourceParamHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyProvider patientProvider = new DummyProvider(); RestfulServer server = new RestfulServer(ourCtx); @@ -234,7 +233,8 @@ public class TransactionWithBundleResourceParamHl7OrgDstu2Test { proxyHandler.addServlet(handler, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(500000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateConditionalHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateConditionalHl7OrgDstu2Test.java index 0e56b9f729f..e280428f1a1 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateConditionalHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateConditionalHl7OrgDstu2Test.java @@ -30,7 +30,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; public class UpdateConditionalHl7OrgDstu2Test { private static CloseableHttpClient ourClient; @@ -131,14 +131,13 @@ public class UpdateConditionalHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -148,7 +147,8 @@ public class UpdateConditionalHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateHl7OrgDstu2Test.java index 29d4b5a206f..1fd2ccaca74 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/rest/server/ValidateHl7OrgDstu2Test.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.rest.annotation.ResourceParam; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; /** * Created by dsotnikov on 2/25/2014. @@ -147,13 +147,12 @@ public class ValidateHl7OrgDstu2Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -163,7 +162,8 @@ public class ValidateHl7OrgDstu2Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java index fc690862fdd..d2d0f6c6d33 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientHeadersR4Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.RequestFormatParamStyleEnum; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; -import ca.uhn.fhir.util.RandomServerPortProvider; import ca.uhn.fhir.util.TestUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -28,6 +27,8 @@ import java.util.List; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ClientHeadersR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ClientHeadersR4Test.class); private static FhirContext ourCtx; @@ -164,7 +165,8 @@ public class ClientHeadersR4Test { } @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -172,13 +174,11 @@ public class ClientHeadersR4Test { public static void beforeClass() throws Exception { ourCtx = FhirContext.forR4(); - int myPort = RandomServerPortProvider.findFreePort(); - ourServer = new Server(myPort); + ourServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); - ourServerBase = "http://localhost:" + myPort + "/fhir/context"; ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ServletHolder servletHolder = new ServletHolder(); @@ -186,7 +186,9 @@ public class ClientHeadersR4Test { proxyHandler.addServlet(servletHolder, "/fhir/context/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int myPort = JettyUtil.getPortForStartedServer(ourServer); + ourServerBase = "http://localhost:" + myPort + "/fhir/context"; } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientIntegrationTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientIntegrationTest.java index f4d523c47bb..bbf1056a099 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientIntegrationTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/ClientIntegrationTest.java @@ -25,20 +25,17 @@ import ca.uhn.fhir.rest.client.api.IBasicClient; import ca.uhn.fhir.rest.client.impl.HttpBasicAuthInterceptor; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ClientIntegrationTest { - - private int myPort; private Server myServer; private MyPatientResourceProvider myPatientProvider; private static FhirContext ourCtx = FhirContext.forR4(); @Before public void before() { - myPort = PortUtil.findFreePort(); - myServer = new Server(myPort); + myServer = new Server(0); myPatientProvider = new MyPatientResourceProvider(); @@ -55,7 +52,8 @@ public class ClientIntegrationTest { @Test public void testClientSecurity() throws Exception { - myServer.start(); + JettyUtil.startServer(myServer); + int myPort = JettyUtil.getPortForStartedServer(myServer); FhirContext ctx = FhirContext.forR4(); @@ -78,7 +76,7 @@ public class ClientIntegrationTest { @After public void after() throws Exception { - myServer.stop(); + JettyUtil.closeServer(myServer); } public static class MyPatientResourceProvider implements IResourceProvider { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/CompressOutgoingContentInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/CompressOutgoingContentInterceptorTest.java index bc1914d01f9..46e5ca6d6dd 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/CompressOutgoingContentInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/CompressOutgoingContentInterceptorTest.java @@ -11,7 +11,6 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; @@ -23,6 +22,8 @@ import javax.servlet.http.HttpServletRequest; import static org.junit.Assert.assertEquals; +import ca.uhn.fhir.util.JettyUtil; + public class CompressOutgoingContentInterceptorTest { private static IGenericClient ourClient; @@ -56,14 +57,13 @@ public class CompressOutgoingContentInterceptorTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -75,7 +75,8 @@ public class CompressOutgoingContentInterceptorTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); } public static class DummyPatientResourceProvider implements IResourceProvider { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/HttpProxyTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/HttpProxyTest.java index 948dce39cef..4013b782752 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/HttpProxyTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/HttpProxyTest.java @@ -23,7 +23,7 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class HttpProxyTest { @@ -36,8 +36,7 @@ public class HttpProxyTest { @SuppressWarnings("serial") @Test public void testProxiedRequest() throws Exception { - int port = PortUtil.findFreePort(); - Server server = new Server(port); + Server server = new Server(0); myFirstRequest = true; myAuthHeader = null; @@ -74,7 +73,8 @@ public class HttpProxyTest { server.setHandler(contexts); server.setHandler(ch); - server.start(); + JettyUtil.startServer(server); + int port = JettyUtil.getPortForStartedServer(server); try { // final String authUser = "username"; @@ -107,7 +107,7 @@ public class HttpProxyTest { assertEquals("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", myAuthHeader); } finally { - server.stop(); + JettyUtil.closeServer(server); } } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/LoggingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/LoggingInterceptorTest.java index 1fc31bb48b3..752c4ec99df 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/LoggingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/client/LoggingInterceptorTest.java @@ -8,7 +8,6 @@ import ca.uhn.fhir.rest.client.api.ServerValidationModeEnum; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; @@ -31,6 +30,8 @@ import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + public class LoggingInterceptorTest { private static FhirContext ourCtx = FhirContext.forR4(); @@ -124,7 +125,7 @@ public class LoggingInterceptorTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -139,8 +140,7 @@ public class LoggingInterceptorTest { context.reset(); configurator.doConfigure(conf); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyProvider patientProvider = new DummyProvider(); @@ -150,7 +150,8 @@ public class LoggingInterceptorTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/DateRangeParamR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/DateRangeParamR4Test.java index f756ff0de84..57dd4d2a934 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/DateRangeParamR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/param/DateRangeParamR4Test.java @@ -11,7 +11,6 @@ import ca.uhn.fhir.rest.api.QualifiedParamList; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import com.google.common.testing.EqualsTester; @@ -49,6 +48,8 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import ca.uhn.fhir.util.JettyUtil; + public class DateRangeParamR4Test { private static final SimpleDateFormat ourFmtLower; @@ -436,14 +437,13 @@ public class DateRangeParamR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -455,7 +455,8 @@ public class DateRangeParamR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java index 3779995b9fa..363b15178a5 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/BinaryServerR4Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -34,6 +33,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class BinaryServerR4Test { private static CloseableHttpClient ourClient; private static FhirContext ourCtx = FhirContext.forR4(); @@ -226,14 +227,13 @@ public class BinaryServerR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); BinaryProvider binaryProvider = new BinaryProvider(); @@ -243,7 +243,8 @@ public class BinaryServerR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateR4Test.java index 5de6d3c9c5d..4166daa96f6 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CreateR4Test.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.api.PreferReturnEnum; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -37,6 +36,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class CreateR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(CreateR4Test.class); public static OperationOutcome ourReturnOo; @@ -377,14 +378,13 @@ public class CreateR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProviderCreate patientProviderCreate = new PatientProviderCreate(); PatientProviderRead patientProviderRead = new PatientProviderRead(); @@ -397,7 +397,8 @@ public class CreateR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerR4.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerR4.java index db560aa7835..b166ec3f44a 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerR4.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/CustomTypeServerR4.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class CustomTypeServerR4 { @@ -119,14 +119,13 @@ public class CustomTypeServerR4 { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -136,7 +135,8 @@ public class CustomTypeServerR4 { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalR4Test.java index 52232ad16db..e3bf9ff57c2 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/DeleteConditionalR4Test.java @@ -10,7 +10,6 @@ import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.gclient.IDelete; import ca.uhn.fhir.rest.gclient.IDeleteWithQuery; import ca.uhn.fhir.rest.gclient.IDeleteWithQueryTyped; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.lang3.Validate; import org.apache.http.impl.client.CloseableHttpClient; @@ -33,6 +32,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import ca.uhn.fhir.util.JettyUtil; + public class DeleteConditionalR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DeleteConditionalR4Test.class); private static CloseableHttpClient ourClient; @@ -89,14 +90,13 @@ public class DeleteConditionalR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -106,7 +106,8 @@ public class DeleteConditionalR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java index 0fe52e59d86..c246706aaa8 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; -import ca.uhn.fhir.util.PortUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; import org.apache.http.Header; @@ -35,6 +34,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ETagServerR4Test { private static CloseableHttpClient ourClient; @@ -172,13 +173,12 @@ public class ETagServerR4Test { @AfterClass public static void afterClass() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -188,7 +188,8 @@ public class ETagServerR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); ourConnectionManager = new PoolingHttpClientConnectionManager(50000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ElementsParamR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ElementsParamR4Test.java index 0682a563df9..be718728a17 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ElementsParamR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ElementsParamR4Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Include; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.EncodingEnum; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -30,6 +29,8 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import ca.uhn.fhir.util.JettyUtil; + public class ElementsParamR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ElementsParamR4Test.class); @@ -479,14 +480,13 @@ public class ElementsParamR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); ourServlet = new RestfulServer(ourCtx); @@ -498,7 +498,8 @@ public class ElementsParamR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4ProviderTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4ProviderTest.java index ef1ff5b4c72..46630adf900 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4ProviderTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4ProviderTest.java @@ -6,7 +6,6 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; @@ -37,6 +36,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class GraphQLR4ProviderTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(GraphQLR4ProviderTest.class); @@ -162,14 +163,13 @@ public class GraphQLR4ProviderTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -182,7 +182,8 @@ public class GraphQLR4ProviderTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4RawTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4RawTest.java index 012526f0638..45b5b7d1581 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4RawTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/GraphQLR4RawTest.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; @@ -37,6 +36,8 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class GraphQLR4RawTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(GraphQLR4RawTest.class); @@ -51,14 +52,13 @@ public class GraphQLR4RawTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -70,7 +70,8 @@ public class GraphQLR4RawTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/HistoryR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/HistoryR4Test.java index 895a99183fd..5cd5b907acf 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/HistoryR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/HistoryR4Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.ParamPrefixEnum; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -32,6 +31,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class HistoryR4Test { private static final Logger ourLog = LoggerFactory.getLogger(HistoryR4Test.class); @@ -173,14 +174,13 @@ public class HistoryR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPlainProvider plainProvider = new DummyPlainProvider(); DummyResourceProvider patientProvider = new DummyResourceProvider(); @@ -191,7 +191,8 @@ public class HistoryR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/IncludeTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/IncludeTest.java index a821eda93b0..8e722e5c247 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/IncludeTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/IncludeTest.java @@ -243,7 +243,7 @@ public class IncludeTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -251,8 +251,7 @@ public class IncludeTest { public static void beforeClass() throws Exception { ourCtx = FhirContext.forR4(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -263,7 +262,8 @@ public class IncludeTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/MultitenancyR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/MultitenancyR4Test.java index 4081e00161b..976a11745e7 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/MultitenancyR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/MultitenancyR4Test.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.tenant.UrlBaseTenantIdentificationStrategy; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; @@ -36,6 +35,8 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class MultitenancyR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MultitenancyR4Test.class); @@ -56,8 +57,7 @@ public class MultitenancyR4Test { @Test public void testUrlBaseStrategy() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -71,7 +71,8 @@ public class MultitenancyR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/TENANT2/Patient?identifier=foo%7Cbar"); CloseableHttpResponse status = ourClient.execute(httpGet); @@ -109,7 +110,7 @@ public class MultitenancyR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationGenericServerR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationGenericServerR4Test.java index f77882c6f22..00cc4fc4a58 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationGenericServerR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationGenericServerR4Test.java @@ -3,7 +3,6 @@ package ca.uhn.fhir.rest.server; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.CloseableHttpResponse; @@ -35,6 +34,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import ca.uhn.fhir.util.JettyUtil; + public class OperationGenericServerR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OperationGenericServerR4Test.class); private static CloseableHttpClient ourClient; @@ -270,15 +271,14 @@ public class OperationGenericServerR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forR4(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -291,7 +291,8 @@ public class OperationGenericServerR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationServerR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationServerR4Test.java index b07bdfba4a2..5ef63373850 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationServerR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/OperationServerR4Test.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -41,6 +40,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class OperationServerR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(OperationServerR4Test.class); private static final String TEXT_HTML = "text/html"; @@ -821,15 +822,14 @@ public class OperationServerR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { ourCtx = FhirContext.forR4(); - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -842,7 +842,8 @@ public class OperationServerR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingUsingNamedPagesR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingUsingNamedPagesR4Test.java index 2249738da8e..6cea1089542 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingUsingNamedPagesR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PagingUsingNamedPagesR4Test.java @@ -5,7 +5,6 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.server.IBundleProvider; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import org.apache.commons.io.IOUtils; @@ -41,6 +40,8 @@ import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import ca.uhn.fhir.util.JettyUtil; + public class PagingUsingNamedPagesR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(PagingUsingNamedPagesR4Test.class); @@ -193,14 +194,13 @@ public class PagingUsingNamedPagesR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -213,7 +213,8 @@ public class PagingUsingNamedPagesR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PlainProviderR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PlainProviderR4Test.java index 61dcd0b341e..8ce71201d1d 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PlainProviderR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/PlainProviderR4Test.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Count; import ca.uhn.fhir.rest.annotation.*; import ca.uhn.fhir.rest.param.TokenParam; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -33,6 +32,8 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class PlainProviderR4Test { private static FhirContext ourCtx = FhirContext.forR4(); @@ -44,13 +45,12 @@ public class PlainProviderR4Test { @After public void after() throws Exception { - myServer.stop(); + JettyUtil.closeServer(myServer); } @Before public void before() throws Exception { - myPort = PortUtil.findFreePort(); - myServer = new Server(myPort); + myServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); ServletHolder servletHolder = new ServletHolder(); @@ -71,7 +71,8 @@ public class PlainProviderR4Test { public void testGlobalHistory() throws Exception { GlobalHistoryProvider provider = new GlobalHistoryProvider(); myRestfulServer.setProviders(provider); - myServer.start(); + JettyUtil.startServer(myServer); + myPort = JettyUtil.getPortForStartedServer(myServer); String baseUri = "http://localhost:" + myPort + "/fhir/context"; HttpResponse status = myClient.execute(new HttpGet(baseUri + "/_history?_since=2012-01-02T00%3A01%3A02&_count=12")); @@ -109,7 +110,8 @@ public class PlainProviderR4Test { public void testGlobalHistoryNoParams() throws Exception { GlobalHistoryProvider provider = new GlobalHistoryProvider(); myRestfulServer.setProviders(provider); - myServer.start(); + JettyUtil.startServer(myServer); + myPort = JettyUtil.getPortForStartedServer(myServer); String baseUri = "http://localhost:" + myPort + "/fhir/context"; CloseableHttpResponse status = myClient.execute(new HttpGet(baseUri + "/_history")); @@ -126,7 +128,8 @@ public class PlainProviderR4Test { @Test public void testSearchByParamIdentifier() throws Exception { myRestfulServer.setProviders(new SearchProvider()); - myServer.start(); + JettyUtil.startServer(myServer); + myPort = JettyUtil.getPortForStartedServer(myServer); String baseUri = "http://localhost:" + myPort + "/fhir/context"; String uri = baseUri + "/Patient?identifier=urn:hapitest:mrns%7C00001"; diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeR4Test.java index acc27ed17c8..cae72a722a6 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchBundleProviderWithNoSizeR4Test.java @@ -35,7 +35,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.server.IBundleProvider; import ca.uhn.fhir.rest.param.TokenAndListParam; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchBundleProviderWithNoSizeR4Test { @@ -141,14 +141,13 @@ public class SearchBundleProviderWithNoSizeR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -160,7 +159,8 @@ public class SearchBundleProviderWithNoSizeR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamR4Test.java index 2716f5f3d58..036dc4b69a3 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchHasParamR4Test.java @@ -26,7 +26,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.OptionalParam; import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.param.*; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchHasParamR4Test { @@ -64,14 +64,13 @@ public class SearchHasParamR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -83,7 +82,8 @@ public class SearchHasParamR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchR4Test.java index d57263707de..bb97d680ffe 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchR4Test.java @@ -13,7 +13,6 @@ import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; import ca.uhn.fhir.rest.gclient.StringClientParam; import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import com.google.common.collect.Lists; @@ -45,6 +44,8 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class SearchR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SearchR4Test.class); @@ -475,14 +476,13 @@ public class SearchR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); DummyMedicationRequestResourceProvider medRequestProvider = new DummyMedicationRequestResourceProvider(); @@ -496,7 +496,8 @@ public class SearchR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSearchServerDstu1Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSearchServerDstu1Test.java index 9fa5bff32d7..60c7902b19a 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSearchServerDstu1Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSearchServerDstu1Test.java @@ -461,14 +461,13 @@ public class SearchSearchServerDstu1Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -481,7 +480,8 @@ public class SearchSearchServerDstu1Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSortR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSortR4Test.java index d712dc52e20..ba32d3f8c5f 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSortR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SearchSortR4Test.java @@ -25,7 +25,7 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.annotation.Sort; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class SearchSortR4Test { @@ -74,14 +74,13 @@ public class SearchSortR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -93,7 +92,8 @@ public class SearchSortR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeR4Test.java index f7f3edc2168..a1e67558c50 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ServerMimetypeR4Test.java @@ -50,7 +50,7 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.client.MyPatientWithExtensions; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; public class ServerMimetypeR4Test { @@ -408,14 +408,13 @@ public class ServerMimetypeR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -426,7 +425,8 @@ public class ServerMimetypeR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java index d4cfa0fe910..de5bf777f66 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/SummaryParamR4Test.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.rest.annotation.Search; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.SummaryEnum; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -36,6 +35,8 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import ca.uhn.fhir.util.JettyUtil; + public class SummaryParamR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SummaryParamR4Test.class); @@ -366,14 +367,13 @@ public class SummaryParamR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); RestfulServer servlet = new RestfulServer(ourCtx); @@ -382,7 +382,8 @@ public class SummaryParamR4Test { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptorTest.java index f5d280eb084..0b8296d41e3 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionHandlingInterceptorTest.java @@ -27,7 +27,7 @@ import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.*; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import junit.framework.AssertionFailedError; @@ -88,13 +88,12 @@ public class ExceptionHandlingInterceptorTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -104,7 +103,8 @@ public class ExceptionHandlingInterceptorTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionInterceptorMethodTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionInterceptorMethodTest.java index 4550967ddbb..199f9d6dcee 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionInterceptorMethodTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ExceptionInterceptorMethodTest.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -32,6 +31,8 @@ import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; +import ca.uhn.fhir.util.JettyUtil; + public class ExceptionInterceptorMethodTest { private static CloseableHttpClient ourClient; @@ -99,14 +100,13 @@ public class ExceptionInterceptorMethodTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -116,7 +116,8 @@ public class ExceptionInterceptorMethodTest { ServletHolder servletHolder = new ServletHolder(servlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InjectionAttackTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InjectionAttackTest.java index 4d0c63f3727..2e84eb670c5 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InjectionAttackTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InjectionAttackTest.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import com.google.common.base.Charsets; @@ -36,6 +35,8 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class InjectionAttackTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InjectionAttackTest.class); private static CloseableHttpClient ourClient; @@ -196,15 +197,13 @@ public class InjectionAttackTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -216,7 +215,8 @@ public class InjectionAttackTest { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorThrowingExceptionR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorThrowingExceptionR4Test.java index 0ff8eb86fb5..6a765f43e91 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorThrowingExceptionR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/InterceptorThrowingExceptionR4Test.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -35,6 +34,8 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.contains; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class InterceptorThrowingExceptionR4Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(InterceptorThrowingExceptionR4Test.class); @@ -142,16 +143,14 @@ public class InterceptorThrowingExceptionR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); try { - ourServer = new Server(ourPort); + ourServer = new Server(0); } catch (Exception e) { ourLog.error("FAILED", e); throw e; @@ -165,7 +164,8 @@ public class InterceptorThrowingExceptionR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java index 62eba17ddea..5877f159643 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ResponseHighlightingInterceptorTest.java @@ -12,7 +12,6 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.UrlUtil; import com.google.common.base.Charsets; @@ -47,10 +46,13 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import ca.uhn.fhir.util.JettyUtil; + public class ResponseHighlightingInterceptorTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResponseHighlightingInterceptorTest.class); private static ResponseHighlighterInterceptor ourInterceptor = new ResponseHighlighterInterceptor(); + private static Server ourServer; private static CloseableHttpClient ourClient; private static FhirContext ourCtx = FhirContext.forR4(); private static int ourPort; @@ -741,15 +743,14 @@ public class ResponseHighlightingInterceptorTest { } @AfterClass - public static void afterClassClearContext() { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourLog.info("Using port: {}", ourPort); - Server ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -780,7 +781,8 @@ public class ResponseHighlightingInterceptorTest { proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptorTest.java index b0b29cce710..16a8c24553d 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServeMediaResourceRawInterceptorTest.java @@ -7,7 +7,6 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; @@ -32,12 +31,15 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ServeMediaResourceRawInterceptorTest { private static final Logger ourLog = LoggerFactory.getLogger(ServeMediaResourceRawInterceptorTest.class); private static int ourPort; private static RestfulServer ourServlet; + private static Server ourServer; private static FhirContext ourCtx = FhirContext.forR4(); private static CloseableHttpClient ourClient; private static Media ourNextResponse; @@ -127,18 +129,16 @@ public class ServeMediaResourceRawInterceptorTest { } @AfterClass - public static void afterClassClearContext() throws IOException { + public static void afterClassClearContext() throws Exception { + JettyUtil.closeServer(ourServer); ourClient.close(); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - // Create server - ourLog.info("Using port: {}", ourPort); - Server ourServer = new Server(ourPort); + ourServer = new Server(0); ServletHandler proxyHandler = new ServletHandler(); ourServlet = new RestfulServer(ourCtx); ourServlet.setDefaultResponseEncoding(EncodingEnum.JSON); @@ -146,7 +146,8 @@ public class ServeMediaResourceRawInterceptorTest { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); // Create client PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerWithResponseHighlightingInterceptorExceptionTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerWithResponseHighlightingInterceptorExceptionTest.java index 0a8f2f0dd5e..7602032e53f 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerWithResponseHighlightingInterceptorExceptionTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/ServerWithResponseHighlightingInterceptorExceptionTest.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; @@ -31,6 +30,8 @@ import java.util.concurrent.TimeUnit; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class ServerWithResponseHighlightingInterceptorExceptionTest { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerWithResponseHighlightingInterceptorExceptionTest.class); private static CloseableHttpClient ourClient; @@ -66,15 +67,13 @@ public class ServerWithResponseHighlightingInterceptorExceptionTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider(); @@ -86,7 +85,8 @@ public class ServerWithResponseHighlightingInterceptorExceptionTest { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/SearchNarrowingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/SearchNarrowingInterceptorTest.java index ccd6c11a2c2..501439b5a7e 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/SearchNarrowingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/SearchNarrowingInterceptorTest.java @@ -15,7 +15,6 @@ import ca.uhn.fhir.rest.param.TokenAndListParam; import ca.uhn.fhir.rest.server.FifoMemoryPagingProvider; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.apache.commons.lang3.Validate; import org.eclipse.jetty.server.Server; @@ -40,6 +39,8 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import ca.uhn.fhir.util.JettyUtil; + public class SearchNarrowingInterceptorTest { private static String ourLastHitMethod; @@ -286,7 +287,7 @@ public class SearchNarrowingInterceptorTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @@ -294,8 +295,7 @@ public class SearchNarrowingInterceptorTest { public static void beforeClass() throws Exception { ourCtx = FhirContext.forR4(); - int ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyPatientResourceProvider patProvider = new DummyPatientResourceProvider(); DummyObservationResourceProvider obsProv = new DummyObservationResourceProvider(); @@ -309,7 +309,8 @@ public class SearchNarrowingInterceptorTest { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + int ourPort = JettyUtil.getPortForStartedServer(ourServer); ourCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER); ourCtx.getRestfulClientFactory().setSocketTimeout(1000000); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/provider/HashMapResourceProviderTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/provider/HashMapResourceProviderTest.java index df0faea3962..f03d0bf3f8b 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/provider/HashMapResourceProviderTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/provider/HashMapResourceProviderTest.java @@ -8,7 +8,6 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.exceptions.ResourceGoneException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -32,6 +31,8 @@ import java.util.stream.Collectors; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import ca.uhn.fhir.util.JettyUtil; + public class HashMapResourceProviderTest { private static final Logger ourLog = LoggerFactory.getLogger(HashMapResourceProviderTest.class); @@ -306,19 +307,15 @@ public class HashMapResourceProviderTest { @AfterClass public static void afterClassClearContext() throws Exception { - ourListenerServer.stop(); + JettyUtil.closeServer(ourListenerServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void startListenerServer() throws Exception { - int ourListenerPort = PortUtil.findFreePort(); ourRestServer = new MyRestfulServer(); - String ourBase = "http://localhost:" + ourListenerPort + "/"; - ourListenerServer = new Server(ourListenerPort); - - ourCtx.getRestfulClientFactory().setSocketTimeout(120000); - ourClient = ourCtx.newRestfulGenericClient(ourBase); + + ourListenerServer = new Server(0); ServletContextHandler proxyHandler = new ServletContextHandler(); proxyHandler.setContextPath("/"); @@ -328,7 +325,11 @@ public class HashMapResourceProviderTest { proxyHandler.addServlet(servletHolder, "/*"); ourListenerServer.setHandler(proxyHandler); - ourListenerServer.start(); + JettyUtil.startServer(ourListenerServer); + int ourListenerPort = JettyUtil.getPortForStartedServer(ourListenerServer); + String ourBase = "http://localhost:" + ourListenerPort + "/"; + ourCtx.getRestfulClientFactory().setSocketTimeout(120000); + ourClient = ourCtx.newRestfulGenericClient(ourBase); } private static class MyRestfulServer extends RestfulServer { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/RandomServerPortProvider.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/RandomServerPortProvider.java deleted file mode 100644 index 4747b741847..00000000000 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/RandomServerPortProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -package ca.uhn.fhir.util; - -import java.io.IOException; -import java.net.ServerSocket; -import java.util.ArrayList; -import java.util.List; - -/** - * Provides server ports - */ -public class RandomServerPortProvider { - - private static List ourPorts = new ArrayList(); - - public static int findFreePort() { - ServerSocket server; - try { - server = new ServerSocket(0); - int port = server.getLocalPort(); - ourPorts.add(port); - server.close(); - Thread.sleep(500); - return port; - } catch (IOException e) { - throw new Error(e); - } catch (InterruptedException e) { - throw new Error(e); - } - } - - public static List list() { - return ourPorts; - } - -} - \ No newline at end of file diff --git a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorDstu3Test.java b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorDstu3Test.java index 58273cc5374..1f03067a3c3 100644 --- a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorDstu3Test.java +++ b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorDstu3Test.java @@ -9,7 +9,6 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor; -import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.IValidationContext; import ca.uhn.fhir.validation.IValidatorModule; @@ -48,6 +47,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; +import ca.uhn.fhir.util.JettyUtil; + public class RequestValidatingInterceptorDstu3Test { private static CloseableHttpClient ourClient; @@ -460,14 +461,13 @@ public class RequestValidatingInterceptorDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -477,7 +477,8 @@ public class RequestValidatingInterceptorDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorR4Test.java b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorR4Test.java index 92839eaadd0..2b9bbc5e5fa 100644 --- a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorR4Test.java +++ b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/RequestValidatingInterceptorR4Test.java @@ -42,7 +42,7 @@ import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.*; @@ -461,14 +461,13 @@ public class RequestValidatingInterceptorR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -478,7 +477,8 @@ public class RequestValidatingInterceptorR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorDstu3Test.java b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorDstu3Test.java index d8ccd553b88..cb47b79f585 100644 --- a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorDstu3Test.java +++ b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorDstu3Test.java @@ -38,7 +38,7 @@ import ca.uhn.fhir.rest.api.RestOperationTypeEnum; import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.interceptor.ResponseValidatingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.IValidationContext; import ca.uhn.fhir.validation.IValidatorModule; @@ -465,14 +465,13 @@ public class ResponseValidatingInterceptorDstu3Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -482,7 +481,8 @@ public class ResponseValidatingInterceptorDstu3Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorR4Test.java b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorR4Test.java index 58a83c97574..d66d1dc958a 100644 --- a/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorR4Test.java +++ b/hapi-fhir-validation/src/test/java/ca/uhn/fhir/rest/server/ResponseValidatingInterceptorR4Test.java @@ -40,7 +40,7 @@ import ca.uhn.fhir.rest.api.RestOperationTypeEnum; import ca.uhn.fhir.rest.param.StringParam; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.interceptor.ResponseValidatingInterceptor; -import ca.uhn.fhir.util.PortUtil; +import ca.uhn.fhir.util.JettyUtil; import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.validation.*; @@ -469,14 +469,13 @@ public class ResponseValidatingInterceptorR4Test { @AfterClass public static void afterClassClearContext() throws Exception { - ourServer.stop(); + JettyUtil.closeServer(ourServer); TestUtil.clearAllStaticFieldsForUnitTest(); } @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); PatientProvider patientProvider = new PatientProvider(); @@ -486,7 +485,8 @@ public class ResponseValidatingInterceptorR4Test { ServletHolder servletHolder = new ServletHolder(ourServlet); proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); - ourServer.start(); + JettyUtil.startServer(ourServer); + ourPort = JettyUtil.getPortForStartedServer(ourServer); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create(); diff --git a/tests/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ReadTest.java b/tests/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ReadTest.java index 269607d357f..e3470cb3ee7 100644 --- a/tests/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ReadTest.java +++ b/tests/hapi-fhir-base-test-mindeps-server/src/test/java/ca/uhn/fhir/testmindeps/ReadTest.java @@ -27,7 +27,8 @@ import ca.uhn.fhir.rest.annotation.Read; import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; -import ca.uhn.fhir.util.PortUtil; +import org.mortbay.jetty.Connector; +import org.mortbay.jetty.bio.SocketConnector; /** * Created by dsotnikov on 2/25/2014. @@ -117,8 +118,7 @@ public class ReadTest { @BeforeClass public static void beforeClass() throws Exception { - ourPort = PortUtil.findFreePort(); - ourServer = new Server(ourPort); + ourServer = new Server(0); DummyProvider patientProvider = new DummyProvider(); @@ -130,6 +130,9 @@ public class ReadTest { proxyHandler.addServletWithMapping(servletHolder, "/*"); ourServer.setHandler(proxyHandler); ourServer.start(); + Connector[] connectors = ourServer.getConnectors(); + assert connectors.length == 1; + ourPort = ((SocketConnector) (connectors[0])).getLocalPort(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); HttpClientBuilder builder = HttpClientBuilder.create();