001 package org.apache.tapestry.pageload; 002 003 import org.apache.tapestry.BaseComponentTestCase; 004 import org.apache.tapestry.IComponent; 005 import org.apache.tapestry.IForm; 006 import org.apache.tapestry.IPage; 007 import org.apache.tapestry.internal.event.ComponentEventProperty; 008 import org.apache.tapestry.internal.event.IComponentEventInvoker; 009 import org.apache.tapestry.spec.ComponentSpecification; 010 import org.apache.tapestry.spec.IComponentSpecification; 011 import static org.easymock.EasyMock.checkOrder; 012 import static org.easymock.EasyMock.expect; 013 import org.testng.annotations.Test; 014 015 import java.util.Collections; 016 import java.util.HashMap; 017 import java.util.Map; 018 019 /** 020 * Tests functionality of {@link EventConnectionVisitor}. 021 */ 022 @Test 023 public class TestEventConnectionVisitor extends BaseComponentTestCase { 024 025 public void test_Wire_Component_Event() 026 { 027 IComponentSpecification spec = new ComponentSpecification(); 028 spec.addEventListener("comp1", new String[] {"onClick"}, "testFoo", null, false, false, false, false); 029 030 IComponent comp = newComponent(spec, "comp1", "path/", "Home/"); 031 IComponentEventInvoker invoker = newMock(IComponentEventInvoker.class); 032 033 invoker.addEventListener("path/comp1", spec); 034 035 replay(); 036 037 EventConnectionVisitor v = new EventConnectionVisitor(); 038 v.setEventInvoker(invoker); 039 040 v.visitComponent(comp); 041 042 verify(); 043 } 044 045 public void test_Wire_Element_Form_Events() 046 { 047 IComponentSpecification spec = new ComponentSpecification(); 048 spec.addElementEventListener("elem1", new String[] {"onClick"}, "testFoo", "form", false, false, false); 049 050 IComponent comp = newComponent(spec); 051 IPage page = newMock(IPage.class); 052 IForm form = newMock(IForm.class); 053 IComponentEventInvoker invoker = newMock(IComponentEventInvoker.class); 054 055 Map comps = new HashMap(); 056 comps.put("form", form); 057 058 expect(comp.getPage()).andReturn(page).anyTimes(); 059 expect(page.getComponents()).andReturn(comps).anyTimes(); 060 expect(comp.getComponents()).andReturn(Collections.EMPTY_MAP).anyTimes(); 061 expect(form.getComponents()).andReturn(Collections.EMPTY_MAP).anyTimes(); 062 063 expect(form.getId()).andReturn("form").anyTimes(); 064 expect(form.getExtendedId()).andReturn("path/form").anyTimes(); 065 066 invoker.addFormEventListener("path/form", spec); 067 invoker.addFormEventListener("path/form", spec); 068 069 replay(); 070 071 EventConnectionVisitor v = new EventConnectionVisitor(); 072 v.setEventInvoker(invoker); 073 074 v.visitComponent(comp); 075 v.visitComponent(comp); 076 077 verify(); 078 } 079 080 public void test_Spec_Rewire_Id() 081 { 082 IComponentSpecification spec = newMock(IComponentSpecification.class); 083 IComponentEventInvoker invoker = newMock(IComponentEventInvoker.class); 084 IComponent comp = newComponent(spec, "comp1", "path/", "Home/"); 085 086 ComponentEventProperty p = new ComponentEventProperty("comp1"); 087 p.addListener(new String[] {"onClick"}, "testFoo", null, false, false, false, false); 088 089 Map compEvents = new HashMap(); 090 compEvents.put("comp1", p); 091 092 expect(spec.getComponentEvents()).andReturn(compEvents); 093 expect(spec.getElementEvents()).andReturn(Collections.EMPTY_MAP); 094 095 invoker.addEventListener("path/comp1", spec); 096 spec.rewireComponentId("comp1", "path/comp1", "Home/path/comp1"); 097 098 replay(); 099 100 EventConnectionVisitor v = new EventConnectionVisitor(); 101 v.setEventInvoker(invoker); 102 103 v.visitComponent(comp); 104 105 verify(); 106 } 107 108 IComponent newComponent(IComponentSpecification spec, String findCompId, Object... args) 109 { 110 IComponent comp = newComponent(spec); 111 IPage page = newMock(IPage.class); 112 113 expect(comp.getPage()).andReturn(page).anyTimes(); 114 115 Map comps = new HashMap(); 116 comps.put(findCompId, comp); 117 118 expect(page.getComponents()).andReturn(comps).anyTimes(); 119 expect(comp.getComponents()).andReturn(null).anyTimes(); 120 121 if (args.length > 0) 122 { 123 expect(comp.getExtendedId()).andReturn(args[0] + findCompId).anyTimes(); 124 } 125 126 if (args.length > 1) 127 { 128 expect(comp.getIdPath()).andReturn((String)args[1] + (String)args[0] + findCompId).anyTimes(); 129 } 130 131 return comp; 132 } 133 134 IComponent newComponent(IComponentSpecification spec) 135 { 136 IComponent comp = newMock(IComponent.class); 137 138 checkOrder(comp, false); 139 expect(comp.getSpecification()).andReturn(spec).anyTimes(); 140 141 return comp; 142 } 143 }