001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.tests;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019: import junit.framework.TestCase;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.araneaframework.tests.mock.MockEnvironment;
023: import org.araneaframework.tests.mock.TestVO;
024: import org.araneaframework.uilib.form.FormWidget;
025: import org.araneaframework.uilib.form.control.CheckboxControl;
026: import org.araneaframework.uilib.form.control.TextControl;
027: import org.araneaframework.uilib.form.data.BooleanData;
028: import org.araneaframework.uilib.form.data.LongData;
029: import org.araneaframework.uilib.form.data.StringData;
030: import org.araneaframework.uilib.form.reader.BeanFormReader;
031: import org.araneaframework.uilib.form.reader.BeanFormWriter;
032: import org.araneaframework.uilib.form.reader.MapFormReader;
033: import org.araneaframework.uilib.form.reader.MapFormWriter;
034:
035: /**
036: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
037: *
038: */
039: public class FormRWTest extends TestCase {
040:
041: private static final Log log = LogFactory.getLog(FormRWTest.class);
042:
043: private FormWidget makeVoForm() throws Exception {
044:
045: //Creating form :-)
046: FormWidget voForm = new FormWidget();
047: voForm._getComponent().init(null, new MockEnvironment());
048:
049: //Adding elements to form
050: voForm.addElement("booleanValue", "vo checkbox",
051: new CheckboxControl(), new BooleanData(), true);
052: voForm.addElement("stringValue", "vo text", new TextControl(),
053: new StringData(), true);
054: voForm.addElement("longValue", "vo long", new TextControl(),
055: new LongData(), true);
056:
057: //Adding a composite element
058: FormWidget subTestVO = voForm.addSubForm("subTestVO");
059: subTestVO.addElement("booleanValue", "vo checkbox",
060: new CheckboxControl(), new BooleanData(), true);
061: subTestVO.addElement("stringValue", "vo text",
062: new TextControl(), new StringData(), true);
063: subTestVO.addElement("longValue", "vo long", new TextControl(),
064: new LongData(), true);
065:
066: return voForm;
067: }
068:
069: /**
070: * Tests basic Value Object reading.
071: */
072: public void testFormBasicVoReading() throws Exception {
073:
074: FormWidget voForm = makeVoForm();
075: TestVO test = new TestVO();
076:
077: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
078: final Long TEN = new Long(10);
079:
080: voForm.setValueByFullName("booleanValue", Boolean.TRUE);
081: voForm.setValueByFullName("stringValue", LIFE_IS_BEAUTIFUL);
082: voForm.setValueByFullName("longValue", TEN);
083:
084: BeanFormReader voReader = new BeanFormReader(voForm);
085:
086: voReader.readFormBean(test);
087:
088: log.debug("Value Object after reading from form" + test);
089:
090: assertTrue("Boolean value read properly", test
091: .getBooleanValue().equals(Boolean.TRUE));
092: assertTrue("String value read properly", test.getStringValue()
093: .equals(LIFE_IS_BEAUTIFUL));
094: assertTrue("Long value read properly", test.getLongValue()
095: .equals(TEN));
096: }
097:
098: /**
099: * Tests hierarchical Value Object reading.
100: */
101: public void testFormHierarchicalVoReading() throws Exception {
102:
103: FormWidget voForm = makeVoForm();
104: TestVO test = new TestVO();
105:
106: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
107: final Long TEN = new Long(10);
108:
109: voForm.setValueByFullName("subTestVO.booleanValue",
110: Boolean.TRUE);
111: voForm.setValueByFullName("subTestVO.stringValue",
112: LIFE_IS_BEAUTIFUL);
113: voForm.setValueByFullName("subTestVO.longValue", TEN);
114:
115: BeanFormReader voReader = new BeanFormReader(voForm);
116:
117: voReader.readFormBean(test);
118:
119: log.debug("Value Object after reading from form");
120:
121: assertTrue("Boolean value read properly", test.getSubTestVO()
122: .getBooleanValue().equals(Boolean.TRUE));
123: assertTrue("String value read properly", test.getSubTestVO()
124: .getStringValue().equals(LIFE_IS_BEAUTIFUL));
125: assertTrue("Long value read properly", test.getSubTestVO()
126: .getLongValue().equals(TEN));
127: }
128:
129: /**
130: * Tests basic Value Object writing.
131: */
132: public void testFormBasicVoWriting() throws Exception {
133:
134: FormWidget voForm = makeVoForm();
135: TestVO test = new TestVO();
136:
137: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
138: final Long TEN = new Long(10);
139:
140: test.setBooleanValue(Boolean.TRUE);
141: test.setStringValue(LIFE_IS_BEAUTIFUL);
142: test.setLongValue(TEN);
143:
144: BeanFormWriter voWriter = new BeanFormWriter(test.getClass());
145:
146: voWriter.writeFormBean(voForm, test);
147:
148: assertTrue("Boolean value written properly", voForm
149: .getValueByFullName("booleanValue")
150: .equals(Boolean.TRUE));
151: assertTrue("String value written properly", voForm
152: .getValueByFullName("stringValue").equals(
153: LIFE_IS_BEAUTIFUL));
154: assertTrue("Long value written properly", voForm
155: .getValueByFullName("longValue").equals(TEN));
156: }
157:
158: /**
159: * Tests hierarchical Value Object writing.
160: */
161: public void testFormHierarchicalVoWriting() throws Exception {
162:
163: FormWidget voForm = makeVoForm();
164: TestVO test = new TestVO();
165:
166: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
167: final Long TEN = new Long(10);
168:
169: test.setSubTestVO(new TestVO());
170:
171: test.getSubTestVO().setBooleanValue(Boolean.TRUE);
172: test.getSubTestVO().setStringValue(LIFE_IS_BEAUTIFUL);
173: test.getSubTestVO().setLongValue(TEN);
174:
175: BeanFormWriter voWriter = new BeanFormWriter(test.getClass());
176:
177: voWriter.writeFormBean(voForm, test);
178:
179: assertTrue("Boolean value written properly", voForm
180: .getValueByFullName("subTestVO.booleanValue").equals(
181: Boolean.TRUE));
182: assertTrue("String value written properly", voForm
183: .getValueByFullName("subTestVO.stringValue").equals(
184: LIFE_IS_BEAUTIFUL));
185: assertTrue("Long value written properly", voForm
186: .getValueByFullName("subTestVO.longValue").equals(TEN));
187: }
188:
189: /**
190: * Tests basic Map reading.
191: */
192: public void testFormBasicMapReading() throws Exception {
193:
194: FormWidget mapForm = makeVoForm();
195:
196: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
197: final Long TEN = new Long(10);
198:
199: mapForm.setValueByFullName("booleanValue", Boolean.TRUE);
200: mapForm.setValueByFullName("stringValue", LIFE_IS_BEAUTIFUL);
201: mapForm.setValueByFullName("longValue", TEN);
202:
203: MapFormReader mapReader = new MapFormReader(mapForm);
204:
205: Map readMap = mapReader.getMap();
206:
207: log.debug("Map read from form: " + readMap);
208:
209: assertTrue("Boolean value read properly", readMap.get(
210: "booleanValue").equals(Boolean.TRUE));
211: assertTrue("String value read properly", readMap.get(
212: "stringValue").equals(LIFE_IS_BEAUTIFUL));
213: assertTrue("Long value read properly", readMap.get("longValue")
214: .equals(TEN));
215: }
216:
217: /**
218: * Tests hierarchical Map reading.
219: */
220: public void testFormHierarchicalMapReading() throws Exception {
221:
222: FormWidget mapForm = makeVoForm();
223:
224: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
225: final Long TEN = new Long(10);
226:
227: mapForm.setValueByFullName("subTestVO.booleanValue",
228: Boolean.TRUE);
229: mapForm.setValueByFullName("subTestVO.stringValue",
230: LIFE_IS_BEAUTIFUL);
231: mapForm.setValueByFullName("subTestVO.longValue", TEN);
232:
233: MapFormReader mapReader = new MapFormReader(mapForm);
234:
235: Map readMap = mapReader.getMap();
236:
237: log.debug("Map read from form: " + readMap);
238:
239: Map subTestVoMap = (Map) readMap.get("subTestVO");
240: assertEquals(Boolean.FALSE, readMap.get("booleanValue"));
241: assertNull("String value read properly", readMap
242: .get("stringValue"));
243: assertNull("Long value read properly", readMap.get("longValue"));
244:
245: assertTrue("Boolean value read properly", subTestVoMap.get(
246: "booleanValue").equals(Boolean.TRUE));
247: assertTrue("String value read properly", subTestVoMap.get(
248: "stringValue").equals(LIFE_IS_BEAUTIFUL));
249: assertTrue("Long value read properly", subTestVoMap.get(
250: "longValue").equals(TEN));
251: }
252:
253: /**
254: * Tests basic Map writing.
255: */
256: public void testFormBasicMapWriting() throws Exception {
257:
258: FormWidget mapForm = makeVoForm();
259: Map testMap = new HashMap();
260:
261: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
262: final Long TEN = new Long(10);
263:
264: testMap.put("booleanValue", Boolean.TRUE);
265: testMap.put("stringValue", LIFE_IS_BEAUTIFUL);
266: testMap.put("longValue", TEN);
267:
268: MapFormWriter mapWriter = new MapFormWriter();
269:
270: mapWriter.writeForm(mapForm, testMap);
271:
272: assertTrue("Boolean value written properly", mapForm
273: .getValueByFullName("booleanValue")
274: .equals(Boolean.TRUE));
275: assertTrue("String value written properly", mapForm
276: .getValueByFullName("stringValue").equals(
277: LIFE_IS_BEAUTIFUL));
278: assertTrue("Long value written properly", mapForm
279: .getValueByFullName("longValue").equals(TEN));
280: }
281:
282: /**
283: * Tests hierarchical Map writing.
284: */
285: public void testFormHierarchicalMapWriting() throws Exception {
286:
287: FormWidget mapForm = makeVoForm();
288: Map testMap = new HashMap();
289:
290: final String LIFE_IS_BEAUTIFUL = "Life is beautiful";
291: final Long TEN = new Long(10);
292:
293: testMap.put("booleanValue", Boolean.TRUE);
294: testMap.put("stringValue", LIFE_IS_BEAUTIFUL);
295: testMap.put("longValue", TEN);
296:
297: Map topTestMap = new HashMap();
298: topTestMap.put("subTestVO", testMap);
299:
300: MapFormWriter mapWriter = new MapFormWriter();
301:
302: mapWriter.writeForm(mapForm, topTestMap);
303:
304: assertTrue("Boolean value written properly", mapForm
305: .getValueByFullName("subTestVO.booleanValue").equals(
306: Boolean.TRUE));
307: assertTrue("String value written properly", mapForm
308: .getValueByFullName("subTestVO.stringValue").equals(
309: LIFE_IS_BEAUTIFUL));
310: assertTrue("Long value written properly", mapForm
311: .getValueByFullName("subTestVO.longValue").equals(TEN));
312: }
313:
314: }
|