Class AppServer


  • public class AppServer
    extends java.lang.Object
    Tiny web application server for unit testing.

    Tests should start the server in their setUp() method and stop the server in their tearDown() method. Only while started the server's URL and/or port number can be obtained.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String authRole
      Role for authentication.
      private org.eclipse.jetty.server.HttpConfiguration config  
      private org.eclipse.jetty.server.ServerConnector connector  
      private org.eclipse.jetty.server.handler.ContextHandlerCollection contexts  
      private java.util.List<java.io.File> filesToDelete  
      private static java.lang.String keyPassword
      SSL keystore password; must have at least 6 characters.
      private TestRequestLog log  
      static java.lang.String password
      Password for username in secured access areas.
      static java.lang.String realm
      Realm name for the secure access areas.
      private org.eclipse.jetty.server.HttpConfiguration secureConfig  
      private org.eclipse.jetty.server.ServerConnector secureConnector  
      private org.eclipse.jetty.server.Server server  
      static java.lang.String username
      Username for secured access areas.
    • Constructor Summary

      Constructors 
      Constructor Description
      AppServer()
      Constructor for AppServer.
      AppServer​(int port)
      Constructor for AppServer.
      AppServer​(int port, int sslPort)
      Constructor for AppServer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.eclipse.jetty.servlet.ServletContextHandler addContext​(java.lang.String path)
      Create a new servlet context within the server.
      private void assertAlreadySetUp()  
      private void assertNotYetSetUp()  
      private void auth​(org.eclipse.jetty.servlet.ServletContextHandler ctx, org.eclipse.jetty.security.Authenticator authType, java.lang.String... methods)  
      org.eclipse.jetty.servlet.ServletContextHandler authBasic​(org.eclipse.jetty.servlet.ServletContextHandler ctx, java.lang.String... methods)
      Configure basic authentication.
      private org.eclipse.jetty.security.ConstraintMapping createConstraintMapping()  
      private org.eclipse.jetty.util.ssl.SslContextFactory createTestSslContextFactory​(java.lang.String hostName)  
      int getPort()
      Get port.
      java.util.List<AccessEvent> getRequests()
      Get requests.
      java.util.List<AccessEvent> getRequests​(java.lang.String path)
      Get requests.
      java.util.List<AccessEvent> getRequests​(URIish base, java.lang.String path)
      Get requests.
      int getSecurePort()
      Get secure port.
      java.net.URI getURI()
      Get the URI to reference this server.
      private void makePrivate​(java.io.File file)  
      void setUp()
      Start the server on a random local port.
      void tearDown()
      Shutdown the server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • realm

        public static final java.lang.String realm
        Realm name for the secure access areas.
        See Also:
        Constant Field Values
      • username

        public static final java.lang.String username
        Username for secured access areas.
        See Also:
        Constant Field Values
      • keyPassword

        private static final java.lang.String keyPassword
        SSL keystore password; must have at least 6 characters.
        See Also:
        Constant Field Values
      • authRole

        private static final java.lang.String authRole
        Role for authentication.
        See Also:
        Constant Field Values
      • server

        private final org.eclipse.jetty.server.Server server
      • config

        private final org.eclipse.jetty.server.HttpConfiguration config
      • connector

        private final org.eclipse.jetty.server.ServerConnector connector
      • secureConfig

        private final org.eclipse.jetty.server.HttpConfiguration secureConfig
      • secureConnector

        private final org.eclipse.jetty.server.ServerConnector secureConnector
      • contexts

        private final org.eclipse.jetty.server.handler.ContextHandlerCollection contexts
      • filesToDelete

        private java.util.List<java.io.File> filesToDelete
    • Constructor Detail

      • AppServer

        public AppServer()
        Constructor for AppServer.
      • AppServer

        public AppServer​(int port)
        Constructor for AppServer.
        Parameters:
        port - the http port number; may be zero to allocate a port dynamically
        Since:
        4.2
      • AppServer

        public AppServer​(int port,
                         int sslPort)
        Constructor for AppServer.
        Parameters:
        port - for http, may be zero to allocate a port dynamically
        sslPort - for https,may be zero to allocate a port dynamically. If negative, the server will be set up without https support.
        Since:
        4.9
    • Method Detail

      • createTestSslContextFactory

        private org.eclipse.jetty.util.ssl.SslContextFactory createTestSslContextFactory​(java.lang.String hostName)
      • makePrivate

        private void makePrivate​(java.io.File file)
      • addContext

        public org.eclipse.jetty.servlet.ServletContextHandler addContext​(java.lang.String path)
        Create a new servlet context within the server.

        This method should be invoked before the server is started, once for each context the caller wants to register.

        Parameters:
        path - path of the context; use "/" for the root context if binding to the root is desired.
        Returns:
        the context to add servlets into.
      • authBasic

        public org.eclipse.jetty.servlet.ServletContextHandler authBasic​(org.eclipse.jetty.servlet.ServletContextHandler ctx,
                                                                         java.lang.String... methods)
        Configure basic authentication.
        Parameters:
        ctx -
        methods -
        Returns:
        servlet context handler
      • createConstraintMapping

        private org.eclipse.jetty.security.ConstraintMapping createConstraintMapping()
      • auth

        private void auth​(org.eclipse.jetty.servlet.ServletContextHandler ctx,
                          org.eclipse.jetty.security.Authenticator authType,
                          java.lang.String... methods)
      • setUp

        public void setUp()
                   throws java.lang.Exception
        Start the server on a random local port.
        Throws:
        java.lang.Exception - the server cannot be started, testing is not possible.
      • tearDown

        public void tearDown()
                      throws java.lang.Exception
        Shutdown the server.
        Throws:
        java.lang.Exception - the server refuses to halt, or wasn't running.
      • getURI

        public java.net.URI getURI()
        Get the URI to reference this server.

        The returned URI includes the proper host name and port number, but does not contain a path.

        Returns:
        URI to reference this server's root context.
      • getPort

        public int getPort()
        Get port.
        Returns:
        the local port number the server is listening on.
      • getSecurePort

        public int getSecurePort()
        Get secure port.
        Returns:
        the HTTPS port or -1 if not configured.
      • getRequests

        public java.util.List<AccessEvent> getRequests()
        Get requests.
        Returns:
        all requests since the server was started.
      • getRequests

        public java.util.List<AccessEvent> getRequests​(URIish base,
                                                       java.lang.String path)
        Get requests.
        Parameters:
        base - base URI used to access the server.
        path - the path to locate requests for, relative to base.
        Returns:
        all requests which match the given path.
      • getRequests

        public java.util.List<AccessEvent> getRequests​(java.lang.String path)
        Get requests.
        Parameters:
        path - the path to locate requests for.
        Returns:
        all requests which match the given path.
      • assertNotYetSetUp

        private void assertNotYetSetUp()
      • assertAlreadySetUp

        private void assertAlreadySetUp()