001: package org.apache.ojb.broker;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.junit.PBTestCase;
019:
020: import java.io.Serializable;
021:
022: /**
023: * Test case for java.lang.Character/java.lang.String to CHAR/VARCHAR mappings.
024: *
025: * @author <a href="mailto:mkalen@apache.org">Martin Kalén</a>
026: * @version $Id: CharacterTest.java,v 1.1.2.3 2005/12/21 22:31:23 tomdz Exp $
027: * @since 1.0.2
028: */
029: public class CharacterTest extends PBTestCase {
030:
031: public void testJavaCharacterToJdbcCharMapping() {
032: final Character x = new Character('x');
033:
034: ObjectWithCharField obj = new ObjectWithCharField();
035: obj.setCharacterCharField(x);
036: pbPersist(obj);
037:
038: broker.clearCache();
039:
040: Identity oid = new Identity(obj, broker);
041: ObjectWithCharField dbObj;
042: assertNotNull(dbObj = (ObjectWithCharField) broker
043: .getObjectByIdentity(oid));
044: assertEquals(obj, dbObj);
045: }
046:
047: public void testJavaCharacterToJdbcVarcharMapping() {
048: final Character x = new Character('x');
049:
050: ObjectWithCharField obj = new ObjectWithCharField();
051: obj.setCharacterVarcharField(x);
052: pbPersist(obj);
053:
054: broker.clearCache();
055:
056: Identity oid = new Identity(obj, broker);
057: ObjectWithCharField dbObj;
058: assertNotNull(dbObj = (ObjectWithCharField) broker
059: .getObjectByIdentity(oid));
060: assertEquals(obj, dbObj);
061: }
062:
063: public void testJavaStringToJdbcCharMapping() {
064: final String strIn = "12345";
065:
066: ObjectWithCharField obj = new ObjectWithCharField();
067: obj.setStringCharField(strIn);
068: pbPersist(obj);
069:
070: broker.clearCache();
071:
072: Identity oid = new Identity(obj, broker);
073: ObjectWithCharField dbObj;
074: assertNotNull(dbObj = (ObjectWithCharField) broker
075: .getObjectByIdentity(oid));
076: assertEquals(obj.getId(), dbObj.getId());
077:
078: // mkalen: different JDBC-drivers do readback of CHAR(x) => java.lang.String differently,
079: // Oracle pads String with spaces to exactly x characters
080: // hsqldb clears trailing spaces
081: // PostgreSQL keeps some(v7)/all(v8) spaces
082: // We can't assert much here, other than that the start of string is not mutated.
083: final String strOut;
084: assertNotNull(strOut = dbObj.getStringCharField());
085: assertTrue(strOut, strOut.startsWith(strIn));
086: }
087:
088: public void testJavaStringToJdbcVarcharMapping() {
089: final String str = "12345";
090:
091: ObjectWithCharField obj = new ObjectWithCharField();
092: obj.setStringVarcharField(str);
093: pbPersist(obj);
094:
095: broker.clearCache();
096:
097: Identity oid = new Identity(obj, broker);
098: ObjectWithCharField dbObj;
099: assertNotNull(dbObj = (ObjectWithCharField) broker
100: .getObjectByIdentity(oid));
101: assertEquals(obj, dbObj);
102: }
103:
104: public static void main(String[] args) {
105: String[] testClasses = new String[] { CharacterTest.class
106: .getName() };
107: junit.textui.TestRunner.main(testClasses);
108: }
109:
110: public static class ObjectWithCharField implements Serializable {
111: private int id;
112: private Object characterCharField;
113: private Object characterVarcharField;
114: private String stringCharField;
115: private String stringVarcharField;
116:
117: public ObjectWithCharField() {
118: }
119:
120: public int getId() {
121: return id;
122: }
123:
124: public void setId(int id) {
125: this .id = id;
126: }
127:
128: public Object getCharacterCharField() {
129: return characterCharField;
130: }
131:
132: public void setCharacterCharField(Object characterCharField) {
133: this .characterCharField = characterCharField;
134: }
135:
136: public void setCharacterCharField(Character characterCharField) {
137: this .characterCharField = characterCharField;
138: }
139:
140: public Object getCharacterVarcharField() {
141: return characterVarcharField;
142: }
143:
144: public void setCharacterVarcharField(
145: Object characterVarcharField) {
146: this .characterVarcharField = characterVarcharField;
147: }
148:
149: public void setCharacterVarcharField(
150: Character characterVarcharField) {
151: this .characterVarcharField = characterVarcharField;
152: }
153:
154: public String getStringCharField() {
155: return stringCharField;
156: }
157:
158: public void setStringCharField(String stringCharField) {
159: this .stringCharField = stringCharField;
160: }
161:
162: public String getStringVarcharField() {
163: return stringVarcharField;
164: }
165:
166: public void setStringVarcharField(String stringVarcharField) {
167: this .stringVarcharField = stringVarcharField;
168: }
169:
170: public boolean equals(Object o) {
171: if (this == o)
172: return true;
173: if (!(o instanceof ObjectWithCharField))
174: return false;
175:
176: final ObjectWithCharField objectWithCharField = (ObjectWithCharField) o;
177:
178: if (id != objectWithCharField.id)
179: return false;
180: if (stringCharField != null ? !stringCharField
181: .equals(objectWithCharField.stringCharField)
182: : objectWithCharField.stringCharField != null)
183: return false;
184: if (stringVarcharField != null ? !stringVarcharField
185: .equals(objectWithCharField.stringVarcharField)
186: : objectWithCharField.stringVarcharField != null)
187: return false;
188: // Special comparison for the Object fields; use toString-representations if != null:
189: final String myCharacterCharField = characterCharField != null ? characterCharField
190: .toString()
191: : null;
192: final String myCharacterVarcharField = characterVarcharField != null ? characterVarcharField
193: .toString()
194: : null;
195: final String otherCharacterCharField = objectWithCharField.characterCharField != null ? objectWithCharField.characterCharField
196: .toString()
197: : null;
198: final String otherCharacterVarcharField = objectWithCharField.characterVarcharField != null ? objectWithCharField.characterVarcharField
199: .toString()
200: : null;
201: if (myCharacterCharField != null ? !myCharacterCharField
202: .equals(otherCharacterCharField)
203: : otherCharacterCharField != null)
204: return false;
205: if (myCharacterVarcharField != null ? !myCharacterVarcharField
206: .equals(otherCharacterVarcharField)
207: : otherCharacterVarcharField != null)
208: return false;
209:
210: return true;
211: }
212:
213: public int hashCode() {
214: int result;
215: result = id;
216: result = 29
217: * result
218: + (characterCharField != null ? characterCharField
219: .hashCode() : 0);
220: result = 29
221: * result
222: + (characterVarcharField != null ? characterVarcharField
223: .hashCode()
224: : 0);
225: result = 29
226: * result
227: + (stringCharField != null ? stringCharField
228: .hashCode() : 0);
229: result = 29
230: * result
231: + (stringVarcharField != null ? stringVarcharField
232: .hashCode() : 0);
233: return result;
234: }
235:
236: public String toString() {
237: return "CharacterTest$ObjectWithCharField (id=" + id
238: + ", characterCharField=[" + characterCharField
239: + "], characterVarcharField=["
240: + characterVarcharField + "], stringCharField=["
241: + stringCharField + "], stringVarcharField=["
242: + stringVarcharField + "])";
243: }
244:
245: }
246:
247: }
|