001: package org.apache.ojb.broker;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Collection;
019:
020: import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
021: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.QueryByCriteria;
024: import org.apache.ojb.broker.query.QueryFactory;
025: import org.apache.ojb.junit.PBTestCase;
026:
027: /**
028: * Test extents having a different type of the same field.
029: * the field 'price' is of type double in Article and String in BookArticle
030: *
031: * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
032: * @version $Id: FieldTypeTest.java,v 1.1.2.2 2005/12/21 22:31:23 tomdz Exp $
033: */
034: public class FieldTypeTest extends PBTestCase {
035: public void testDifferentFieldTypes() {
036: QueryByCriteria q;
037: Collection result;
038: Criteria c = new Criteria();
039:
040: q = QueryFactory.newQuery(AbstractArticle.class, c);
041: result = broker.getCollectionByQuery(q);
042: assertNotNull(result);
043: }
044:
045: public static void main(String[] args) {
046: String[] arr = { FieldTypeTest.class.getName() };
047: junit.textui.TestRunner.main(arr);
048: }
049:
050: public static class AbstractArticle {
051: /** maps to db-column "Artikel-Nr";INT;PrimaryKey*/
052: private Integer articleId;
053: /** maps to db-column Artikelname;CHAR*/
054: private String articleName;
055:
056: /**
057: * @return Returns the articleId.
058: */
059:
060: public Integer getArticleId() {
061: return articleId;
062: }
063:
064: /**
065: * @param articleId The articleId to set.
066: */
067: public void setArticleId(Integer articleId) {
068: this .articleId = articleId;
069: }
070:
071: /**
072: * @return Returns the articleName.
073: */
074: public String getArticleName() {
075: return articleName;
076: }
077:
078: /**
079: * @param articleName The articleName to set.
080: */
081: public void setArticleName(String articleName) {
082: this .articleName = articleName;
083: }
084:
085: }
086:
087: public static class Article extends AbstractArticle {
088: /** maps to db-column Einzelpreis;DECIMAL*/
089: private double price;
090:
091: /**
092: * @return Returns the price.
093: */
094: public double getPrice() {
095: return price;
096: }
097:
098: /**
099: * @param price The price to set.
100: */
101: public void setPrice(double price) {
102: this .price = price;
103: }
104:
105: }
106:
107: public static class BookArticle extends AbstractArticle {
108: /** books author*/
109: private String author;
110: /** ISBN No of Book*/
111: private String isbn;
112:
113: /** maps to db-column Einzelpreis;DECIMAL*/
114: private String price;
115:
116: /**
117: * @return Returns the author.
118: */
119: public String getAuthor() {
120: return author;
121: }
122:
123: /**
124: * @param author The author to set.
125: */
126: public void setAuthor(String author) {
127: this .author = author;
128: }
129:
130: /**
131: * @return Returns the isbn.
132: */
133: public String getIsbn() {
134: return isbn;
135: }
136:
137: /**
138: * @param isbn The isbn to set.
139: */
140: public void setIsbn(String isbn) {
141: this .isbn = isbn;
142: }
143:
144: /**
145: * @return Returns the price.
146: */
147: public String getPrice() {
148: return price;
149: }
150:
151: /**
152: * @param price The price to set.
153: */
154: public void setPrice(String price) {
155: this .price = price;
156: }
157:
158: }
159:
160: public static class DoubleToStringConversion implements
161: FieldConversion {
162: public Object javaToSql(Object source)
163: throws ConversionException {
164: if (source instanceof String) {
165: return Double.valueOf((String) source);
166: } else {
167: return null;
168: }
169: }
170:
171: public Object sqlToJava(Object source)
172: throws ConversionException {
173: if (source instanceof Double) {
174: return source.toString();
175: } else {
176: return null;
177: }
178: }
179: }
180:
181: }
|