1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.test.utils;
20
21 import java.util.Map;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25
26 import org.apache.shale.test.mock.MockResponseWriter;
27
28
29
30
31
32 public class HtmlCheckAttributesUtil
33 {
34
35
36
37
38
39 public static void addBaseAttributes(UIComponent component, HtmlRenderedAttr[] attrs)
40 {
41 Map map = component.getAttributes();
42 for(int i = 0; i < attrs.length; i++)
43 {
44 HtmlRenderedAttr attr = attrs[i];
45 map.put(attr.getName(), attr.getValue());
46 }
47 }
48
49
50
51
52
53
54
55 public static void checkRenderedAttributes(HtmlRenderedAttr[] attrs, String output) throws Exception
56 {
57 for(int i = 0; i < attrs.length; i++)
58 {
59
60 checkAttributeOccurrences(attrs[i], output);
61 }
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public static void checkRenderedAttributes(
75 UIComponent component,
76 FacesContext context,
77 MockResponseWriter writer,
78 HtmlRenderedAttr[] attrs) throws Exception
79 {
80
81 addBaseAttributes(component, attrs);
82 component.encodeBegin(context);
83 component.encodeChildren(context);
84 component.encodeEnd(context);
85 context.renderResponse();
86 checkRenderedAttributes(attrs, writer.getWriter().toString());
87 }
88
89
90
91
92
93
94 public static boolean hasFailedAttrRender(HtmlRenderedAttr[] attrs)
95 {
96 for(int i = 0; i < attrs.length; i++)
97 {
98 if(!attrs[i].isRenderSuccessful()) return true;
99 }
100 return false;
101 }
102
103
104
105
106
107
108
109
110 public static String constructErrorMessage(HtmlRenderedAttr[] attrs, String actual)
111 {
112 StringBuffer messgBuffer = new StringBuffer();
113 for(int i = 0; i < attrs.length; i++)
114 {
115 if(attrs[i].getErrorCode() == HtmlRenderedAttr.RENDERED_MORE_TIMES_THAN_EXPECTED)
116 {
117 messgBuffer.append(attrs[i].getName()).append(" (");
118 messgBuffer.append(attrs[i].getExpectedHtml()).append(") was rendered more times (");
119 messgBuffer.append(attrs[i].getActualOccurrences()).append(") than expected (");
120 messgBuffer.append(attrs[i].getExpectedOccurrences()).append(").");
121 messgBuffer.append(System.getProperty("line.separator"));
122 }
123 else if(attrs[i].getErrorCode() == HtmlRenderedAttr.RENDERED_LESS_TIMES_THAN_EXPECTED)
124 {
125 messgBuffer.append(attrs[i].getName()).append(" (");
126 messgBuffer.append(attrs[i].getExpectedHtml()).append(") was rendered less times (");
127 messgBuffer.append(attrs[i].getActualOccurrences()).append(") than expected (");
128 messgBuffer.append(attrs[i].getExpectedOccurrences()).append(").");
129 messgBuffer.append(System.getProperty("line.separator"));
130 }
131 }
132 messgBuffer.append("Actual HTML: ").append(actual);
133 return messgBuffer.toString();
134 }
135
136
137
138
139
140
141
142 public static void checkAttributeOccurrences(HtmlRenderedAttr attr, String actual)
143 {
144 String expectedHtml = attr.getExpectedHtml();
145
146 int index;
147 int offset = 0;
148 while((index=actual.indexOf(expectedHtml,offset)) != -1)
149 {
150 attr.increaseActualOccurrences();
151 if(attr.getActualOccurrences() > attr.getExpectedOccurrences())
152 {
153 attr.setErrorCode(HtmlRenderedAttr.RENDERED_MORE_TIMES_THAN_EXPECTED);
154 return;
155 }
156
157 offset += index + expectedHtml.length();
158 }
159
160 if(attr.getActualOccurrences() < attr.getExpectedOccurrences())
161 {
162 attr.setErrorCode(HtmlRenderedAttr.RENDERED_LESS_TIMES_THAN_EXPECTED);
163 }
164 else
165 {
166 attr.setRenderSuccessful(true);
167 }
168 }
169
170 public static HtmlRenderedAttr[] generateBasicAttrs() {
171 HtmlRenderedAttr[] attrs = {
172
173 new HtmlRenderedAttr("accesskey"),
174
175 new HtmlRenderedAttr("dir"),
176 new HtmlRenderedAttr("lang"),
177 new HtmlRenderedAttr("title"),
178
179 new HtmlRenderedAttr("onfocus"),
180 new HtmlRenderedAttr("onblur"),
181
182 new HtmlRenderedAttr("onchange"),
183 new HtmlRenderedAttr("onselect"),
184
185 new HtmlRenderedAttr("onclick"),
186 new HtmlRenderedAttr("ondblclick"),
187 new HtmlRenderedAttr("onkeydown"),
188 new HtmlRenderedAttr("onkeypress"),
189 new HtmlRenderedAttr("onkeyup"),
190 new HtmlRenderedAttr("onmousedown"),
191 new HtmlRenderedAttr("onmousemove"),
192 new HtmlRenderedAttr("onmouseout"),
193 new HtmlRenderedAttr("onmouseover"),
194 new HtmlRenderedAttr("onmouseup"),
195
196 new HtmlRenderedAttr("style"),
197 new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
198
199 new HtmlRenderedAttr("tabindex")
200 };
201
202 return attrs;
203 }
204
205 public static HtmlRenderedAttr[] generateAttrsNotRenderedForReadOnly()
206 {
207 HtmlRenderedAttr[] attrs = {
208
209 new HtmlRenderedAttr("accesskey", 0),
210
211 new HtmlRenderedAttr("onfocus", 0),
212 new HtmlRenderedAttr("onblur", 0),
213
214 new HtmlRenderedAttr("onchange", 0),
215 new HtmlRenderedAttr("onselect", 0),
216
217 new HtmlRenderedAttr("tabindex", 0)
218 };
219 return attrs;
220 }
221
222 public static HtmlRenderedAttr[] generateBasicReadOnlyAttrs() {
223 HtmlRenderedAttr[] attrs = {
224
225 new HtmlRenderedAttr("dir"),
226 new HtmlRenderedAttr("lang"),
227 new HtmlRenderedAttr("title"),
228
229 new HtmlRenderedAttr("onclick"),
230 new HtmlRenderedAttr("ondblclick"),
231 new HtmlRenderedAttr("onkeydown"),
232 new HtmlRenderedAttr("onkeypress"),
233 new HtmlRenderedAttr("onkeyup"),
234 new HtmlRenderedAttr("onmousedown"),
235 new HtmlRenderedAttr("onmousemove"),
236 new HtmlRenderedAttr("onmouseout"),
237 new HtmlRenderedAttr("onmouseover"),
238 new HtmlRenderedAttr("onmouseup"),
239
240 new HtmlRenderedAttr("style"),
241 new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
242 };
243
244 return attrs;
245 }
246 }