proper formatting of WicketTester.java
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c30e3291 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c30e3291 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c30e3291 Branch: refs/heads/WICKET-6563 Commit: c30e329162984facf76fdf3686b6bc0cd44c91aa Parents: b0a96e9 Author: renoth <[email protected]> Authored: Tue Jul 31 13:54:07 2018 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Wed Sep 26 16:10:12 2018 +0300 ---------------------------------------------------------------------- .../apache/wicket/util/tester/FormTester.java | 3 +- .../apache/wicket/util/tester/WicketTester.java | 280 +++++++++---------- 2 files changed, 138 insertions(+), 145 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/c30e3291/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java index e5d71a6..81550bd 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java @@ -580,7 +580,8 @@ public class FormTester public FormTester setValue(final String formComponentId, final String value) { Component component = workingForm.get(formComponentId); - assertNotNull(component, "Unable to set value. Couldn't find component with name: " + formComponentId); + assertNotNull(component, "Unable to set value. Couldn't find component with name: " + + formComponentId); return setValue(component, value); } http://git-wip-us.apache.org/repos/asf/wicket/blob/c30e3291/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java index e4e08df..66af566 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/WicketTester.java @@ -148,14 +148,16 @@ import org.slf4j.LoggerFactory; * * Cookie handling: * - * There are some expectations about wicket tester cookie handling which should match as best as it - * can be with a real client server request response cycle: - all valid cookies set before a request - * is made (tester.getRequest().addCookie()) should appear in the page request - all cookies set in - * the response should appear in the last response (tester.getLastResponse()) after the request is - * made (expired cookies and others) - all cookies set in the response should appear even after a - * redirect response is made until the final response (tester.getLastResponse()) is written to the - * client (wicket tester) - all valid cookies (maxAge!=0) from the last response should be added to - * the next request cookies (tester.getRequest().getCookies()) + * There are some expectations about wicket tester cookie handling which should match as best as + * it can be with a real client server request response cycle: + * - all valid cookies set before a request is made (tester.getRequest().addCookie()) should + * appear in the page request + * - all cookies set in the response should appear in the last response (tester.getLastResponse()) + * after the request is made (expired cookies and others) + * - all cookies set in the response should appear even after a redirect response is made + * until the final response (tester.getLastResponse()) is written to the client (wicket tester) + * - all valid cookies (maxAge!=0) from the last response should be added to + * the next request cookies (tester.getRequest().getCookies()) * * * TODO General: Example usage of FormTester @@ -258,41 +260,19 @@ public class WicketTester extends BaseWicketTester } /** - * Returns the current Maven build directory taken from the <tt>basedir</tt> system property, or - * null if not set - * - * @return path with a trailing slash - */ - public static String getBasedir() - { - String basedir = System.getProperty("basedir"); - if (basedir != null) - { - basedir = basedir + "/"; - } - else - { - basedir = ""; - } - return basedir; - } - - /** * Asserts that the Ajax location header is present. */ public void assertAjaxLocation() { if (null != getLastResponse().getHeader("Location")) { - throw new AssertionFailedError( - "Location header should *not* be present when using Ajax"); + throw new AssertionFailedError("Location header should *not* be present when using Ajax"); } String ajaxLocation = getLastResponse().getHeader("Ajax-Location"); if (null == ajaxLocation) { - throw new AssertionFailedError( - "Ajax-Location header should be present when using Ajax"); + throw new AssertionFailedError("Ajax-Location header should be present when using Ajax"); } int statusCode = getLastResponse().getStatus(); @@ -316,19 +296,8 @@ public class WicketTester extends BaseWicketTester } /** - * - * @param result - */ - private void assertResult(Result result) - { - if (result.wasFailed()) - { - throw new AssertionFailedError(result.getMessage()); - } - } - - /** - * Asserts that the <code>Component</code> a the given path has a behavior of the given type. + * Asserts that the <code>Component</code> a the given path has a behavior + * of the given type. * * @param path * path to <code>Component</code> @@ -342,44 +311,44 @@ public class WicketTester extends BaseWicketTester Component component = assertExists(path); List<? extends Behavior> behaviors = component.getBehaviors(expectedBehaviorClass); final String message = String.format("Component '%s' has no behaviors of type '%s'", - component.getPageRelativePath(), expectedBehaviorClass); + component.getPageRelativePath(), expectedBehaviorClass); assertResult(new Result(CollectionUtils.isEmpty(behaviors), message)); } /** * Tests that a <code>Component</code> has been added to a <code>AjaxRequestTarget</code>, using - * {@link org.apache.wicket.ajax.AjaxRequestTarget#add(Component...)}. This method actually - * tests that a <code>Component</code> is on the Ajax response sent back to the client. + * {@link org.apache.wicket.ajax.AjaxRequestTarget#add(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 just have to trust that Wicket * Ajax JavaScript works. * - * @param componentPath - * a <code>Component</code> path to test + * @param component + * a <code>Component</code> to be tested */ - public void assertComponentOnAjaxResponse(String componentPath) + public void assertComponentOnAjaxResponse(Component component) { - Component component = getComponentFromLastRenderedPage(componentPath, false); - assertComponentOnAjaxResponse(component); + Result result = isComponentOnAjaxResponse(component); + assertResult(result); } /** * Tests that a <code>Component</code> has been added to a <code>AjaxRequestTarget</code>, using - * {@link org.apache.wicket.ajax.AjaxRequestTarget#add(Component...)}. This method actually - * tests that a <code>Component</code> is on the Ajax response sent back to the client. + * {@link org.apache.wicket.ajax.AjaxRequestTarget#add(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 just have to trust that Wicket * Ajax JavaScript works. * - * @param component - * a <code>Component</code> to be tested + * @param componentPath + * a <code>Component</code> path to test */ - public void assertComponentOnAjaxResponse(Component component) + public void assertComponentOnAjaxResponse(String componentPath) { - Result result = isComponentOnAjaxResponse(component); - assertResult(result); + Component component = getComponentFromLastRenderedPage(componentPath, false); + assertComponentOnAjaxResponse(component); } /** @@ -408,9 +377,9 @@ public class WicketTester extends BaseWicketTester * Asserts that a component's markup has loaded with the given variation * * @param component - * The component which markup to check + * The component which markup to check * @param expectedVariation - * The expected variation of the component's markup + * The expected variation of the component's markup */ public void assertMarkupVariation(Component component, String expectedVariation) { @@ -420,44 +389,21 @@ public class WicketTester extends BaseWicketTester String actualVariation = markup.getMarkupResourceStream().getVariation(); if (Objects.equal(expectedVariation, actualVariation) == false) { - result = Result.fail( - String.format("Wrong variation for component '%s'. Actual: '%s', expected: '%s'", + result = Result.fail(String.format("Wrong variation for component '%s'. Actual: '%s', expected: '%s'", component.getPageRelativePath(), actualVariation, expectedVariation)); } assertResult(result); } - private IMarkupFragment getMarkupFragment(Component component) - { - IMarkupFragment markup = null; - if (component instanceof MarkupContainer) - { - markup = ((MarkupContainer)component).getAssociatedMarkup(); - } - - if (markup == null) - { - markup = component.getMarkup(); - } - - if (markup == null) - { - throw new AssertionFailedError(String.format("Cannot find the markup of component: %s", - component.getPageRelativePath())); - } - - return markup; - } - /** * Asserts that a component's markup has loaded with the given style. * * @param component - * The component which markup to check + * The component which markup to check * @param expectedStyle - * The expected style of the component's markup. For example: <em>green</em> in - * <code>MyPanel_green.html</code> + * The expected style of the component's markup. + * For example: <em>green</em> in <code>MyPanel_green.html</code> */ public void assertMarkupStyle(Component component, String expectedStyle) { @@ -467,8 +413,7 @@ public class WicketTester extends BaseWicketTester String actualStyle = markup.getMarkupResourceStream().getStyle(); if (Objects.equal(expectedStyle, actualStyle) == false) { - result = Result - .fail(String.format("Wrong style for component '%s'. Actual: '%s', expected: '%s'", + result = Result.fail(String.format("Wrong style for component '%s'. Actual: '%s', expected: '%s'", component.getPageRelativePath(), actualStyle, expectedStyle)); } @@ -479,9 +424,9 @@ public class WicketTester extends BaseWicketTester * Asserts that a component's markup has loaded with the given locale * * @param component - * The component which markup to check + * The component which markup to check * @param expectedLocale - * The expected locale of the component's markup + * The expected locale of the component's markup */ public void assertMarkupLocale(Component component, Locale expectedLocale) { @@ -491,14 +436,34 @@ public class WicketTester extends BaseWicketTester Locale actualLocale = markup.getMarkupResourceStream().getLocale(); if (Objects.equal(expectedLocale, actualLocale) == false) { - result = Result - .fail(String.format("Wrong locale for component '%s'. Actual: '%s', expected: '%s'", + result = Result.fail(String.format("Wrong locale for component '%s'. Actual: '%s', expected: '%s'", component.getPageRelativePath(), actualLocale, expectedLocale)); } assertResult(result); } + private IMarkupFragment getMarkupFragment(Component component) + { + IMarkupFragment markup = null; + if (component instanceof MarkupContainer) + { + markup = ((MarkupContainer) component).getAssociatedMarkup(); + } + + if (markup == null) + { + markup = component.getMarkup(); + } + + if (markup == null) + { + throw new AssertionFailedError(String.format("Cannot find the markup of component: %s", component.getPageRelativePath())); + } + + return markup; + } + /** * Asserts error-level feedback messages. * @@ -507,8 +472,7 @@ public class WicketTester extends BaseWicketTester */ public void assertErrorMessages(Serializable... expectedErrorMessages) { - assertFeedbackMessages(new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR), - expectedErrorMessages); + assertFeedbackMessages(new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR), expectedErrorMessages); } /** @@ -531,8 +495,7 @@ public class WicketTester extends BaseWicketTester * @param expectedMessages * expected feedback messages */ - public void assertFeedbackMessages(IFeedbackMessageFilter filter, - Serializable... expectedMessages) + public void assertFeedbackMessages(IFeedbackMessageFilter filter, Serializable... expectedMessages) { List<FeedbackMessage> feedbackMessages = getFeedbackMessages(filter); List<Serializable> actualMessages = getActualFeedbackMessages(feedbackMessages); @@ -543,33 +506,31 @@ public class WicketTester extends BaseWicketTester * Asserts that there is a feedback message provided by a given component * * @param component - * the component that provided the expected feedback message. Optional. + * the component that provided the expected feedback message. Optional. * @param key - * the resource key for the feedback message. Mandatory. + * the resource key for the feedback message. Mandatory. * @param model - * the model used for interpolating the feedback message. Optional. + * the model used for interpolating the feedback message. Optional. * @param filter - * the filter that decides in which messages to look in. E.g. with a specific level, - * rendered or not, etc. + * the filter that decides in which messages to look in. E.g. with a specific + * level, rendered or not, etc. */ - public void assertComponentFeedbackMessage(Component component, String key, IModel<?> model, - IFeedbackMessageFilter filter) + public void assertComponentFeedbackMessage(Component component, String key, IModel<?> model, IFeedbackMessageFilter filter) { Args.notNull(key, "key"); - String expectedMessage = getApplication().getResourceSettings().getLocalizer().getString( - key, component, model); + String expectedMessage = getApplication().getResourceSettings().getLocalizer().getString(key, component, model); List<FeedbackMessage> feedbackMessages = getFeedbackMessages(filter); List<Serializable> actualMessages = getActualFeedbackMessages(feedbackMessages); - assertTrue(actualMessages.contains(expectedMessage), String - .format("Feedback message with key '%s' cannot be found in %s", key, actualMessages)); + assertTrue(actualMessages.contains(expectedMessage), + String.format("Feedback message with key '%s' cannot be found in %s", key, actualMessages)); } /** - * Extracts the actual messages from the passed feedback messages. Specially handles - * ValidationErrorFeedback messages by extracting their String message + * Extracts the actual messages from the passed feedback messages. + * Specially handles ValidationErrorFeedback messages by extracting their String message * * @param feedbackMessages * the feedback messages @@ -612,15 +573,15 @@ public class WicketTester extends BaseWicketTester final List<FeedbackMessage> renderedMessages = model.getObject(); if (renderedMessages == null) { - fail( - String.format("feedback panel at path [%s] returned null messages", path)); + throw new AssertionFailedError(String.format("feedback panel at path [%s] returned null messages", path)); } + if (messages.length != renderedMessages.size()) { - fail(String.format( - "you expected '%d' messages for the feedback panel [%s], but there were actually '%d'", - messages.length, path, renderedMessages.size())); + throw new AssertionFailedError(String.format("you expected '%d' messages for the feedback panel [%s], but there were actually '%d'", + messages.length, path, renderedMessages.size())); } + for (int i = 0; i < messages.length && i < renderedMessages.size(); i++) { final Serializable expected = messages[i]; @@ -698,30 +659,19 @@ public class WicketTester extends BaseWicketTester } /** - * 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. - * - * @param clazz - * <code>Class</code> used to load the file (relative to <code>clazz</code> package) - * @param filename - * expected output filename <code>String</code> - * @throws Exception + * Asserts no error-level feedback messages. */ - @Override - public void assertResultPage(final Class<?> clazz, final String filename) throws Exception + public void assertNoErrorMessage() { - String document = getLastResponseAsString(); - DiffUtil.validatePage(document, clazz, filename, true); + assertNoFeedbackMessage(FeedbackMessage.ERROR); } /** - * Asserts no error-level feedback messages. + * Asserts no info-level feedback messages. */ - public void assertNoErrorMessage() + public void assertNoInfoMessage() { - assertNoFeedbackMessage(FeedbackMessage.ERROR); + assertNoFeedbackMessage(FeedbackMessage.INFO); } /** @@ -748,11 +698,22 @@ public class WicketTester extends BaseWicketTester } /** - * Asserts no info-level feedback messages. + * 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. + * + * @param clazz + * <code>Class</code> used to load the file (relative to <code>clazz</code> package) + * @param filename + * expected output filename <code>String</code> + * @throws Exception */ - public void assertNoInfoMessage() + @Override + public void assertResultPage(final Class<?> clazz, final String filename) throws Exception { - assertNoFeedbackMessage(FeedbackMessage.INFO); + String document = getLastResponseAsString(); + DiffUtil.validatePage(document, clazz, filename, true); } /** @@ -825,6 +786,18 @@ public class WicketTester extends BaseWicketTester } /** + * + * @param result + */ + private void assertResult(Result result) + { + if (result.wasFailed()) + { + throw new AssertionFailedError(result.getMessage()); + } + } + + /** * Checks whether a component is visible and/or enabled before usage * * @param component @@ -861,16 +834,15 @@ public class WicketTester extends BaseWicketTester } catch (ClassCastException e) { - throw new IllegalArgumentException( - "Component with id:" + id + " is not a BookmarkablePageLink"); + throw new IllegalArgumentException("Component with id:" + id + + " is not a BookmarkablePageLink"); } - assertEquals(pageClass, pageLink.getPageClass(), - "BookmarkablePageLink: " + id + " is pointing to the wrong page"); + assertEquals(pageClass, + pageLink.getPageClass(), "BookmarkablePageLink: " + id + " is pointing to the wrong page"); - assertEquals(parameters, pageLink.getPageParameters(), - "One or more of the parameters associated with the BookmarkablePageLink: " + id + - " do not match"); + assertEquals(parameters, pageLink.getPageParameters(), "One or more of the parameters associated with the BookmarkablePageLink: " + + id + " do not match"); } /** @@ -998,4 +970,24 @@ public class WicketTester extends BaseWicketTester String actualRedirectUrl = getLastResponse().getRedirectLocation(); assertEquals(expectedRedirectUrl, actualRedirectUrl); } + + /** + * Returns the current Maven build directory taken from the <tt>basedir</tt> system property, or + * null if not set + * + * @return path with a trailing slash + */ + public static String getBasedir() + { + String basedir = System.getProperty("basedir"); + if (basedir != null) + { + basedir = basedir + "/"; + } + else + { + basedir = ""; + } + return basedir; + } }
