1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 import org.apache.commons.lang.builder.ToStringBuilder; 5 import org.apache.commons.lang.builder.ToStringStyle; 6 import org.apache.ojb.broker.accesslayer.conversions.ConversionException; 7 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; 8 9 import java.io.Serializable ; 10 11 17 public class FieldConversionTest extends TestCase 18 { 19 private PersistenceBroker broker; 20 21 public static void main(String [] args) 22 { 23 String [] arr = {FieldConversionTest.class.getName()}; 24 junit.textui.TestRunner.main(arr); 25 } 26 27 31 public void setUp() throws PBFactoryException 32 { 33 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 34 } 35 36 40 public void tearDown() 41 { 42 try 43 { 44 broker.clearCache(); 45 broker.close(); 46 } 47 catch (PersistenceBrokerException e) 48 { 49 } 50 } 51 52 public void testConversion() 53 { 54 int id = (int) (System.currentTimeMillis() % Integer.MAX_VALUE); 55 ConversionVO vo = new ConversionVO( 56 null, new ConversionId(new Integer (id)), null, new ConversionId(new Integer (id + 1))); 57 broker.beginTransaction(); 58 broker.store(vo); 59 broker.commitTransaction(); 60 61 Identity identity = new Identity(vo, broker); 62 broker.clearCache(); 63 ConversionVO find_vo = (ConversionVO) broker.getObjectByIdentity(identity); 64 } 65 66 public static class FieldConversionConversionIdToInteger implements FieldConversion 67 { 68 public Object javaToSql(Object source) throws ConversionException 69 { 70 if (!(source instanceof ConversionId)) 71 { 72 throw new ConversionException( 73 "Wrong java field type when java-->sql, expected " + 74 ConversionId.class.getClass() + ", found " 75 + source.getClass()); 76 } 77 return ((ConversionId) source).getConversionId(); 78 } 79 80 public Object sqlToJava(Object source) throws ConversionException 81 { 82 if (!(source instanceof Integer )) 83 { 84 throw new ConversionException( 85 "Wrong java field type when java-->sql, expected java.lang.Integer, found " 86 + source.getClass()); 87 } 88 return new ConversionId((Integer ) source); 89 } 90 } 91 92 public static class FieldConversionLongToInteger implements FieldConversion 93 { 94 public Object javaToSql(Object source) throws ConversionException 95 { 96 if(source == null) return null; 97 if (!(source instanceof Long )) 98 { 99 throw new ConversionException( 100 "Wrong java field type when java-->sql, expected java.lang.Long, found " 101 + source.getClass()); 102 } 103 return new Integer (((Long ) source).intValue()); 104 } 105 106 public Object sqlToJava(Object source) throws ConversionException 107 { 108 if(source == null) return null; 109 if (!(source instanceof Integer )) 110 { 111 throw new ConversionException( 112 "Wrong java field type when java-->sql, expected java.lang.Integer, found " 113 + source.getClass()); 114 } 115 return new Long (((Integer ) source).longValue()); 116 } 117 } 118 119 public static class ConversionId implements Serializable 120 { 121 private Integer conversionId; 122 123 public ConversionId(Integer conversionId) 124 { 125 this.conversionId = conversionId; 126 } 127 128 public Integer getConversionId() 129 { 130 return conversionId; 131 } 132 133 public void setConversionId(Integer conversionId) 134 { 135 this.conversionId = conversionId; 136 } 137 138 public String toString() 139 { 140 return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE). 141 append("conversionId", conversionId).toString(); 142 } 143 } 144 145 public static class ConversionVO implements Serializable 146 { 147 Long pkWithAutoIncrement; 148 ConversionId pkWithoutAutoIncrement; 149 Long normalWithAutoIncrement; 150 ConversionId normalWithoutAutoIncrement; 151 152 public ConversionVO() 153 { 154 } 155 156 public ConversionVO(Long pkWithAutoIncrement, ConversionId pkWithoutAutoIncrement, Long normalWithAutoIncrement, ConversionId normalWithoutAutoIncrement) 157 { 158 this.pkWithAutoIncrement = pkWithAutoIncrement; 159 this.pkWithoutAutoIncrement = pkWithoutAutoIncrement; 160 this.normalWithAutoIncrement = normalWithAutoIncrement; 161 this.normalWithoutAutoIncrement = normalWithoutAutoIncrement; 162 } 163 164 public String toString() 165 { 166 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE). 167 append("pkWithAutoIncrement", pkWithAutoIncrement). 168 append("pkWithoutAutoIncrement", pkWithoutAutoIncrement). 169 append("normalWithAutoIncrement", normalWithAutoIncrement). 170 append("normalWithoutAutoIncrement", normalWithoutAutoIncrement).toString(); 171 } 172 173 public Long getPkWithAutoIncrement() 174 { 175 return pkWithAutoIncrement; 176 } 177 178 public void setPkWithAutoIncrement(Long pkWithAutoIncrement) 179 { 180 this.pkWithAutoIncrement = pkWithAutoIncrement; 181 } 182 183 public ConversionId getPkWithoutAutoIncrement() 184 { 185 return pkWithoutAutoIncrement; 186 } 187 188 public void setPkWithoutAutoIncrement(ConversionId pkWithoutAutoIncrement) 189 { 190 this.pkWithoutAutoIncrement = pkWithoutAutoIncrement; 191 } 192 193 public Long getNormalWithAutoIncrement() 194 { 195 return normalWithAutoIncrement; 196 } 197 198 public void setNormalWithAutoIncrement(Long normalWithAutoIncrement) 199 { 200 this.normalWithAutoIncrement = normalWithAutoIncrement; 201 } 202 203 public ConversionId getNormalWithoutAutoIncrement() 204 { 205 return normalWithoutAutoIncrement; 206 } 207 208 public void setNormalWithoutAutoIncrement(ConversionId normalWithoutAutoIncrement) 209 { 210 this.normalWithoutAutoIncrement = normalWithoutAutoIncrement; 211 } 212 } 213 } 214 | Popular Tags |