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

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

There is a newer version: 1.1.17
Show 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.mock.component;

import static org.easymock.classextension.EasyMock.*;

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.easymock.Capture;
import org.easymock.IAnswer;
import org.jboss.test.faces.mock.FacesMock;

/**
 * 

* 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(); expect(component.getChildren()).andStubReturn(children); expect(component.getFacets()).andStubReturn(facets); expect(component.getChildCount()).andStubAnswer(new IAnswer() { public Integer answer() throws Throwable { return children.size(); } }); final Capture facetNameCapture = new Capture(); expect(component.getFacet(capture(facetNameCapture))).andStubAnswer(new IAnswer() { public UIComponent answer() throws Throwable { return facets.get(facetNameCapture.getValue()); } }); expect(component.getFacetCount()).andStubAnswer(new IAnswer() { public Integer answer() throws Throwable { return facets.size(); } }); expect(component.getFacetsAndChildren()).andStubAnswer(new IAnswer>() { public Iterator answer() 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) { expect(component.getId()).andStubReturn(id); return this; } /* * (non-Javadoc) * * @see org.jboss.test.faces.mock.component.TreeBuilder#replay() */ public void replay() { visitTree(new TreeVisitor() { public void visit(TreeBuilder toVisit) { FacesMock.replay(toVisit.getComponent()); } }); } /* * (non-Javadoc) * * @see org.jboss.test.faces.mock.component.TreeBuilder#verify() */ public void verify() { visitTree(new TreeVisitor() { public void visit(TreeBuilder toVisit) { FacesMock.verify(toVisit.getComponent()); } }); } 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); expect(treeBuilder.getComponent().getParent()).andStubReturn(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