This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new e7db20b029 fix possible Pattern collision with obj.value == {} (#794)
e7db20b029 is described below

commit e7db20b029b7846df6291a35d9b82b096ce1fbef
Author: Johannes Renoth <ren...@users.noreply.github.com>
AuthorDate: Mon Feb 26 12:33:04 2024 +0100

    fix possible Pattern collision with obj.value == {} (#794)
    
    Co-authored-by: renoth <johan...@renoth.dev>
---
 .../wicket/util/tester/BaseWicketTester.java       | 188 +++++++++++----------
 1 file changed, 99 insertions(+), 89 deletions(-)

diff --git 
a/wicket-tester/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 
b/wicket-tester/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
index b9ada8c0db..4c409658f0 100644
--- 
a/wicket-tester/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
+++ 
b/wicket-tester/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
@@ -37,11 +37,6 @@ import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Pattern;
 
-import jakarta.servlet.FilterConfig;
-import jakarta.servlet.ServletContext;
-import jakarta.servlet.http.Cookie;
-import jakarta.servlet.http.HttpSession;
-
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
 import org.apache.wicket.IPageManagerProvider;
@@ -135,6 +130,11 @@ import org.apache.wicket.util.visit.IVisitor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletContext;
+import jakarta.servlet.http.Cookie;
+import jakarta.servlet.http.HttpSession;
+
 /**
  * A helper class to ease unit testing of Wicket applications without the need 
for a servlet
  * container. See javadoc of <code>WicketTester</code> for example usage. This 
class can be used as
@@ -287,9 +287,11 @@ public class BaseWicketTester
                        Args.notNull(application, "application");
                }
 
-               servletContext = servletCtx != null ? servletCtx
+               servletContext = servletCtx != null
+                       ? servletCtx
                        // If it's provided from the container it's not 
necessary to mock.
-                       : !init && application.getServletContext() != null ? 
application.getServletContext()
+                       : !init && application.getServletContext() != null
+                               ? application.getServletContext()
                                : new MockServletContext(application, null);
 
                // If using Arquillian and it's configured in a web.xml it'll 
be provided. If not, mock it.
@@ -389,8 +391,8 @@ public class BaseWicketTester
                request.setURL(request.getContextPath() + 
request.getServletPath() + "/");
 
                // assign protocol://host:port to next request unless the last 
request was ajax
-               final boolean assignBaseLocation = lastRequest != null &&
-                       lastRequest.getHeader("Wicket-Ajax") == null;
+               final boolean assignBaseLocation = lastRequest != null
+                       && lastRequest.getHeader("Wicket-Ajax") == null;
 
                // resume request processing with scheme://host:port from last 
request
                if (assignBaseLocation)
@@ -494,20 +496,21 @@ public class BaseWicketTester
         */
        protected void cleanupFeedbackMessages(IFeedbackMessageFilter filter)
        {
-               
+
                IVisitor<Component, Void> clearer = new IVisitor<Component, 
Void>()
                {
                        @Override
                        public void component(Component component, IVisit<Void> 
visit)
                        {
-                               if (component.hasFeedbackMessage()) {
+                               if (component.hasFeedbackMessage())
+                               {
                                        
component.getFeedbackMessages().clear(filter);
                                }
                        }
                };
                clearer.component(getLastRenderedPage(), null);
                getLastRenderedPage().visitChildren(clearer);
-               
+
                getSession().getFeedbackMessages().clear(filter);
        }
 
@@ -1002,16 +1005,15 @@ public class BaseWicketTester
         * @throws IOException
         * @throws ParseException
         */
-       public String getWicketAjaxBaseUrlEncodedInLastResponse()
-               throws IOException, ParseException
+       public String getWicketAjaxBaseUrlEncodedInLastResponse() throws 
IOException, ParseException
        {
                XmlPullParser parser = new XmlPullParser();
                parser.parse(getLastResponseAsString());
                XmlTag tag;
                while ((tag = parser.nextTag()) != null)
                {
-                       if (tag.isOpen() && tag.getName().equals("script") &&
-                               
"wicket-ajax-base-url".equals(tag.getAttribute("id")))
+                       if (tag.isOpen() && tag.getName().equals("script")
+                               && 
"wicket-ajax-base-url".equals(tag.getAttribute("id")))
                        {
                                parser.next();
                                return 
parser.getString().toString().split("\\\"")[1];
@@ -1377,9 +1379,9 @@ public class BaseWicketTester
 
                // process the component
                C started = startComponentInPage(comp, pageMarkup);
-               
+
                componentInPage.isInstantiated = true;
-               
+
                return started;
        }
 
@@ -1453,15 +1455,14 @@ public class BaseWicketTester
                                MarkupResourceStream markupResourceStream = new 
MarkupResourceStream(
                                        new StringResourceStream(markup), 
containerInfo, page.getClass());
 
-                               MarkupParser markupParser = 
getApplication().getMarkupSettings()
-                                       .getMarkupFactory()
+                               MarkupParser markupParser = 
getApplication().getMarkupSettings().getMarkupFactory()
                                        .newMarkupParser(markupResourceStream);
                                pageMarkup = markupParser.parse();
                        }
                        catch (Exception e)
                        {
-                               String errorMessage = "Error while parsing the 
markup for the autogenerated page: " +
-                                       e.getMessage();
+                               String errorMessage = "Error while parsing the 
markup for the autogenerated page: "
+                                       + e.getMessage();
                                log.error(errorMessage, e);
                                fail(errorMessage);
                        }
@@ -1484,7 +1485,7 @@ public class BaseWicketTester
 
                componentInPage = new ComponentInPage();
                componentInPage.component = component;
-               
+
                return component;
        }
 
@@ -1498,8 +1499,8 @@ public class BaseWicketTester
         */
        protected String createPageMarkup(final String componentId)
        {
-               return "<html><head></head><body><span wicket:id='" + 
componentId +
-                       "'></span></body></html>";
+               return "<html><head></head><body><span wicket:id='" + 
componentId
+                       + "'></span></body></html>";
        }
 
        /**
@@ -1513,8 +1514,7 @@ public class BaseWicketTester
                return new StartComponentInPage();
        }
 
-       public Component getComponentFromLastRenderedPage(String path,
-                       boolean wantVisibleInHierarchy)
+       public Component getComponentFromLastRenderedPage(String path, boolean 
wantVisibleInHierarchy)
        {
                return getComponentFromLastRenderedPage(path, 
wantVisibleInHierarchy, true);
        }
@@ -1530,8 +1530,8 @@ public class BaseWicketTester
         * @return The component at the path
         * @see org.apache.wicket.MarkupContainer#get(String)
         */
-       public Component getComponentFromLastRenderedPage(String path,
-               boolean wantVisibleInHierarchy, boolean failOnAbsent)
+       public Component getComponentFromLastRenderedPage(String path, boolean 
wantVisibleInHierarchy,
+               boolean failOnAbsent)
        {
                if (componentInPage != null && componentInPage.isInstantiated)
                {
@@ -1547,8 +1547,8 @@ public class BaseWicketTester
                {
                        if (failOnAbsent)
                        {
-                               fail("path: '" + path + "' does not exist for 
page: " +
-                                               
Classes.simpleName(getLastRenderedPage().getClass()));
+                               fail("path: '" + path + "' does not exist for 
page: "
+                                       + 
Classes.simpleName(getLastRenderedPage().getClass()));
                        }
                        return null;
                }
@@ -1578,22 +1578,26 @@ public class BaseWicketTester
        }
 
        /**
-        * Returns the first {@link Component} (breadth-first search) matching 
the given Wicket-ID. If no such
-        * {@link Component} exists it returns {@link Optional#empty()}
+        * Returns the first {@link Component} (breadth-first search) matching 
the given Wicket-ID. If
+        * no such {@link Component} exists it returns {@link Optional#empty()}
         *
         * @param wicketId
         *            the Wicket-ID of the {@link Component} to be found
-        * @return Optional of the component, {@link Optional#empty()} if no 
matching component can be found or wicketId is
-        *         null or blank.
+        * @return Optional of the component, {@link Optional#empty()} if no 
matching component can be
+        *         found or wicketId is null or blank.
         */
-       public Optional<Component> getFirstComponentByWicketId(String wicketId) 
{
-               if (wicketId == null || wicketId.isBlank()) {
+       public Optional<Component> getFirstComponentByWicketId(String wicketId)
+       {
+               if (wicketId == null || wicketId.isBlank())
+               {
                        return Optional.empty();
                }
 
-               if (getLastRenderedPage() != null && componentInPage != null) {
+               if (getLastRenderedPage() != null && componentInPage != null)
+               {
                        Component component = 
getLastRenderedPage().visitChildren((c, visit) -> {
-                               if (c.getId().equals(wicketId)) {
+                               if (c.getId().equals(wicketId))
+                               {
                                        visit.stop(c);
                                }
                        });
@@ -1605,24 +1609,28 @@ public class BaseWicketTester
        }
 
        /**
-        * Returns a {@link List} of all {@link Component}s matching the given 
Wicket-ID. Returns an empty list if no such
-        * component can be found or the Wicket-ID is null.
+        * Returns a {@link List} of all {@link Component}s matching the given 
Wicket-ID. Returns an
+        * empty list if no such component can be found or the Wicket-ID is 
null.
         *
         * @param wicketId
         *            the Wicket-ID of the components to be found
-        * @return A list of all {@link Component}s that have the given 
Wicket-ID, an empty List if no such component can be
-        *         found. Returns an empty List of wicketId is null or blank.
+        * @return A list of all {@link Component}s that have the given 
Wicket-ID, an empty List if no
+        *         such component can be found. Returns an empty List of 
wicketId is null or blank.
         */
-       public List<Component> getAllComponentsByWicketId(String wicketId) {
+       public List<Component> getAllComponentsByWicketId(String wicketId)
+       {
                var result = new ArrayList<Component>();
 
-               if (wicketId == null || wicketId.isBlank()) {
+               if (wicketId == null || wicketId.isBlank())
+               {
                        return result;
                }
 
-               if (getLastRenderedPage() != null && componentInPage != null) {
+               if (getLastRenderedPage() != null && componentInPage != null)
+               {
                        getLastRenderedPage().visitChildren((c, visit) -> {
-                               if (c.getId().equals(wicketId)) {
+                               if (c.getId().equals(wicketId))
+                               {
                                        result.add(c);
                                }
                        });
@@ -1664,8 +1672,8 @@ public class BaseWicketTester
                Component component = assertExists(path);
 
                return isTrue(
-                       "component '" + Classes.name(component.getClass()) + "' 
is not of type: " +
-                               Classes.name(expectedComponentClass),
+                       "component '" + Classes.name(component.getClass()) + "' 
is not of type: "
+                               + Classes.name(expectedComponentClass),
                        
expectedComponentClass.isAssignableFrom(component.getClass()));
        }
 
@@ -1683,8 +1691,8 @@ public class BaseWicketTester
                Component component = getComponentFromLastRenderedPage(path, 
false);
                if (component == null)
                {
-                       result = Result.fail("path: '" + path + "' does not 
exist for page: " +
-                               
Classes.simpleName(getLastRenderedPage().getClass()));
+                       result = Result.fail("path: '" + path + "' does not 
exist for page: "
+                               + 
Classes.simpleName(getLastRenderedPage().getClass()));
                }
                else
                {
@@ -1709,8 +1717,8 @@ public class BaseWicketTester
                Component component = getComponentFromLastRenderedPage(path, 
false);
                if (component == null)
                {
-                       result = Result.fail("path: '" + path + "' does not 
exist for page: " +
-                               
Classes.simpleName(getLastRenderedPage().getClass()));
+                       result = Result.fail("path: '" + path + "' does not 
exist for page: "
+                               + 
Classes.simpleName(getLastRenderedPage().getClass()));
                }
                else
                {
@@ -1754,8 +1762,8 @@ public class BaseWicketTester
                Component component = getComponentFromLastRenderedPage(path);
                if (component == null)
                {
-                       fail("path: '" + path + "' does not exist for page: " +
-                               
Classes.simpleName(getLastRenderedPage().getClass()));
+                       fail("path: '" + path + "' does not exist for page: "
+                               + 
Classes.simpleName(getLastRenderedPage().getClass()));
                        return null;
                }
                return component;
@@ -1766,8 +1774,8 @@ public class BaseWicketTester
                Component component = getComponentFromLastRenderedPage(path, 
true, false);
                if (component != null)
                {
-                       fail("path: '" + path + "' does exists for page: " +
-                                       
Classes.simpleName(getLastRenderedPage().getClass()));
+                       fail("path: '" + path + "' does exists for page: "
+                               + 
Classes.simpleName(getLastRenderedPage().getClass()));
                }
        }
 
@@ -1914,8 +1922,8 @@ public class BaseWicketTester
                        // 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.");
+                               fail("Link " + path + "is an AjaxLink and will "
+                                       + "not be invoked when AJAX 
(javascript) is disabled.");
                        }
 
                        List<AjaxEventBehavior> behaviors = WicketTesterHelper
@@ -1932,8 +1940,8 @@ public class BaseWicketTester
                        // 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.");
+                               fail("Link " + path + " is an AjaxSubmitLink 
and "
+                                       + "will not be invoked when AJAX 
(javascript) is disabled.");
                        }
 
                        AjaxSubmitLink link = (AjaxSubmitLink)linkComponent;
@@ -1945,8 +1953,8 @@ public class BaseWicketTester
                                
(AjaxFormSubmitBehavior)WicketTesterHelper.findAjaxEventBehavior(link, 
"click"));
                }
                // if the link is an IAjaxLink, use it (do check if AJAX is 
expected)
-               else if (isAjax &&
-                       (linkComponent instanceof IAjaxLink || linkComponent 
instanceof AjaxFallbackLink))
+               else if (isAjax
+                       && (linkComponent instanceof IAjaxLink || linkComponent 
instanceof AjaxFallbackLink))
                {
                        List<AjaxEventBehavior> behaviors = WicketTesterHelper
                                .findAjaxEventBehaviors(linkComponent, "click");
@@ -2009,8 +2017,8 @@ public class BaseWicketTester
                                }
                                catch (Exception e)
                                {
-                                       throw new 
WicketRuntimeException("Internal error in WicketTester. " +
-                                               "Please report this in Wicket's 
Issue Tracker.", e);
+                                       throw new 
WicketRuntimeException("Internal error in WicketTester. "
+                                               + "Please report this in 
Wicket's Issue Tracker.", e);
                                }
                        }
                        else if (link instanceof ResourceLink)
@@ -2036,8 +2044,8 @@ public class BaseWicketTester
                // The link requires AJAX
                else if (linkComponent instanceof IAjaxLink && isAjax == false)
                {
-                       fail("Link " + path + "is an IAjaxLink and will " +
-                               "not be invoked when AJAX (javascript) is 
disabled.");
+                       fail("Link " + path + "is an IAjaxLink and will "
+                               + "not be invoked when AJAX (javascript) is 
disabled.");
                }
                else
                {
@@ -2088,8 +2096,8 @@ public class BaseWicketTester
         */
        private Url transform(final Url url)
        {
-               while (url.getSegments().size() > 0 &&
-                       (url.getSegments().get(0).equals("..") || 
url.getSegments().get(0).equals(".")))
+               while (url.getSegments().size() > 0
+                       && (url.getSegments().get(0).equals("..") || 
url.getSegments().get(0).equals(".")))
                {
                        url.getSegments().remove(0);
                }
@@ -2148,8 +2156,9 @@ public class BaseWicketTester
         * @param expectedDocument
         *            expected output
         * @return a <code>Result</code>
-     */
-       public Result isResultPage(final String expectedDocument) {
+        */
+       public Result isResultPage(final String expectedDocument)
+       {
                // Validate the document
                String document = getLastResponseAsString();
                return isTrue("expected rendered page equals", 
document.equals(expectedDocument));
@@ -2258,8 +2267,10 @@ public class BaseWicketTester
                {
                        if (obj.path.matches(".*" + filter + ".*"))
                        {
-                               log.info("[{}{}] path\t" + obj.path + " \t" + 
obj.type + " \t[" + obj.value + "]",
-                                       obj.isEnabled ? "E" : "-", 
obj.isVisible ? "V" : "-");
+                               var enabled = obj.isEnabled ? "E" : "-";
+                               var visible = obj.isVisible ? "V" : "-";
+                               log.info("[{}{}] path\t{} \t{} \t[{}]", 
enabled, visible, obj.path, obj.type,
+                                       obj.value);
                        }
                }
        }
@@ -2289,9 +2300,9 @@ public class BaseWicketTester
                String componentInfo = component.toString();
                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. " +
-                               componentInfo;
+                       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. "
+                               + componentInfo;
                        result = isTrue(failMessage, 
component.getOutputMarkupPlaceholderTag());
                        if (result.wasFailed())
                        {
@@ -2303,13 +2314,12 @@ public class BaseWicketTester
                String ajaxResponse = getLastResponseAsString();
 
                // 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() 
or " +
-                       "#executeAjaxEvent(), before using this assertion 
method";
+               failMessage = "The previous response was not an AJAX response. "
+                       + "You need to execute an AJAX event, using 
#clickLink() or "
+                       + "#executeAjaxEvent(), before using this assertion 
method";
                boolean isAjaxResponse = Pattern
                        .compile("^<\\?xml version=\"1.0\" 
encoding=\".*?\"\\?><ajax-response>")
-                       .matcher(ajaxResponse)
-                       .find();
+                       .matcher(ajaxResponse).find();
                result = isTrue(failMessage, isAjaxResponse);
                if (result.wasFailed())
                {
@@ -2319,8 +2329,8 @@ public class BaseWicketTester
                // 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. " + componentInfo;
+               failMessage = "The component doesn't have a markup id, "
+                       + "which means that it can't have been added to the 
AJAX response. " + componentInfo;
                result = isTrue(failMessage, !Strings.isEmpty(markupId));
                if (result.wasFailed())
                {
@@ -2696,15 +2706,15 @@ public class BaseWicketTester
                if (component.isVisibleInHierarchy() == false)
                {
                        res = Result.fail(
-                               "The component is currently not visible in the 
hierarchy and thus you can not be used." +
-                                       " Component: " + component);
+                               "The component is currently not visible in the 
hierarchy and thus you can not be used."
+                                       + " Component: " + component);
                }
 
                if (component.isEnabledInHierarchy() == false)
                {
                        res = Result.fail(
-                               "The component is currently not enabled in the 
hierarchy and thus you can not be used." +
-                                       " Component: " + component);
+                               "The component is currently not enabled in the 
hierarchy and thus you can not be used."
+                                       + " Component: " + component);
                }
 
                if (throwException && res.wasFailed())
@@ -2913,8 +2923,8 @@ public class BaseWicketTester
                                        if 
(handler.getPageProvider().hasPageInstance())
                                        {
                                                Page renderedPage = 
(Page)handler.getPageProvider().getPageInstance();
-                                               if (componentInPage != null && 
lastPage != null && renderedPage != null &&
-                                                       lastPage.getPageClass() 
!= renderedPage.getPageClass())
+                                               if (componentInPage != null && 
lastPage != null && renderedPage != null
+                                                       && 
lastPage.getPageClass() != renderedPage.getPageClass())
                                                {
                                                        // WICKET-3913: reset 
startComponent if a new page
                                                        // type is rendered

Reply via email to