Class ServerRule

java.lang.Object
org.junit.rules.ExternalResource
com.linecorp.armeria.testing.junit4.server.ServerRule
All Implemented Interfaces:
TestRule

public abstract class ServerRule
extends ExternalResource
A TestRule that allows easy set-up and tear-down of a Server.
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected ServerRule()
    Creates a new instance with auto-start enabled.
    protected ServerRule​(boolean autoStart)
    Creates a new instance.
  • Method Summary

    Modifier and Type Method Description
    protected void after()
    Calls stop(), without waiting until the Server is stopped completely.
    protected void before()
    Calls start() if auto-start is enabled.
    protected abstract void configure​(com.linecorp.armeria.server.ServerBuilder sb)
    Configures the Server with the given ServerBuilder.
    com.linecorp.armeria.client.Endpoint endpoint​(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the Endpoint of the specified SessionProtocol for the Server.
    boolean hasHttp()
    Returns true if the Server is started and it has an HTTP port open.
    boolean hasHttps()
    Returns true if the Server is started and it has an HTTPS port open.
    com.linecorp.armeria.client.Endpoint httpEndpoint()
    Returns the HTTP Endpoint for the Server.
    int httpPort()
    Returns the HTTP port number of the Server.
    com.linecorp.armeria.client.Endpoint httpsEndpoint()
    Returns the HTTPS Endpoint for the Server.
    InetSocketAddress httpSocketAddress()
    Returns the HTTP InetSocketAddress of the Server.
    int httpsPort()
    Returns the HTTPS port number of the Server.
    InetSocketAddress httpsSocketAddress()
    Returns the HTTPS InetSocketAddress of the Server.
    URI httpsUri()
    Returns the HTTPS URI for the Server.
    URI httpsUri​(com.linecorp.armeria.common.SerializationFormat format)
    Returns the HTTPS URI for the Server.
    URI httpUri()
    Returns the HTTP URI for the Server.
    URI httpUri​(com.linecorp.armeria.common.SerializationFormat format)
    Returns the HTTP URI for the Server.
    int port​(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the port number of the Server for the specified SessionProtocol.
    com.linecorp.armeria.server.Server server()
    Returns the started Server.
    InetSocketAddress socketAddress​(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the InetSocketAddress of the specified SessionProtocol for the Server.
    com.linecorp.armeria.server.Server start()
    Starts the Server configured by configure(ServerBuilder).
    CompletableFuture<Void> stop()
    Stops the Server asynchronously.
    URI uri​(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the URI for the Server of the specified SessionProtocol.
    URI uri​(com.linecorp.armeria.common.SessionProtocol protocol, com.linecorp.armeria.common.SerializationFormat format)
    Returns the URI for the Server of the specified SessionProtocol and SerializationFormat.

    Methods inherited from class org.junit.rules.ExternalResource

    apply

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ServerRule

      protected ServerRule()
      Creates a new instance with auto-start enabled.
    • ServerRule

      protected ServerRule​(boolean autoStart)
      Creates a new instance.
      Parameters:
      autoStart - true if the Server should start automatically. false if the Server should start when a user calls start().
  • Method Details

    • before

      protected void before() throws Throwable
      Calls start() if auto-start is enabled.
      Overrides:
      before in class ExternalResource
      Throws:
      Throwable
    • after

      protected void after()
      Calls stop(), without waiting until the Server is stopped completely.
      Overrides:
      after in class ExternalResource
    • start

      public com.linecorp.armeria.server.Server start()
      Starts the Server configured by configure(ServerBuilder). If the Server has been started up already, the existing Server is returned. Note that this operation blocks until the Server finished the start-up.
      Returns:
      the started Server
    • configure

      protected abstract void configure​(com.linecorp.armeria.server.ServerBuilder sb) throws Exception
      Configures the Server with the given ServerBuilder.
      Throws:
      Exception
    • stop

      public CompletableFuture<Void> stop()
      Stops the Server asynchronously.
      Returns:
      the CompletableFuture that will complete when the Server is stopped.
    • server

      public com.linecorp.armeria.server.Server server()
      Returns the started Server.
      Throws:
      IllegalStateException - if the Server is not started
    • httpPort

      public int httpPort()
      Returns the HTTP port number of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port
    • httpsPort

      public int httpsPort()
      Returns the HTTPS port number of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port
    • port

      public int port​(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the port number of the Server for the specified SessionProtocol.
      Throws:
      IllegalStateException - if the Server is not started or it did not open a port of the specified protocol.
    • hasHttp

      public boolean hasHttp()
      Returns true if the Server is started and it has an HTTP port open.
    • hasHttps

      public boolean hasHttps()
      Returns true if the Server is started and it has an HTTPS port open.
    • endpoint

      public com.linecorp.armeria.client.Endpoint endpoint​(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the Endpoint of the specified SessionProtocol for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • httpEndpoint

      public com.linecorp.armeria.client.Endpoint httpEndpoint()
      Returns the HTTP Endpoint for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpsEndpoint

      public com.linecorp.armeria.client.Endpoint httpsEndpoint()
      Returns the HTTPS Endpoint for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • uri

      public URI uri​(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the URI for the Server of the specified SessionProtocol.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • uri

      public URI uri​(com.linecorp.armeria.common.SessionProtocol protocol, com.linecorp.armeria.common.SerializationFormat format)
      Returns the URI for the Server of the specified SessionProtocol and SerializationFormat.
      Throws:
      IllegalStateException - if the Server is not started or it did not open the port for the specified SessionProtocol.
    • httpUri

      public URI httpUri()
      Returns the HTTP URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpUri

      public URI httpUri​(com.linecorp.armeria.common.SerializationFormat format)
      Returns the HTTP URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port.
    • httpsUri

      public URI httpsUri()
      Returns the HTTPS URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • httpsUri

      public URI httpsUri​(com.linecorp.armeria.common.SerializationFormat format)
      Returns the HTTPS URI for the Server.
      Returns:
      the absolute URI without a path.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port.
    • socketAddress

      public InetSocketAddress socketAddress​(com.linecorp.armeria.common.SessionProtocol protocol)
      Returns the InetSocketAddress of the specified SessionProtocol for the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open a port for the specified SessionProtocol.
    • httpSocketAddress

      public InetSocketAddress httpSocketAddress()
      Returns the HTTP InetSocketAddress of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTP port
    • httpsSocketAddress

      public InetSocketAddress httpsSocketAddress()
      Returns the HTTPS InetSocketAddress of the Server.
      Throws:
      IllegalStateException - if the Server is not started or it did not open an HTTPS port