Committed by
Gerrit Code Review
Let JerseyTest pick random port
Change-Id: Ibc2544d0f27487cfd25317f6f5894acda9b89f51
Showing
1 changed file
with
5 additions
and
23 deletions
| ... | @@ -17,26 +17,23 @@ package org.onosproject.rest.resources; | ... | @@ -17,26 +17,23 @@ package org.onosproject.rest.resources; |
| 17 | 17 | ||
| 18 | import org.glassfish.jersey.server.ResourceConfig; | 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.glassfish.jersey.test.JerseyTest; | 19 | import org.glassfish.jersey.test.JerseyTest; |
| 20 | +import org.glassfish.jersey.test.TestProperties; | ||
| 20 | import org.glassfish.jersey.test.jetty.JettyTestContainerFactory; | 21 | import org.glassfish.jersey.test.jetty.JettyTestContainerFactory; |
| 21 | import org.glassfish.jersey.test.spi.TestContainerException; | 22 | import org.glassfish.jersey.test.spi.TestContainerException; |
| 22 | import org.glassfish.jersey.test.spi.TestContainerFactory; | 23 | import org.glassfish.jersey.test.spi.TestContainerFactory; |
| 23 | 24 | ||
| 24 | -import java.io.IOException; | ||
| 25 | -import java.net.ServerSocket; | ||
| 26 | - | ||
| 27 | /** | 25 | /** |
| 28 | * Base class for REST API tests. | 26 | * Base class for REST API tests. |
| 29 | * Performs common configuration operations. | 27 | * Performs common configuration operations. |
| 30 | */ | 28 | */ |
| 31 | public class ResourceTest extends JerseyTest { | 29 | public class ResourceTest extends JerseyTest { |
| 32 | - private static final int DEFAULT_PORT = 9998; | ||
| 33 | 30 | ||
| 34 | /** | 31 | /** |
| 35 | * Creates a new web-resource test. | 32 | * Creates a new web-resource test. |
| 36 | */ | 33 | */ |
| 37 | public ResourceTest() { | 34 | public ResourceTest() { |
| 38 | super(ResourceConfig.forApplicationClass(CoreWebApplication.class)); | 35 | super(ResourceConfig.forApplicationClass(CoreWebApplication.class)); |
| 39 | - this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT)); | 36 | + configureProperties(); |
| 40 | } | 37 | } |
| 41 | 38 | ||
| 42 | /** | 39 | /** |
| ... | @@ -44,26 +41,11 @@ public class ResourceTest extends JerseyTest { | ... | @@ -44,26 +41,11 @@ public class ResourceTest extends JerseyTest { |
| 44 | */ | 41 | */ |
| 45 | public ResourceTest(ResourceConfig config) { | 42 | public ResourceTest(ResourceConfig config) { |
| 46 | super(config); | 43 | super(config); |
| 47 | - this.set("jersey.config.test.container.port", getRandomPort(DEFAULT_PORT)); | 44 | + configureProperties(); |
| 48 | } | 45 | } |
| 49 | 46 | ||
| 50 | - /** | 47 | + private void configureProperties() { |
| 51 | - * Returns an unused port number to make sure that each unit test runs in | 48 | + set(TestProperties.CONTAINER_PORT, 0); |
| 52 | - * different port number. | ||
| 53 | - * | ||
| 54 | - * @param defaultPort default port number | ||
| 55 | - * @return a randomized unique port number | ||
| 56 | - */ | ||
| 57 | - private int getRandomPort(int defaultPort) { | ||
| 58 | - try { | ||
| 59 | - ServerSocket socket = new ServerSocket(0); | ||
| 60 | - socket.setReuseAddress(true); | ||
| 61 | - int port = socket.getLocalPort(); | ||
| 62 | - socket.close(); | ||
| 63 | - return port; | ||
| 64 | - } catch (IOException ioe) { | ||
| 65 | - return defaultPort; | ||
| 66 | - } | ||
| 67 | } | 49 | } |
| 68 | 50 | ||
| 69 | /** | 51 | /** | ... | ... |
-
Please register or login to post a comment