1 package org.apache.ojb.broker; 2 3 17 18 import java.util.Collection ; 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 34 public class FieldTypeTest extends PBTestCase 35 { 36 public void testDifferentFieldTypes() 37 { 38 QueryByCriteria q; 39 Collection 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 [] args) 49 { 50 String [] arr = {FieldTypeTest.class.getName()}; 51 junit.textui.TestRunner.main(arr); 52 } 53 54 public static class AbstractArticle 55 { 56 57 private Integer articleId; 58 59 private String articleName; 60 63 64 public Integer getArticleId() 65 { 66 return articleId; 67 } 68 71 public void setArticleId(Integer articleId) 72 { 73 this.articleId = articleId; 74 } 75 78 public String getArticleName() 79 { 80 return articleName; 81 } 82 85 public void setArticleName(String articleName) 86 { 87 this.articleName = articleName; 88 } 89 90 } 91 92 public static class Article extends AbstractArticle 93 { 94 95 private double price; 96 97 100 public double getPrice() 101 { 102 return price; 103 } 104 105 108 public void setPrice(double price) 109 { 110 this.price = price; 111 } 112 113 } 114 public static class BookArticle extends AbstractArticle 115 { 116 117 private String author; 118 119 private String isbn; 120 121 122 private String price; 123 124 127 public String getAuthor() 128 { 129 return author; 130 } 131 132 135 public void setAuthor(String author) 136 { 137 this.author = author; 138 } 139 140 143 public String getIsbn() 144 { 145 return isbn; 146 } 147 148 151 public void setIsbn(String isbn) 152 { 153 this.isbn = isbn; 154 } 155 156 159 public String getPrice() 160 { 161 return price; 162 } 163 164 167 public void setPrice(String price) 168 { 169 this.price = price; 170 } 171 172 } 173 174 public static class DoubleToStringConversion implements FieldConversion 175 { 176 public Object javaToSql(Object source) throws ConversionException 177 { 178 if (source instanceof String ) 179 { 180 return Double.valueOf((String )source); 181 } 182 else 183 { 184 return null; 185 } 186 } 187 188 public Object sqlToJava(Object source) throws ConversionException 189 { 190 if (source instanceof Double ) 191 { 192 return source.toString(); 193 } 194 else 195 { 196 return null; 197 } 198 } 199 } 200 201 } 202 203 | Popular Tags |