Source Code Cross Referenced for TestDbPreparedStatement.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » database » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.database 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003:         * Distributed under the terms of either:
0004:         * - the common development and distribution license (CDDL), v1.0; or
0005:         * - the GNU Lesser General Public License, v2.1 or later
0006:         * $Id: TestDbPreparedStatement.java 3726 2007-04-30 18:20:33Z gbevin $
0007:         */
0008:        package com.uwyn.rife.database;
0009:
0010:        import com.uwyn.rife.database.exceptions.*;
0011:        import java.sql.*;
0012:
0013:        import com.uwyn.rife.database.queries.CreateTable;
0014:        import com.uwyn.rife.database.queries.Delete;
0015:        import com.uwyn.rife.database.queries.DropTable;
0016:        import com.uwyn.rife.database.queries.Insert;
0017:        import com.uwyn.rife.database.queries.Select;
0018:        import com.uwyn.rife.tools.ExceptionUtils;
0019:        import com.uwyn.rife.tools.exceptions.BeanUtilsException;
0020:        import java.math.BigDecimal;
0021:        import java.net.MalformedURLException;
0022:        import java.net.URL;
0023:        import java.util.Calendar;
0024:        import junit.framework.TestCase;
0025:
0026:        public class TestDbPreparedStatement extends TestCase {
0027:            private Datasource mDatasource = null;
0028:
0029:            public TestDbPreparedStatement(Datasource datasource,
0030:                    String datasourceName, String name) {
0031:                super (name);
0032:                mDatasource = datasource;
0033:            }
0034:
0035:            public void setUp() {
0036:                // create the temporary table
0037:                CreateTable query_create = new CreateTable(mDatasource);
0038:                query_create.table("parametersbean").columns(BeanImpl.class)
0039:                        .column("notbeanInt", int.class).precision(
0040:                                "propertyString", 255).precision(
0041:                                "propertyStringbuffer", 255).precision(
0042:                                "propertyChar", 1).precision("propertyDouble",
0043:                                7, 2).precision("propertyFloat", 8, 3)
0044:                        .precision("propertyBigDecimal", 16, 6);
0045:                DbStatement statement = mDatasource.getConnection()
0046:                        .createStatement();
0047:                try {
0048:                    try {
0049:                        statement.executeUpdate(query_create);
0050:                    } catch (DatabaseException e) {
0051:                        e.printStackTrace();
0052:                    }
0053:                } finally {
0054:                    try {
0055:                        statement.close();
0056:                    } catch (DatabaseException e) {
0057:                        // do nothing
0058:                    }
0059:                }
0060:            }
0061:
0062:            public void tearDown() {
0063:                try {
0064:                    DbConnection connection = mDatasource.getConnection();
0065:
0066:                    // drop temporary table
0067:                    DropTable query_drop = new DropTable(mDatasource);
0068:                    query_drop.table("parametersbean");
0069:                    connection.createStatement().executeUpdate(query_drop);
0070:
0071:                    connection.close();
0072:                } catch (DatabaseException e) {
0073:                    fail(ExceptionUtils.getExceptionStackTrace(e));
0074:                }
0075:            }
0076:
0077:            public void testInstationSql() {
0078:                try {
0079:                    String sql = "DELETE FROM parametersbean";
0080:                    DbPreparedStatement statement_delete = mDatasource
0081:                            .getConnection().getPreparedStatement(sql);
0082:                    assertEquals(sql, statement_delete.getSql());
0083:                    assertNull(statement_delete.getQuery());
0084:                    statement_delete.executeUpdate();
0085:                    statement_delete.close();
0086:                } catch (DatabaseException e) {
0087:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0088:                }
0089:            }
0090:
0091:            public void testInstationQuery() {
0092:                try {
0093:                    Delete query_delete = new Delete(mDatasource);
0094:                    query_delete.from("parametersbean");
0095:                    DbPreparedStatement statement_delete = mDatasource
0096:                            .getConnection().getPreparedStatement(query_delete);
0097:                    assertEquals(query_delete.getSql(), statement_delete
0098:                            .getSql());
0099:                    assertEquals(query_delete, statement_delete.getQuery());
0100:                    statement_delete.executeUpdate();
0101:                    statement_delete.close();
0102:                } catch (DatabaseException e) {
0103:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0104:                }
0105:            }
0106:
0107:            public void testExecuteQuery() {
0108:                try {
0109:                    Select query_select = new Select(mDatasource);
0110:                    query_select.from("parametersbean");
0111:                    DbPreparedStatement statement_select = mDatasource
0112:                            .getConnection().getPreparedStatement(query_select);
0113:                    statement_select.executeQuery();
0114:                    assertNotNull(statement_select.getResultSet());
0115:                    statement_select.close();
0116:                } catch (DatabaseException e) {
0117:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0118:                }
0119:            }
0120:
0121:            public void testExecuteQueryException() {
0122:                try {
0123:                    Select query_select = new Select(mDatasource);
0124:                    query_select.from("inexistenttable");
0125:
0126:                    DbPreparedStatement statement_select = null;
0127:                    try {
0128:                        statement_select = mDatasource.getConnection()
0129:                                .getPreparedStatement(query_select);
0130:
0131:                        try {
0132:                            statement_select.executeQuery();
0133:                            fail();
0134:                        } catch (ExecutionErrorException e) {
0135:                            assertSame(mDatasource, e.getDatasource());
0136:                            assertEquals(query_select.getSql(), e.getSql());
0137:                        }
0138:                        assertNull(statement_select.getResultSet());
0139:                    } catch (PreparedStatementCreationErrorException e) {
0140:                        assertSame(mDatasource, e.getDatasource());
0141:                    }
0142:                } catch (DatabaseException e) {
0143:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0144:                }
0145:            }
0146:
0147:            public void testExecuteUpdate() {
0148:                try {
0149:                    Delete query_delete = new Delete(mDatasource);
0150:                    query_delete.from("parametersbean");
0151:                    DbPreparedStatement statement_select = mDatasource
0152:                            .getConnection().getPreparedStatement(query_delete);
0153:                    statement_select.executeUpdate();
0154:                    statement_select.close();
0155:                } catch (DatabaseException e) {
0156:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0157:                }
0158:            }
0159:
0160:            public void testExecuteUpdateException() {
0161:                try {
0162:                    Delete query_delete = new Delete(mDatasource);
0163:                    query_delete.from("inexistenttable");
0164:                    DbPreparedStatement statement_update = null;
0165:
0166:                    try {
0167:                        statement_update = mDatasource.getConnection()
0168:                                .getPreparedStatement(query_delete);
0169:                        try {
0170:                            statement_update.executeUpdate();
0171:                            fail();
0172:                        } catch (ExecutionErrorException e) {
0173:                            assertSame(mDatasource, e.getDatasource());
0174:                            assertEquals(query_delete.getSql(), e.getSql());
0175:                        }
0176:                    } catch (PreparedStatementCreationErrorException e) {
0177:                        assertSame(mDatasource, e.getDatasource());
0178:                    }
0179:                } catch (DatabaseException e) {
0180:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0181:                }
0182:            }
0183:
0184:            public void testNotParametrized() {
0185:                try {
0186:                    String sql = "SELECT * FROM parametersbean WHERE propertyString = ?";
0187:                    DbPreparedStatement statement_select = mDatasource
0188:                            .getConnection().getPreparedStatement(sql);
0189:                    try {
0190:                        statement_select.setString("propertyString", "ok");
0191:                        fail();
0192:                    } catch (DatabaseException e) {
0193:                        assertTrue(e instanceof  NoParametrizedQueryException);
0194:                        assertSame(statement_select,
0195:                                ((NoParametrizedQueryException) e)
0196:                                        .getPreparedStatement());
0197:                    }
0198:                    statement_select.close();
0199:                } catch (DatabaseException e) {
0200:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0201:                }
0202:            }
0203:
0204:            public void testNoParameters() {
0205:                try {
0206:                    Select query_select = new Select(mDatasource);
0207:                    query_select.from("parametersbean");
0208:                    DbPreparedStatement statement_select = mDatasource
0209:                            .getConnection().getPreparedStatement(query_select);
0210:                    try {
0211:                        statement_select.setString("propertyString", "ok");
0212:                        fail();
0213:                    } catch (DatabaseException e) {
0214:                        assertTrue(e instanceof  NoParametersException);
0215:                        assertSame(statement_select,
0216:                                ((NoParametersException) e)
0217:                                        .getPreparedStatement());
0218:                    }
0219:                    statement_select.close();
0220:                } catch (DatabaseException e) {
0221:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0222:                }
0223:            }
0224:
0225:            public void testAddBatch() {
0226:                CreateTable query_create = new CreateTable(mDatasource);
0227:                query_create.table("batchtest").column("intcol", int.class);
0228:                try {
0229:                    DbPreparedStatement statement_create = mDatasource
0230:                            .getConnection().getPreparedStatement(query_create);
0231:                    statement_create.executeUpdate();
0232:                    statement_create.close();
0233:
0234:                    Insert query_insert = new Insert(mDatasource);
0235:                    query_insert.into(query_create.getTable()).fieldParameter(
0236:                            "intcol");
0237:                    DbPreparedStatement statement_insert = mDatasource
0238:                            .getConnection().getPreparedStatement(query_insert);
0239:                    int first = 1;
0240:                    int second = 5;
0241:                    int third = 9;
0242:                    int fourth = 12;
0243:                    statement_insert.setInt("intcol", first);
0244:                    statement_insert.addBatch();
0245:                    statement_insert.setInt("intcol", second);
0246:                    statement_insert.addBatch();
0247:                    statement_insert.setInt("intcol", third);
0248:                    statement_insert.addBatch();
0249:                    statement_insert.setInt("intcol", fourth);
0250:                    statement_insert.addBatch();
0251:                    statement_insert.executeBatch();
0252:                    statement_insert.close();
0253:
0254:                    Select query_select = new Select(mDatasource);
0255:                    query_select.from(query_create.getTable());
0256:                    DbStatement statement_select = mDatasource.getConnection()
0257:                            .createStatement();
0258:                    statement_select.executeQuery(query_select);
0259:                    boolean got_first = false;
0260:                    boolean got_second = false;
0261:                    boolean got_third = false;
0262:                    boolean got_fourth = false;
0263:                    ResultSet resultset = statement_select.getResultSet();
0264:                    int result = -1;
0265:                    while (resultset.next()) {
0266:                        result = resultset.getInt("intcol");
0267:                        if (first == result) {
0268:                            if (got_first) {
0269:                                assertTrue("Got " + first + " more than once",
0270:                                        false);
0271:                            }
0272:                            got_first = true;
0273:                        } else if (second == result) {
0274:                            if (got_second) {
0275:                                assertTrue("Got " + second + " more than once",
0276:                                        false);
0277:                            }
0278:                            got_second = true;
0279:                        } else if (third == result) {
0280:                            if (got_third) {
0281:                                assertTrue("Got " + third + " more than once",
0282:                                        false);
0283:                            }
0284:                            got_third = true;
0285:                        } else if (fourth == result) {
0286:                            if (got_fourth) {
0287:                                assertTrue("Got " + fourth + " more than once",
0288:                                        false);
0289:                            }
0290:                            got_fourth = true;
0291:                        } else {
0292:                            assertTrue("Unknown value : " + result, false);
0293:                        }
0294:                    }
0295:                    statement_select.close();
0296:
0297:                    assertTrue(got_first);
0298:                    assertTrue(got_second);
0299:                    assertTrue(got_third);
0300:                    assertTrue(got_fourth);
0301:                } catch (SQLException e) {
0302:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0303:                } catch (DatabaseException e) {
0304:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0305:                } finally {
0306:                    try {
0307:                        DropTable query_drop = new DropTable(mDatasource);
0308:                        query_drop.table(query_create.getTable());
0309:                        DbPreparedStatement statement_drop = mDatasource
0310:                                .getConnection().getPreparedStatement(
0311:                                        query_drop);
0312:                        statement_drop.executeUpdate();
0313:                        statement_drop.close();
0314:                    } catch (DatabaseException e) {
0315:                        assertTrue(ExceptionUtils.getExceptionStackTrace(e),
0316:                                false);
0317:                    }
0318:                }
0319:            }
0320:
0321:            public void testGetMetaData() {
0322:
0323:                try {
0324:                    Select query_select = new Select(mDatasource);
0325:                    query_select.from("parametersbean").whereParameter(
0326:                            "propertyString", "=");
0327:                    DbPreparedStatement statement_select = mDatasource
0328:                            .getConnection().getPreparedStatement(query_select);
0329:                    statement_select.setString("propertyString", "ok");
0330:                    ResultSetMetaData metadata = null;
0331:                    metadata = statement_select.getMetaData();
0332:                    if (mDatasource.getDriver().equals("org.h2.Driver")) {
0333:                        assertNull(metadata);
0334:                    } else {
0335:                        assertNotNull(metadata);
0336:                    }
0337:                    statement_select.close();
0338:                } catch (DatabaseException e) {
0339:                    if (e.getCause() != null) {
0340:                        // mysql
0341:                        if (e.getCause().getClass().getName().equals(
0342:                                "com.mysql.jdbc.NotImplemented")) {
0343:                            return;
0344:                        }
0345:                        // oracle
0346:                        if (e.getCause().getClass().getName().equals(
0347:                                "java.sql.SQLException")
0348:                                && e
0349:                                        .getCause()
0350:                                        .getMessage()
0351:                                        .indexOf(
0352:                                                "statement handle not executed: getMetaData") != -1) {
0353:                            return;
0354:                        }
0355:                        // mckoi
0356:                        if (e.getCause().getClass().getName().equals(
0357:                                "com.mckoi.database.jdbc.MSQLException")
0358:                                && e.getCause().getMessage().startsWith(
0359:                                        "Not Supported")) {
0360:                            return;
0361:                        }
0362:                    }
0363:
0364:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0365:                }
0366:            }
0367:
0368:            public void testGetParameterMetaData() {
0369:
0370:                try {
0371:                    Select query_select = new Select(mDatasource);
0372:                    query_select.from("parametersbean").whereParameter(
0373:                            "propertyString", "=");
0374:                    DbPreparedStatement statement_select = mDatasource
0375:                            .getConnection().getPreparedStatement(query_select);
0376:                    statement_select.setString("propertyString", "ok");
0377:                    ParameterMetaData metadata = null;
0378:                    try {
0379:                        metadata = statement_select.getParameterMetaData();
0380:                        assertNotNull(metadata);
0381:                    } catch (AbstractMethodError e) {
0382:                        assertTrue(mDatasource.getDriver().equals(
0383:                                "oracle.jdbc.driver.OracleDriver")
0384:                                || mDatasource.getDriver().equals(
0385:                                        "org.apache.derby.jdbc.EmbeddedDriver"));
0386:                    }
0387:                    statement_select.close();
0388:                } catch (DatabaseException e) {
0389:                    if (e.getCause() != null) {
0390:                        if (e.getCause().getClass().getName().equals(
0391:                                "com.mysql.jdbc.NotImplemented")) {
0392:                            return;
0393:                        }
0394:                        if (e.getCause().getClass().getName().equals(
0395:                                "org.postgresql.util.PSQLException")
0396:                                && e.getCause().getMessage().equals(
0397:                                        "This method is not yet implemented.")) {
0398:                            return;
0399:                        }
0400:                        if (e.getCause().getClass().getName().equals(
0401:                                "com.mckoi.database.jdbc.MSQLException")
0402:                                && e.getCause().getMessage().startsWith(
0403:                                        "Not Supported")) {
0404:                            return;
0405:                        }
0406:                    }
0407:
0408:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0409:                }
0410:            }
0411:
0412:            public void testSetBeanNull() {
0413:                try {
0414:                    // insert some data
0415:                    Insert query_insert = new Insert(mDatasource);
0416:                    query_insert.into("parametersbean").fieldsParameters(
0417:                            BeanImpl.class);
0418:                    DbPreparedStatement statement_insert = mDatasource
0419:                            .getConnection().getPreparedStatement(query_insert);
0420:                    try {
0421:                        statement_insert.setBean(null);
0422:                        fail();
0423:                    } catch (IllegalArgumentException e) {
0424:                        assertTrue(true);
0425:                    } finally {
0426:                        statement_insert.close();
0427:                    }
0428:                } catch (DatabaseException e) {
0429:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0430:                }
0431:            }
0432:
0433:            public void testSetBeanError() {
0434:                try {
0435:                    // insert some data
0436:                    Insert query_insert = new Insert(mDatasource);
0437:                    query_insert.into("parametersbean").fieldsParameters(
0438:                            BeanImpl.class);
0439:                    DbPreparedStatement statement_insert = mDatasource
0440:                            .getConnection().getPreparedStatement(query_insert);
0441:                    try {
0442:                        statement_insert.setBean(BeanErrorImpl
0443:                                .getPopulatedBean());
0444:                        fail();
0445:                    } catch (DatabaseException e) {
0446:                        assertTrue(e.getCause() instanceof  BeanUtilsException);
0447:                    }
0448:                } catch (DatabaseException e) {
0449:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0450:                }
0451:            }
0452:
0453:            public void testSetBean() {
0454:                try {
0455:                    // insert some data
0456:                    Insert query_insert = new Insert(mDatasource);
0457:                    query_insert.into("parametersbean").fieldsParameters(
0458:                            BeanImpl.class).fieldParameter("notbeanInt");
0459:                    DbPreparedStatement statement_insert = mDatasource
0460:                            .getConnection().getPreparedStatement(query_insert);
0461:                    try {
0462:                        try {
0463:                            statement_insert.setBean(null);
0464:                            fail();
0465:                        } catch (IllegalArgumentException e) {
0466:                            assertTrue(true);
0467:                        }
0468:                        statement_insert.setBean(BeanImpl.getPopulatedBean());
0469:                        statement_insert.setInt("notbeanInt", 23);
0470:                        statement_insert.executeUpdate();
0471:
0472:                        // retrieve the data
0473:                        BeanManager bean_manager = new BeanManager();
0474:                        BeanImpl retrieved_bean = bean_manager.fetchBean();
0475:                        BeanImpl new_bean = BeanImpl.getPopulatedBean();
0476:                        assertEquals(retrieved_bean.getPropertyString(),
0477:                                new_bean.getPropertyString());
0478:                        assertEquals(retrieved_bean.getPropertyStringbuffer()
0479:                                .toString(), new_bean.getPropertyStringbuffer()
0480:                                .toString());
0481:
0482:                        // don't compare milliseconds since each db stores it differently
0483:                        assertEquals((retrieved_bean.getPropertyDate()
0484:                                .getTime() / 1000) * 1000, (new_bean
0485:                                .getPropertyDate().getTime() / 1000) * 1000);
0486:                        assertEquals((retrieved_bean.getPropertyCalendar()
0487:                                .getTime().getTime() / 1000) * 1000,
0488:                                (new_bean.getPropertyCalendar().getTime()
0489:                                        .getTime() / 1000) * 1000);
0490:                        assertEquals(
0491:                                (retrieved_bean.getPropertyTimestamp()
0492:                                        .getTime() / 1000) * 1000,
0493:                                (new_bean.getPropertyTimestamp().getTime() / 1000) * 1000);
0494:
0495:                        assertEquals(retrieved_bean.getPropertySqlDate()
0496:                                .toString(), new_bean.getPropertySqlDate()
0497:                                .toString());
0498:                        assertEquals(retrieved_bean.getPropertyTime()
0499:                                .toString(), new_bean.getPropertyTime()
0500:                                .toString());
0501:                        assertEquals(retrieved_bean.getPropertyChar(), new_bean
0502:                                .getPropertyChar());
0503:                        assertEquals(retrieved_bean
0504:                                .getPropertyCharacterObject(), new_bean
0505:                                .getPropertyCharacterObject());
0506:                        assertEquals(retrieved_bean.isPropertyBoolean(),
0507:                                new_bean.isPropertyBoolean());
0508:                        assertEquals(retrieved_bean.getPropertyBooleanObject(),
0509:                                new_bean.getPropertyBooleanObject());
0510:                        assertEquals(retrieved_bean.getPropertyByte(), new_bean
0511:                                .getPropertyByte());
0512:                        assertEquals(retrieved_bean.getPropertyByteObject(),
0513:                                new_bean.getPropertyByteObject());
0514:                        assertEquals(retrieved_bean.getPropertyDouble(),
0515:                                new_bean.getPropertyDouble(), 0.01);
0516:                        assertEquals(retrieved_bean.getPropertyDoubleObject()
0517:                                .doubleValue(), new_bean
0518:                                .getPropertyDoubleObject().doubleValue(), 0.01);
0519:                        assertEquals(retrieved_bean.getPropertyFloat(),
0520:                                new_bean.getPropertyFloat(), 0.01);
0521:                        assertEquals(retrieved_bean.getPropertyFloatObject()
0522:                                .floatValue(), new_bean
0523:                                .getPropertyFloatObject().floatValue(), 0.01);
0524:                        assertEquals(retrieved_bean.getPropertyInt(), new_bean
0525:                                .getPropertyInt());
0526:                        assertEquals(retrieved_bean.getPropertyIntegerObject(),
0527:                                new_bean.getPropertyIntegerObject());
0528:                        assertEquals(retrieved_bean.getPropertyLong(), new_bean
0529:                                .getPropertyLong());
0530:                        assertEquals(retrieved_bean.getPropertyLongObject(),
0531:                                new_bean.getPropertyLongObject());
0532:                        assertEquals(retrieved_bean.getPropertyShort(),
0533:                                new_bean.getPropertyShort());
0534:                        assertEquals(retrieved_bean.getPropertyShortObject(),
0535:                                new_bean.getPropertyShortObject());
0536:                        assertEquals(retrieved_bean.getPropertyBigDecimal(),
0537:                                new_bean.getPropertyBigDecimal());
0538:                    } finally {
0539:                        statement_insert.close();
0540:                    }
0541:                } catch (DatabaseException e) {
0542:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0543:                }
0544:            }
0545:
0546:            public void testSetBeanNulls() {
0547:                try {
0548:                    // insert some data
0549:                    Insert query_insert = new Insert(mDatasource);
0550:                    query_insert.into("parametersbean").fieldsParameters(
0551:                            BeanImpl.class);
0552:                    DbPreparedStatement statement_insert = mDatasource
0553:                            .getConnection().getPreparedStatement(query_insert);
0554:                    try {
0555:                        BeanImpl null_bean = BeanImpl.getNullBean();
0556:                        // each database has its oddities here, sadly
0557:                        Calendar cal = Calendar.getInstance();
0558:                        cal.set(2002, 5, 18, 15, 26, 14);
0559:                        cal.set(Calendar.MILLISECOND, 764);
0560:                        if (mDatasource.getDriver().equals(
0561:                                "org.postgresql.Driver")) {
0562:                            // postgres doesn't handle null chars
0563:                            null_bean.setPropertyChar(' ');
0564:                        } else if (mDatasource.getAliasedDriver().equals(
0565:                                "com.mysql.jdbc.Driver")) {
0566:                            // mysql automatically set the current time to timestamps
0567:                            null_bean.setPropertyDate(cal.getTime());
0568:                            null_bean.setPropertyCalendar(cal);
0569:                            null_bean.setPropertyTimestamp(new Timestamp(cal
0570:                                    .getTime().getTime()));
0571:                        }
0572:                        statement_insert.setBean(null_bean);
0573:                        statement_insert.executeUpdate();
0574:
0575:                        // retrieve the data
0576:                        BeanManager bean_manager = new BeanManager();
0577:                        BeanImpl retrieved_bean = bean_manager.fetchBean();
0578:                        BeanImpl new_bean = BeanImpl.getNullBean();
0579:                        // apply the database oddities
0580:                        if (mDatasource.getDriver().equals(
0581:                                "org.postgresql.Driver")) {
0582:                            // postgres doesn't handle null chars
0583:                            new_bean.setPropertyChar(' ');
0584:                        } else if (mDatasource.getAliasedDriver().equals(
0585:                                "com.mysql.jdbc.Driver")) {
0586:                            // mysql automatically set the current time to timestamps
0587:                            new_bean.setPropertyDate(cal.getTime());
0588:                            new_bean.setPropertyCalendar(cal);
0589:                            new_bean.setPropertyTimestamp(new Timestamp(cal
0590:                                    .getTime().getTime()));
0591:                        }
0592:                        assertEquals(retrieved_bean.getPropertyString(),
0593:                                new_bean.getPropertyString());
0594:                        assertEquals(retrieved_bean.getPropertyStringbuffer(),
0595:                                new_bean.getPropertyStringbuffer());
0596:                        // don't compare milliseconds since each db stores it differently
0597:                        if (mDatasource.getAliasedDriver().equals(
0598:                                "com.mysql.jdbc.Driver")) {
0599:                            // don't compare milliseconds since each db stores it differently
0600:                            assertEquals((retrieved_bean.getPropertyDate()
0601:                                    .getTime() / 1000) * 1000, (new_bean
0602:                                    .getPropertyDate().getTime() / 1000) * 1000);
0603:                            assertEquals((retrieved_bean.getPropertyCalendar()
0604:                                    .getTime().getTime() / 1000) * 1000,
0605:                                    (new_bean.getPropertyCalendar().getTime()
0606:                                            .getTime() / 1000) * 1000);
0607:                            assertEquals(
0608:                                    (retrieved_bean.getPropertyTimestamp()
0609:                                            .getTime() / 1000) * 1000,
0610:                                    (new_bean.getPropertyTimestamp().getTime() / 1000) * 1000);
0611:                        } else {
0612:                            assertEquals(retrieved_bean.getPropertyDate(),
0613:                                    new_bean.getPropertyDate());
0614:                            assertEquals(retrieved_bean.getPropertyCalendar(),
0615:                                    new_bean.getPropertyCalendar());
0616:                            assertEquals(retrieved_bean.getPropertyTimestamp(),
0617:                                    new_bean.getPropertyTimestamp());
0618:                        }
0619:                        assertEquals(retrieved_bean.getPropertySqlDate(),
0620:                                new_bean.getPropertySqlDate());
0621:                        assertEquals(retrieved_bean.getPropertyTime(), new_bean
0622:                                .getPropertyTime());
0623:                        assertEquals(retrieved_bean.getPropertyChar(), new_bean
0624:                                .getPropertyChar());
0625:                        assertEquals(retrieved_bean
0626:                                .getPropertyCharacterObject(), new_bean
0627:                                .getPropertyCharacterObject());
0628:                        assertEquals(retrieved_bean.isPropertyBoolean(),
0629:                                new_bean.isPropertyBoolean());
0630:                        assertEquals(retrieved_bean.getPropertyBooleanObject(),
0631:                                new_bean.getPropertyBooleanObject());
0632:                        assertEquals(retrieved_bean.getPropertyByte(), new_bean
0633:                                .getPropertyByte());
0634:                        assertEquals(retrieved_bean.getPropertyByteObject(),
0635:                                new_bean.getPropertyByteObject());
0636:                        assertEquals(retrieved_bean.getPropertyDouble(),
0637:                                new_bean.getPropertyDouble(), 0.01);
0638:                        assertEquals(retrieved_bean.getPropertyDoubleObject()
0639:                                .doubleValue(), new_bean
0640:                                .getPropertyDoubleObject().doubleValue(), 0.01);
0641:                        assertEquals(retrieved_bean.getPropertyFloat(),
0642:                                new_bean.getPropertyFloat(), 0.01);
0643:                        assertEquals(retrieved_bean.getPropertyFloatObject()
0644:                                .floatValue(), new_bean
0645:                                .getPropertyFloatObject().floatValue(), 0.01);
0646:                        assertEquals(retrieved_bean.getPropertyInt(), new_bean
0647:                                .getPropertyInt());
0648:                        assertEquals(retrieved_bean.getPropertyIntegerObject(),
0649:                                new_bean.getPropertyIntegerObject());
0650:                        assertEquals(retrieved_bean.getPropertyLong(), new_bean
0651:                                .getPropertyLong());
0652:                        assertEquals(retrieved_bean.getPropertyLongObject(),
0653:                                new_bean.getPropertyLongObject());
0654:                        assertEquals(retrieved_bean.getPropertyShort(),
0655:                                new_bean.getPropertyShort());
0656:                        assertEquals(retrieved_bean.getPropertyShortObject(),
0657:                                new_bean.getPropertyShortObject());
0658:                        assertEquals(retrieved_bean.getPropertyBigDecimal(),
0659:                                new_bean.getPropertyBigDecimal());
0660:                    } finally {
0661:                        statement_insert.close();
0662:                    }
0663:                } catch (DatabaseException e) {
0664:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0665:                }
0666:            }
0667:
0668:            public void testSetNamedParameters() {
0669:                try {
0670:                    // insert some data
0671:                    Insert query_insert = new Insert(mDatasource);
0672:                    query_insert.into("parametersbean").fieldsParameters(
0673:                            BeanImpl.class);
0674:                    DbPreparedStatement statement_insert = mDatasource
0675:                            .getConnection().getPreparedStatement(query_insert);
0676:                    try {
0677:                        Calendar cal = Calendar.getInstance();
0678:                        cal.set(2002, 5, 18, 15, 26, 14);
0679:                        cal.set(Calendar.MILLISECOND, 764);
0680:                        statement_insert.setString("propertyString",
0681:                                "someotherstring");
0682:                        statement_insert.setString("propertyStringbuffer",
0683:                                "someotherstringbuff");
0684:                        statement_insert.setTimestamp("propertyDate",
0685:                                new Timestamp(cal.getTime().getTime()));
0686:                        statement_insert.setTimestamp("propertyCalendar",
0687:                                new Timestamp(cal.getTime().getTime()));
0688:                        statement_insert.setDate("propertySqlDate",
0689:                                new java.sql.Date(cal.getTime().getTime()));
0690:                        statement_insert.setTime("propertyTime", new Time(cal
0691:                                .getTime().getTime()));
0692:                        statement_insert.setTimestamp("propertyTimestamp",
0693:                                new Timestamp(cal.getTime().getTime()));
0694:                        statement_insert.setString("propertyChar", "v");
0695:                        statement_insert.setString("propertyCharacterObject",
0696:                                "r");
0697:                        statement_insert.setBoolean("propertyBoolean", true);
0698:                        statement_insert.setBoolean("propertyBooleanObject",
0699:                                false);
0700:                        statement_insert.setByte("propertyByte", (byte) 89);
0701:                        statement_insert.setByte("propertyByteObject",
0702:                                (byte) 34);
0703:                        statement_insert.setDouble("propertyDouble", 53348.34d);
0704:                        statement_insert.setDouble("propertyDoubleObject",
0705:                                143298.692d);
0706:                        statement_insert.setFloat("propertyFloat", 98634.2f);
0707:                        statement_insert.setFloat("propertyFloatObject",
0708:                                8734.7f);
0709:                        statement_insert.setInt("propertyInt", 545);
0710:                        statement_insert.setInt("propertyIntegerObject", 968);
0711:                        statement_insert.setLong("propertyLong", 34563L);
0712:                        statement_insert.setLong("propertyLongObject", 66875L);
0713:                        statement_insert.setShort("propertyShort", (short) 43);
0714:                        statement_insert.setShort("propertyShortObject",
0715:                                (short) 68);
0716:                        statement_insert.setBigDecimal("propertyBigDecimal",
0717:                                new BigDecimal("219038743.392874"));
0718:                        statement_insert.setString("propertyEnum",
0719:                                SomeEnum.VALUE_TWO.toString());
0720:
0721:                        statement_insert.executeUpdate();
0722:
0723:                        // retrieve the data
0724:                        BeanManager bean_manager = new BeanManager();
0725:                        BeanImpl retrieved_bean = bean_manager.fetchBean();
0726:                        BeanImpl new_bean = BeanImpl.getPopulatedBean();
0727:                        assertEquals(retrieved_bean.getPropertyString(),
0728:                                new_bean.getPropertyString());
0729:                        assertEquals(retrieved_bean.getPropertyStringbuffer()
0730:                                .toString(), new_bean.getPropertyStringbuffer()
0731:                                .toString());
0732:
0733:                        // don't compare milliseconds since each db stores it differently
0734:                        assertEquals((retrieved_bean.getPropertyDate()
0735:                                .getTime() / 1000) * 1000, (new_bean
0736:                                .getPropertyDate().getTime() / 1000) * 1000);
0737:                        assertEquals((retrieved_bean.getPropertyCalendar()
0738:                                .getTime().getTime() / 1000) * 1000,
0739:                                (new_bean.getPropertyCalendar().getTime()
0740:                                        .getTime() / 1000) * 1000);
0741:                        assertEquals(
0742:                                (retrieved_bean.getPropertyTimestamp()
0743:                                        .getTime() / 1000) * 1000,
0744:                                (new_bean.getPropertyTimestamp().getTime() / 1000) * 1000);
0745:
0746:                        assertEquals(retrieved_bean.getPropertySqlDate()
0747:                                .toString(), new_bean.getPropertySqlDate()
0748:                                .toString());
0749:                        assertEquals(retrieved_bean.getPropertyTime()
0750:                                .toString(), new_bean.getPropertyTime()
0751:                                .toString());
0752:                        assertEquals(retrieved_bean.getPropertyChar(), new_bean
0753:                                .getPropertyChar());
0754:                        assertEquals(retrieved_bean
0755:                                .getPropertyCharacterObject(), new_bean
0756:                                .getPropertyCharacterObject());
0757:                        assertEquals(retrieved_bean.isPropertyBoolean(),
0758:                                new_bean.isPropertyBoolean());
0759:                        assertEquals(retrieved_bean.getPropertyBooleanObject(),
0760:                                new_bean.getPropertyBooleanObject());
0761:                        assertEquals(retrieved_bean.getPropertyByte(), new_bean
0762:                                .getPropertyByte());
0763:                        assertEquals(retrieved_bean.getPropertyByteObject(),
0764:                                new_bean.getPropertyByteObject());
0765:                        assertEquals(retrieved_bean.getPropertyDouble(),
0766:                                new_bean.getPropertyDouble(), 0.01);
0767:                        assertEquals(retrieved_bean.getPropertyDoubleObject()
0768:                                .doubleValue(), new_bean
0769:                                .getPropertyDoubleObject().doubleValue(), 0.01);
0770:                        assertEquals(retrieved_bean.getPropertyFloat(),
0771:                                new_bean.getPropertyFloat(), 0.01);
0772:                        assertEquals(retrieved_bean.getPropertyFloatObject()
0773:                                .floatValue(), new_bean
0774:                                .getPropertyFloatObject().floatValue(), 0.01);
0775:                        assertEquals(retrieved_bean.getPropertyInt(), new_bean
0776:                                .getPropertyInt());
0777:                        assertEquals(retrieved_bean.getPropertyIntegerObject(),
0778:                                new_bean.getPropertyIntegerObject());
0779:                        assertEquals(retrieved_bean.getPropertyLong(), new_bean
0780:                                .getPropertyLong());
0781:                        assertEquals(retrieved_bean.getPropertyLongObject(),
0782:                                new_bean.getPropertyLongObject());
0783:                        assertEquals(retrieved_bean.getPropertyShort(),
0784:                                new_bean.getPropertyShort());
0785:                        assertEquals(retrieved_bean.getPropertyShortObject(),
0786:                                new_bean.getPropertyShortObject());
0787:                        assertEquals(retrieved_bean.getPropertyBigDecimal(),
0788:                                new_bean.getPropertyBigDecimal());
0789:                    } finally {
0790:                        statement_insert.close();
0791:                    }
0792:                } catch (DatabaseException e) {
0793:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0794:                }
0795:            }
0796:
0797:            public void testIllegalParameterName() {
0798:                try {
0799:                    Insert query_insert = new Insert(mDatasource);
0800:                    query_insert.into("parametersbean")
0801:                            .fieldParameter("intcol");
0802:                    DbPreparedStatement statement_insert = null;
0803:
0804:                    try {
0805:                        statement_insert = mDatasource.getConnection()
0806:                                .getPreparedStatement(query_insert);
0807:                        try {
0808:                            statement_insert.setInt(null, 1);
0809:                            fail();
0810:                        } catch (IllegalArgumentException e) {
0811:                            assertTrue(true);
0812:                        }
0813:                        try {
0814:                            statement_insert.setInt("", 1);
0815:                            fail();
0816:                        } catch (IllegalArgumentException e) {
0817:                            assertTrue(true);
0818:                        }
0819:                        statement_insert.close();
0820:                    } catch (PreparedStatementCreationErrorException e) {
0821:                        assertSame(mDatasource, e.getDatasource());
0822:                    }
0823:                } catch (DatabaseException e) {
0824:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0825:                }
0826:            }
0827:
0828:            public void testInexistantParameterName() {
0829:                try {
0830:                    Insert query_insert = new Insert(mDatasource);
0831:                    query_insert.into("parametersbean")
0832:                            .fieldParameter("intcol");
0833:                    DbPreparedStatement statement_insert = null;
0834:
0835:                    try {
0836:                        statement_insert = mDatasource.getConnection()
0837:                                .getPreparedStatement(query_insert);
0838:                        try {
0839:                            statement_insert.setInt("doesntexist", 1);
0840:                            fail();
0841:                        } catch (ParameterDoesntExistException e) {
0842:                            assertSame(statement_insert, e
0843:                                    .getPreparedStatement());
0844:                            assertEquals("doesntexist", e.getParameterName());
0845:                        }
0846:                        statement_insert.close();
0847:                    } catch (PreparedStatementCreationErrorException e) {
0848:                        assertSame(mDatasource, e.getDatasource());
0849:                    }
0850:                } catch (DatabaseException e) {
0851:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
0852:                }
0853:            }
0854:
0855:            public void testIllegalArgumentTypedParameters() {
0856:                try {
0857:                    Insert query_insert = new Insert(mDatasource);
0858:                    query_insert.into("parametersbean")
0859:                            .fieldParameter("intcol");
0860:                    DbPreparedStatement statement_insert = null;
0861:
0862:                    try {
0863:                        statement_insert = mDatasource.getConnection()
0864:                                .getPreparedStatement(query_insert);
0865:                        try {
0866:                            statement_insert.setDoubles(null, 1d);
0867:                            fail();
0868:                        } catch (IllegalArgumentException e) {
0869:                            assertTrue(true);
0870:                        }
0871:                        try {
0872:                            statement_insert.setShorts(null, (short) 1);
0873:                            fail();
0874:                        } catch (IllegalArgumentException e) {
0875:                            assertTrue(true);
0876:                        }
0877:                        try {
0878:                            statement_insert.setNulls(null, Types.INTEGER);
0879:                            fail();
0880:                        } catch (IllegalArgumentException e) {
0881:                            assertTrue(true);
0882:                        }
0883:                        try {
0884:                            statement_insert.setNulls(null, Types.INTEGER,
0885:                                    "INT");
0886:                            fail();
0887:                        } catch (IllegalArgumentException e) {
0888:                            assertTrue(true);
0889:                        }
0890:                        try {
0891:                            statement_insert.setBooleans(null, true);
0892:                            fail();
0893:                        } catch (IllegalArgumentException e) {
0894:                            assertTrue(true);
0895:                        }
0896:                        try {
0897:                            statement_insert.setBytes(null, (byte) 1);
0898:                            fail();
0899:                        } catch (IllegalArgumentException e) {
0900:                            assertTrue(true);
0901:                        }
0902:                        try {
0903:                            statement_insert.setDates(null, new Date(0));
0904:                            fail();
0905:                        } catch (IllegalArgumentException e) {
0906:                            assertTrue(true);
0907:                        }
0908:                        try {
0909:                            statement_insert.setDates(null, new Date(0),
0910:                                    Calendar.getInstance());
0911:                            fail();
0912:                        } catch (IllegalArgumentException e) {
0913:                            assertTrue(true);
0914:                        }
0915:                        try {
0916:                            statement_insert.setInts(null, 1);
0917:                            fail();
0918:                        } catch (IllegalArgumentException e) {
0919:                            assertTrue(true);
0920:                        }
0921:                        try {
0922:                            statement_insert.setLongs(null, 1L);
0923:                            fail();
0924:                        } catch (IllegalArgumentException e) {
0925:                            assertTrue(true);
0926:                        }
0927:                        try {
0928:                            statement_insert.setFloats(null, 1f);
0929:                            fail();
0930:                        } catch (IllegalArgumentException e) {
0931:                            assertTrue(true);
0932:                        }
0933:                        try {
0934:                            statement_insert.setBigDecimals(null,
0935:                                    new BigDecimal(String.valueOf(1)));
0936:                            fail();
0937:                        } catch (IllegalArgumentException e) {
0938:                            assertTrue(true);
0939:                        }
0940:                        try {
0941:                            statement_insert.setStrings(null, "1");
0942:                            fail();
0943:                        } catch (IllegalArgumentException e) {
0944:                            assertTrue(true);
0945:                        }
0946:                        try {
0947:                            statement_insert.setBytes((int[]) null,
0948:                                    new byte[] { (byte) 1 });
0949:                            fail();
0950:                        } catch (IllegalArgumentException e) {
0951:                            assertTrue(true);
0952:                        }
0953:                        try {
0954:                            statement_insert.setObjects(null, "1",
0955:                                    Types.VARCHAR, 0);
0956:                            fail();
0957:                        } catch (IllegalArgumentException e) {
0958:                            assertTrue(true);
0959:                        }
0960:                        try {
0961:                            statement_insert.setObjects(null, "1",
0962:                                    Types.VARCHAR);
0963:                            fail();
0964:                        } catch (IllegalArgumentException e) {
0965:                            assertTrue(true);
0966:                        }
0967:                        try {
0968:                            statement_insert.setTimes(null, new Time(0));
0969:                            fail();
0970:                        } catch (IllegalArgumentException e) {
0971:                            assertTrue(true);
0972:                        }
0973:                        try {
0974:                            statement_insert.setTimes(null, new Time(0),
0975:                                    Calendar.getInstance());
0976:                            fail();
0977:                        } catch (IllegalArgumentException e) {
0978:                            assertTrue(true);
0979:                        }
0980:                        try {
0981:                            statement_insert.setTimestamps(null, new Timestamp(
0982:                                    0));
0983:                            fail();
0984:                        } catch (IllegalArgumentException e) {
0985:                            assertTrue(true);
0986:                        }
0987:                        try {
0988:                            statement_insert.setTimestamps(null, new Timestamp(
0989:                                    0), Calendar.getInstance());
0990:                            fail();
0991:                        } catch (IllegalArgumentException e) {
0992:                            assertTrue(true);
0993:                        }
0994:                        try {
0995:                            statement_insert.setObjects(null, "1");
0996:                            fail();
0997:                        } catch (IllegalArgumentException e) {
0998:                            assertTrue(true);
0999:                        }
1000:                        try {
1001:                            try {
1002:                                statement_insert.setURLs(null, new URL(
1003:                                        "http://www.uwyn.com"));
1004:                            } catch (MalformedURLException e) {
1005:                                assertTrue(ExceptionUtils
1006:                                        .getExceptionStackTrace(e), false);
1007:                            }
1008:                            fail();
1009:                        } catch (IllegalArgumentException e) {
1010:                            assertTrue(true);
1011:                        }
1012:                        statement_insert.close();
1013:                    } catch (PreparedStatementCreationErrorException e) {
1014:                        assertSame(mDatasource, e.getDatasource());
1015:                    }
1016:                } catch (DatabaseException e) {
1017:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
1018:                }
1019:            }
1020:
1021:            class BeanManager extends DbQueryManager {
1022:                public BeanManager() {
1023:                    super (mDatasource);
1024:                }
1025:
1026:                protected BeanImpl fetchBean() throws DatabaseException {
1027:                    Select query_select = new Select(getDatasource());
1028:                    query_select.from("parametersbean").fields(BeanImpl.class);
1029:                    DbBeanFetcher<BeanImpl> fetcher = new DbBeanFetcher<BeanImpl>(
1030:                            getDatasource(), BeanImpl.class);
1031:
1032:                    DbStatement statement = executeQuery(query_select);
1033:                    fetch(statement.getResultSet(), fetcher);
1034:                    BeanImpl bean = fetcher.getBeanInstance();
1035:                    statement.close();
1036:
1037:                    return bean;
1038:                }
1039:            }
1040:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.