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

adelbene 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 cb06af2b8e WICKET-7151 Misleading message when a Link component is 
added with wrong id
cb06af2b8e is described below

commit cb06af2b8e20d50f3e3091b5bb7042d51ded2b5e
Author: Andrea Del Bene <[email protected]>
AuthorDate: Thu Mar 20 18:40:07 2025 +0100

    WICKET-7151 Misleading message when a Link component is added with wrong
    id
---
 .../apache/wicket/markup/html/link/LinkTest.java   | 46 +++++++++++++++++++
 .../org/apache/wicket/markup/html/link/Link.java   | 52 ++++++++++++----------
 2 files changed, 75 insertions(+), 23 deletions(-)

diff --git 
a/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/link/LinkTest.java
 
b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/link/LinkTest.java
new file mode 100644
index 0000000000..9ec3c048b9
--- /dev/null
+++ 
b/wicket-core-tests/src/test/java/org/apache/wicket/markup/html/link/LinkTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.link;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.wicket.MockPageWithLink;
+import org.apache.wicket.markup.MarkupNotFoundException;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.jupiter.api.Test;
+
+public class LinkTest extends WicketTestCase
+{
+    @Test
+    void testWrongComponentId()
+    {
+        MockPageWithLink mockPageWithLink = new MockPageWithLink();
+        Link<Void> link = new Link<Void>("linkx")
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            public void onClick()
+            {
+            }
+
+        };
+
+        mockPageWithLink.add(link);
+        assertThrows(MarkupNotFoundException.class, () -> 
tester.startPage(mockPageWithLink));
+    }
+}
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/link/Link.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/link/Link.java
index a374db164f..1943baf657 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/link/Link.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/link/Link.java
@@ -22,6 +22,7 @@ import org.apache.wicket.IRequestListener;
 import org.apache.wicket.Page;
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.MarkupNotFoundException;
 import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.markup.head.OnEventHeaderItem;
 import org.apache.wicket.model.IModel;
@@ -34,7 +35,7 @@ import 
org.apache.wicket.request.mapper.parameter.PageParameters;
  * other element, a click javascript event handler will be added.
  * <p>
  * You can use a link like:
- * 
+ *
  * <pre>
  * add(new Link(&quot;myLink&quot;)
  * {
@@ -44,23 +45,23 @@ import 
org.apache.wicket.request.mapper.parameter.PageParameters;
  *     }
  * );
  * </pre>
- * 
+ *
  * and in your HTML file:
- * 
+ *
  * <pre>
  *  &lt;a href=&quot;#&quot; wicket:id=&quot;myLink&quot;&gt;click 
here&lt;/a&gt;
  * </pre>
- * 
+ *
  * or:
- * 
+ *
  * <pre>
  *  &lt;td wicket:id=&quot;myLink&quot;&gt;my clickable column&lt;/td&gt;
  * </pre>
- * 
+ *
  * <p>
  * The following snippet shows how to pass a parameter from the Page creating 
the Page to the Page
  * responded by the Link.
- * 
+ *
  * <pre>
  * add(new Link&lt;MyObject&gt;(&quot;link&quot;, listItem.getModel())
  * {
@@ -70,7 +71,7 @@ import 
org.apache.wicket.request.mapper.parameter.PageParameters;
  *         setResponsePage(new MyPage(obj));
  *     }
  * </pre>
- * 
+ *
  * @author Jonathan Locke
  * @author Eelco Hillenius
  * @param <T>
@@ -120,7 +121,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Gets any anchor component.
-        * 
+        *
         * @return Any anchor component to jump to, might be null
         */
        public Component getAnchor()
@@ -130,7 +131,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Gets whether link should automatically enable/disable based on 
current page.
-        * 
+        *
         * @return Whether this link should automatically enable/disable based 
on current page.
         */
        public final boolean getAutoEnable()
@@ -141,7 +142,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
        /**
         * Gets the popup specification. If not-null, a javascript on-click 
event handler will be
         * generated that opens a new window using the popup properties.
-        * 
+        *
         * @return the popup specification.
         */
        public final PopupSettings getPopupSettings()
@@ -180,7 +181,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * THIS METHOD IS NOT PART OF THE WICKET API. DO NOT ATTEMPT TO 
OVERRIDE OR CALL IT.
-        * 
+        *
         * Called when a link is clicked. The implementation of this method is 
currently to simply call
         * onClick(), but this may be augmented in the future.
         */
@@ -198,7 +199,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
         * {@link Component#getOutputMarkupId()} flag true, or it must be 
attached to a &lt;a tag with a
         * href attribute of more than one character starting with '#' ('&lt;a 
href="#someAnchor" ...
         * ').
-        * 
+        *
         * @param anchor
         *            The anchor
         * @return this
@@ -212,7 +213,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Sets whether this link should automatically enable/disable based on 
current page.
-        * 
+        *
         * @param autoEnable
         *            whether this link should automatically enable/disable 
based on current page.
         * @return This
@@ -226,7 +227,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
        /**
         * Sets the popup specification. If not-null, a javascript on-click 
event handler will be
         * generated that opens a new window using the popup properties.
-        * 
+        *
         * @param popupSettings
         *            the popup specification.
         * @return This
@@ -252,7 +253,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
         * with any set anchor component yourself. You also have to manually 
append the '#' at the right
         * place.
         * </p>
-        * 
+        *
         * @param tag
         *            The component tag
         * @param url
@@ -320,7 +321,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Gets the url to use for this link.
-        * 
+        *
         * @return The URL that this link links to
         */
        protected CharSequence getURL()
@@ -330,7 +331,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Whether this link refers to the given page.
-        * 
+        *
         * @param page
         *            A page
         * @return True if this link goes to the given page
@@ -342,7 +343,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
 
        /**
         * Handles this link's tag. OVERRIDES MUST CALL SUPER.
-        * 
+        *
         * @param tag
         *            the component tag
         * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
@@ -380,7 +381,7 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
                        disableLink(tag);
                }
        }
-       
+
        @Override
        public void renderHead(IHeaderResponse response)
        {
@@ -390,6 +391,12 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
                {
                        ComponentTag tag = getMarkupTag();
 
+                       if (tag == null)
+                       {
+                           throw new MarkupNotFoundException("No valid 
ComponentTag could be found for component with id '" +
+                                   getId() + "'. (Are markup id and Java id 
the same?)");
+            }
+
                        // Set href to link to this link's linkClicked method
                        CharSequence url = getURL();
 
@@ -426,15 +433,14 @@ public abstract class Link<T> extends AbstractLink 
implements IRequestListener,
                                        "var win = 
this.ownerDocument.defaultView || this.ownerDocument.parentWindow; "
                                                + "if (win == window) { 
window.location.href='" + url
                                                + "'; } ;return false"));
-                               return;
                        }
                }
        }
-       
+
        /**
         * This method can be overridden by a subclass to disable the JS event 
binding or provide custom
         * event binding code is used.
-        * 
+        *
         * @return true when a javascripot event binding must used to handle 
the click event.
         */
        protected boolean useJSEventBindingWhenNeeded()

Reply via email to