1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.application;
20
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.faces.FactoryFinder;
25 import javax.faces.application.ResourceDependencies;
26 import javax.faces.application.ResourceDependency;
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.UIComponentBase;
29 import javax.faces.component.UIOutput;
30 import javax.faces.component.UIPanel;
31 import javax.faces.component.behavior.ClientBehavior;
32 import javax.faces.component.behavior.ClientBehaviorBase;
33 import javax.faces.component.behavior.FacesBehavior;
34
35 import org.apache.myfaces.component.ComponentResourceContainer;
36 import org.apache.myfaces.config.RuntimeConfig;
37 import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMockTestCase;
38 import org.apache.myfaces.test.el.MockExpressionFactory;
39 import org.apache.myfaces.test.mock.MockPropertyResolver;
40 import org.apache.myfaces.test.mock.MockVariableResolver;
41 import org.junit.Assert;
42 import org.junit.Test;
43
44 public class ClientBehaviorTestCase extends AbstractJsfConfigurableMockTestCase
45 {
46
47 public ClientBehaviorTestCase()
48 {
49 }
50
51 @Override
52 protected void setFactories() throws Exception
53 {
54 super.setFactories();
55 FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
56 ApplicationFactoryImpl.class.getName());
57 FactoryFinder.setFactory(
58 FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY,
59 "org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl");
60 }
61
62 @Override
63 protected void setUpExternalContext() throws Exception
64 {
65 super.setUpExternalContext();
66
67 RuntimeConfig.getCurrentInstance(externalContext).setPropertyResolver(
68 new MockPropertyResolver());
69 RuntimeConfig.getCurrentInstance(externalContext).setVariableResolver(
70 new MockVariableResolver());
71 RuntimeConfig.getCurrentInstance(externalContext).setExpressionFactory(
72 new MockExpressionFactory());
73 }
74
75 @Override
76 protected void setUpApplication() throws Exception
77 {
78 super.setUpApplication();
79
80 application.addComponent(UIOutput.COMPONENT_TYPE, UIOutput.class
81 .getName());
82 application.addComponent(UIPanel.COMPONENT_TYPE, UIPanel.class
83 .getName());
84 application.addComponent(ComponentResourceContainer.COMPONENT_TYPE,
85 ComponentResourceContainer.class.getName());
86 }
87
88 @Override
89 public void tearDown() throws Exception
90 {
91 RuntimeConfig.getCurrentInstance(externalContext).purge();
92 super.tearDown();
93 }
94
95 @FacesBehavior("org.apache.myfaces.component.MockClientBehavior")
96 @ResourceDependencies({
97 @ResourceDependency(name="test.js", library="test", target="head")
98 })
99 public static class MockClientBehavior extends ClientBehaviorBase
100 {
101 }
102
103 public static class UITestComponentWithBehavior extends UIComponentBase
104 {
105 public static final String COMPONENT_TYPE = "javax.faces.UITestComponentWithBehavior";
106 public static final String COMPONENT_FAMILY = "javax.faces.UITestComponentWithBehavior";
107 public static final String DEFAULT_RENDERER_TYPE = "javax.faces.UITestComponentWithBehavior";
108
109 static private final java.util.Collection<String> CLIENT_EVENTS_LIST =
110 java.util.Collections.unmodifiableCollection(
111 java.util.Arrays.asList(
112 "click"
113 ));
114
115 public UITestComponentWithBehavior()
116 {
117 setRendererType(DEFAULT_RENDERER_TYPE);
118 }
119
120 @Override
121 public String getFamily()
122 {
123 return COMPONENT_FAMILY;
124 }
125
126 public java.util.Collection<String> getEventNames()
127 {
128 return CLIENT_EVENTS_LIST;
129 }
130 }
131
132 @Test
133 public void testAddBehaviorWithResourceDependencies() throws Exception
134 {
135
136 application.addComponent(UITestComponentWithBehavior.COMPONENT_TYPE,
137 UITestComponentWithBehavior.class.getName());
138 application.addComponent(UIOutput.COMPONENT_TYPE,
139 UIOutput.class.getName());
140
141 UITestComponentWithBehavior comp = (UITestComponentWithBehavior)
142 application.createComponent(UITestComponentWithBehavior.COMPONENT_TYPE);
143
144
145 application.addBehavior("myBehaviorId", MockClientBehavior.class.getName());
146 ClientBehavior behavior = (ClientBehavior) application.createBehavior("myBehaviorId");
147 comp.addClientBehavior("click", behavior);
148
149
150 Assert.assertTrue(comp.getClientBehaviors().get("click").contains(behavior));
151
152
153 List<UIComponent> resources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
154 Assert.assertEquals(1, resources.size());
155 Map<String,Object> attrMap = resources.get(0).getAttributes();
156 Assert.assertEquals("test.js", attrMap.get("name"));
157 }
158 }