Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.wicket.markup.MarkupNotFoundException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.Test;

/**
* Tests the message of the exception thrown when a component has no markup.
*/
class ComponentMarkupNotFoundTest extends WicketTestCase
{
@Test
void messageNamesPageAndPath()
{
MockPageWithOneComponent page = new MockPageWithOneComponent();
page.add(new Label(MockPageWithOneComponent.COMPONENT_ID, "text"));
tester.startPage(page);

// the component is not in the page markup, like one added to an Ajax update
Label missing = new Label("missing", "text");
page.add(missing);

MarkupNotFoundException e = assertThrows(MarkupNotFoundException.class, missing::render);

assertThat(e.getMessage()).contains("Markup not found for Component")
.contains("page: " + MockPageWithOneComponent.class.getName())
.contains("path: missing");
}
}
11 changes: 9 additions & 2 deletions wicket-core/src/main/java/org/apache/wicket/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,14 @@ public final void render()
}
}

/**
* Builds the message for a component that has no markup. It names the page and the
* page-relative path of the component, because the id alone is ambiguous in a large page.
*
* @param prefix
* the start of the message
* @return the message
*/
private String markupNotFoundMessage(String prefix)
{
StringBuilder message = new StringBuilder(prefix).append(toString());
Expand Down Expand Up @@ -2166,8 +2174,7 @@ private void internalRender()
IMarkupFragment markup = getMarkup();
if (markup == null)
{
throw new MarkupNotFoundException(
markupNotFoundMessage("Markup not found for Component: "));
throw new MarkupNotFoundException(markupNotFoundMessage("Markup not found for Component: "));
}

// MarkupStream is an Iterator for the markup
Expand Down