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.mock;
016:
017: import java.io.Serializable;
018: import java.util.Iterator;
019: import java.util.List;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.araneaframework.backend.util.BeanMapper;
023:
024: /**
025: * This VO is used by tests only ({@link org.araneaframework.uilib.tests.ListTest},
026: * {@link org.araneaframework.uilib.tests.FormTest},{@link org.araneaframework.uilib.tests.VoProcessingTest}.
027: *
028: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
029: *
030: */
031: public class TestVO implements Serializable, Cloneable {
032:
033: private static final Log log = LogFactory.getLog(TestVO.class);
034:
035: /**
036: * Private VoMapper, used for <code>toString</code> and <code>equals</code> methods.
037: */
038: private BeanMapper beanMapper;
039: private Long id;
040: private String stringValue;
041: private Boolean booleanValue;
042: private Long longValue;
043: private TestVO subTestVO;
044:
045: public TestVO() {
046: beanMapper = new BeanMapper(getClass());
047: }
048:
049: /**
050: * Returns id.
051: *
052: * @return the id.
053: */
054: public Long getId() {
055: return id;
056: }
057:
058: public void setId(Long id) {
059: this .id = id;
060: }
061:
062: /**
063: * <code>Boolean</code> value.
064: */
065: public Boolean getBooleanValue() {
066: return booleanValue;
067: }
068:
069: public void setBooleanValue(Boolean booleanValue) {
070: this .booleanValue = booleanValue;
071: }
072:
073: /**
074: * <code>Long</code> value.
075: */
076: public Long getLongValue() {
077: return longValue;
078: }
079:
080: public void setLongValue(Long longValue) {
081: this .longValue = longValue;
082: }
083:
084: /**
085: * <code>String</code> value.
086: */
087: public String getStringValue() {
088: return stringValue;
089: }
090:
091: public void setStringValue(String stringValue) {
092: this .stringValue = stringValue;
093: }
094:
095: /**
096: * Included <code>TestVO</code> for hierarchy tests.
097: */
098: public TestVO getSubTestVO() {
099: return subTestVO;
100: }
101:
102: public void setSubTestVO(TestVO subTestVO) {
103: this .subTestVO = subTestVO;
104: }
105:
106: //*******************************************************************
107: // PUBLIC METHODS
108: //*******************************************************************
109:
110: /**
111: * Overrides default <code>toString</code> method. <P/>
112: *
113: * @return <code>java.lang.String</code> representation of this value object.
114: */
115: public String toString() {
116: StringBuffer result = new StringBuffer();
117: List voFields = beanMapper.getFields();
118: for (Iterator i = voFields.iterator(); i.hasNext();) {
119: String field = (String) i.next();
120: result.append(field);
121: result.append("=");
122: result.append("" + beanMapper.getFieldValue(this , field));
123: result.append("; ");
124: }
125: return result.toString();
126: }
127:
128: /**
129: * Compares Value Object for equality by their fields.
130: *
131: * @param otherVo Value Object to compare to.
132: * @return <code>boolean</code>- if Value Object are equal.
133: */
134: public boolean equals(Object otherVo) {
135: //In case other VO is null, or of other class.
136: if (otherVo == null
137: || (!this .getClass().equals(otherVo.getClass()))) {
138: return false;
139: }
140:
141: //Otherwise compare all fields
142: boolean result = true;
143: List voFields = beanMapper.getFields();
144: for (Iterator i = voFields.iterator(); i.hasNext() && result;) {
145: String field = (String) i.next();
146: result = valuesEqual(beanMapper.getFieldValue(this , field),
147: beanMapper.getFieldValue(otherVo, field));
148: }
149: return result;
150: }
151:
152: /**
153: * Implements hash using Value Object fields.
154: */
155: public int hashCode() {
156: int result = 17;
157: List voFields = beanMapper.getFields();
158: for (Iterator i = voFields.iterator(); i.hasNext();) {
159: String field = (String) i.next();
160: result = 37 * result
161: + beanMapper.getFieldValue(this , field).hashCode();
162: }
163: return result;
164: }
165:
166: /**
167: * Overrides default <code>clone()</code> operation. <P/>
168: *
169: * @return clone of this object.
170: */
171: public Object clone() {
172: try {
173: return super .clone();
174: } catch (CloneNotSupportedException e) {
175: log
176: .warn("TestVO threw CloneNotSupportedException, check that everything is defined Cloneable");
177: return null;
178: }
179: }
180:
181: //*******************************************************************
182: // PRIVATE HELPER METHODS
183: //*******************************************************************
184:
185: /**
186: * Checks for object equality.
187: */
188: private boolean valuesEqual(Object value1, Object value2) {
189: return (value1 == null ? value2 == null : value1.equals(value2));
190: }
191:
192: }
|