📄 basewickettester.java
字号:
/** * Throw "standard" WicketRuntimeException * * @param e * @return RuntimeException */ private RuntimeException convertoUnexpect(Exception e) { return new WicketRuntimeException("tester: unexpected", e); } /** * 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 org.apache.wicket.MarkupContainer#get(String) */ public Component getComponentFromLastRenderedPage(String path) { final Component component = getLastRenderedPage().get(path); if (component == null) { fail("path: '" + path + "' does not exist for page: " + Classes.simpleName(getLastRenderedPage().getClass())); return component; } if (component.isVisibleInHierarchy()) { return component; } return null; } /** * assert the text of <code>Label</code> component. * * @param path * path to <code>Label</code> component * @param expectedLabelText * expected label text * @return a <code>Result</code> */ public Result hasLabel(String path, String expectedLabelText) { Label label = (Label)getComponentFromLastRenderedPage(path); return isEqual(expectedLabelText, label.getModelObjectAsString()); } /** * assert <code>PageLink</code> link to page class. * * @param path * path to <code>PageLink</code> component * @param expectedPageClass * expected page class to link * @return a <code>Result</code> */ public Result isPageLink(String path, Class expectedPageClass) { PageLink pageLink = (PageLink)getComponentFromLastRenderedPage(path); try { Field iPageLinkField = pageLink.getClass().getDeclaredField("pageLink"); iPageLinkField.setAccessible(true); IPageLink iPageLink = (IPageLink)iPageLinkField.get(pageLink); return isEqual(expectedPageClass, iPageLink.getPageIdentity()); } catch (SecurityException e) { throw convertoUnexpect(e); } catch (NoSuchFieldException e) { throw convertoUnexpect(e); } catch (IllegalAccessException e) { throw convertoUnexpect(e); } } /** * assert component class * * @param path * path to component * @param expectedComponentClass * expected component class * @return a <code>Result</code> */ public Result isComponent(String path, Class expectedComponentClass) { Component component = getComponentFromLastRenderedPage(path); return isTrue("component '" + Classes.simpleName(component.getClass()) + "' is not type:" + Classes.simpleName(expectedComponentClass), expectedComponentClass.isAssignableFrom(component.getClass())); } /** * assert component visible. * * @param path * path to component * @return a <code>Result</code> */ public Result isVisible(String path) { Component component = getLastRenderedPage().get(path); if (component == null) { fail("path: '" + path + "' does no exist for page: " + Classes.simpleName(getLastRenderedPage().getClass())); } return isTrue("component '" + path + "' is not visible", component.isVisible()); } /** * assert component invisible. * * @param path * path to component * @return a <code>Result</code> */ public Result isInvisible(String path) { return isNull("component '" + path + "' is visible", getComponentFromLastRenderedPage(path)); } /** * assert the content of last rendered page contains(matches) regex pattern. * * @param pattern * reqex pattern to match * @return a <code>Result</code> */ public Result ifContains(String pattern) { return isTrue("pattern '" + pattern + "' not found", getServletResponse().getDocument() .matches("(?s).*" + pattern + ".*")); } /** * assert the model of {@link ListView} use expectedList * * @param path * path to {@link ListView} component * @param expectedList * expected list in the model of {@link ListView} */ public void assertListView(String path, List expectedList) { ListView listView = (ListView)getComponentFromLastRenderedPage(path); WicketTesterHelper.assertEquals(expectedList, listView.getList()); } /** * Click the {@link Link} in the last rendered Page. * <p> * Simulate that AJAX is enabled. * * @see WicketTester#clickLink(String, boolean) * @param path * Click the <code>Link</code> in the last rendered Page. */ public void clickLink(String path) { clickLink(path, true); } /** * Click the {@link Link} in the last rendered Page. * <p> * This method also works for {@link AjaxLink}, {@link AjaxFallbackLink} and * {@link AjaxSubmitLink}. * <p> * On AjaxLinks and AjaxFallbackLinks the onClick method is invoked with a valid * AjaxRequestTarget. In that way you can test the flow of your application when using AJAX. * <p> * When clicking an AjaxSubmitLink the form, which the AjaxSubmitLink is attached to is first * submitted, and then the onSubmit method on AjaxSubmitLink is invoked. If you have changed * some values in the form during your test, these will also be submitted. This should not be * used as a replacement for the {@link FormTester} to test your forms. It should be used to * test that the code in your onSubmit method in AjaxSubmitLink actually works. * <p> * This method is also able to simulate that AJAX (javascript) is disabled on the client. This * is done by setting the isAjax parameter to false. If you have an AjaxFallbackLink you can * then check that it doesn't fail when invoked as a normal link. * * @param path * path to <code>Link</code> component * @param isAjax * Whether to simulate that AJAX (javascript) is enabled or not. If it's false then * AjaxLink and AjaxSubmitLink will fail, since it wouldn't work in real life. * AjaxFallbackLink will be invoked with null as the AjaxRequestTarget parameter. */ public void clickLink(String path, boolean isAjax) { Component linkComponent = getComponentFromLastRenderedPage(path); // if the link is an AjaxLink, we process it differently // than a normal link if (linkComponent instanceof AjaxLink) { // If it's not ajax we fail if (isAjax == false) { fail("Link " + path + "is an AjaxLink and will " + "not be invoked when AJAX (javascript) is disabled."); } AjaxLink link = (AjaxLink)linkComponent; setupRequestAndResponse(true); RequestCycle requestCycle = createRequestCycle(); AjaxRequestTarget target = new AjaxRequestTarget(link.getPage()); requestCycle.setRequestTarget(target); link.onClick(target); // process the request target target.respond(requestCycle); requestCycle.detach(); } // AjaxFallbackLinks is processed like an AjaxLink if isAjax is true // If it's not handling of the linkComponent is passed through to the // Link. else if (linkComponent instanceof AjaxFallbackLink && isAjax) { AjaxFallbackLink link = (AjaxFallbackLink)linkComponent; setupRequestAndResponse(true); RequestCycle requestCycle = createRequestCycle(); AjaxRequestTarget target = new AjaxRequestTarget(link.getPage()); requestCycle.setRequestTarget(target); link.onClick(target); // process the request target target.respond(requestCycle); requestCycle.detach(); } // if the link is an AjaxSubmitLink, we need to find the form // from it using reflection so we know what to submit. else if (linkComponent instanceof AjaxSubmitLink) { // If it's not ajax we fail if (isAjax == false) { fail("Link " + path + "is an AjaxSubmitLink and " + "will not be invoked when AJAX (javascript) is disabled."); } AjaxSubmitLink link = (AjaxSubmitLink)linkComponent; // We cycle through the attached behaviors and select the // LAST matching behavior as the one we handle. List behaviors = link.getBehaviors(); AjaxFormSubmitBehavior ajaxFormSubmitBehavior = null; for (Iterator iter = behaviors.iterator(); iter.hasNext();) { Object behavior = iter.next(); if (behavior instanceof AjaxFormSubmitBehavior) { AjaxFormSubmitBehavior submitBehavior = (AjaxFormSubmitBehavior)behavior; ajaxFormSubmitBehavior = submitBehavior; } } String failMessage = "No form submit behavior found on the submit link. Strange!!"; notNull(failMessage, ajaxFormSubmitBehavior); setupRequestAndResponse(true); RequestCycle requestCycle = createRequestCycle(); submitAjaxFormSubmitBehavior(ajaxFormSubmitBehavior); // Ok, finally we "click" the link ajaxFormSubmitBehavior.onRequest(); // process the request target requestCycle.getRequestTarget().respond(requestCycle); requestCycle.detach(); } // if the link is a normal link (or ResourceLink) else if (linkComponent instanceof AbstractLink) { AbstractLink link = (AbstractLink)linkComponent; /* * If the link is a bookmarkable link, then we need to transfer the parameters to the * next request. */ if (link instanceof BookmarkablePageLink) { BookmarkablePageLink bookmarkablePageLink = (BookmarkablePageLink)link; try { Field parametersField = BookmarkablePageLink.class.getDeclaredField("parameters"); Method getParametersMethod = BookmarkablePageLink.class.getDeclaredMethod( "getPageParameters", null); getParametersMethod.setAccessible(true); PageParameters parameters = (PageParameters)getParametersMethod.invoke( bookmarkablePageLink, null); setParametersForNextRequest(parameters); } catch (Exception e) { fail("Internal error in WicketTester. " + "Please report this in Wickets Issue Tracker."); } } executeListener(link); } else { fail("Link " + path + " is not a Link, AjaxLink, AjaxFallbackLink or AjaxSubmitLink"); } } /** * Submits the <code>Form</code> in the last rendered <code>Page</code>. * * @param path * path to <code>Form</code> component */ public void submitForm(String path) { Form form = (Form)getComponentFromLastRenderedPage(path); executeListener(form); } /** * Sets a parameter for the <code>Component</code> with the given path to be used with the * next request. * <p> * NOTE: this method only works when a <code>Page</code> was rendered first. * * @param componentPath * path to the <code>Component</code> * @param value * the parameter value to set */ public void setParameterForNextRequest(String componentPath, Object value) { if (getLastRenderedPage() == null) { fail("before using this method, at least one page has to be rendered"); } Component c = getComponentFromLastRenderedPage(componentPath); if (c == null) { fail("component " + componentPath + " was not found"); return; } if (c instanceof FormComponent) { getParametersForNextRequest().put(((FormComponent)c).getInputName(), value); } else { getParametersForNextRequest().put(c.getPath(), value); } } /** * Asserts the last rendered <code>Page</code> class. * * FIXME explain why the code is so complicated to compare two classes, or simplify * * @param expectedRenderedPageClass * expected class of last rendered page * @return a <code>Result</code> */ public Result isRenderedPage(Class expectedRenderedPageClass) { Page page = getLastRenderedPage(); if (page == null) { return Result.fail("page was null"); } if (!page.getClass().isAssignableFrom(expectedRenderedPageClass)) { return isEqual(Classes.simpleName(expectedRenderedPageClass), Classes.simpleName(page.getClass())); } return Result.pass(); } /** * Asserts last rendered <code>Page</code> against an expected HTML document. * <p> * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the * expected output file. * </p> * * @param pageClass * used to load the <code>File</code> (relative to <code>clazz</code> package) * @param filename * expected output <code>File</code> name * @throws Exception */ public void assertResultPage(final Class pageClass, final String filename) throws Exception { // Validate the document String document = getServletResponse().getDocument(); DiffUtil.validatePage(document, pageClass, filename, true); } /** * Asserts last rendered <code>Page</code> against an expected HTML document as a * <code>String</code>. * * @param expectedDocument * expected output * @return a <code>Result</code> * @throws Exception */ public Result isResultPage(final String expectedDocument) throws Exception { // Validate the document String document = getServletResponse().getDocument(); return isTrue("expected rendered page equals", document.equals(expectedDocument)); } /** * Asserts no error-level feedback messages. * * @return a <code>Result</code> */ public Result hasNoErrorMessage() { List messages = getMessages(FeedbackMessage.ERROR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -