KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
10 import java.math.BigDecimal JavaDoc;
11
12 /**
13  * Test using field conversions for PK values.
14  *
15  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
16  * @version $Id: FieldConversionTest_3.java,v 1.4.2.2 2005/03/03 17:18:17 arminw Exp $
17  */

18 public class FieldConversionTest_3 extends PBTestCase
19 {
20
21     public static void main(String JavaDoc[] args)
22     {
23         String JavaDoc[] 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 JavaDoc e)
35         {
36             //ignored
37
}
38     }
39
40     /**
41      * store nested classes needed field conversion of a primary key field.
42      */

43     public void testStoreNestedNodes() throws Exception JavaDoc
44     {
45         //String strQuery = "select allNodes from " + Node.class.getName();
46
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     /**
68      * store class needed field conversion of a primary key field.
69      */

70     public void testStoreNode() throws Exception JavaDoc
71     {
72         //String strQuery = "select allNodes from " + Node.class.getName();
73
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     /**
94      * Assert that StatementManager handles NULL-values correct when binding deletions.
95      */

96     public void testDeleteNode() throws Exception JavaDoc
97     {
98         NodeWoAutoInc node = new NodeWoAutoInc(0);
99
100         try
101         {
102             broker.beginTransaction();
103             // mkalen: Try to issue delete with numeric field=NULL after field conversion,
104
// which will make eg Oracle JDBC throw SQLException if not using stmt.setNull()
105
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 JavaDoc ignore)
117                 {
118                     //ignore
119
}
120             }
121         }
122     }
123
124
125     //****************************************************************************
126
// inner class
127
//****************************************************************************
128
public static class LongToBigDecimalConversion implements FieldConversion
129     {
130         private static final Long JavaDoc NULL_BIG_DECIMAL = null;
131         private static final Long JavaDoc ZERO = new Long JavaDoc(0);
132
133         public LongToBigDecimalConversion()
134         {
135         }
136
137         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
138         {
139             if(source == null) return null;
140             Object JavaDoc ret;
141             if (source instanceof Long JavaDoc)
142             {
143                 if (ZERO.equals(source))
144                 {
145                     ret = NULL_BIG_DECIMAL;
146                 }
147                 else
148                 {
149                     ret = new BigDecimal JavaDoc(((Long JavaDoc) source).doubleValue());
150                 }
151             }
152             else
153             {
154                 throw new ConversionException(
155                         "java-->sql, expected type was"+Long JavaDoc.class.getClass()+
156                         ", found type "+source.getClass());
157             }
158             return ret;
159         }
160
161         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
162         {
163             if(source == null) return null;
164             Object JavaDoc ret;
165             if (source instanceof BigDecimal JavaDoc)
166             {
167                 ret = new Long JavaDoc(((BigDecimal JavaDoc) source).longValue());
168             }
169             else
170             {
171                 throw new ConversionException(
172                         "sql-->java, expected type was"+BigDecimal JavaDoc.class.getClass()+
173                         ", found type "+source.getClass());
174             }
175             return ret;
176         }
177     }
178
179
180     //****************************************************************************
181
// inner class
182
//****************************************************************************
183
public static class Node implements Serializable JavaDoc
184     {
185         private long uid; // primary key
186
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 JavaDoc
243     {
244         private long uid; // primary key, no auto increment
245

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