/**
  * assert component class
  *
  * @param path path to component
  * @param expectedComponentClass expected component class
  */
 public void assertComponent(String path, Class<?> expectedComponentClass) {
   Component component = getComponentFromLastRenderedPage(path);
   Assert.assertTrue(
       "component '"
           + Classes.simpleName(component.getClass())
           + "' is not type:"
           + Classes.simpleName(expectedComponentClass),
       expectedComponentClass.isAssignableFrom(component.getClass()));
 }
  /**
   * assert component visible.
   *
   * @param path path to component
   */
  public void assertVisible(String path) {
    Component component = getLastRenderedPage().get(path);
    if (component == null) {
      Assert.fail(
          "path: '"
              + path
              + "' does no exist for page: "
              + Classes.simpleName(getLastRenderedPage().getClass()));
    }

    Assert.assertTrue("component '" + path + "' is not visible", component.isVisible());
  }
 /**
  * Gets the component with the given path from last rendered page. This method fails in case the
  * component couldn't be found, and it will return null if the component was found, but is not
  * visible.
  *
  * @param path Path to component
  * @return The component at the path
  * @see wicket.MarkupContainer#get(String)
  */
 public Component getComponentFromLastRenderedPage(String path) {
   final Component component = getLastRenderedPage().get(path);
   if (component == null) {
     Assert.fail(
         "path: '"
             + path
             + "' does no exist for page: "
             + Classes.simpleName(getLastRenderedPage().getClass()));
     return component;
   }
   if (component.isVisibleInHierarchy()) {
     return component;
   }
   return null;
 }
  /**
   * Test that a component has been added to a AjaxRequestTarget, using {@link
   * AjaxRequestTarget#addComponent(Component)}. This method actually tests that a component is on
   * the AJAX response sent back to the client.
   *
   * <p>PLEASE NOTE! This method doesn't actually insert the component in the client DOM tree, using
   * javascript. But it shouldn't be needed because you have to trust that the Wicket Ajax
   * Javascript just works.
   *
   * @param component The component to test whether it's on the response.
   */
  public void assertComponentOnAjaxResponse(Component component) {
    String failMessage = "A component which is null could not have been added to the AJAX response";
    Assert.assertNotNull(failMessage, component);

    // Get the AJAX response
    String ajaxResponse = getServletResponse().getDocument();

    // Test that the previous response was actually a AJAX response
    failMessage =
        "The Previous response was not an AJAX response. "
            + "You need to execute an AJAX event, using clickLink, before using this assert";
    boolean isAjaxResponse =
        ajaxResponse.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ajax-response>");
    Assert.assertTrue(failMessage, isAjaxResponse);

    // See if the component has a markup id
    String markupId = component.getMarkupId();

    failMessage =
        "The component doesn't have a markup id, "
            + "which means that it can't have been added to the AJAX response";
    Assert.assertFalse(failMessage, Strings.isEmpty(markupId));

    // Look for that the component is on the response, using the markup id
    boolean isComponentInAjaxResponse =
        ajaxResponse.matches(".*<component id=\"" + markupId + "\" ?>.*");
    failMessage = "Component wasn't found in the AJAX response";
    Assert.assertTrue(failMessage, isComponentInAjaxResponse);
  }
  /**
   * Sets a parameter for the component with the given path to be used with the next request. NOTE:
   * this method only works when a page was rendered first.
   *
   * @param componentPath path of the component
   * @param value the parameter value to set
   */
  public void setParameterForNextRequest(String componentPath, Object value) {
    if (getLastRenderedPage() == null) {
      Assert.fail("before using this method, at least one page has to be rendered");
    }

    Component c = getComponentFromLastRenderedPage(componentPath);
    if (c == null) {
      Assert.fail("component " + componentPath + " was not found");
      return;
    }

    if (c instanceof FormComponent) {
      getParametersForNextRequest().put(((FormComponent) c).getInputName(), value);
    } else {
      getParametersForNextRequest().put(c.getPath(), value);
    }
  }
  /**
   * Encode a listener interface target.
   *
   * <p>If you override this method to behave different then also {@link
   * #addInterfaceParameters(Request, RequestParameters)} should be overridden to by in sync with
   * that behaviour.
   *
   * @param requestCycle the current request cycle
   * @param requestTarget the target to encode
   * @return the encoded url
   */
  protected CharSequence encode(
      RequestCycle requestCycle, IListenerInterfaceRequestTarget requestTarget) {
    final RequestListenerInterface rli = requestTarget.getRequestListenerInterface();

    // Start string buffer for url
    final AppendingStringBuffer url = new AppendingStringBuffer(64);
    url.append(urlPrefix(requestCycle));
    url.append('?');
    url.append(INTERFACE_PARAMETER_NAME);
    url.append('=');

    // Get component and page for request target
    final Component component = requestTarget.getTarget();
    final Page page = component.getPage();

    // Add pagemap
    final PageMap pageMap = page.getPageMap();
    if (!pageMap.isDefault()) {
      url.append(pageMap.getName());
    }
    url.append(Component.PATH_SEPARATOR);

    // Add path to component
    url.append(component.getPath());
    url.append(Component.PATH_SEPARATOR);

    // Add version
    final int versionNumber = component.getPage().getCurrentVersionNumber();
    if (!rli.getRecordsPageVersion()) {
      url.append(Page.LATEST_VERSION);
    } else if (versionNumber > 0) {
      url.append(versionNumber);
    }
    url.append(Component.PATH_SEPARATOR);

    // Add listener interface
    final String listenerName = rli.getName();
    if (!IRedirectListener.INTERFACE.getName().equals(listenerName)) {
      url.append(listenerName);
    }

    return requestCycle.getOriginalResponse().encodeURL(url);
  }
  /**
   * Simulate that an AJAX event has been fired. You add an AJAX event to a component by using:
   *
   * <pre>
   *      ...
   *      component.add(new AjaxEventBehavior(ClientEvent.DBLCLICK) {
   *          public void onEvent(AjaxRequestTarget) {
   *              // Do something.
   *          }
   *      });
   *      ...
   * </pre>
   *
   * You can then test that the code inside onEvent actually does what it's supposed to, using the
   * WicketTester:
   *
   * <pre>
   *      ...
   *      tester.executeAjaxEvent(component, ClientEvent.DBLCLICK);
   *
   *      // Test that the code inside onEvent is correct.
   *      ...
   * </pre>
   *
   * PLEASE NOTE! This method doesn't actually insert the component in the client DOM tree, using
   * javascript.
   *
   * @param component The component which has the AjaxEventBehavior we wan't to test. If the
   *     component is null, the test will fail.
   * @param event The event which we simulate is fired. If the event is null, the test will fail.
   */
  @SuppressWarnings("unchecked")
  public void executeAjaxEvent(Component component, ClientEvent event) {
    String failMessage = "Can't execute event on a component which is null.";
    Assert.assertNotNull(failMessage, component);

    failMessage = "event must not be null";
    Assert.assertNotNull(failMessage, event);

    // Run through all the behavior and select the LAST ADDED behavior which
    // matches the event parameter.
    AjaxEventBehavior ajaxEventBehavior = null;
    List<IBehavior> behaviors = component.getBehaviors();
    for (IBehavior behavior : behaviors) {
      // AjaxEventBehavior is the one to look for
      if (behavior instanceof AjaxEventBehavior) {
        AjaxEventBehavior tmp = (AjaxEventBehavior) behavior;

        if (tmp.getEvent() == event) {
          ajaxEventBehavior = tmp;
        }
      }
    }

    // If there haven't been found any event behaviors on the component
    // which maches the parameters we fail.
    failMessage =
        "No AjaxEventBehavior found on component: "
            + component.getId()
            + " which matches the event: "
            + event.toString();
    Assert.assertNotNull(failMessage, ajaxEventBehavior);

    setupRequestAndResponse();
    RequestCycle requestCycle = createRequestCycle();

    ajaxEventBehavior.onRequest();

    // process the request target
    requestCycle.getRequestTarget().respond(requestCycle);
  }