KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import junit.framework.TestCase;
4 import org.apache.commons.lang.builder.ToStringBuilder;
5 import org.apache.commons.lang.builder.ToStringStyle;
6 import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
7 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
8
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * Test case to check the field conversion.
13  *
14  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
15  * @version $Id: FieldConversionTest.java,v 1.2.4.1 2005/12/13 18:18:25 arminw Exp $
16  */

17 public class FieldConversionTest extends TestCase
18 {
19     private PersistenceBroker broker;
20
21     public static void main(String JavaDoc[] args)
22     {
23         String JavaDoc[] arr = {FieldConversionTest.class.getName()};
24         junit.textui.TestRunner.main(arr);
25     }
26
27     /**
28      * Insert the method's description here.
29      * Creation date: (06.12.2000 21:58:53)
30      */

31     public void setUp() throws PBFactoryException
32     {
33         broker = PersistenceBrokerFactory.defaultPersistenceBroker();
34     }
35
36     /**
37      * Insert the method's description here.
38      * Creation date: (06.12.2000 21:59:14)
39      */

40     public void tearDown()
41     {
42         try
43         {
44             broker.clearCache();
45             broker.close();
46         }
47         catch (PersistenceBrokerException e)
48         {
49         }
50     }
51
52     public void testConversion()
53     {
54         int id = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
55         ConversionVO vo = new ConversionVO(
56                 null, new ConversionId(new Integer JavaDoc(id)), null, new ConversionId(new Integer JavaDoc(id + 1)));
57         broker.beginTransaction();
58         broker.store(vo);
59         broker.commitTransaction();
60
61         Identity identity = new Identity(vo, broker);
62         broker.clearCache();
63         ConversionVO find_vo = (ConversionVO) broker.getObjectByIdentity(identity);
64     }
65
66     public static class FieldConversionConversionIdToInteger implements FieldConversion
67     {
68         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
69         {
70             if (!(source instanceof ConversionId))
71             {
72                 throw new ConversionException(
73                         "Wrong java field type when java-->sql, expected " +
74                         ConversionId.class.getClass() + ", found "
75                         + source.getClass());
76             }
77             return ((ConversionId) source).getConversionId();
78         }
79
80         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
81         {
82             if (!(source instanceof Integer JavaDoc))
83             {
84                 throw new ConversionException(
85                         "Wrong java field type when java-->sql, expected java.lang.Integer, found "
86                         + source.getClass());
87             }
88             return new ConversionId((Integer JavaDoc) source);
89         }
90     }
91
92     public static class FieldConversionLongToInteger implements FieldConversion
93     {
94         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
95         {
96             if(source == null) return null;
97             if (!(source instanceof Long JavaDoc))
98             {
99                 throw new ConversionException(
100                         "Wrong java field type when java-->sql, expected java.lang.Long, found "
101                         + source.getClass());
102             }
103             return new Integer JavaDoc(((Long JavaDoc) source).intValue());
104         }
105
106         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
107         {
108             if(source == null) return null;
109             if (!(source instanceof Integer JavaDoc))
110             {
111                 throw new ConversionException(
112                         "Wrong java field type when java-->sql, expected java.lang.Integer, found "
113                         + source.getClass());
114             }
115             return new Long JavaDoc(((Integer JavaDoc) source).longValue());
116         }
117     }
118
119     public static class ConversionId implements Serializable JavaDoc
120     {
121         private Integer JavaDoc conversionId;
122
123         public ConversionId(Integer JavaDoc conversionId)
124         {
125             this.conversionId = conversionId;
126         }
127
128         public Integer JavaDoc getConversionId()
129         {
130             return conversionId;
131         }
132
133         public void setConversionId(Integer JavaDoc conversionId)
134         {
135             this.conversionId = conversionId;
136         }
137
138         public String JavaDoc toString()
139         {
140             return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).
141                     append("conversionId", conversionId).toString();
142         }
143     }
144
145     public static class ConversionVO implements Serializable JavaDoc
146     {
147         Long JavaDoc pkWithAutoIncrement;
148         ConversionId pkWithoutAutoIncrement;
149         Long JavaDoc normalWithAutoIncrement;
150         ConversionId normalWithoutAutoIncrement;
151
152         public ConversionVO()
153         {
154         }
155
156         public ConversionVO(Long JavaDoc pkWithAutoIncrement, ConversionId pkWithoutAutoIncrement, Long JavaDoc normalWithAutoIncrement, ConversionId normalWithoutAutoIncrement)
157         {
158             this.pkWithAutoIncrement = pkWithAutoIncrement;
159             this.pkWithoutAutoIncrement = pkWithoutAutoIncrement;
160             this.normalWithAutoIncrement = normalWithAutoIncrement;
161             this.normalWithoutAutoIncrement = normalWithoutAutoIncrement;
162         }
163
164         public String JavaDoc toString()
165         {
166             return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
167                     append("pkWithAutoIncrement", pkWithAutoIncrement).
168                     append("pkWithoutAutoIncrement", pkWithoutAutoIncrement).
169                     append("normalWithAutoIncrement", normalWithAutoIncrement).
170                     append("normalWithoutAutoIncrement", normalWithoutAutoIncrement).toString();
171         }
172
173         public Long JavaDoc getPkWithAutoIncrement()
174         {
175             return pkWithAutoIncrement;
176         }
177
178         public void setPkWithAutoIncrement(Long JavaDoc pkWithAutoIncrement)
179         {
180             this.pkWithAutoIncrement = pkWithAutoIncrement;
181         }
182
183         public ConversionId getPkWithoutAutoIncrement()
184         {
185             return pkWithoutAutoIncrement;
186         }
187
188         public void setPkWithoutAutoIncrement(ConversionId pkWithoutAutoIncrement)
189         {
190             this.pkWithoutAutoIncrement = pkWithoutAutoIncrement;
191         }
192
193         public Long JavaDoc getNormalWithAutoIncrement()
194         {
195             return normalWithAutoIncrement;
196         }
197
198         public void setNormalWithAutoIncrement(Long JavaDoc normalWithAutoIncrement)
199         {
200             this.normalWithAutoIncrement = normalWithAutoIncrement;
201         }
202
203         public ConversionId getNormalWithoutAutoIncrement()
204         {
205             return normalWithoutAutoIncrement;
206         }
207
208         public void setNormalWithoutAutoIncrement(ConversionId normalWithoutAutoIncrement)
209         {
210             this.normalWithoutAutoIncrement = normalWithoutAutoIncrement;
211         }
212     }
213 }
214
Popular Tags