Wicket HTML Java JUnit Test

Wicket requires that html pages and panels be located in the same package as their corresponding java components.

Here’s a junit test that searches your classpath for panels and pages and asserts that the corresponding html is where it’s supposed to be:

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.RegexPatternTypeFilter;

public class WicketTest {
    @Test
    public void testAllWicketPanelsAndPages() throws Exception {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(Panel.class));
        provider.addIncludeFilter(new AssignableTypeFilter(WebPage.class));

        Set<BeanDefinition> components = provider.findCandidateComponents("path/to/my/project");
        for (BeanDefinition component : components) {
            if (component.isAbstract()) {
                continue;
            }
            @SuppressWarnings("unchecked")
            Class<MarkupContainer> clazz = (Class<MarkupContainer>) Class.forName(component.getBeanClassName());
            String resource = clazz.getSimpleName()+".html";
            assertNotNull("Resource ["+resource+"] not found with "+component.getBeanClassName(), clazz.getResourceAsStream(resource));
        }
    }
}
It's only fair to share...
Share on Facebook
Facebook
Google+
Google+
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

1 thought on “Wicket HTML Java JUnit Test

  1. Raul Vieira
    Raul Vieira

    Very cool. It’s worth mentioning that by default this is where Wicket expects to find the HTML, but can be overridden in the Application class by providing a custom resource stream locator.

    Though, I’ve never had a need to change the default.

Leave a Reply