001: /*
002: * $Id: TestBean.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.exercise;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Vector;
030: import javax.servlet.http.HttpServletRequest;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionMapping;
033: import org.apache.struts.util.LabelValueBean;
034:
035: /**
036: * General purpose test bean for Struts custom tag tests.
037: *
038: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
039: */
040:
041: public class TestBean extends ActionForm {
042:
043: // ------------------------------------------------------------- Properties
044:
045: /**
046: * A collection property where the elements of the collection are
047: * of type <code>LabelValueBean</code>.
048: */
049: private Collection beanCollection = null;
050:
051: public Collection getBeanCollection() {
052: if (beanCollection == null) {
053: Vector entries = new Vector(10);
054:
055: entries.add(new LabelValueBean("Label 0", "Value 0"));
056: entries.add(new LabelValueBean("Label 1", "Value 1"));
057: entries.add(new LabelValueBean("Label 2", "Value 2"));
058: entries.add(new LabelValueBean("Label 3", "Value 3"));
059: entries.add(new LabelValueBean("Label 4", "Value 4"));
060: entries.add(new LabelValueBean("Label 5", "Value 5"));
061: entries.add(new LabelValueBean("Label 6", "Value 6"));
062: entries.add(new LabelValueBean("Label 7", "Value 7"));
063: entries.add(new LabelValueBean("Label 8", "Value 8"));
064: entries.add(new LabelValueBean("Label 9", "Value 9"));
065:
066: beanCollection = entries;
067: }
068:
069: return (beanCollection);
070: }
071:
072: public void setBeanCollection(Collection beanCollection) {
073: this .beanCollection = beanCollection;
074: }
075:
076: /**
077: * A multiple-String SELECT element using a bean collection.
078: */
079: private String[] beanCollectionSelect = { "Value 1", "Value 3",
080: "Value 5" };
081:
082: public String[] getBeanCollectionSelect() {
083: return (this .beanCollectionSelect);
084: }
085:
086: public void setBeanCollectionSelect(String beanCollectionSelect[]) {
087: this .beanCollectionSelect = beanCollectionSelect;
088: }
089:
090: /**
091: * A boolean property whose initial value is true.
092: */
093: private boolean booleanProperty = true;
094:
095: public boolean getBooleanProperty() {
096: return (booleanProperty);
097: }
098:
099: public void setBooleanProperty(boolean booleanProperty) {
100: this .booleanProperty = booleanProperty;
101: }
102:
103: /**
104: * A multiple-String SELECT element using a collection.
105: */
106: private String[] collectionSelect = { "Value 2", "Value 4",
107: "Value 6" };
108:
109: public String[] getCollectionSelect() {
110: return (this .collectionSelect);
111: }
112:
113: public void setCollectionSelect(String collectionSelect[]) {
114: this .collectionSelect = collectionSelect;
115: }
116:
117: /**
118: * A double property.
119: */
120: private double doubleProperty = 321.0;
121:
122: public double getDoubleProperty() {
123: return (this .doubleProperty);
124: }
125:
126: public void setDoubleProperty(double doubleProperty) {
127: this .doubleProperty = doubleProperty;
128: }
129:
130: /**
131: * A boolean property whose initial value is false
132: */
133: private boolean falseProperty = false;
134:
135: public boolean getFalseProperty() {
136: return (falseProperty);
137: }
138:
139: public void setFalseProperty(boolean falseProperty) {
140: this .falseProperty = falseProperty;
141: }
142:
143: /**
144: * A float property.
145: */
146: private float floatProperty = (float) 123.0;
147:
148: public float getFloatProperty() {
149: return (this .floatProperty);
150: }
151:
152: public void setFloatProperty(float floatProperty) {
153: this .floatProperty = floatProperty;
154: }
155:
156: /**
157: * Integer arrays that are accessed as an array as well as indexed.
158: */
159: private int intArray[] = { 0, 10, 20, 30, 40 };
160:
161: public int[] getIntArray() {
162: return (this .intArray);
163: }
164:
165: public void setIntArray(int intArray[]) {
166: this .intArray = intArray;
167: }
168:
169: private int intIndexed[] = { 0, 10, 20, 30, 40 };
170:
171: public int getIntIndexed(int index) {
172: return (intIndexed[index]);
173: }
174:
175: public void setIntIndexed(int index, int value) {
176: intIndexed[index] = value;
177: }
178:
179: private int intMultibox[] = new int[0];
180:
181: public int[] getIntMultibox() {
182: return (this .intMultibox);
183: }
184:
185: public void setIntMultibox(int intMultibox[]) {
186: this .intMultibox = intMultibox;
187: }
188:
189: /**
190: * An integer property.
191: */
192: private int intProperty = 123;
193:
194: public int getIntProperty() {
195: return (this .intProperty);
196: }
197:
198: public void setIntProperty(int intProperty) {
199: this .intProperty = intProperty;
200: }
201:
202: /**
203: * A long property.
204: */
205: private long longProperty = 321;
206:
207: public long getLongProperty() {
208: return (this .longProperty);
209: }
210:
211: public void setLongProperty(long longProperty) {
212: this .longProperty = longProperty;
213: }
214:
215: /**
216: * A multiple-String SELECT element.
217: */
218: private String[] multipleSelect = { "Multiple 3", "Multiple 5",
219: "Multiple 7" };
220:
221: public String[] getMultipleSelect() {
222: return (this .multipleSelect);
223: }
224:
225: public void setMultipleSelect(String multipleSelect[]) {
226: this .multipleSelect = multipleSelect;
227: }
228:
229: /**
230: * A nested reference to another test bean (populated as needed).
231: */
232: private TestBean nested = null;
233:
234: public TestBean getNested() {
235: if (nested == null)
236: nested = new TestBean();
237: return (nested);
238: }
239:
240: /**
241: * A String property with an initial value of null.
242: */
243: private String nullProperty = null;
244:
245: public String getNullProperty() {
246: return (this .nullProperty);
247: }
248:
249: public void setNullProperty(String nullProperty) {
250: this .nullProperty = nullProperty;
251: }
252:
253: /**
254: * A short property.
255: */
256: private short shortProperty = (short) 987;
257:
258: public short getShortProperty() {
259: return (this .shortProperty);
260: }
261:
262: public void setShortProperty(short shortProperty) {
263: this .shortProperty = shortProperty;
264: }
265:
266: /**
267: * A single-String value for a SELECT element.
268: */
269: private String singleSelect = "Single 5";
270:
271: public String getSingleSelect() {
272: return (this .singleSelect);
273: }
274:
275: public void setSingleSelect(String singleSelect) {
276: this .singleSelect = singleSelect;
277: }
278:
279: /**
280: * String arrays that are accessed as an array as well as indexed.
281: */
282: private String stringArray[] = { "String 0", "String 1",
283: "String 2", "String 3", "String 4" };
284:
285: public String[] getStringArray() {
286: return (this .stringArray);
287: }
288:
289: public void setStringArray(String stringArray[]) {
290: this .stringArray = stringArray;
291: }
292:
293: private String stringIndexed[] = { "String 0", "String 1",
294: "String 2", "String 3", "String 4" };
295:
296: public String getStringIndexed(int index) {
297: return (stringIndexed[index]);
298: }
299:
300: public void setStringIndexed(int index, String value) {
301: stringIndexed[index] = value;
302: }
303:
304: private String stringMultibox[] = new String[0];
305:
306: public String[] getStringMultibox() {
307: return (this .stringMultibox);
308: }
309:
310: public void setStringMultibox(String stringMultibox[]) {
311: this .stringMultibox = stringMultibox;
312: }
313:
314: /**
315: * A String property.
316: */
317: private String stringProperty = "This is a string";
318:
319: public String getStringProperty() {
320: return (this .stringProperty);
321: }
322:
323: public void setStringProperty(String stringProperty) {
324: this .stringProperty = stringProperty;
325: }
326:
327: /**
328: * An empty String property.
329: */
330: private String emptyStringProperty = "";
331:
332: public String getEmptyStringProperty() {
333: return (this .emptyStringProperty);
334: }
335:
336: public void setEmptyStringProperty(String emptyStringProperty) {
337: this .emptyStringProperty = emptyStringProperty;
338: }
339:
340: /**
341: * A single-String value for a SELECT element based on resource strings.
342: */
343: private String resourcesSelect = "Resources 2";
344:
345: public String getResourcesSelect() {
346: return (this .resourcesSelect);
347: }
348:
349: public void setResourcesSelect(String resourcesSelect) {
350: this .resourcesSelect = resourcesSelect;
351: }
352:
353: /**
354: * A property that allows a null value but is still used in a SELECT.
355: */
356: private String withNulls = null;
357:
358: public String getWithNulls() {
359: return (this .withNulls);
360: }
361:
362: public void setWithNulls(String withNulls) {
363: this .withNulls = withNulls;
364: }
365:
366: /**
367: * A List property.
368: */
369: private List listProperty = null;
370:
371: public List getListProperty() {
372: if (listProperty == null) {
373: listProperty = new ArrayList();
374: listProperty.add("dummy");
375: }
376: return listProperty;
377: }
378:
379: public void setListProperty(List listProperty) {
380: this .listProperty = listProperty;
381: }
382:
383: /**
384: * An empty List property.
385: */
386: private List emptyListProperty = null;
387:
388: public List getEmptyListProperty() {
389: if (emptyListProperty == null) {
390: emptyListProperty = new ArrayList();
391: }
392: return emptyListProperty;
393: }
394:
395: public void setEmptyListProperty(List emptyListProperty) {
396: this .emptyListProperty = emptyListProperty;
397: }
398:
399: /**
400: * A Map property.
401: */
402: private Map mapProperty = null;
403:
404: public Map getMapProperty() {
405: if (mapProperty == null) {
406: mapProperty = new HashMap();
407: mapProperty.put("dummy", "dummy");
408: }
409: return mapProperty;
410: }
411:
412: public void setMapProperty(Map mapProperty) {
413: this .mapProperty = mapProperty;
414: }
415:
416: /**
417: * An empty Map property.
418: */
419: private Map emptyMapProperty = null;
420:
421: public Map getEmptyMapProperty() {
422: if (emptyMapProperty == null) {
423: emptyMapProperty = new HashMap();
424: }
425: return emptyMapProperty;
426: }
427:
428: public void setEmptyMapProperty(Map emptyMapProperty) {
429: this .emptyMapProperty = emptyMapProperty;
430: }
431:
432: // --------------------------------------------------------- Public Methods
433:
434: /**
435: * Reset the properties that will be received as input.
436: */
437: public void reset(ActionMapping mapping, HttpServletRequest request) {
438:
439: booleanProperty = false;
440: collectionSelect = new String[0];
441: intMultibox = new int[0];
442: multipleSelect = new String[0];
443: stringMultibox = new String[0];
444: if (nested != null)
445: nested.reset(mapping, request);
446:
447: }
448:
449: }
|