Permalink
Browse files

o add fluent way to start with random port, set port later

  • Loading branch information...
nabcos committed Jun 29, 2012
1 parent e4bec94 commit c6751de55107832eac2eb9802f572d64afcb9eb9
@@ -32,7 +32,7 @@ void initServer()
throws Exception;
/**
- * Set to -1 to auto-choose a free port.
+ * Set to 0 to auto-choose a free port.
*
* @param port
*/
@@ -62,4 +62,10 @@ void initServer()
void addUser( String user, Object password );
// void addFilter( String pathSpec, Filter filter );
+
+ /*
+ * Returns true if the server is currently running, false otherwise.
+ * @since 0.6
+ */
+ boolean isStarted();
}
@@ -77,4 +77,9 @@ public void addUser( String user, Object password )
}
+ public boolean isStarted()
+ {
+ return true;
+ }
+
}
@@ -7,18 +7,37 @@
import org.sonatype.tests.http.server.jetty.impl.JettyServerProvider;
/**
- * @since 1.0
+ * @since 0.4.3
*/
public class Server
{
private final ServerProvider serverProvider;
+ public Server()
+ throws Exception
+ {
+ this.serverProvider = new JettyServerProvider();
+ }
+
public Server( final ServerProvider serverProvider )
{
this.serverProvider = serverProvider;
}
+ /**
+ * Start a server on a random port.
+ * @since 0.6
+ */
+ public static Server server()
+ throws Exception
+ {
+ return new Server();
+ }
+
+ /**
+ * Start a server on the given port (0 for random).
+ */
public static Server withPort(int port)
throws Exception
{
@@ -27,6 +46,23 @@ public static Server withPort(int port)
return new Server(jettyServerProvider);
}
+ /**
+ * Set the port for the server.
+ * @throws IllegalStateException if the server is already started.
+ * @since 0.6
+ */
+ public Server port(int port)
+ throws IllegalStateException
+ {
+ if ( serverProvider.isStarted() )
+ {
+ throw new IllegalStateException( "Server is currently running, cannot change port." );
+ }
+ serverProvider.setPort( port );
+
+ return this;
+ }
+
public ServeContext serve(String pattern)
{
return new ServeContext(serverProvider, pattern, this );
@@ -111,6 +111,11 @@ public void initServer()
server = createServer();
}
+ public boolean isStarted()
+ {
+ return server != null && server.isStarted();
+ }
+
public void getServer()
throws Exception
{
@@ -603,4 +608,5 @@ public void checkServerTrusted( X509Certificate[] arg0, String arg1 )
}
}
+
}

0 comments on commit c6751de

Please sign in to comment.