1 package org.apache.ojb.broker; 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.broker.query.Criteria; 8 import org.apache.ojb.broker.query.Query; 9 import org.apache.ojb.broker.query.QueryFactory; 10 import org.apache.ojb.junit.PBTestCase; 11 12 import java.io.Serializable ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 16 21 public class FieldConversionTest_2 extends PBTestCase 22 { 23 public FieldConversionTest_2(String testName) 24 { 25 super(testName); 26 } 27 28 public static void main(String [] args) 29 { 30 String [] arr = {FieldConversionTest_2.class.getName()}; 31 junit.textui.TestRunner.main(arr); 32 } 33 34 public void testConvertedReferenceLookup() 35 { 36 Collection coll = null; 37 Criteria c = null; 38 Query q = QueryFactory.newQuery(ConversionReferrer.class, c); 39 coll = broker.getCollectionByQuery(q); 40 assertTrue("There should be more than 0 matching items", coll.size() > 0); 41 42 Iterator i = coll.iterator(); 43 while (i.hasNext()) 44 { 45 ConversionReferrer cref = (ConversionReferrer) i.next(); 46 assertTrue("PK value should not be converted, id should be < 1000, found " + cref, cref.getPk1() < 1000); 47 assertTrue("Reference id should be > 1000, found " + cref, cref.getRef1() > 1000); 48 assertTrue("Reference should be non-null, found " + cref, cref.getReferred() != null); 49 assertEquals("Reference does not select correct item", cref.getRef1(), cref.getReferred().getPk1()); 50 } 51 } 52 53 public void testMultipleConverted() 54 { 55 String error = "Indicate that the field conversion was not/or multiple times called for a value, expected > 100 - found "; 56 Collection coll = null; 57 Criteria c = null; 58 Query q = QueryFactory.newQuery(ConversionReferrer.class, c); 59 coll = broker.getCollectionByQuery(q); 60 assertTrue("There should be more than 0 matching items", coll.size() > 0); 61 62 Iterator i = coll.iterator(); 63 while (i.hasNext()) 64 { 65 ConversionReferrer cref = (ConversionReferrer) i.next(); 66 assertTrue("PK value should not be converted, id should be < 1000, found " + cref, cref.getPk1() < 1000); 67 assertTrue("Reference should be non-null, found " + cref, cref.getReferred() != null); 68 assertEquals("Reference selected incorrect item", cref.getRef1(), cref.getReferred().getPk1()); 69 70 79 80 int value = 0; 81 value = cref.getRef1(); 82 assertTrue(error + cref, value > 1000); 83 84 value = cref.getTestId(); 85 assertTrue(error + cref, value > 1000); 86 87 value = cref.getReferred().getPk1(); 88 assertTrue(error + cref, value > 1000); 89 90 value = cref.getReferred().getTestId(); 91 assertTrue(error + cref, value > 1000); 92 } 93 } 94 95 public void testConvertedReferenceInsert() 96 { 97 String error = "Maybe field conversion was not called or multiple times"; 98 int no = 110; 99 int noRef = Integer.MAX_VALUE - 109; 100 int noTest = Integer.MAX_VALUE - 108; 101 int noTestRef = Integer.MAX_VALUE - 107; 102 103 ConversionReferrer cref = new ConversionReferrer(); 104 cref.setPk1(no); 105 cref.setTestId(noTest); 106 107 ConversionReferred crefed = new ConversionReferred(); 108 crefed.setPk1(noRef); 109 crefed.setTestId(noTestRef); 110 cref.setReferred(crefed); 112 113 broker.beginTransaction(); 114 broker.store(crefed); 115 broker.store(cref); 116 broker.commitTransaction(); 117 118 broker.clearCache(); 119 120 Identity id = new Identity(cref, broker); 122 broker.clearCache(); 123 124 ConversionReferrer referrer = (ConversionReferrer) broker.getObjectByIdentity(id); 125 assertNotNull(cref.getReferred()); 126 assertNotNull("We should found a reference, found " + referrer, referrer.getReferred()); 127 assertEquals("Stored reference ID should match refed object pk", referrer.getRef1(), crefed.getPk1()); 128 assertEquals(error, cref.getPk1(), referrer.getPk1()); 129 assertEquals(error, cref.getTestId(), referrer.getTestId()); 130 assertEquals(error, cref.getReferred().getPk1(), referrer.getReferred().getPk1()); 131 132 assertEquals(error, cref.getReferred().getTestId(), referrer.getReferred().getTestId()); 133 134 broker.beginTransaction(); 135 broker.delete(crefed); 137 broker.delete(cref); 138 broker.commitTransaction(); 139 } 140 141 142 150 public static class TestInt2IntConverter implements FieldConversion 151 { 152 153 154 public TestInt2IntConverter() 155 { 156 } 157 158 159 public Object javaToSql(Object source) throws ConversionException 160 { 161 162 int val = ((Integer ) source).intValue(); 163 val = Integer.MAX_VALUE - val; 164 return new Integer (val); 165 } 166 167 168 public Object sqlToJava(Object source) throws ConversionException 169 { 170 int val = ((Integer ) source).intValue(); 171 val = Integer.MAX_VALUE - val; 172 return new Integer (val); 173 } 174 } 175 176 177 public static class ConversionReferrer implements Serializable 181 { 182 183 private int pk1; 184 private int testId; 185 private int ref1; 186 private ConversionReferred referred; 187 188 189 public ConversionReferrer() 190 { 191 } 192 193 public String toString() 194 { 195 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE); 196 buf.append("pk1", pk1); 197 buf.append("ref1", ref1); 198 buf.append("testId", testId); 199 buf.append("referred", referred); 200 return buf.toString(); 201 } 202 203 public int getTestId() 204 { 205 return testId; 206 } 207 208 public void setTestId(int testId) 209 { 210 this.testId = testId; 211 } 212 213 217 public int getPk1() 218 { 219 return pk1; 220 } 221 222 226 public void setPk1(int pk1) 227 { 228 this.pk1 = pk1; 229 } 230 231 235 public int getRef1() 236 { 237 return ref1; 238 } 239 240 244 public void setRef1(int ref1) 245 { 246 this.ref1 = ref1; 247 } 248 249 253 public ConversionReferred getReferred() 254 { 255 return referred; 256 } 257 258 262 public void setReferred(ConversionReferred referred) 263 { 264 this.referred = referred; 265 } 266 } 267 268 public static class ConversionReferred implements Serializable 272 { 273 274 private int pk1; 275 private int testId; 276 277 278 public ConversionReferred() 279 { 280 } 281 282 public String toString() 283 { 284 ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE); 285 buf.append("pk1", pk1); 286 buf.append("testId", testId); 287 return buf.toString(); 288 } 289 290 public int getTestId() 291 { 292 return testId; 293 } 294 295 public void setTestId(int testId) 296 { 297 this.testId = testId; 298 } 299 300 304 public int getPk1() 305 { 306 return pk1; 307 } 308 309 313 public void setPk1(int pk1) 314 { 315 this.pk1 = pk1; 316 } 317 } 318 } 319 | Popular Tags |