📄 basewickettester.java
字号:
return isTrue("expect no error message, but contains\n" + WicketTesterHelper.asLined(messages), messages.isEmpty()); } /** * Asserts no info-level feedback messages. * * @return a <code>Result</code> */ public Result hasNoInfoMessage() { List messages = getMessages(FeedbackMessage.INFO); return isTrue("expect no info message, but contains\n" + WicketTesterHelper.asLined(messages), messages.isEmpty()); } /** * Retrieves <code>FeedbackMessages</code>. * * @param level * level of feedback message, for example: * <code>FeedbackMessage.DEBUG or FeedbackMessage.INFO.. etc</code> * @return <code>List</code> of messages (as <code>String</code>s) * @see FeedbackMessage */ public List getMessages(final int level) { FeedbackMessages feedbackMessages = Session.get().getFeedbackMessages(); List allMessages = feedbackMessages.messages(new IFeedbackMessageFilter() { private static final long serialVersionUID = 1L; public boolean accept(FeedbackMessage message) { return message.getLevel() == level; } }); List actualMessages = new ArrayList(); for (Iterator iter = allMessages.iterator(); iter.hasNext();) { actualMessages.add(((FeedbackMessage)iter.next()).getMessage()); } return actualMessages; } /** * Dumps the source of last rendered <code>Page</code>. */ public void dumpPage() { log.info(getServletResponse().getDocument()); } /** * Dumps the <code>Component</code> trees. */ public void debugComponentTrees() { debugComponentTrees(""); } /** * Dumps the <code>Component</code> trees to log. Show only the <code>Component</code>s * whose paths contain the filter <code>String</code>. * * @param filter * a filter <code>String</code> */ public void debugComponentTrees(String filter) { log.info("debugging ----------------------------------------------"); for (Iterator iter = WicketTesterHelper.getComponentData(getLastRenderedPage()).iterator(); iter.hasNext();) { WicketTesterHelper.ComponentData obj = (WicketTesterHelper.ComponentData)iter.next(); if (obj.path.matches(".*" + filter + ".*")) { log.info("path\t" + obj.path + " \t" + obj.type + " \t[" + obj.value + "]"); } } } /** * Tests that a <code>Component</code> has been added to a <code>AjaxRequestTarget</code>, * using {@link AjaxRequestTarget#addComponent(Component)}. This method actually tests that a * <code>Component</code> is on the Ajax response sent back to the client. * <p> * PLEASE NOTE! This method doesn't actually insert the <code>Component</code> 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 <code>Component</code> to test * @return a <code>Result</code> */ public Result isComponentOnAjaxResponse(Component component) { String failMessage = "A component which is null could not have been added to the AJAX response"; notNull(failMessage, component); Result result; // test that the component renders the placeholder tag if it's not visible if (!component.isVisible()) { failMessage = "A component which is invisible and doesn't render a placeholder tag" + " will not be rendered at all and thus won't be accessible for subsequent AJAX interaction"; result = isTrue(failMessage, component.getOutputMarkupPlaceholderTag()); if (result.wasFailed()) { return result; } } // 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>"); result = isTrue(failMessage, isAjaxResponse); if (result.wasFailed()) { return result; } // 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"; result = isTrue(failMessage, !Strings.isEmpty(markupId)); if (result.wasFailed()) { return result; } // Look for that the component is on the response, using the markup id boolean isComponentInAjaxResponse = ajaxResponse.matches("(?s).*<component id=\"" + markupId + "\" ?>.*"); failMessage = "Component wasn't found in the AJAX response"; return isTrue(failMessage, isComponentInAjaxResponse); } /** * Simulates the firing of an Ajax event. * * @see #executeAjaxEvent(Component, String) * * @since 1.2.3 * @param componentPath * the <code>Component</code> path * @param event * the event which we simulate being fired. If <code>event</code> is * <code>null</code>, the test will fail. */ public void executeAjaxEvent(String componentPath, String event) { Component component = getComponentFromLastRenderedPage(componentPath); executeAjaxEvent(component, event); } /** * Simulates the firing of an Ajax event. You add an Ajax event to a <code>Component</code> by * using: * * <pre> * ... * component.add(new AjaxEventBehavior("ondblclick") { * public void onEvent(AjaxRequestTarget) {} * }); * ... * </pre> * * You can then test that the code inside <code>onEvent</code> actually does what it's * supposed to, using the <code>WicketTester</code>: * * <pre> * ... * tester.executeAjaxEvent(component, "ondblclick"); * // Test that the code inside onEvent is correct. * ... * </pre> * * This also works with <code>AjaxFormSubmitBehavior</code>, where it will "submit" the * <code>Form</code> before executing the command. * <p> * PLEASE NOTE! This method doesn't actually insert the <code>Component</code> in the client * DOM tree, using Javascript. * * * @param component * the <code>Component</code> that has the <code>AjaxEventBehavior</code> we want * to test. If the <code>Component</code> is <code>null</code>, the test will * fail. * @param event * the event to simulate being fired. If <code>event</code> is <code>null</code>, * the test will fail. */ public void executeAjaxEvent(Component component, String event) { String failMessage = "Can't execute event on a component which is null."; notNull(failMessage, component); failMessage = "event must not be null"; notNull(failMessage, event); // Run through all the behavior and select the LAST ADDED behavior which // matches the event parameter. AjaxEventBehavior ajaxEventBehavior = null; List behaviors = component.getBehaviors(); for (Iterator iter = behaviors.iterator(); iter.hasNext();) { IBehavior behavior = (IBehavior)iter.next(); // AjaxEventBehavior is the one to look for if (behavior instanceof AjaxEventBehavior) { AjaxEventBehavior tmp = (AjaxEventBehavior)behavior; if (event.equals(tmp.getEvent())) { ajaxEventBehavior = tmp; } } } // If there haven't been found any event behaviors on the component // which matches the parameters we fail. failMessage = "No AjaxEventBehavior found on component: " + component.getId() + " which matches the event: " + event; notNull(failMessage, ajaxEventBehavior); // initialize the request only if needed to allow the user to pass // request parameters, see // WICKET-254 WebRequestCycle requestCycle; if (RequestCycle.get() == null) { requestCycle = setupRequestAndResponse(true); } else { requestCycle = (WebRequestCycle)RequestCycle.get(); } // when the requestcycle is not created via setupRequestAndResponse(true), it can happen // that the request is not an ajax request -> we have to set the header manually if (!requestCycle.getWebRequest().isAjax()) { HttpServletRequest req = requestCycle.getWebRequest().getHttpServletRequest(); if (req instanceof MockHttpServletRequest) { ((MockHttpServletRequest)req).addHeader("Wicket-Ajax", "Yes"); } } // If the event is an FormSubmitBehavior then also "submit" the form if (ajaxEventBehavior instanceof AjaxFormSubmitBehavior) { AjaxFormSubmitBehavior ajaxFormSubmitBehavior = (AjaxFormSubmitBehavior)ajaxEventBehavior; submitAjaxFormSubmitBehavior(ajaxFormSubmitBehavior); } ajaxEventBehavior.onRequest(); // process the request target processRequestCycle(requestCycle); } /** * Retrieves a <code>TagTester</code> based on a <code>wicket:id</code>. If more * <code>Component</code>s exist with the same <code>wicket:id</code> in the markup, only * the first one is returned. * * @param wicketId * the <code>wicket:id</code> to search for * @return the <code>TagTester</code> for the tag which has the given <code>wicket:id</code> */ public TagTester getTagByWicketId(String wicketId) { return TagTester.createTagByAttribute(getServletResponse().getDocument(), "wicket:id", wicketId); } /** * Retrieves a <code>TagTester</code> based on an DOM id. If more <code>Component</code>s * exist with the same id in the markup, only the first one is returned. * * @param id * the DOM id to search for. * @return the <code>TagTester</code> for the tag which has the given DOM id */ public TagTester getTagById(String id) { return TagTester.createTagByAttribute(getServletResponse().getDocument(), "id", id); } /** * Helper method for all the places where an Ajax call should submit an associated * <code>Form</code>. * * @param behavior * The <code>AjaxFormSubmitBehavior</code> with the <code>Form</code> to "submit" */ private void submitAjaxFormSubmitBehavior(AjaxFormSubmitBehavior behavior) { // We need to get the form submitted, using reflection. // It needs to be "submitted". Form form = null; try { Field formField = AjaxFormSubmitBehavior.class.getDeclaredField("form"); formField.setAccessible(true); form = (Form)formField.get(behavior); } catch (Exception e) { fail(e.getMessage()); } String failMessage = "No form attached to the submitlink."; notNull(failMessage, form); form.visitFormComponents(new FormComponent.AbstractVisitor() { public void onFormComponent(FormComponent formComponent) { if (!(formComponent instanceof Button) && !(formComponent instanceof RadioGroup) && !(formComponent instanceof CheckGroup)) { String name = formComponent.getInputName(); String value = formComponent.getValue(); // Set request parameter with the field value, but do not // modify an existing // request parameter explicitly set using // FormTester.setValue() if (getServletRequest().getParameterMap().get(name) == null) { getServletRequest().setParameter(name, value); } } } }); } /** * Retrieves the content type from the response header. * * @return the content type from the response header */ public String getContentTypeFromResponseHeader() { String contentType = ((MockHttpServletResponse)getWicketResponse().getHttpServletResponse()).getHeader("Content-Type"); if (contentType == null) { throw new WicketRuntimeException("No Content-Type header found"); } return contentType; } /** * Retrieves the content length from the response header. * * @return the content length from the response header */ public int getContentLengthFromResponseHeader() { String contentLength = ((MockHttpServletResponse)getWicketResponse().getHttpServletResponse()).getHeader("Content-Length"); if (contentLength == null) { throw new WicketRuntimeException("No Content-Length header found"); } return Integer.parseInt(contentLength); } /** * Retrieves the last-modified value from the response header. * * @return the last-modified value from the response header */ public String getLastModifiedFromResponseHeader() { return ((MockHttpServletResponse)getWicketResponse().getHttpServletResponse()).getHeader("Last-Modified"); } /** * Retrieves the content disposition from the response header. * * @return the content disposition from the response header */ public String getContentDispositionFromResponseHeader() { return ((MockHttpServletResponse)getWicketResponse().getHttpServletResponse()).getHeader("Content-Disposition"); } private Result isTrue(String message, boolean condition) { if (condition) { return Result.pass(); } return Result.fail(message); } private Result isEqual(Object expected, Object actual) { if (expected == null && actual == null) { return Result.pass(); } if (expected != null && expected.equals(actual)) { return Result.pass(); } String message = "expected:<" + expected + "> but was:<" + actual + ">"; return Result.fail(message); } private void notNull(String message, Object object) { if (object == null) { fail(message); } } private Result isNull(String message, Object object) { if (object != null) { return Result.fail(message); } return Result.pass(); } private void fail(String message) { throw new WicketRuntimeException(message); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -