Expose local port in EmbeddedServletContainer
This commit is contained in:
		
							parent
							
								
									d1dcb015b6
								
							
						
					
					
						commit
						67cc427d2b
					
				|  | @ -44,6 +44,10 @@ public interface EmbeddedServletContainer { | ||||||
| 		public void stop() throws EmbeddedServletContainerException { | 		public void stop() throws EmbeddedServletContainerException { | ||||||
| 			// Do nothing | 			// Do nothing | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
|  | 		public int getPort() { | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
|  | @ -60,4 +64,9 @@ public interface EmbeddedServletContainer { | ||||||
| 	 */ | 	 */ | ||||||
| 	void stop() throws EmbeddedServletContainerException; | 	void stop() throws EmbeddedServletContainerException; | ||||||
| 
 | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * @return the port this server is listening on (or zero if none) | ||||||
|  | 	 */ | ||||||
|  | 	int getPort(); | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -109,6 +109,16 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public int getPort() { | ||||||
|  | 		Connector[] connectors = this.server.getConnectors(); | ||||||
|  | 		for (Connector connector : connectors) { | ||||||
|  | 			// Probably only one... | ||||||
|  | 			return connector.getLocalPort(); | ||||||
|  | 		} | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Returns access to the underlying Jetty Server. | 	 * Returns access to the underlying Jetty Server. | ||||||
| 	 */ | 	 */ | ||||||
|  |  | ||||||
|  | @ -126,11 +126,20 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer | ||||||
| 			this.tomcat.destroy(); | 			this.tomcat.destroy(); | ||||||
| 		} | 		} | ||||||
| 		catch (Exception ex) { | 		catch (Exception ex) { | ||||||
| 			throw new EmbeddedServletContainerException( | 			throw new EmbeddedServletContainerException("Unable to stop embedded Tomcat", | ||||||
| 					"Unable to stop embedded Tomcat", ex); | 					ex); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public int getPort() { | ||||||
|  | 		Connector connector = this.tomcat.getConnector(); | ||||||
|  | 		if (connector != null) { | ||||||
|  | 			return connector.getLocalPort(); | ||||||
|  | 		} | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Returns access to the underlying Tomcat server. | 	 * Returns access to the underlying Tomcat server. | ||||||
| 	 */ | 	 */ | ||||||
|  |  | ||||||
|  | @ -47,6 +47,7 @@ import org.springframework.util.StreamUtils; | ||||||
| 
 | 
 | ||||||
| import static org.hamcrest.Matchers.equalTo; | import static org.hamcrest.Matchers.equalTo; | ||||||
| import static org.hamcrest.Matchers.notNullValue; | import static org.hamcrest.Matchers.notNullValue; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
| import static org.junit.Assert.assertThat; | import static org.junit.Assert.assertThat; | ||||||
| import static org.mockito.Matchers.anyObject; | import static org.mockito.Matchers.anyObject; | ||||||
| import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | ||||||
|  | @ -172,6 +173,7 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { | ||||||
| 				.getEmbeddedServletContainer(exampleServletRegistration()); | 				.getEmbeddedServletContainer(exampleServletRegistration()); | ||||||
| 		this.container.start(); | 		this.container.start(); | ||||||
| 		assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World")); | 		assertThat(getResponse("http://localhost:8081/hello"), equalTo("Hello World")); | ||||||
|  | 		assertEquals(8081, this.container.getPort()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue