1 package org.apache.ojb.odmg; 2 3 import org.apache.commons.lang.builder.ToStringBuilder; 4 import org.apache.commons.lang.builder.ToStringStyle; 5 import org.apache.ojb.broker.accesslayer.conversions.ConversionException; 6 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; 7 import org.apache.ojb.junit.ODMGTestCase; 8 import org.odmg.OQLQuery; 9 import org.odmg.Transaction; 10 11 import java.io.Serializable ; 12 import java.math.BigDecimal ; 13 import java.util.List ; 14 15 22 public class FieldConversionTest_4 extends ODMGTestCase 23 { 24 public static void main(String [] args) 25 { 26 String [] arr = {FieldConversionTest_4.class.getName()}; 27 junit.textui.TestRunner.main(arr); 28 } 29 30 public void testSelfReferingParent() throws Exception 31 { 32 String strQuery = "select allNodes from " + Node.class.getName(); 33 long id = System.currentTimeMillis(); 34 Node node = new Node(id, null, true); 35 node.setParent(node); 36 37 List result; 38 int before; 39 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 40 try 41 { 42 tx.begin(); 43 44 OQLQuery query = odmg.newOQLQuery(); 45 query.create(strQuery); 46 result = (List ) query.execute(); 47 before = result.size(); 48 49 database.makePersistent(node); 50 tx.commit(); 51 52 tx.begin(); 53 tx.getBroker().clearCache(); 54 query = odmg.newOQLQuery(); 55 query.create(strQuery); 56 result = (List ) query.execute(); 57 tx.commit(); 58 } 59 finally 60 { 61 if (tx != null && tx.isOpen()) 62 { 63 tx.abort(); 64 } 65 } 66 int after = result.size(); 67 assertFalse(after == 0); 68 assertEquals(before + 1, after); 69 70 tx.begin(); 71 database.deletePersistent(node); 72 tx.commit(); 73 74 OQLQuery query = odmg.newOQLQuery(); 75 query.create(strQuery); 76 result = (List ) query.execute(); 77 after = result.size(); 78 assertEquals(before, after); 79 } 80 81 public void testMakePersistentNode() throws Exception 82 { 83 String strQuery = "select allNodes from " + Node.class.getName(); 84 long id = System.currentTimeMillis(); 85 Node node = new Node(id, null, true); 86 Node child = new Node(id + 1, node, false); 87 88 89 List result; 90 int before; 91 Transaction tx = odmg.newTransaction(); 92 try 93 { 94 tx.begin(); 95 96 OQLQuery query = odmg.newOQLQuery(); 97 query.create(strQuery); 98 result = (List ) query.execute(); 99 before = result.size(); 100 101 database.makePersistent(child); 102 103 tx.commit(); 104 105 tx.begin(); 106 query = odmg.newOQLQuery(); 107 query.create(strQuery); 108 result = (List ) query.execute(); 109 tx.commit(); 110 } 111 finally 112 { 113 if (tx != null && tx.isOpen()) 114 { 115 tx.abort(); 116 } 117 } 118 119 int after = result.size(); 120 121 assertFalse(after == 0); 122 assertEquals(before + 2, after); 123 124 } 125 126 public void testLockNode() throws Exception 127 { 128 String strQuery = "select allNodes from " + Node.class.getName(); 129 long id = System.currentTimeMillis(); 130 Node node = new Node(id, null, true); 131 Node child = new Node(id + 1, node, false); 132 133 Transaction tx = odmg.newTransaction(); 134 List result; 135 int before; 136 137 try 138 { 139 tx.begin(); 140 OQLQuery query = odmg.newOQLQuery(); 141 query.create(strQuery); 142 result = (List ) query.execute(); 143 before = result.size(); 144 145 tx.lock(child, Transaction.WRITE); 146 tx.commit(); 147 148 tx.begin(); 149 query = odmg.newOQLQuery(); 150 query.create(strQuery); 151 result = (List ) query.execute(); 152 tx.commit(); 153 } 154 finally 155 { 156 if (tx != null && tx.isOpen()) 157 { 158 tx.abort(); 159 } 160 } 161 162 int after = result.size(); 163 164 assertFalse(after == 0); 165 assertEquals(before + 2, after); 166 } 167 168 169 public static class LongToBigDecimalConversion implements FieldConversion 173 { 174 public LongToBigDecimalConversion() 175 { 176 } 177 178 public Object javaToSql(Object source) throws ConversionException 179 { 180 Object ret; 181 if (source == null) 182 { 183 ret = null; 184 } 185 else if (source instanceof Long ) 186 { 187 ret = new BigDecimal (((Long ) source).doubleValue()); 188 } 189 else 190 { 191 throw new ConversionException( 192 "java-->sql, expected type was"+Long .class.getClass()+ 193 ", found type "+source.getClass()); 194 } 195 return ret; 196 } 197 198 public Object sqlToJava(Object source) throws ConversionException 199 { 200 Object ret; 201 if (source == null) 202 { 203 ret = null; 204 } 205 else if (source instanceof BigDecimal ) 206 { 207 ret = new Long (((BigDecimal ) source).longValue()); 208 } 209 else 210 { 211 throw new ConversionException( 212 "sql-->java, expected type was"+BigDecimal .class.getClass()+ 213 ", found type "+source.getClass()); 214 } 215 return ret; 216 } 217 } 218 219 220 public static class Node implements Serializable 224 { 225 private long uid; private long refId; 227 private boolean nodeState; 228 Node parent; 229 230 public Node() 231 { 232 } 233 234 public Node(long uid, Node parent, boolean nodeState) 235 { 236 this.uid = uid; 237 this.parent = parent; 238 this.nodeState = nodeState; 239 } 240 241 public String toString() 242 { 243 return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE); 244 } 245 246 public long getUid() 247 { 248 return uid; 249 } 250 251 public void setUid(long uid) 252 { 253 this.uid = uid; 254 } 255 256 public boolean isState() 257 { 258 return nodeState; 259 } 260 261 public void setState(boolean state) 262 { 263 this.nodeState = state; 264 } 265 266 public long getRefId() 267 { 268 return refId; 269 } 270 271 public void setRefId(long refId) 272 { 273 this.refId = refId; 274 } 275 276 public Node getParent() 277 { 278 return parent; 279 } 280 281 public void setParent(Node parent) 282 { 283 this.parent = parent; 284 } 285 } 286 } 287 | Popular Tags |