All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.test.faces.mockito.component.TreeBuilderImpl Maven / Gradle / Ivy

The newest version!
/*
 * $Id$
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.test.faces.mockito.component;

import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;

import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 * 

* This class builds tree of mock components *

* * @author [email protected] * */ public class TreeBuilderImpl implements TreeBuilder { private final C component; private final List> childrenBuilders; private final List children; private final Map facets; protected TreeBuilderImpl(C component) { this.component = component; this.childrenBuilders = new ArrayList>(); this.children = new ArrayList(); this.facets = new HashMap(); when(component.getChildren()).thenReturn(children); when(component.getFacets()).thenReturn(facets); when(component.getChildCount()).thenAnswer(new Answer() { public Integer answer(InvocationOnMock invocation) throws Throwable { return children.size(); } }); final ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); when(component.getFacet(argument.capture())).thenAnswer(new Answer() { public UIComponent answer(InvocationOnMock invocation) throws Throwable { return facets.get(argument.getValue()); } }); when(component.getFacetCount()).thenAnswer(new Answer() { public Integer answer(InvocationOnMock invocation) throws Throwable { return facets.size(); } }); when(component.getFacetsAndChildren()).thenAnswer(new Answer>() { public Iterator answer(InvocationOnMock invocation) throws Throwable { ArrayList facetsAndChildren = new ArrayList(facets.values()); facetsAndChildren.addAll(children); return facetsAndChildren.iterator(); } }); } /* * (non-Javadoc) * * @see org.jboss.test.faces.mock.component.TreeBuilder#setId(java.lang.String) */ public TreeBuilder id(String id) { when(component.getId()).thenReturn(id); return this; } public void visitTree(TreeVisitor visitor) { visitor.visit(this); for (TreeBuilder child : childrenBuilders) { child.visitTree(visitor); } } /* * (non-Javadoc) * * @see org.jboss.test.faces.mock.component.TreeBuilder#getComponent() */ public C getComponent() { return this.component; } public TreeBuilder children(TreeBuilder... builders) { for (TreeBuilder treeBuilder : builders) { children.add(treeBuilder.getComponent()); addChildBuilder(treeBuilder); } return this; } private void addChildBuilder(TreeBuilder treeBuilder) { childrenBuilders.add(treeBuilder); when(treeBuilder.getComponent().getParent()).thenReturn(component); } public TreeBuilder facets(Facet... facets) { for (Facet facet : facets) { this.facets.put(facet.getName(), facet.getBuilder().getComponent()); addChildBuilder(facet.getBuilder()); } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy