1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.renderkit.html.behavior;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.component.UIViewRoot;
23 import javax.faces.component.behavior.AjaxBehavior;
24 import javax.faces.component.behavior.ClientBehaviorHolder;
25 import javax.faces.context.ResponseWriter;
26
27 import org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl;
28 import org.apache.myfaces.shared.util.FastWriter;
29 import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
30 import org.apache.myfaces.test.config.ConfigParser;
31 import org.apache.myfaces.test.mock.MockFacesContext22;
32 import org.junit.Assert;
33 import org.junit.Test;
34
35
36
37
38
39 public abstract class AbstractClientBehaviorTestCase extends AbstractJsfTestCase
40 {
41 protected ResponseWriter writer;
42 protected FastWriter outputWriter;
43 protected ConfigParser parser;
44
45
46
47 protected abstract HtmlRenderedClientEventAttr[] getClientBehaviorHtmlRenderedAttributes();
48
49 protected abstract UIComponent createComponentToTest();
50
51 @Override
52 protected void setUpJSFObjects() throws Exception
53 {
54 super.setUpJSFObjects();
55 outputWriter = new FastWriter();
56 writer = new HtmlResponseWriterImpl(outputWriter, null, null);
57 facesContext.setResponseWriter(writer);
58 facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", true);
59 }
60
61 @Override
62 protected void setUpApplication() throws Exception
63 {
64 super.setUpApplication();
65 }
66
67 @Override
68 protected void setUpRenderKit() throws Exception
69 {
70 super.setUpRenderKit();
71 parser = new ConfigParser();
72 parser.parse(parser.getPlatformURLs());
73
74 }
75
76
77
78
79 @Test
80 public void testClientBehaviorHolderRendersIdAndName()
81 {
82 HtmlRenderedClientEventAttr[] attrs = getClientBehaviorHtmlRenderedAttributes();
83
84 for (int i = 0; i < attrs.length; i++)
85 {
86 UIComponent component = createComponentToTest();
87 ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
88 clientBehaviorHolder.addClientBehavior(attrs[i].getClientEvent(), new AjaxBehavior());
89 try
90 {
91 component.encodeAll(facesContext);
92 String output = outputWriter.toString();
93 Assert.assertTrue(output.indexOf(" id=\""+component.getClientId(facesContext)+"\"") > -1);
94 Assert.assertTrue(output.indexOf(" name=\""+component.getClientId(facesContext)+"\"") > -1);
95 outputWriter.reset();
96 }
97 catch (Exception e)
98 {
99 Assert.fail(e.getMessage());
100 }
101 }
102 }
103
104 @Test
105 public void testClientBehaviorRendered()
106 {
107 HtmlRenderedClientEventAttr[] attrs = getClientBehaviorHtmlRenderedAttributes();
108
109 for (int i = 0; i < attrs.length; i++)
110 {
111 UIComponent component = createComponentToTest();
112
113 if (!component.isInView())
114 {
115 UIViewRoot root = facesContext.getViewRoot();
116 root.getChildren().add(component);
117 facesContext.setViewRoot(root);
118 root.getClientId(facesContext);
119 }
120
121 ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
122 clientBehaviorHolder.addClientBehavior(attrs[i].getClientEvent(), new AjaxBehavior());
123 try
124 {
125 component.encodeAll(facesContext);
126 String output = outputWriter.toString();
127
128
129
130
131 int index = checkClientBehaviorRenderedOnClientEventProperty(output, 0, attrs[i]);
132 outputWriter.reset();
133 }
134 catch (Exception e)
135 {
136 Assert.fail(e.getMessage());
137 }
138 }
139 }
140
141 public int checkClientBehaviorRenderedOnClientEventProperty(String output, int start, HtmlRenderedClientEventAttr attr)
142 {
143 String propStart = " "+attr.getName()+"=\"";
144 int propIndex = output.indexOf(propStart, start);
145 if (propIndex > -1)
146 {
147 int c = '"';
148 int startPropIndex = propIndex + propStart.length();
149 int endPropIndex = output.indexOf('"' , startPropIndex );
150 String propertyValue = output.substring(startPropIndex, endPropIndex);
151 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains("jsf.ajax.request("));
152 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains("javax.faces.behavior.event"));
153 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains(attr.getClientEvent()));
154 return endPropIndex + 1;
155 }
156 else
157 {
158 Assert.fail("Property " + attr.getName() + "not found");
159 return -1;
160 }
161 }
162
163 @Test
164 public void testClientBehaviorRenderedWithHtmlAttribute()
165 {
166 HtmlRenderedClientEventAttr[] attrs = getClientBehaviorHtmlRenderedAttributes();
167
168 for (int i = 0; i < attrs.length; i++)
169 {
170 UIComponent component = createComponentToTest();
171 ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
172 clientBehaviorHolder.addClientBehavior(attrs[i].getClientEvent(), new AjaxBehavior());
173 component.getAttributes().put(attrs[i].getName(), "htmlValue");
174 try
175 {
176 component.encodeAll(facesContext);
177 String output = outputWriter.toString();
178
179
180
181
182 int index = checkClientBehaviorRenderedOnClientEventPropertyAndHtmlValue(output, 0, attrs[i], "htmlValue");
183 outputWriter.reset();
184 }
185 catch (Exception e)
186 {
187 Assert.fail(e.getMessage());
188 }
189 }
190 }
191
192 public int checkClientBehaviorRenderedOnClientEventPropertyAndHtmlValue(String output, int start, HtmlRenderedClientEventAttr attr, String value)
193 {
194 String propStart = " "+attr.getName()+"=\"";
195 int propIndex = output.indexOf(propStart, start);
196 if (propIndex > -1)
197 {
198 int c = '"';
199 int startPropIndex = propIndex + propStart.length();
200 int endPropIndex = output.indexOf('"' , startPropIndex );
201 String propertyValue = output.substring(startPropIndex, endPropIndex);
202 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.startsWith("jsf.util.chain("));
203 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains("jsf.ajax.request("));
204 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains("javax.faces.behavior.event"));
205 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains(attr.getClientEvent()));
206 Assert.assertTrue("Property: " + attr.getName()+" Output: "+output, propertyValue.contains(value));
207 return endPropIndex + 1;
208 }
209 else
210 {
211 Assert.fail("Property " + attr.getName() + "not found"+" Output: "+output);
212 return -1;
213 }
214 }
215 }