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
    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
    Calls stop(), without waiting until the Server is stopped completely.
    protected void
    Calls start() if auto-start is enabled.
    com.linecorp.armeria.client.BlockingWebClient
    Returns the BlockingWebClient configured by configureWebClient(WebClientBuilder).
    com.linecorp.armeria.client.BlockingWebClient
    blockingWebClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
    Returns a newly created BlockingWebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    protected abstract void
    configure(com.linecorp.armeria.server.ServerBuilder sb)
    Configures the Server with the given ServerBuilder.
    protected void
    configureWebClient(com.linecorp.armeria.client.WebClientBuilder webClientBuilder)
    Configures the WebClient with the given WebClientBuilder.
    com.linecorp.armeria.client.Endpoint
    endpoint(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the Endpoint of the specified SessionProtocol for the Server.
    boolean
    Returns true if the Server is started and it has an HTTP port open.
    boolean
    Returns true if the Server is started and it has an HTTPS port open.
    com.linecorp.armeria.client.Endpoint
    Returns the HTTP Endpoint for the Server.
    int
    Returns the HTTP port number of the Server.
    com.linecorp.armeria.client.Endpoint
    Returns the HTTPS Endpoint for the Server.
    Returns the HTTP InetSocketAddress of the Server.
    int
    Returns the HTTPS port number of the Server.
    Returns the HTTPS InetSocketAddress of the Server.
    Returns the HTTPS URI for the Server.
    httpsUri(com.linecorp.armeria.common.SerializationFormat format)
    Returns the HTTPS URI for the Server.
    Returns the HTTP URI for the Server.
    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.client.RestClient
    Returns the RestClient configured by configureWebClient(WebClientBuilder).
    com.linecorp.armeria.client.RestClient
    restClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
    Returns a newly created RestClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    com.linecorp.armeria.server.Server
    Returns the started Server.
    socketAddress(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the InetSocketAddress of the specified SessionProtocol for the Server.
    com.linecorp.armeria.server.Server
    Starts the Server configured by configure(ServerBuilder).
    Stops the Server asynchronously.
    uri(com.linecorp.armeria.common.SessionProtocol protocol)
    Returns the URI for the Server of the specified SessionProtocol.
    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.
    com.linecorp.armeria.client.WebClient
    Returns the WebClient configured by configureWebClient(WebClientBuilder).
    com.linecorp.armeria.client.WebClient
    webClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
    Returna a newly created WebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.

    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
    • configureWebClient

      protected void configureWebClient(com.linecorp.armeria.client.WebClientBuilder webClientBuilder) throws Exception
      Configures the WebClient with the given WebClientBuilder. You can get the configured WebClient using webClient().
      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
    • webClient

      public com.linecorp.armeria.client.WebClient webClient()
      Returns the WebClient configured by configureWebClient(WebClientBuilder).
    • webClient

      public com.linecorp.armeria.client.WebClient webClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returna a newly created WebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    • blockingWebClient

      @UnstableApi public com.linecorp.armeria.client.BlockingWebClient blockingWebClient()
      Returns the BlockingWebClient configured by configureWebClient(WebClientBuilder).
    • blockingWebClient

      @UnstableApi public com.linecorp.armeria.client.BlockingWebClient blockingWebClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returns a newly created BlockingWebClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.
    • restClient

      @UnstableApi public com.linecorp.armeria.client.RestClient restClient()
      Returns the RestClient configured by configureWebClient(WebClientBuilder).
    • restClient

      @UnstableApi public com.linecorp.armeria.client.RestClient restClient(Consumer<com.linecorp.armeria.client.WebClientBuilder> webClientCustomizer)
      Returns a newly created RestClient configured by configureWebClient(WebClientBuilder) and then the specified customizer.