1 package org.apache.ojb.broker; 2 3 17 18 import org.apache.ojb.junit.PBTestCase; 19 20 import java.io.Serializable ; 21 22 29 public class CharacterTest extends PBTestCase 30 { 31 32 public void testJavaCharacterToJdbcCharMapping() { 33 final Character x = new Character ('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 x = new Character ('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 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 final String strOut; 82 assertNotNull(strOut = dbObj.getStringCharField()); 83 assertTrue(strOut, strOut.startsWith(strIn)); 84 } 85 86 public void testJavaStringToJdbcVarcharMapping() { 87 final String 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 [] args) 102 { 103 String [] testClasses = new String []{ CharacterTest.class.getName() }; 104 junit.textui.TestRunner.main(testClasses); 105 } 106 107 public static class ObjectWithCharField implements Serializable 108 { 109 private int id; 110 private Object characterCharField; 111 private Object characterVarcharField; 112 private String stringCharField; 113 private String 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 getCharacterCharField() 130 { 131 return characterCharField; 132 } 133 134 public void setCharacterCharField(Object characterCharField) 135 { 136 this.characterCharField = characterCharField; 137 } 138 139 public void setCharacterCharField(Character characterCharField) 140 { 141 this.characterCharField = characterCharField; 142 } 143 144 public Object getCharacterVarcharField() 145 { 146 return characterVarcharField; 147 } 148 149 public void setCharacterVarcharField(Object characterVarcharField) 150 { 151 this.characterVarcharField = characterVarcharField; 152 } 153 154 public void setCharacterVarcharField(Character characterVarcharField) 155 { 156 this.characterVarcharField = characterVarcharField; 157 } 158 159 public String getStringCharField() 160 { 161 return stringCharField; 162 } 163 164 public void setStringCharField(String stringCharField) 165 { 166 this.stringCharField = stringCharField; 167 } 168 169 public String getStringVarcharField() 170 { 171 return stringVarcharField; 172 } 173 174 public void setStringVarcharField(String stringVarcharField) 175 { 176 this.stringVarcharField = stringVarcharField; 177 } 178 179 public boolean equals(Object 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 final String myCharacterCharField = characterCharField != null ? characterCharField.toString() : null; 191 final String myCharacterVarcharField = characterVarcharField != null ? characterVarcharField.toString() : null; 192 final String otherCharacterCharField = objectWithCharField.characterCharField != null ? objectWithCharField.characterCharField.toString() : null; 193 final String 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 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 |