KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > FieldConversionTest_2


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 JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  *
18  * @author J. Russell Smyth
19  * @version $Id: FieldConversionTest_2.java,v 1.3 2004/05/31 22:57:21 arminw Exp $
20  */

21 public class FieldConversionTest_2 extends PBTestCase
22 {
23     public FieldConversionTest_2(String JavaDoc testName)
24     {
25         super(testName);
26     }
27
28     public static void main(String JavaDoc[] args)
29     {
30         String JavaDoc[] arr = {FieldConversionTest_2.class.getName()};
31         junit.textui.TestRunner.main(arr);
32     }
33
34     public void testConvertedReferenceLookup()
35     {
36         Collection JavaDoc 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 JavaDoc 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 JavaDoc error = "Indicate that the field conversion was not/or multiple times called for a value, expected > 100 - found ";
56         Collection JavaDoc 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 JavaDoc 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             /*
71             The used conversion does the following
72             val = Integer.MAX_VALUE - val;
73             for both conversion directions.
74             The result was e.g.
75             sqlToJava: 10 --> 2147483637
76             javaToSql: 2147483637 --> 10
77
78             */

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 JavaDoc 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         // set reference
111
cref.setReferred(crefed);
112
113         broker.beginTransaction();
114         broker.store(crefed);
115         broker.store(cref);
116         broker.commitTransaction();
117
118         broker.clearCache();
119
120         // save id for recapturing object
121
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         // delete objects
136
broker.delete(crefed);
137         broker.delete(cref);
138         broker.commitTransaction();
139     }
140
141
142     //****************************************************************************
143
// inner class
144
//****************************************************************************
145
/**
146      * A conversion class for unit testing. The conversion is nonsensical - java
147      * field is difference of Integer.MAX_VALUE and db value.
148      * @author drfish
149      */

150     public static class TestInt2IntConverter implements FieldConversion
151     {
152
153         /** Creates a new instance of FromMaxInt2IntConversion */
154         public TestInt2IntConverter()
155         {
156         }
157
158         /** convert a Java object to its SQL pendant, used for insert & update */
159         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
160         {
161
162             int val = ((Integer JavaDoc) source).intValue();
163             val = Integer.MAX_VALUE - val;
164             return new Integer JavaDoc(val);
165         }
166
167         /** convert a SQL value to a Java Object, used for SELECT */
168         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
169         {
170             int val = ((Integer JavaDoc) source).intValue();
171             val = Integer.MAX_VALUE - val;
172             return new Integer JavaDoc(val);
173         }
174     }
175
176
177     //****************************************************************************
178
// inner class
179
//****************************************************************************
180
public static class ConversionReferrer implements Serializable JavaDoc
181     {
182
183         private int pk1;
184         private int testId;
185         private int ref1;
186         private ConversionReferred referred;
187
188         /** Creates a new instance of ConversionParent */
189         public ConversionReferrer()
190         {
191         }
192
193         public String JavaDoc 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         /** Getter for property pk1.
214          * @return Value of property pk1.
215          *
216          */

217         public int getPk1()
218         {
219             return pk1;
220         }
221
222         /** Setter for property pk1.
223          * @param pk1 New value of property pk1.
224          *
225          */

226         public void setPk1(int pk1)
227         {
228             this.pk1 = pk1;
229         }
230
231         /** Getter for property ref1.
232          * @return Value of property ref1.
233          *
234          */

235         public int getRef1()
236         {
237             return ref1;
238         }
239
240         /** Setter for property ref1.
241          * @param ref1 New value of property ref1.
242          *
243          */

244         public void setRef1(int ref1)
245         {
246             this.ref1 = ref1;
247         }
248
249         /** Getter for property referred.
250          * @return Value of property referred.
251          *
252          */

253         public ConversionReferred getReferred()
254         {
255             return referred;
256         }
257
258         /** Setter for property referred.
259          * @param referred New value of property referred.
260          *
261          */

262         public void setReferred(ConversionReferred referred)
263         {
264             this.referred = referred;
265         }
266     }
267
268     //****************************************************************************
269
// inner class
270
//****************************************************************************
271
public static class ConversionReferred implements Serializable JavaDoc
272     {
273
274         private int pk1;
275         private int testId;
276
277         /** Creates a new instance of ConversionReferred */
278         public ConversionReferred()
279         {
280         }
281
282         public String JavaDoc 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         /** Getter for property pk1.
301          * @return Value of property pk1.
302          *
303          */

304         public int getPk1()
305         {
306             return pk1;
307         }
308
309         /** Setter for property pk1.
310          * @param pk1 New value of property pk1.
311          *
312          */

313         public void setPk1(int pk1)
314         {
315             this.pk1 = pk1;
316         }
317     }
318 }
319
Popular Tags