/**
   * 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());
  }
 /** {@inheritDoc} */
 @Override
 public void writeHeader(final PrintWriter writer) {
   UIContext uic = UIContextHolder.getCurrent();
   addHeadlines(writer, uic.getHeaders().getHeadLines());
   addJsHeadlines(writer, uic.getHeaders().getHeadLines(Headers.JAVASCRIPT_HEADLINE));
   addCssHeadlines(writer, uic.getHeaders().getHeadLines(Headers.CSS_HEADLINE));
 }
Ejemplo n.º 3
0
  /** {@inheritDoc} */
  @Override
  public void serviceRequest(final Request request) {
    String triggerId = request.getParameter(WServlet.AJAX_TRIGGER_PARAM_NAME);

    AjaxOperation ajaxOperation = AjaxHelper.getCurrentOperation();
    if (ajaxOperation == null) {
      throw new IllegalStateException("No AJAX operation available for trigger " + triggerId + ".");
    }

    ComponentWithContext triggerWithContext = AjaxHelper.getCurrentTriggerAndContext();
    if (triggerWithContext == null) {
      throw new IllegalStateException(
          "No component/context available for AJAX trigger " + triggerId + ".");
    }

    UIContext uic = UIContextHolder.getCurrent();

    // Reset the focus for this new request.
    uic.setFocussed(null, null);

    // We've hit the action phase, so we do want focus on this app.
    uic.setFocusRequired(true);

    // Process trigger only
    if (isProcessTriggerOnly(triggerWithContext, ajaxOperation)) {
      // Get user context
      UIContext tuic = triggerWithContext.getContext();
      UIContextHolder.pushContext(tuic);
      try {
        WComponent trigger = triggerWithContext.getComponent();
        trigger.serviceRequest(request);
        // Manually invoke laters as the InvokeLaters in the service request is not run due to the
        // trigger
        // having a "parent"
        tuic.doInvokeLaters();
      } finally {
        UIContextHolder.popContext();
      }
    }
    // GET only supports the above scenarios
    else if ("GET".equals(request.getMethod())) {
      throw new IllegalStateException(
          "GET is not supported for the AJAX trigger " + triggerId + ".");
    } else {
      // service the request
      super.serviceRequest(request);
    }
  }
  /**
   * Render the component and execute the interceptor.
   *
   * @param testUI the test component
   * @return the response
   */
  private TestResult generateOutput(final MyComponent testUI) {
    InterceptorComponent interceptor = new TransformXMLInterceptor();
    interceptor.setBackingComponent(testUI);

    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);

    StringWriter writer = new StringWriter();
    UIContext uic = createUIContext();
    uic.setLocale(new Locale("xx"));
    setActiveContext(uic);

    try {
      interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    } finally {
      resetContext();
    }

    return new TestResult(writer.toString(), response.getContentType());
  }
  /**
   * Retrieves the application title for the given context. This mess is required due to WWindow
   * essentially existing as a separate UI root. If WWindow is eventually removed, then we can just
   * retrieve the title from the root WApplication.
   *
   * @param uic the context to check.
   * @return the current application title.
   */
  private String getApplicationTitle(final UIContext uic) {
    WComponent root = uic.getUI();
    String title = root instanceof WApplication ? ((WApplication) root).getTitle() : null;

    Map<String, String> params = uic.getEnvironment().getHiddenParameters();
    String target = params.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);

    if (target != null) {
      ComponentWithContext targetComp = WebUtilities.getComponentById(target, true);

      if (targetComp != null && targetComp.getComponent() instanceof WWindow) {
        try {
          UIContextHolder.pushContext(targetComp.getContext());
          title = ((WWindow) targetComp.getComponent()).getTitle();
        } finally {
          UIContextHolder.popContext();
        }
      }
    }

    return title == null ? "" : title;
  }