KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > FieldConversionTest_4


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 JavaDoc;
12 import java.math.BigDecimal JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * class FieldConversion_ForeigenKeyTest,
17  * check the field conversion behaviour.
18  *
19  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
20  * @version $Id: FieldConversionTest_4.java,v 1.1.2.2 2005/08/08 13:18:49 arminw Exp $
21  */

22 public class FieldConversionTest_4 extends ODMGTestCase
23 {
24     public static void main(String JavaDoc[] args)
25     {
26         String JavaDoc[] arr = {FieldConversionTest_4.class.getName()};
27         junit.textui.TestRunner.main(arr);
28     }
29
30     public void testSelfReferingParent() throws Exception JavaDoc
31     {
32         String JavaDoc 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 JavaDoc 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 JavaDoc) 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 JavaDoc) 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 JavaDoc) query.execute();
77         after = result.size();
78         assertEquals(before, after);
79     }
80
81     public void testMakePersistentNode() throws Exception JavaDoc
82     {
83         String JavaDoc 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 JavaDoc 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 JavaDoc) 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 JavaDoc) 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 JavaDoc
127     {
128         String JavaDoc 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 JavaDoc result;
135         int before;
136
137         try
138         {
139             tx.begin();
140             OQLQuery query = odmg.newOQLQuery();
141             query.create(strQuery);
142             result = (List JavaDoc) 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 JavaDoc) 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     //****************************************************************************
170
// inner class
171
//****************************************************************************
172
public static class LongToBigDecimalConversion implements FieldConversion
173     {
174         public LongToBigDecimalConversion()
175         {
176         }
177
178         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
179         {
180             Object JavaDoc ret;
181             if (source == null)
182             {
183                 ret = null;
184             }
185             else if (source instanceof Long JavaDoc)
186             {
187                 ret = new BigDecimal JavaDoc(((Long JavaDoc) source).doubleValue());
188             }
189             else
190             {
191                 throw new ConversionException(
192                         "java-->sql, expected type was"+Long JavaDoc.class.getClass()+
193                         ", found type "+source.getClass());
194             }
195             return ret;
196         }
197
198         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
199         {
200             Object JavaDoc ret;
201             if (source == null)
202             {
203                 ret = null;
204             }
205             else if (source instanceof BigDecimal JavaDoc)
206             {
207                 ret = new Long JavaDoc(((BigDecimal JavaDoc) source).longValue());
208             }
209             else
210             {
211                 throw new ConversionException(
212                         "sql-->java, expected type was"+BigDecimal JavaDoc.class.getClass()+
213                         ", found type "+source.getClass());
214             }
215             return ret;
216         }
217     }
218
219
220     //****************************************************************************
221
// inner class
222
//****************************************************************************
223
public static class Node implements Serializable JavaDoc
224     {
225         private long uid; // primary key
226
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 JavaDoc 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