View Javadoc

1   /*
2    * $Id: MockFormBean.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.mock;
19  
20  import org.apache.struts.action.ActionForm;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /***
26   * <p>General purpose form bean for unit tests.</p>
27   *
28   * @version $Rev: 421119 $ $Date: 2005-05-07 12:45:39 -0400 (Sat, 07 May 2005)
29   *          $
30   */
31  public class MockFormBean extends ActionForm {
32      /*
33       * <p>
34       * Flag to indicate whether certain methods should complete properly
35       * or throw an Exception
36       * </p>
37       */
38      private boolean throwException = false;
39      private boolean returnNulls = false;
40      private String defaultValue;
41      private Double defaultDouble;
42      private int arrayCount;
43      protected boolean booleanProperty = false;
44      protected String stringProperty = null;
45  
46      // ------------------- Constructors
47      public MockFormBean() {
48          this(null);
49      }
50  
51      public MockFormBean(boolean throwException, boolean returnNulls) {
52          super();
53          this.throwException = throwException;
54          this.returnNulls = returnNulls;
55      }
56  
57      public MockFormBean(boolean throwException) {
58          this.throwException = throwException;
59      }
60  
61      public MockFormBean(boolean throwException, boolean returnNulls,
62          String defaultValue) {
63          this(throwException, returnNulls);
64          this.defaultValue = defaultValue;
65      }
66  
67      public MockFormBean(String stringProperty) {
68          this.stringProperty = stringProperty;
69      }
70  
71      public MockFormBean(boolean throwException, boolean returnNulls,
72          String defaultValue, int arrayCount) {
73          this(throwException, returnNulls, defaultValue);
74          this.arrayCount = arrayCount;
75      }
76  
77      public MockFormBean(boolean throwException, boolean returnNulls,
78          Double defaultDouble) {
79          this(throwException, returnNulls);
80          this.defaultDouble = defaultDouble;
81      }
82  
83      // ------------------- public methods
84      public String getJustThrowAnException()
85          throws Exception {
86          throw new Exception();
87      }
88  
89      public Object getThrowIllegalAccessException()
90          throws Exception {
91          if (true) {
92              throw new IllegalAccessException();
93          }
94  
95          return null;
96      }
97  
98      public String getStringValue()
99          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()
112         throws Exception {
113         if (throwException) {
114             throw new Exception();
115         }
116 
117         if (returnNulls) {
118             return null;
119         }
120 
121         String[] rtn = new String[arrayCount];
122 
123         for (int i = 0; i < rtn.length; i++) {
124             rtn[i] = defaultValue + i;
125         }
126 
127         return rtn;
128     }
129 
130     public Double getDoubleValue()
131         throws Exception {
132         if (throwException) {
133             throw new Exception();
134         }
135 
136         if (returnNulls) {
137             return null;
138         }
139 
140         return defaultDouble;
141     }
142 
143     public boolean getBooleanProperty() {
144         return (this.booleanProperty);
145     }
146 
147     public void setBooleanProperty(boolean booleanProperty) {
148         this.booleanProperty = booleanProperty;
149     }
150 
151     public Map getMapProperty() {
152         HashMap map = new HashMap();
153 
154         map.put("foo1", "bar1");
155         map.put("foo2", "bar2");
156 
157         return (map);
158     }
159 
160     public Map getMapPropertyArrayValues() {
161         HashMap map = new HashMap();
162 
163         map.put("foo1", new String[] { "bar1", "baz1" });
164         map.put("foo2", new String[] { "bar2", "baz2" });
165 
166         return (map);
167     }
168 
169     public String getStringProperty() {
170         return (this.stringProperty);
171     }
172 
173     public void setStringProperty(String stringProperty) {
174         this.stringProperty = stringProperty;
175     }
176 }