1 package org.apache.ojb.broker; 2 3 import org.apache.ojb.broker.accesslayer.conversions.ConversionException; 4 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; 5 import org.apache.ojb.broker.query.Criteria; 6 import org.apache.ojb.broker.query.QueryFactory; 7 import org.apache.ojb.junit.PBTestCase; 8 9 import java.io.Serializable ; 10 import java.math.BigDecimal ; 11 12 18 public class FieldConversionTest_3 extends PBTestCase 19 { 20 21 public static void main(String [] args) 22 { 23 String [] arr = {FieldConversionTest_3.class.getName()}; 24 junit.textui.TestRunner.main(arr); 25 } 26 27 public void tearDown() 28 { 29 try 30 { 31 broker.clearCache(); 32 super.tearDown(); 33 } 34 catch (Exception e) 35 { 36 } 38 } 39 40 43 public void testStoreNestedNodes() throws Exception 44 { 45 long id = System.currentTimeMillis(); 47 Node node = new Node(id, null, true); 48 Node child = new Node(id + 1, node, false); 49 50 int before; 51 52 broker.beginTransaction(); 53 before = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null)); 54 55 broker.store(child); 56 57 broker.commitTransaction(); 58 59 broker.beginTransaction(); 60 int after = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null)); 61 broker.commitTransaction(); 62 63 assertFalse(after == 0); 64 assertEquals(before + 2, after); 65 } 66 67 70 public void testStoreNode() throws Exception 71 { 72 long id = System.currentTimeMillis(); 74 Node node = new Node(id, null, false); 75 76 int before; 77 78 broker.beginTransaction(); 79 before = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null)); 80 81 broker.store(node); 82 83 broker.commitTransaction(); 84 85 broker.beginTransaction(); 86 int after = broker.getCount(QueryFactory.newQuery(Node.class, (Criteria) null)); 87 broker.commitTransaction(); 88 89 assertFalse(after == 0); 90 assertEquals(before + 1, after); 91 } 92 93 96 public void testDeleteNode() throws Exception 97 { 98 NodeWoAutoInc node = new NodeWoAutoInc(0); 99 100 try 101 { 102 broker.beginTransaction(); 103 broker.delete(node); 106 broker.commitTransaction(); 107 } 108 finally 109 { 110 if (broker.isInTransaction()) 111 { 112 try 113 { 114 broker.abortTransaction(); 115 } 116 catch (Throwable ignore) 117 { 118 } 120 } 121 } 122 } 123 124 125 public static class LongToBigDecimalConversion implements FieldConversion 129 { 130 private static final Long NULL_BIG_DECIMAL = null; 131 private static final Long ZERO = new Long (0); 132 133 public LongToBigDecimalConversion() 134 { 135 } 136 137 public Object javaToSql(Object source) throws ConversionException 138 { 139 if(source == null) return null; 140 Object ret; 141 if (source instanceof Long ) 142 { 143 if (ZERO.equals(source)) 144 { 145 ret = NULL_BIG_DECIMAL; 146 } 147 else 148 { 149 ret = new BigDecimal (((Long ) source).doubleValue()); 150 } 151 } 152 else 153 { 154 throw new ConversionException( 155 "java-->sql, expected type was"+Long .class.getClass()+ 156 ", found type "+source.getClass()); 157 } 158 return ret; 159 } 160 161 public Object sqlToJava(Object source) throws ConversionException 162 { 163 if(source == null) return null; 164 Object ret; 165 if (source instanceof BigDecimal ) 166 { 167 ret = new Long (((BigDecimal ) source).longValue()); 168 } 169 else 170 { 171 throw new ConversionException( 172 "sql-->java, expected type was"+BigDecimal .class.getClass()+ 173 ", found type "+source.getClass()); 174 } 175 return ret; 176 } 177 } 178 179 180 public static class Node implements Serializable 184 { 185 private long uid; private long refId; 187 private boolean nodeState; 188 Node parent; 189 190 public Node() 191 { 192 } 193 194 public Node(long uid, Node parent, boolean nodeState) 195 { 196 this.uid = uid; 197 this.parent = parent; 198 this.nodeState = nodeState; 199 } 200 201 public long getUid() 202 { 203 return uid; 204 } 205 206 public void setUid(long uid) 207 { 208 this.uid = uid; 209 } 210 211 public boolean isNodeState() 212 { 213 return nodeState; 214 } 215 216 public void setNodeState(boolean nodeState) 217 { 218 this.nodeState = nodeState; 219 } 220 221 public long getRefId() 222 { 223 return refId; 224 } 225 226 public void setRefId(long refId) 227 { 228 this.refId = refId; 229 } 230 231 public Node getParent() 232 { 233 return parent; 234 } 235 236 public void setParent(Node parent) 237 { 238 this.parent = parent; 239 } 240 } 241 242 public static class NodeWoAutoInc implements Serializable 243 { 244 private long uid; 246 public NodeWoAutoInc() 247 { 248 } 249 250 public NodeWoAutoInc(long uid) 251 { 252 this.uid = uid; 253 } 254 255 public long getUid() 256 { 257 return uid; 258 } 259 260 public void setUid(long uid) 261 { 262 this.uid = uid; 263 } 264 } 265 266 } 267 | Popular Tags |