KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17
18 import java.util.Collection JavaDoc;
19
20 import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
21 import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
22 import org.apache.ojb.broker.query.Criteria;
23 import org.apache.ojb.broker.query.QueryByCriteria;
24 import org.apache.ojb.broker.query.QueryFactory;
25 import org.apache.ojb.junit.PBTestCase;
26
27 /**
28  * Test extents having a different type of the same field.
29  * the field 'price' is of type double in Article and String in BookArticle
30  *
31  * @author <a HREF="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
32  * @version $Id: FieldTypeTest.java,v 1.1.2.2 2005/12/21 22:31:23 tomdz Exp $
33  */

34 public class FieldTypeTest extends PBTestCase
35 {
36     public void testDifferentFieldTypes()
37     {
38         QueryByCriteria q;
39         Collection JavaDoc result;
40         Criteria c = new Criteria();
41
42         q = QueryFactory.newQuery(AbstractArticle.class, c);
43         result = broker.getCollectionByQuery(q);
44         assertNotNull(result);
45     }
46     
47     
48     public static void main(String JavaDoc[] args)
49     {
50         String JavaDoc[] arr = {FieldTypeTest.class.getName()};
51         junit.textui.TestRunner.main(arr);
52     }
53
54     public static class AbstractArticle
55     {
56         /** maps to db-column "Artikel-Nr";INT;PrimaryKey*/
57         private Integer JavaDoc articleId;
58         /** maps to db-column Artikelname;CHAR*/
59         private String JavaDoc articleName;
60         /**
61          * @return Returns the articleId.
62          */

63         
64         public Integer JavaDoc getArticleId()
65         {
66             return articleId;
67         }
68         /**
69          * @param articleId The articleId to set.
70          */

71         public void setArticleId(Integer JavaDoc articleId)
72         {
73             this.articleId = articleId;
74         }
75         /**
76          * @return Returns the articleName.
77          */

78         public String JavaDoc getArticleName()
79         {
80             return articleName;
81         }
82         /**
83          * @param articleName The articleName to set.
84          */

85         public void setArticleName(String JavaDoc articleName)
86         {
87             this.articleName = articleName;
88         }
89
90     }
91     
92     public static class Article extends AbstractArticle
93     {
94         /** maps to db-column Einzelpreis;DECIMAL*/
95         private double price;
96
97         /**
98          * @return Returns the price.
99          */

100         public double getPrice()
101         {
102             return price;
103         }
104
105         /**
106          * @param price The price to set.
107          */

108         public void setPrice(double price)
109         {
110             this.price = price;
111         }
112
113     }
114     public static class BookArticle extends AbstractArticle
115     {
116         /** books author*/
117         private String JavaDoc author;
118         /** ISBN No of Book*/
119         private String JavaDoc isbn;
120
121         /** maps to db-column Einzelpreis;DECIMAL*/
122         private String JavaDoc price;
123
124         /**
125          * @return Returns the author.
126          */

127         public String JavaDoc getAuthor()
128         {
129             return author;
130         }
131
132         /**
133          * @param author The author to set.
134          */

135         public void setAuthor(String JavaDoc author)
136         {
137             this.author = author;
138         }
139
140         /**
141          * @return Returns the isbn.
142          */

143         public String JavaDoc getIsbn()
144         {
145             return isbn;
146         }
147
148         /**
149          * @param isbn The isbn to set.
150          */

151         public void setIsbn(String JavaDoc isbn)
152         {
153             this.isbn = isbn;
154         }
155
156         /**
157          * @return Returns the price.
158          */

159         public String JavaDoc getPrice()
160         {
161             return price;
162         }
163
164         /**
165          * @param price The price to set.
166          */

167         public void setPrice(String JavaDoc price)
168         {
169             this.price = price;
170         }
171
172     }
173     
174     public static class DoubleToStringConversion implements FieldConversion
175     {
176         public Object JavaDoc javaToSql(Object JavaDoc source) throws ConversionException
177         {
178             if (source instanceof String JavaDoc)
179             {
180                 return Double.valueOf((String JavaDoc)source);
181             }
182             else
183             {
184                 return null;
185             }
186         }
187
188         public Object JavaDoc sqlToJava(Object JavaDoc source) throws ConversionException
189         {
190             if (source instanceof Double JavaDoc)
191             {
192                 return source.toString();
193             }
194             else
195             {
196                 return null;
197             }
198         }
199     }
200
201 }
202
203
Popular Tags