/**
   * Basic sanity-test to ensure that the WComponent app is performing all the processing that it
   * should.
   */
  @Test
  public void testWServletAppCorrectness() throws Exception {
    SimpleWServlet servlet = new SimpleWServlet();
    servlet.init(new MockServletConfig());
    MockHttpSession session = new MockHttpSession();

    // First request
    sendWServletRequest(servlet, session, 0, null);

    // Second request
    WServletHelper helper =
        new WServletHelper(
            servlet, new MockHttpServletRequest(session), new MockHttpServletResponse());
    UIContext uic = helper.getUIContext();
    SimpleApp app = (SimpleApp) uic.getUI();

    sendWServletRequest(servlet, session, 1, uic.getEnvironment().getSessionToken());

    setActiveContext(uic);
    Assert.assertEquals("Incorrect step", 2, uic.getEnvironment().getStep());
    Assert.assertEquals(
        "Incorrect property1 value",
        "p1_1",
        ((SimpleFormBean) app.beanContainer.getBean()).getProperty1());
    Assert.assertEquals(
        "Incorrect property2 value",
        "p2_1",
        ((SimpleFormBean) app.beanContainer.getBean()).getProperty2());
  }
  /**
   * Times the WServlet execution looping the given number of times and returns the elapsed time.
   *
   * @param count the number of times to loop.
   * @return the elapsed time, in nanoseconds.
   */
  private long timeWServlet(final int count) throws Exception {
    final SimpleWServlet servlet = new SimpleWServlet();
    servlet.init(new MockServletConfig());
    final MockHttpSession simpleWServletSession = new MockHttpSession();

    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);

    // Get token
    WServletHelper helper =
        new WServletHelper(
            servlet,
            new MockHttpServletRequest(simpleWServletSession),
            new MockHttpServletResponse());
    String token = helper.getUIContext().getEnvironment().getSessionToken();

    // JIT warm-up
    for (int i = 1; i < count; i++) {
      sendWServletRequest(servlet, simpleWServletSession, i, token);
    }

    simpleWServletSession.getAttributes().clear();

    // Do first request to get session token
    sendWServletRequest(servlet, simpleWServletSession, 0, null);
    final String token2 = helper.getUIContext().getEnvironment().getSessionToken();

    Runnable runnable =
        new Runnable() {
          public void run() {
            try {
              for (int i = 1; i < count; i++) {
                sendWServletRequest(servlet, simpleWServletSession, i, token2);
              }
            } catch (Exception e) {
              log.error("Failed to execute test", e);
            }
          }
        };

    return time(runnable);
  }