001: /*
002: * $Id: MockFormBean.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: package org.apache.struts.mock;
022:
023: import org.apache.struts.action.ActionForm;
024:
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: /**
029: * <p>General purpose form bean for unit tests.</p>
030: *
031: * @version $Rev: 471754 $ $Date: 2005-05-07 12:45:39 -0400 (Sat, 07 May 2005)
032: * $
033: */
034: public class MockFormBean extends ActionForm {
035: /*
036: * <p>
037: * Flag to indicate whether certain methods should complete properly
038: * or throw an Exception
039: * </p>
040: */
041: private boolean throwException = false;
042: private boolean returnNulls = false;
043: private String defaultValue;
044: private Double defaultDouble;
045: private int arrayCount;
046: protected boolean booleanProperty = false;
047: protected String stringProperty = null;
048:
049: // ------------------- Constructors
050: public MockFormBean() {
051: this (null);
052: }
053:
054: public MockFormBean(boolean throwException, boolean returnNulls) {
055: super ();
056: this .throwException = throwException;
057: this .returnNulls = returnNulls;
058: }
059:
060: public MockFormBean(boolean throwException) {
061: this .throwException = throwException;
062: }
063:
064: public MockFormBean(boolean throwException, boolean returnNulls,
065: String defaultValue) {
066: this (throwException, returnNulls);
067: this .defaultValue = defaultValue;
068: }
069:
070: public MockFormBean(String stringProperty) {
071: this .stringProperty = stringProperty;
072: }
073:
074: public MockFormBean(boolean throwException, boolean returnNulls,
075: String defaultValue, int arrayCount) {
076: this (throwException, returnNulls, defaultValue);
077: this .arrayCount = arrayCount;
078: }
079:
080: public MockFormBean(boolean throwException, boolean returnNulls,
081: Double defaultDouble) {
082: this (throwException, returnNulls);
083: this .defaultDouble = defaultDouble;
084: }
085:
086: // ------------------- public methods
087: public String getJustThrowAnException() throws Exception {
088: throw new Exception();
089: }
090:
091: public Object getThrowIllegalAccessException() throws Exception {
092: if (true) {
093: throw new IllegalAccessException();
094: }
095:
096: return null;
097: }
098:
099: public String getStringValue() throws Exception {
100: if (throwException) {
101: throw new Exception();
102: }
103:
104: if (returnNulls) {
105: return null;
106: }
107:
108: return defaultValue;
109: }
110:
111: public String[] getStringArray() throws Exception {
112: if (throwException) {
113: throw new Exception();
114: }
115:
116: if (returnNulls) {
117: return null;
118: }
119:
120: String[] rtn = new String[arrayCount];
121:
122: for (int i = 0; i < rtn.length; i++) {
123: rtn[i] = defaultValue + i;
124: }
125:
126: return rtn;
127: }
128:
129: public Double getDoubleValue() throws Exception {
130: if (throwException) {
131: throw new Exception();
132: }
133:
134: if (returnNulls) {
135: return null;
136: }
137:
138: return defaultDouble;
139: }
140:
141: public boolean getBooleanProperty() {
142: return (this .booleanProperty);
143: }
144:
145: public void setBooleanProperty(boolean booleanProperty) {
146: this .booleanProperty = booleanProperty;
147: }
148:
149: public Map getMapProperty() {
150: HashMap map = new HashMap();
151:
152: map.put("foo1", "bar1");
153: map.put("foo2", "bar2");
154:
155: return (map);
156: }
157:
158: public Map getMapPropertyArrayValues() {
159: HashMap map = new HashMap();
160:
161: map.put("foo1", new String[] { "bar1", "baz1" });
162: map.put("foo2", new String[] { "bar2", "baz2" });
163:
164: return (map);
165: }
166:
167: public String getStringProperty() {
168: return (this .stringProperty);
169: }
170:
171: public void setStringProperty(String stringProperty) {
172: this.stringProperty = stringProperty;
173: }
174: }
|