/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package com.sun.faces.component.visit; import com.sun.faces.TestFormVisit; import com.sun.faces.cactus.ServletFacesTestCase; import com.sun.faces.context.PartialViewContextImpl; import com.sun.faces.util.Util; import java.util.HashSet; import javax.faces.component.UIComponent; import javax.faces.component.UIViewRoot; import javax.faces.component.html.HtmlCommandButton; import javax.faces.component.html.HtmlForm; import javax.faces.component.html.HtmlInputText; import javax.faces.component.html.HtmlPanelGrid; import javax.faces.component.visit.VisitCallback; import javax.faces.component.visit.VisitContext; import javax.faces.component.visit.VisitResult; import javax.faces.context.FacesContext; import javax.faces.context.PartialViewContext; import javax.faces.event.PhaseId; import org.apache.cactus.WebRequest; public class TestTreeVisit extends ServletFacesTestCase { // // Protected Constants // // Class Variables // // // Instance Variables // // Attribute Instance Variables // Relationship Instance Variables // // Constructors and Initializers // public TestTreeVisit() { super("TestTreeVisit.java"); } public TestTreeVisit(String name) { super(name); } // // Class methods // // // General Methods // private void buildTree() { FacesContext context = getFacesContext(); UIViewRoot root = Util.getViewHandler(context).createView(context, null); root.setId("root"); context.setViewRoot(root); HtmlForm form = new HtmlForm(); form.setId("form"); root.getChildren().add(form); buildPanel(form, "panel0"); buildPanel(form, "panel1"); } private void buildPanel(HtmlForm form, String panelId) { UIComponent panel, input0, input1, button0, button1; panel = new HtmlPanelGrid(); panel.setId(panelId); form.getChildren().add(panel); input0 = new HtmlInputText(); input0.setId("input0"); panel.getChildren().add(input0); input1 = new HtmlInputText(); input1.setId("input1"); panel.getChildren().add(input1); button0 = new HtmlCommandButton(); button0.setId("button0"); panel.getChildren().add(button0); button1 = new HtmlCommandButton(); button1.setId("button1"); panel.getChildren().add(button1); } public void testFullTraversal() throws Exception { buildTree(); UIViewRoot root = getFacesContext().getViewRoot(); final StringBuilder builder = new StringBuilder(); root.visitTree(VisitContext.createVisitContext(getFacesContext()), new VisitCallback() { public VisitResult visit(VisitContext context, UIComponent target) { builder.append(target.getClientId(context.getFacesContext()) + " "); return VisitResult.ACCEPT; } }); System.out.println(builder); String result = builder.toString().trim(); assertEquals(result, "root form form:panel0 form:input0 form:input1 form:button0 form:button1 form:panel1 form:input0 form:input1 form:button0 form:button1"); } public void testSpecificIdTraversal() throws Exception { buildTree(); UIViewRoot root = getFacesContext().getViewRoot(); final StringBuilder builder = new StringBuilder(); HashSet ids = new HashSet(); ids.add("form:panel0"); root.visitTree(VisitContext.createVisitContext(getFacesContext(), ids, null), new VisitCallback() { public VisitResult visit(VisitContext context, UIComponent target) { builder.append(target.getClientId(context.getFacesContext()) + " "); return VisitResult.ACCEPT; } }); System.out.println(builder); String result = builder.toString().trim(); assertEquals(result, "form:panel0"); } public void beginPartialTraversal(WebRequest req) { req.addParameter(PartialViewContext.PARTIAL_EXECUTE_PARAM_NAME, "form"); } public void testPartialTraversal() throws Exception { FacesContext context = getFacesContext(); UIViewRoot root = Util.getViewHandler(context).createView(context, null); root.setId("root"); context.setViewRoot(root); TestFormVisit form = new TestFormVisit(); form.setId("form"); root.getChildren().add(form); PartialViewContextImpl pvContext = new PartialViewContextImpl(context); pvContext.processPartial(PhaseId.APPLY_REQUEST_VALUES); if (context.getAttributes().get("VisitHint.EXECUTE_LIFECYCLE") != null) { System.out.println("YESSSSSS"); } else { System.out.println("NOOOOOOOO"); } assertNotNull(context.getAttributes().remove("VisitHint.EXECUTE_LIFECYCLE")); } // PENDING make sure UIData and UIRepeat are tested. } // end of class TestTreeVisit