Class OorianTestHarness

java.lang.Object
com.oorian.test.OorianTestHarness

public class OorianTestHarness extends Object
Test harness for headless rendering of Oorian pages and components.

Provides a lightweight test environment that allows pages to be rendered and inspected without a servlet container or browser. The harness manages mock objects for the application context, HTTP session, request, and response.

Quick Start:


 // One-time setup (e.g., in @BeforeClass)
 OorianTestHarness.setup();

 // Render a page
 String html = OorianTestHarness.render(new MyPage(), "/my-page");

 // Inspect the output
 assert html.contains("<title>My Page</title>");

 // Teardown (e.g., in @AfterClass)
 OorianTestHarness.teardown();
 

Advanced Usage:


 OorianTestHarness.setup();

 // Create custom request with parameters
 TestHttpRequest request = OorianTestHarness.createRequest("/product/42");
 request.setParameter("format", "json");
 request.setHeader("Accept-Language", "fr-FR");

 // Render with custom request
 TestHttpResponse response = OorianTestHarness.render(new ProductPage(), request);
 String html = response.getContent();
 int status = response.getStatus();
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
  • Method Details

    • setup

      public static void setup()
      Initializes the test environment with default settings.

      Creates a TestAppContext with an empty context path and a shared TestHttpSession. The application context is registered with OorianSession so that session management works during rendering.

      Call this method once before running tests (e.g., in a @BeforeClass method).

    • setup

      public static void setup(TestAppContext context)
      Initializes the test environment with a custom application context.
      Parameters:
      context - the application context to use
    • teardown

      public static void teardown()
      Cleans up the test environment.

      Resets the application context and shared session. Call this method after all tests have completed (e.g., in an @AfterClass method).

    • createRequest

      public static TestHttpRequest createRequest(String path)
      Creates a new TestHttpRequest for the specified path.

      The request is pre-configured with the shared test session and the context path from the test application context.

      Parameters:
      path - the request path (e.g., "/my-page")
      Returns:
      a new test request
    • createResponse

      public static TestHttpResponse createResponse()
      Creates a new TestHttpResponse.
      Returns:
      a new test response
    • render

      public static String render(HttpFile page, String path)
      Renders a page and returns the HTML output.

      This is the simplest way to render a page for testing. It creates a default request for the specified path, registers a session, renders the page, and returns the HTML content.

      Parameters:
      page - the page to render
      path - the request path (e.g., "/my-page")
      Returns:
      the rendered HTML content
    • render

      public static TestHttpResponse render(HttpFile page, TestHttpRequest request)
      Renders a page with a custom request and returns the response.

      Registers the session for the current thread, then invokes the full page lifecycle (initializeFile, createFile, rendering). The complete response (content, status, headers) is available on the returned TestHttpResponse.

      Parameters:
      page - the page to render
      request - the test request
      Returns:
      the test response containing the rendered output
    • getSession

      public static TestHttpSession getSession()
      Returns the shared test session.

      The shared session persists across multiple render(com.oorian.HttpFile, java.lang.String) calls within the same test setup, allowing session attributes to accumulate as they would in a real user session.

      Returns:
      the shared test session
    • getAppContext

      public static TestAppContext getAppContext()
      Returns the test application context.
      Returns:
      the test application context
    • resetSession

      public static void resetSession()
      Resets the shared session, clearing all session attributes.

      Creates a new empty session. Useful between tests to ensure a clean session state.