001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestDbBeanFetcher.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import com.uwyn.rife.database.exceptions.DatabaseException;
011: import com.uwyn.rife.database.queries.CreateTable;
012: import com.uwyn.rife.database.queries.DropTable;
013: import com.uwyn.rife.database.queries.Insert;
014: import com.uwyn.rife.database.queries.Select;
015: import com.uwyn.rife.tools.ExceptionUtils;
016: import com.uwyn.rife.tools.InnerClassException;
017: import java.math.BigDecimal;
018: import java.sql.Time;
019: import java.sql.Timestamp;
020: import java.util.Calendar;
021: import junit.framework.TestCase;
022:
023: public class TestDbBeanFetcher extends TestCase {
024: private Datasource mDatasource = null;
025:
026: public TestDbBeanFetcher(Datasource datasource,
027: String datasourceName, String name) {
028: super (name);
029: mDatasource = datasource;
030: }
031:
032: public void testFetchBean() throws Exception {
033: final DbQueryManager manager = new DbQueryManager(mDatasource);
034:
035: // create the temporary table
036: CreateTable query_create = new CreateTable(mDatasource);
037: query_create.table("tmp_beanmanager").columns(BeanImpl.class)
038: .precision("propertyString", 255).precision(
039: "propertyStringbuffer", 255).precision(
040: "propertyChar", 1).precision("propertyDouble",
041: 8, 3).precision("propertyFloat", 10, 5)
042: .precision("propertyDoubleObject", 8, 3).precision(
043: "propertyFloatObject", 8, 2).precision(
044: "propertyBigDecimal", 16, 6);
045:
046: DbBeanFetcher<BeanImpl> fetcher = null;
047: try {
048: manager.executeUpdate(query_create);
049:
050: fetcher = manager.inTransaction(new DbTransactionUser() {
051: public DbBeanFetcher<BeanImpl> useTransaction()
052: throws InnerClassException {
053: DbBeanFetcher<BeanImpl> fetcher = null;
054: try {
055: // populate with test data
056: Calendar cal = Calendar.getInstance();
057: cal.set(2002, 5, 17, 15, 36);
058: cal.set(Calendar.MILLISECOND, 0); // milliseconds are only correctly supported by postgresql, don't include them in generic tests
059:
060: BeanImpl bean_populated = new BeanImpl();
061: bean_populated.setPropertyString("somestring");
062: bean_populated
063: .setPropertyStringbuffer(new StringBuffer(
064: "somestringbuffer"));
065: bean_populated.setPropertyDate(cal.getTime());
066: bean_populated.setPropertyCalendar(cal);
067: bean_populated
068: .setPropertySqlDate(new java.sql.Date(
069: cal.getTime().getTime()));
070: bean_populated.setPropertyTime(new Time(cal
071: .getTime().getTime()));
072: bean_populated
073: .setPropertyTimestamp(new Timestamp(cal
074: .getTime().getTime()));
075: bean_populated.setPropertyChar('v');
076: bean_populated.setPropertyBoolean(true);
077: bean_populated.setPropertyByte((byte) 127);
078: bean_populated.setPropertyDouble(78634.433d);
079: bean_populated.setPropertyFloat(76734.87834f);
080: bean_populated
081: .setPropertyDoubleObject(81432.971d);
082: bean_populated
083: .setPropertyFloatObject(311423.98f);
084: bean_populated.setPropertyInt(13);
085: bean_populated.setPropertyLong(18753L);
086: bean_populated.setPropertyShort((short) 23);
087: bean_populated
088: .setPropertyBigDecimal(new BigDecimal(
089: "7653564654.426587"));
090: Insert query_insert = new Insert(mDatasource);
091: query_insert.into("tmp_beanmanager").fields(
092: bean_populated);
093: assertEquals(manager
094: .executeUpdate(query_insert), 1);
095:
096: // construct the select query
097: Select query_select = new Select(mDatasource);
098: query_select.from("tmp_beanmanager").fields(
099: BeanImpl.class);
100: fetcher = new DbBeanFetcher<BeanImpl>(
101: mDatasource, BeanImpl.class);
102:
103: BeanImpl bean = null;
104: DbStatement statement = null;
105: try {
106: statement = manager
107: .executeQuery(query_select);
108: manager.fetch(statement.getResultSet(),
109: fetcher);
110: bean = fetcher.getBeanInstance();
111: statement.close();
112: } catch (DatabaseException e) {
113: assertTrue(ExceptionUtils
114: .getExceptionStackTrace(e), false);
115: }
116: assertNotNull(bean);
117: assertEquals(bean.getPropertyString(),
118: bean_populated.getPropertyString());
119: assertEquals(bean.getPropertyStringbuffer()
120: .toString(), bean_populated
121: .getPropertyStringbuffer().toString());
122: assertEquals(bean.getPropertyDate().getTime(),
123: bean_populated.getPropertyDate()
124: .getTime());
125: assertEquals(bean.getPropertyCalendar(),
126: bean_populated.getPropertyCalendar());
127: assertEquals(bean.getPropertySqlDate()
128: .toString(), bean_populated
129: .getPropertySqlDate().toString());
130: assertEquals(bean.getPropertyTime().toString(),
131: bean_populated.getPropertyTime()
132: .toString());
133: assertEquals(bean.getPropertyTimestamp(),
134: bean_populated.getPropertyTimestamp());
135: assertEquals(bean.isPropertyBoolean(),
136: bean_populated.isPropertyBoolean());
137: assertEquals(bean.getPropertyChar(),
138: bean_populated.getPropertyChar());
139: assertEquals(bean.getPropertyByte(),
140: bean_populated.getPropertyByte());
141: assertEquals(bean.getPropertyDouble(),
142: bean_populated.getPropertyDouble(),
143: 0.01);
144: assertEquals(bean.getPropertyFloat(),
145: bean_populated.getPropertyFloat(), 0.01);
146: assertEquals(bean.getPropertyDoubleObject()
147: .doubleValue(), bean_populated
148: .getPropertyDoubleObject()
149: .doubleValue(), 0.01);
150: assertEquals(bean.getPropertyFloatObject()
151: .floatValue(), bean_populated
152: .getPropertyFloatObject().floatValue(),
153: 0.01);
154: assertEquals(bean.getPropertyInt(),
155: bean_populated.getPropertyInt());
156: assertEquals(bean.getPropertyLong(),
157: bean_populated.getPropertyLong());
158: assertEquals(bean.getPropertyShort(),
159: bean_populated.getPropertyShort());
160: assertEquals(bean.getPropertyBigDecimal(),
161: bean_populated.getPropertyBigDecimal());
162: } catch (DatabaseException e) {
163: assertTrue(ExceptionUtils
164: .getExceptionStackTrace(e), false);
165: throw e;
166: }
167:
168: return fetcher;
169: }
170: });
171: } finally {
172: // remove the temporary table
173: DropTable query_drop = new DropTable(mDatasource);
174: query_drop.table(query_create.getTable());
175: manager.executeUpdate(query_drop);
176: }
177:
178: assertNotNull(fetcher.getBeanInstance());
179: }
180:
181: public void testFetchNullBean() throws Exception {
182: final DbQueryManager manager = new DbQueryManager(mDatasource);
183:
184: // create the temporary table
185: CreateTable query_create = new CreateTable(mDatasource);
186: query_create.table("tmp_beanmanager").columns(BeanImpl.class)
187: .precision("propertyString", 255).precision(
188: "propertyStringbuffer", 255).precision(
189: "propertyChar", 1).precision("propertyDouble",
190: 5, 4).precision("propertyFloat", 5, 5)
191: .precision("propertyDoubleObject", 5, 4).precision(
192: "propertyFloatObject", 5, 5).precision(
193: "propertyBigDecimal", 16, 6);
194:
195: DbBeanFetcher<BeanImpl> fetcher = null;
196: try {
197: manager.executeUpdate(query_create);
198:
199: fetcher = manager.inTransaction(new DbTransactionUser() {
200: public DbBeanFetcher<BeanImpl> useTransaction()
201: throws InnerClassException {
202: DbBeanFetcher<BeanImpl> fetcher = null;
203: try {
204: BeanImpl bean_null = new BeanImpl();
205: Insert query_insert = new Insert(mDatasource);
206: query_insert.into("tmp_beanmanager").fields(
207: bean_null);
208: assertEquals(manager
209: .executeUpdate(query_insert), 1);
210:
211: // construct the select query
212: Select query_select = new Select(mDatasource);
213: query_select.from("tmp_beanmanager").fields(
214: BeanImpl.class);
215: fetcher = new DbBeanFetcher<BeanImpl>(
216: mDatasource, BeanImpl.class);
217:
218: BeanImpl bean = null;
219: DbStatement statement = null;
220: try {
221: statement = manager
222: .executeQuery(query_select);
223: manager.fetch(statement.getResultSet(),
224: fetcher);
225: bean = fetcher.getBeanInstance();
226: statement.close();
227: } catch (DatabaseException e) {
228: assertTrue(ExceptionUtils
229: .getExceptionStackTrace(e), false);
230: }
231: assertNotNull(bean);
232: assertEquals(bean.getPropertyString(),
233: bean_null.getPropertyString());
234: assertEquals(bean.getPropertyStringbuffer(),
235: bean_null.getPropertyStringbuffer());
236: assertEquals(bean.getPropertyDate(), bean_null
237: .getPropertyDate());
238: assertEquals(bean.getPropertyCalendar(),
239: bean_null.getPropertyCalendar());
240: assertEquals(bean.getPropertySqlDate(),
241: bean_null.getPropertySqlDate());
242: assertEquals(bean.getPropertyTime(), bean_null
243: .getPropertyTime());
244: assertEquals(bean.getPropertyTimestamp(),
245: bean_null.getPropertyTimestamp());
246: assertEquals(bean.isPropertyBoolean(),
247: bean_null.isPropertyBoolean());
248: assertEquals(bean.getPropertyChar(), bean_null
249: .getPropertyChar());
250: assertEquals(bean.getPropertyByte(), bean_null
251: .getPropertyByte());
252: assertEquals(bean.getPropertyDouble(),
253: bean_null.getPropertyDouble(), 0.01);
254: assertEquals(bean.getPropertyFloat(), bean_null
255: .getPropertyFloat(), 0.01);
256: assertEquals(bean.getPropertyDoubleObject(),
257: bean_null.getPropertyDoubleObject());
258: assertEquals(bean.getPropertyFloatObject(),
259: bean_null.getPropertyFloatObject());
260: assertEquals(bean.getPropertyInt(), bean_null
261: .getPropertyInt());
262: assertEquals(bean.getPropertyLong(), bean_null
263: .getPropertyLong());
264: assertEquals(bean.getPropertyShort(), bean_null
265: .getPropertyShort());
266: assertEquals(bean.getPropertyBigDecimal(),
267: bean_null.getPropertyBigDecimal());
268: } catch (DatabaseException e) {
269: assertTrue(ExceptionUtils
270: .getExceptionStackTrace(e), false);
271: throw e;
272: }
273:
274: return fetcher;
275: }
276: });
277: } finally {
278: // remove the temporary table
279: DropTable query_drop = new DropTable(mDatasource);
280: query_drop.table(query_create.getTable());
281: manager.executeUpdate(query_drop);
282: }
283:
284: assertNotNull(fetcher.getBeanInstance());
285: }
286: }
|