KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > CharacterTest


1 package org.apache.ojb.broker;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.junit.PBTestCase;
19
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * Test case for java.lang.Character/java.lang.String to CHAR/VARCHAR mappings.
24  *
25  * @author <a HREF="mailto:mkalen@apache.org">Martin Kal&eacute;n</a>
26  * @version $Id: CharacterTest.java,v 1.1.2.3 2005/12/21 22:31:23 tomdz Exp $
27  * @since 1.0.2
28  */

29 public class CharacterTest extends PBTestCase
30 {
31
32     public void testJavaCharacterToJdbcCharMapping() {
33         final Character JavaDoc x = new Character JavaDoc('x');
34
35         ObjectWithCharField obj = new ObjectWithCharField();
36         obj.setCharacterCharField(x);
37         pbPersist(obj);
38
39         broker.clearCache();
40
41         Identity oid = new Identity(obj, broker);
42         ObjectWithCharField dbObj;
43         assertNotNull(dbObj = (ObjectWithCharField) broker.getObjectByIdentity(oid));
44         assertEquals(obj, dbObj);
45     }
46
47     public void testJavaCharacterToJdbcVarcharMapping() {
48         final Character JavaDoc x = new Character JavaDoc('x');
49
50         ObjectWithCharField obj = new ObjectWithCharField();
51         obj.setCharacterVarcharField(x);
52         pbPersist(obj);
53
54         broker.clearCache();
55
56         Identity oid = new Identity(obj, broker);
57         ObjectWithCharField dbObj;
58         assertNotNull(dbObj = (ObjectWithCharField) broker.getObjectByIdentity(oid));
59         assertEquals(obj, dbObj);
60     }
61
62     public void testJavaStringToJdbcCharMapping() {
63         final String JavaDoc strIn = "12345";
64
65         ObjectWithCharField obj = new ObjectWithCharField();
66         obj.setStringCharField(strIn);
67         pbPersist(obj);
68
69         broker.clearCache();
70
71         Identity oid = new Identity(obj, broker);
72         ObjectWithCharField dbObj;
73         assertNotNull(dbObj = (ObjectWithCharField) broker.getObjectByIdentity(oid));
74         assertEquals(obj.getId(), dbObj.getId());
75
76         // mkalen: different JDBC-drivers do readback of CHAR(x) => java.lang.String differently,
77
// Oracle pads String with spaces to exactly x characters
78
// hsqldb clears trailing spaces
79
// PostgreSQL keeps some(v7)/all(v8) spaces
80
// We can't assert much here, other than that the start of string is not mutated.
81
final String JavaDoc strOut;
82         assertNotNull(strOut = dbObj.getStringCharField());
83         assertTrue(strOut, strOut.startsWith(strIn));
84     }
85
86     public void testJavaStringToJdbcVarcharMapping() {
87         final String JavaDoc str = "12345";
88
89         ObjectWithCharField obj = new ObjectWithCharField();
90         obj.setStringVarcharField(str);
91         pbPersist(obj);
92
93         broker.clearCache();
94
95         Identity oid = new Identity(obj, broker);
96         ObjectWithCharField dbObj;
97         assertNotNull(dbObj = (ObjectWithCharField) broker.getObjectByIdentity(oid));
98         assertEquals(obj, dbObj);
99     }
100
101     public static void main(String JavaDoc[] args)
102     {
103         String JavaDoc[] testClasses = new String JavaDoc[]{ CharacterTest.class.getName() };
104         junit.textui.TestRunner.main(testClasses);
105     }
106
107     public static class ObjectWithCharField implements Serializable JavaDoc
108     {
109         private int id;
110         private Object JavaDoc characterCharField;
111         private Object JavaDoc characterVarcharField;
112         private String JavaDoc stringCharField;
113         private String JavaDoc stringVarcharField;
114
115         public ObjectWithCharField()
116         {
117         }
118
119         public int getId()
120         {
121             return id;
122         }
123
124         public void setId(int id)
125         {
126             this.id = id;
127         }
128
129         public Object JavaDoc getCharacterCharField()
130         {
131             return characterCharField;
132         }
133
134         public void setCharacterCharField(Object JavaDoc characterCharField)
135         {
136             this.characterCharField = characterCharField;
137         }
138
139         public void setCharacterCharField(Character JavaDoc characterCharField)
140         {
141             this.characterCharField = characterCharField;
142         }
143
144         public Object JavaDoc getCharacterVarcharField()
145         {
146             return characterVarcharField;
147         }
148
149         public void setCharacterVarcharField(Object JavaDoc characterVarcharField)
150         {
151             this.characterVarcharField = characterVarcharField;
152         }
153
154         public void setCharacterVarcharField(Character JavaDoc characterVarcharField)
155         {
156             this.characterVarcharField = characterVarcharField;
157         }
158
159         public String JavaDoc getStringCharField()
160         {
161             return stringCharField;
162         }
163
164         public void setStringCharField(String JavaDoc stringCharField)
165         {
166             this.stringCharField = stringCharField;
167         }
168
169         public String JavaDoc getStringVarcharField()
170         {
171             return stringVarcharField;
172         }
173
174         public void setStringVarcharField(String JavaDoc stringVarcharField)
175         {
176             this.stringVarcharField = stringVarcharField;
177         }
178
179         public boolean equals(Object JavaDoc o)
180         {
181             if (this == o) return true;
182             if (!(o instanceof ObjectWithCharField)) return false;
183
184             final ObjectWithCharField objectWithCharField = (ObjectWithCharField) o;
185
186             if (id != objectWithCharField.id) return false;
187             if (stringCharField != null ? !stringCharField.equals(objectWithCharField.stringCharField) : objectWithCharField.stringCharField != null) return false;
188             if (stringVarcharField != null ? !stringVarcharField.equals(objectWithCharField.stringVarcharField) : objectWithCharField.stringVarcharField != null) return false;
189             // Special comparison for the Object fields; use toString-representations if != null:
190
final String JavaDoc myCharacterCharField = characterCharField != null ? characterCharField.toString() : null;
191             final String JavaDoc myCharacterVarcharField = characterVarcharField != null ? characterVarcharField.toString() : null;
192             final String JavaDoc otherCharacterCharField = objectWithCharField.characterCharField != null ? objectWithCharField.characterCharField.toString() : null;
193             final String JavaDoc otherCharacterVarcharField = objectWithCharField.characterVarcharField != null ? objectWithCharField.characterVarcharField.toString() : null;
194             if (myCharacterCharField != null ? !myCharacterCharField.equals(otherCharacterCharField) : otherCharacterCharField != null) return false;
195             if (myCharacterVarcharField != null ? !myCharacterVarcharField.equals(otherCharacterVarcharField) : otherCharacterVarcharField != null) return false;
196
197             return true;
198         }
199
200         public int hashCode()
201         {
202             int result;
203             result = id;
204             result = 29 * result + (characterCharField != null ? characterCharField.hashCode() : 0);
205             result = 29 * result + (characterVarcharField != null ? characterVarcharField.hashCode() : 0);
206             result = 29 * result + (stringCharField != null ? stringCharField.hashCode() : 0);
207             result = 29 * result + (stringVarcharField != null ? stringVarcharField.hashCode() : 0);
208             return result;
209         }
210
211         public String JavaDoc toString()
212         {
213             return "CharacterTest$ObjectWithCharField (id=" + id +
214                    ", characterCharField=[" + characterCharField +
215                    "], characterVarcharField=[" + characterVarcharField +
216                    "], stringCharField=[" + stringCharField +
217                    "], stringVarcharField=[" + stringVarcharField +
218                    "])";
219         }
220
221     }
222
223 }
224
Popular Tags