Search in sources :

Example 1 with IVisit

use of org.apache.wicket.util.visit.IVisit in project syncope by apache.

the class LogsITCase method searchLog.

private Component searchLog(final String property, final String searchPath, final String key) {
    Component component = TESTER.getComponentFromLastRenderedPage(searchPath);
    Component result = component.getPage().visitChildren(ListItem.class, (final ListItem<LoggerTO> object, final IVisit<Component> visit) -> {
        try {
            if (object.getModelObject() instanceof LoggerTO && PropertyResolver.getPropertyGetter(property, object.getModelObject()).invoke(object.getModelObject()).equals(key)) {
                visit.stop(object);
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            LOG.error("Error invoke method", ex);
        }
    });
    return result;
}
Also used : LoggerTO(org.apache.syncope.common.lib.log.LoggerTO) ListItem(org.apache.wicket.markup.html.list.ListItem) Component(org.apache.wicket.Component) IVisit(org.apache.wicket.util.visit.IVisit) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with IVisit

use of org.apache.wicket.util.visit.IVisit in project wicket by apache.

the class AutoLabelResolver method findRelatedComponent.

/**
 * @param container
 * @param id
 * @return Component
 */
static Component findRelatedComponent(MarkupContainer container, final String id) {
    // try the quick and easy route first
    Component component = container.get(id);
    if (component != null) {
        return component;
    }
    // try the long way, search the hierarchy from the closest container up to the page
    final Component[] searched = new Component[] { null };
    while (container != null) {
        component = container.visitChildren(Component.class, new IVisitor<Component, Component>() {

            @Override
            public void component(Component child, IVisit<Component> visit) {
                if (child == searched[0]) {
                    // this container was already searched
                    visit.dontGoDeeper();
                    return;
                }
                if (id.equals(child.getId())) {
                    visit.stop(child);
                    return;
                }
            }
        });
        if (component != null) {
            return component;
        }
        // remember the container so we dont search it again, and search the parent
        searched[0] = container;
        container = container.getParent();
    }
    return null;
}
Also used : IVisitor(org.apache.wicket.util.visit.IVisitor) Component(org.apache.wicket.Component) IVisit(org.apache.wicket.util.visit.IVisit)

Example 3 with IVisit

use of org.apache.wicket.util.visit.IVisit in project wicket by apache.

the class BaseWicketTester method isComponentOnAjaxResponse.

/**
 * Tests that a <code>Component</code> has been added to a <code>AjaxRequestTarget</code>, using
 * {@link org.apache.wicket.ajax.AjaxRequestTarget#add(org.apache.wicket.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 have to trust that the Wicket
 * Ajax JavaScript just works.
 *
 * @param component
 *            the <code>Component</code> to test
 * @return a <code>Result</code>
 */
public Result isComponentOnAjaxResponse(final Component component) {
    String failMessage = "A component which is null could not have been added to the AJAX response";
    notNull(failMessage, component);
    Result result;
    // test that the component renders the placeholder tag if it's not visible
    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;
        result = isTrue(failMessage, component.getOutputMarkupPlaceholderTag());
        if (result.wasFailed()) {
            return result;
        }
    }
    // Get the AJAX response
    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";
    boolean isAjaxResponse = Pattern.compile("^<\\?xml version=\"1.0\" encoding=\".*?\"\\?><ajax-response>").matcher(ajaxResponse).find();
    result = isTrue(failMessage, isAjaxResponse);
    if (result.wasFailed()) {
        return result;
    }
    // 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;
    result = isTrue(failMessage, !Strings.isEmpty(markupId));
    if (result.wasFailed()) {
        return result;
    }
    // Look for that the component is on the response, using the markup id
    boolean isComponentInAjaxResponse = ajaxResponse.matches("(?s).*<component id=\"" + markupId + "\"[^>]*?>.*");
    failMessage = "Component wasn't found in the AJAX response. " + componentInfo;
    result = isTrue(failMessage, isComponentInAjaxResponse);
    if (!result.wasFailed()) {
        return result;
    }
    // Check if the component has been included as part of an enclosure render
    Enclosure enclosure = getLastRenderedPage().visitChildren(Enclosure.class, (Enclosure enc, IVisit<Enclosure> visit) -> {
        if (AjaxEnclosureListener.isControllerOfEnclosure(component, enc)) {
            visit.stop(enc);
        }
    });
    if (enclosure == null) {
        return result;
    }
    failMessage = "Component's enclosure was not found in the AJAX response. " + enclosure;
    boolean isEnclosureInAjaxResponse = !isComponentOnAjaxResponse(enclosure).wasFailed();
    return isTrue(failMessage, isEnclosureInAjaxResponse);
}
Also used : Enclosure(org.apache.wicket.markup.html.internal.Enclosure) IVisit(org.apache.wicket.util.visit.IVisit)

Example 4 with IVisit

use of org.apache.wicket.util.visit.IVisit in project wicket by apache.

the class StatelessChecker method onBeforeRender.

/**
 * @see org.apache.wicket.application.IComponentOnBeforeRenderListener#onBeforeRender(org.apache.wicket.Component)
 */
@Override
public void onBeforeRender(final Component component) {
    if (mustCheck(component)) {
        final IVisitor<Component, Component> visitor = new IVisitor<Component, Component>() {

            @Override
            public void component(final Component comp, final IVisit<Component> visit) {
                if ((component instanceof Page) && mustCheck(comp)) {
                    // Do not go deeper, because this component will be
                    // checked by checker
                    // itself.
                    // Actually we could go deeper but that would mean we
                    // traverse it twice
                    // (for current component and for inspected one).
                    // We go deeper for Page because full tree will be
                    // inspected during
                    // isPageStateless call.
                    visit.dontGoDeeper();
                } else if (!comp.isStateless()) {
                    visit.stop(comp);
                } else {
                // continue
                }
            }
        };
        if (component.isStateless() == false) {
            StringList statefulBehaviors = new StringList();
            for (Behavior b : component.getBehaviors()) {
                if (b.getStatelessHint(component) == false) {
                    statefulBehaviors.add(Classes.name(b.getClass()));
                }
            }
            String reason;
            if (statefulBehaviors.size() == 0) {
                reason = " Possible reason: no stateless hint";
            } else {
                reason = " Stateful behaviors: " + statefulBehaviors.join();
            }
            fail(new StatelessCheckFailureException(component, reason));
            return;
        }
        if (component instanceof MarkupContainer) {
            MarkupContainer container = ((MarkupContainer) component);
            // Traverse children
            final Object o = container.visitChildren(visitor);
            if (o != null) {
                fail(new StatelessCheckFailureException(container, " Offending component: " + o));
                return;
            }
        }
        if (component instanceof Page) {
            final Page p = (Page) component;
            if (!p.isBookmarkable()) {
                fail(new StatelessCheckFailureException(p, " Only bookmarkable pages can be stateless"));
                return;
            }
            if (!p.isPageStateless()) {
                fail(new StatelessCheckFailureException(p, " for unknown reason"));
                return;
            }
        }
    }
}
Also used : MarkupContainer(org.apache.wicket.MarkupContainer) IVisitor(org.apache.wicket.util.visit.IVisitor) StringList(org.apache.wicket.util.string.StringList) Page(org.apache.wicket.Page) Behavior(org.apache.wicket.behavior.Behavior) Component(org.apache.wicket.Component) IVisit(org.apache.wicket.util.visit.IVisit)

Example 5 with IVisit

use of org.apache.wicket.util.visit.IVisit in project wicket by apache.

the class VisitorTest method testVisitParents.

/**
 * https://issues.apache.org/jira/browse/WICKET-3805
 *
 * Visit parents with arbitrary type
 */
@Test
public void testVisitParents() {
    TestContainer testContainer = new TestContainer();
    IVisitor<MarkupContainer, MarkerInterface> visitor = new IVisitor<MarkupContainer, MarkerInterface>() {

        @Override
        public void component(MarkupContainer object, IVisit<MarkerInterface> visit) {
            visit.stop((MarkerInterface) object);
        }
    };
    MarkerInterface markedParent = testContainer.get("G:H").visitParents(MarkupContainer.class, visitor, new ClassVisitFilter(MarkerInterface.class));
    assertEquals("G", markedParent.getId());
}
Also used : WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) IVisitor(org.apache.wicket.util.visit.IVisitor) ClassVisitFilter(org.apache.wicket.util.visit.ClassVisitFilter) IVisit(org.apache.wicket.util.visit.IVisit) Test(org.junit.Test)

Aggregations

IVisit (org.apache.wicket.util.visit.IVisit)5 Component (org.apache.wicket.Component)3 IVisitor (org.apache.wicket.util.visit.IVisitor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LoggerTO (org.apache.syncope.common.lib.log.LoggerTO)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 Page (org.apache.wicket.Page)1 Behavior (org.apache.wicket.behavior.Behavior)1 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)1 Enclosure (org.apache.wicket.markup.html.internal.Enclosure)1 ListItem (org.apache.wicket.markup.html.list.ListItem)1 StringList (org.apache.wicket.util.string.StringList)1 ClassVisitFilter (org.apache.wicket.util.visit.ClassVisitFilter)1 Test (org.junit.Test)1