2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.clay.config;
19
20 import java.io.StringWriter;
21 import java.util.Iterator;
22
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.html.HtmlCommandButton;
25 import javax.faces.component.html.HtmlInputText;
26 import javax.faces.context.ResponseWriter;
27 import javax.faces.convert.Converter;
28 import javax.faces.convert.IntegerConverter;
29 import javax.faces.event.ActionEvent;
30 import javax.faces.event.ActionListener;
31 import javax.faces.event.ValueChangeEvent;
32 import javax.faces.event.ValueChangeListener;
33 import javax.faces.validator.LengthValidator;
34 import javax.faces.validator.Validator;
35
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38
39 import org.apache.shale.clay.component.Clay;
40
41 public class BindingTestCase extends AbstractTestCaseConfig {
42
43
44 public BindingTestCase(String name) {
45 super(name);
46 }
47
48
49 public static Test suite() {
50 return (new TestSuite(BindingTestCase.class));
51 }
52
53 private HtmlCommandButton command = null;
54 private HtmlInputText input = null;
55 private Converter converter = null;
56 private Validator validator = null;
57 private ActionListener actionListener = null;
58 private ValueChangeListener valueChangeListener = null;
59
60
61
62 private Clay clay = null;
63 protected void setUp() throws Exception {
64 super.setUp();
65
66
67 loadConfigFiles(null, null);
68
69 clay = (Clay) application
70 .createComponent("org.apache.shale.clay.component.Clay");
71 clay.setId("test");
72 clay.setJsfid("/org/apache/shale/clay/config/binding.html");
73 clay.setManagedBeanName("test");
74
75 facesContext.getExternalContext().getSessionMap().put("test", this);
76
77
78 StringWriter writer = new StringWriter();
79
80 ResponseWriter buffResponsewriter = facesContext.getRenderKit()
81 .createResponseWriter(writer, null,
82 response.getCharacterEncoding());
83
84 facesContext.setResponseWriter(buffResponsewriter);
85
86 buffResponsewriter.startDocument();
87
88
89 clay.encodeBegin(facesContext);
90 }
91
92 public void testInputBindings() {
93 HtmlInputText findInput = (HtmlInputText) findComponent(clay, "input");
94 assertNotNull(findInput);
95 assertTrue(getInput() == findInput);
96
97 assertEquals(1, findInput.getValidators().length);
98 assertTrue(getValidator() == findInput.getValidators()[0]);
99
100
101 assertEquals(100, ((LengthValidator) getValidator()).getMaximum());
102
103 assertNotNull(findInput.getConverter());
104 assertTrue(getConverter() == findInput.getConverter());
105
106 assertEquals(1, findInput.getValueChangeListeners().length);
107 assertTrue(getValueChangeListener() == findInput.getValueChangeListeners()[0]);
108 }
109
110 public void testCommandBindings() {
111 HtmlCommandButton findCommand = (HtmlCommandButton) findComponent(clay, "command");
112 assertNotNull(findCommand);
113
114 assertEquals(1, findCommand.getActionListeners().length);
115 assertTrue(getActionListener() == findCommand.getActionListeners()[0]);
116 }
117
118 public ActionListener getActionListener() {
119 if (actionListener == null) {
120 actionListener = new ActionListener() {
121 public void processAction(ActionEvent event) {
122
123 };
124 };
125 }
126 return actionListener;
127 }
128
129 public void setActionListener(ActionListener actionListener) {
130 this.actionListener = actionListener;
131 }
132
133 public HtmlCommandButton getCommand() {
134 if (command == null) {
135 command = new HtmlCommandButton();
136 }
137 return command;
138 }
139
140 public void setCommand(HtmlCommandButton command) {
141 this.command = command;
142 }
143
144 public Converter getConverter() {
145 if (converter == null) {
146 converter = new IntegerConverter();
147 }
148 return converter;
149 }
150
151 public void setConverter(Converter converter) {
152 this.converter = converter;
153 }
154
155 public HtmlInputText getInput() {
156 if (input == null) {
157 input = new HtmlInputText();
158 }
159 return input;
160 }
161
162 public void setInput(HtmlInputText input) {
163 this.input = input;
164 }
165
166 public Validator getValidator() {
167 if (validator == null) {
168 validator = new LengthValidator(5, 1);
169 }
170 return validator;
171 }
172
173 public void setValidator(Validator validator) {
174 this.validator = validator;
175 }
176
177 public ValueChangeListener getValueChangeListener() {
178 if (valueChangeListener == null) {
179 valueChangeListener = new ValueChangeListener() {
180 public void processValueChange(ValueChangeEvent event) {
181
182 };
183 };
184 }
185 return valueChangeListener;
186 }
187
188 public void setValueChangeListener(ValueChangeListener valueChangeListener) {
189 this.valueChangeListener = valueChangeListener;
190 }
191
192
193 private UIComponent findComponent(UIComponent parent, String id) {
194 if (parent == null) {
195 return null;
196 }
197
198 if (parent.getId() != null && parent.getId().equals(id)) {
199 return parent;
200 } else {
201 Iterator ci = parent.getChildren().iterator();
202 while (ci.hasNext()) {
203 UIComponent child = (UIComponent) ci.next();
204 UIComponent target = findComponent(child, id);
205 if (target != null) {
206 return target;
207 }
208 }
209 }
210
211 return null;
212 }
213
214
215 }