Source Code Cross Referenced for TestDbConnection.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) 


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: TestDbConnection.java 3796 2007-06-20 21:44:23Z 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.tools.ExceptionUtils;
013:        import junit.framework.TestCase;
014:
015:        public class TestDbConnection extends TestCase {
016:            private Datasource mDatasource = null;
017:
018:            public TestDbConnection(Datasource datasource,
019:                    String datasourceName, String name) {
020:                super (name);
021:                mDatasource = datasource;
022:            }
023:
024:            public void testConnection() {
025:                DbConnection connection = null;
026:                try {
027:                    connection = mDatasource.getConnection();
028:                    assertFalse(connection.isClosed());
029:                } catch (DatabaseException e) {
030:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
031:                } finally {
032:                    if (null != connection) {
033:                        try {
034:                            connection.close();
035:                        } catch (DatabaseException e) {
036:                            assertTrue(
037:                                    ExceptionUtils.getExceptionStackTrace(e),
038:                                    false);
039:                        }
040:                    }
041:                }
042:            }
043:
044:            public void testDriverNameMapping() throws Exception {
045:                DbConnection connection = mDatasource.getConnection();
046:                try {
047:                    String name = connection.getMetaData().getDriverName();
048:                    assertEquals(name + " : "
049:                            + Datasource.sDriverNames.get(name) + " "
050:                            + mDatasource.getAliasedDriver(),
051:                            Datasource.sDriverNames.get(name), mDatasource
052:                                    .getAliasedDriver());
053:                } finally {
054:                    if (null != connection) {
055:                        try {
056:                            connection.close();
057:                        } catch (DatabaseException e) {
058:                            assertTrue(
059:                                    ExceptionUtils.getExceptionStackTrace(e),
060:                                    false);
061:                        }
062:                    }
063:                }
064:            }
065:
066:            public void testClose() {
067:                DbConnection connection = null;
068:                try {
069:                    connection = mDatasource.getConnection();
070:                    assertFalse(connection.isClosed());
071:                    connection.close();
072:                    if (mDatasource.isPooled()) {
073:                        assertFalse(connection.isClosed());
074:                    } else {
075:                        assertTrue(connection.isClosed());
076:                    }
077:                } catch (DatabaseException e) {
078:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
079:                }
080:            }
081:
082:            public void testGetMetaData() {
083:                DbConnection connection = null;
084:                try {
085:                    connection = mDatasource.getConnection();
086:                    assertNotNull(connection.getMetaData());
087:                } catch (DatabaseException e) {
088:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
089:                } finally {
090:                    if (null != connection) {
091:                        try {
092:                            connection.close();
093:                        } catch (DatabaseException e) {
094:                            assertTrue(
095:                                    ExceptionUtils.getExceptionStackTrace(e),
096:                                    false);
097:                        }
098:                    }
099:                }
100:            }
101:
102:            public void testGetStatement() {
103:                DbConnection connection = null;
104:                try {
105:                    connection = mDatasource.getConnection();
106:                    DbStatement statement1 = connection.createStatement();
107:                    assertNotNull(statement1);
108:                    DbStatement statement2 = connection.createStatement();
109:                    assertNotNull(statement2);
110:                    assertTrue(statement1 != statement2);
111:                } catch (DatabaseException e) {
112:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
113:                } finally {
114:                    if (null != connection) {
115:                        try {
116:                            connection.close();
117:                        } catch (DatabaseException e) {
118:                            assertTrue(
119:                                    ExceptionUtils.getExceptionStackTrace(e),
120:                                    false);
121:                        }
122:                    }
123:                }
124:            }
125:
126:            public void testGetPreparedStatement() {
127:                DbConnection connection = null;
128:                try {
129:                    connection = mDatasource.getConnection();
130:                    String sql = "CREATE TABLE tbltest (id INTEGER, stringcol VARCHAR(255))";
131:                    DbPreparedStatement prepared_statement1 = connection
132:                            .getPreparedStatement(sql);
133:                    assertNotNull(prepared_statement1);
134:                    DbPreparedStatement prepared_statement2 = connection
135:                            .getPreparedStatement(sql);
136:                    assertNotNull(prepared_statement2);
137:                    assertTrue(prepared_statement1 != prepared_statement2);
138:                } catch (DatabaseException e) {
139:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
140:                } finally {
141:                    if (null != connection) {
142:                        try {
143:                            connection.close();
144:                        } catch (DatabaseException e) {
145:                            assertTrue(
146:                                    ExceptionUtils.getExceptionStackTrace(e),
147:                                    false);
148:                        }
149:                    }
150:                }
151:            }
152:
153:            public void testGetPreparedStatementQueryBuilder() {
154:                DbConnection connection = null;
155:                try {
156:                    connection = mDatasource.getConnection();
157:                    CreateTable create = new CreateTable(mDatasource);
158:                    create.table("tbltest").column("id", int.class).column(
159:                            "stringcol", String.class, 255);
160:                    DbPreparedStatement prepared_statement1 = connection
161:                            .getPreparedStatement(create);
162:                    assertNotNull(prepared_statement1);
163:                    DbPreparedStatement prepared_statement2 = connection
164:                            .getPreparedStatement(create);
165:                    assertNotNull(prepared_statement2);
166:                    assertTrue(prepared_statement1 != prepared_statement2);
167:                } catch (DatabaseException e) {
168:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
169:                } finally {
170:                    if (null != connection) {
171:                        try {
172:                            connection.close();
173:                        } catch (DatabaseException e) {
174:                            assertTrue(
175:                                    ExceptionUtils.getExceptionStackTrace(e),
176:                                    false);
177:                        }
178:                    }
179:                }
180:            }
181:
182:            public void testTransactionBeginCommitRollback() {
183:                DbConnection connection = mDatasource.getConnection();
184:
185:                DbPreparedStatement prepared_statement_create = null;
186:                DbPreparedStatement prepared_statement_drop = null;
187:                prepared_statement_create = connection
188:                        .getPreparedStatement("CREATE TABLE tbltest (id INTEGER, stringcol VARCHAR(255))");
189:                prepared_statement_create.executeUpdate();
190:
191:                prepared_statement_drop = connection
192:                        .getPreparedStatement("DROP TABLE tbltest");
193:
194:                DbPreparedStatement prepared_statement_insert = null;
195:                DbPreparedStatement prepared_statement_select = null;
196:                try {
197:                    prepared_statement_insert = connection
198:                            .getPreparedStatement("INSERT INTO tbltest VALUES (232, 'somestring')");
199:                    prepared_statement_select = connection
200:                            .getPreparedStatement("SELECT * FROM tbltest");
201:
202:                    if (connection.supportsTransactions()
203:                            && false == mDatasource.getAliasedDriver().equals(
204:                                    "com.mysql.jdbc.Driver")) {
205:                        assertTrue(connection.beginTransaction());
206:                        assertEquals(1, prepared_statement_insert
207:                                .executeUpdate());
208:                        prepared_statement_select.executeQuery();
209:                        assertTrue(prepared_statement_select.getResultSet()
210:                                .hasResultRows());
211:                        assertTrue(connection.rollback());
212:                        assertFalse(connection.commit());
213:
214:                        prepared_statement_select.executeQuery();
215:                        assertFalse(prepared_statement_select.getResultSet()
216:                                .hasResultRows());
217:
218:                        assertTrue(connection.beginTransaction());
219:                        assertEquals(1, prepared_statement_insert
220:                                .executeUpdate());
221:                        prepared_statement_select.executeQuery();
222:                        assertTrue(prepared_statement_select.getResultSet()
223:                                .hasResultRows());
224:                        assertTrue(connection.commit());
225:                        assertFalse(connection.rollback());
226:
227:                        prepared_statement_select.executeQuery();
228:                        assertTrue(prepared_statement_select.getResultSet()
229:                                .hasResultRows());
230:                    } else {
231:                        // FIXME: write tests with non transactional database
232:                    }
233:                } catch (Exception e) {
234:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
235:                } finally {
236:                    prepared_statement_insert.close();
237:                    prepared_statement_select.close();
238:
239:                    assertTrue(connection.beginTransaction());
240:                    prepared_statement_drop.executeUpdate();
241:                    assertTrue(connection.commit());
242:                    assertFalse(connection.rollback());
243:
244:                    try {
245:                        prepared_statement_select = connection
246:                                .getPreparedStatement("SELECT * FROM tbltest");
247:                        prepared_statement_select.executeQuery();
248:                        fail();
249:                    } catch (DatabaseException e) {
250:                        assertTrue(true);
251:                    }
252:
253:                    connection.close();
254:                }
255:            }
256:
257:            public void testTransactionThreadValidity() {
258:                DbConnection connection = null;
259:                try {
260:                    connection = mDatasource.getConnection();
261:                    if (connection.supportsTransactions()
262:                            && false == mDatasource.getAliasedDriver().equals(
263:                                    "com.mysql.jdbc.Driver")) {
264:                        assertTrue(false == connection
265:                                .isTransactionValidForThread());
266:                        assertTrue(true == connection.beginTransaction());
267:                        assertTrue(true == connection
268:                                .isTransactionValidForThread());
269:                        ThreadImpl other_thread = new ThreadImpl(connection);
270:                        other_thread.start();
271:                        while (other_thread.isAlive()) {
272:                            synchronized (other_thread) {
273:                                try {
274:                                    other_thread.wait(3600);
275:                                } catch (InterruptedException e) {
276:                                    other_thread.interrupt();
277:                                    other_thread.stop();
278:                                    throw new RuntimeException(
279:                                            "testTransactionThreadValidity failed for "
280:                                                    + mDatasource
281:                                                            .getAliasedDriver()
282:                                                    + ", timeout", e);
283:                                }
284:                            }
285:                        }
286:                        assertTrue(true == connection
287:                                .isTransactionValidForThread());
288:                        assertTrue(true == connection.rollback());
289:                        assertTrue(false == connection.commit());
290:                        assertTrue(false == connection
291:                                .isTransactionValidForThread());
292:                    } else {
293:                        // FIXME: write tests with non transactional database
294:                    }
295:                } catch (DatabaseException e) {
296:                    assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
297:                } finally {
298:                    if (null != connection) {
299:                        try {
300:                            connection.close();
301:                        } catch (DatabaseException e) {
302:                            assertTrue(
303:                                    ExceptionUtils.getExceptionStackTrace(e),
304:                                    false);
305:                        }
306:                    }
307:                }
308:            }
309:
310:            //	public void testTransactionTimeoutBegin()
311:            //	{
312:            //		DbConnection connection = null;
313:            //		DbPreparedStatement prepared_statement_create = null;
314:            //		DbPreparedStatement prepared_statement_insert = null;
315:            //		DbPreparedStatement prepared_statement_select = null;
316:            //		DbPreparedStatement prepared_statement_drop = null;
317:            //		try
318:            //		{
319:            //			connection = mDatasource.getConnection();
320:            //			prepared_statement_create = connection.getPreparedStatement("CREATE TABLE tbltest (id INTEGER, stringcol VARCHAR(255))");
321:            //			prepared_statement_drop = connection.getPreparedStatement("DROP TABLE tbltest");
322:            //			prepared_statement_create.executeUpdate();
323:            //
324:            //			prepared_statement_insert = connection.getPreparedStatement("INSERT INTO tbltest VALUES (232, 'somestring')");
325:            //			prepared_statement_select = connection.getPreparedStatement("SELECT * FROM tbltest");
326:            //
327:            //			if (connection.supportsTransactions() &&
328:            //				false == mDatasource.getAliasedDriver().equals("com.mysql.jdbc.Driver"))
329:            //			{
330:            //				assertTrue(true == connection.beginTransaction());
331:            //				prepared_statement_insert.executeUpdate();
332:            //				try
333:            //				{
334:            //					Thread.sleep(RifeConfig.Database.getTransactionTimeout()*1000+100);
335:            //				}
336:            //				catch (InterruptedException e)
337:            //				{
338:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
339:            //				}
340:            //
341:            //				try
342:            //				{
343:            //					connection.beginTransaction();
344:            //					fail();
345:            //				}
346:            //				catch (TransactionTimedOutException e)
347:            //				{
348:            //					assertTrue(true);
349:            //				}
350:            //
351:            //				assertTrue(false == connection.commit());
352:            //				assertTrue(false == connection.rollback());
353:            //				try
354:            //				{
355:            //					prepared_statement_select.executeQuery();
356:            //					assertTrue(false == prepared_statement_select.hasResultRows());
357:            //				}
358:            //				catch (DatabaseException e)
359:            //				{
360:            //					assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
361:            //				}
362:            //			}
363:            //			else
364:            //			{
365:            //				// FIXME: write tests with non transactional database
366:            //			}
367:            //		}
368:            //		catch (DatabaseException e)
369:            //		{
370:            //            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
371:            //		}
372:            //		finally
373:            //		{
374:            //			if (null != connection)
375:            //			{
376:            //				try
377:            //				{
378:            //					prepared_statement_drop.executeUpdate();
379:            //					connection.close();
380:            //				}
381:            //				catch (DatabaseException e)
382:            //				{
383:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
384:            //				}
385:            //			}
386:            //		}
387:            //	}
388:
389:            //	public void testTransactionTimeoutCommit()
390:            //	{
391:            //		DbConnection connection = null;
392:            //		DbPreparedStatement prepared_statement_create = null;
393:            //		DbPreparedStatement prepared_statement_insert = null;
394:            //		DbPreparedStatement prepared_statement_select = null;
395:            //		DbPreparedStatement prepared_statement_drop = null;
396:            //		try
397:            //		{
398:            //			connection = mDatasource.getConnection();
399:            //			prepared_statement_create = connection.getPreparedStatement("CREATE TABLE tbltest (id INTEGER, stringcol VARCHAR(255))");
400:            //			prepared_statement_drop = connection.getPreparedStatement("DROP TABLE tbltest");
401:            //			prepared_statement_create.executeUpdate();
402:            //
403:            //			prepared_statement_insert = connection.getPreparedStatement("INSERT INTO tbltest VALUES (232, 'somestring')");
404:            //			prepared_statement_select = connection.getPreparedStatement("SELECT * FROM tbltest");
405:            //
406:            //			if (connection.supportsTransactions() &&
407:            //				false == mDatasource.getAliasedDriver().equals("com.mysql.jdbc.Driver"))
408:            //			{
409:            //				assertTrue(true == connection.beginTransaction());
410:            //				prepared_statement_insert.executeUpdate();
411:            //				try
412:            //				{
413:            //					Thread.sleep(RifeConfig.Database.getTransactionTimeout()*1000+100);
414:            //				}
415:            //				catch (InterruptedException e)
416:            //				{
417:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
418:            //				}
419:            //
420:            //				try
421:            //				{
422:            //					connection.commit();
423:            //					fail();
424:            //				}
425:            //				catch (TransactionTimedOutException e)
426:            //				{
427:            //					assertTrue(true);
428:            //				}
429:            //
430:            //				assertTrue(false == connection.rollback());
431:            //				try
432:            //				{
433:            //					prepared_statement_select.executeQuery();
434:            //					assertTrue(false == prepared_statement_select.hasResultRows());
435:            //				}
436:            //				catch (DatabaseException e)
437:            //				{
438:            //					assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
439:            //				}
440:            //			}
441:            //			else
442:            //			{
443:            //				// FIXME: write tests with non transactional database
444:            //			}
445:            //		}
446:            //		catch (DatabaseException e)
447:            //		{
448:            //            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
449:            //		}
450:            //		finally
451:            //		{
452:            //			if (null != connection)
453:            //			{
454:            //				try
455:            //				{
456:            //					prepared_statement_drop.executeUpdate();
457:            //					connection.close();
458:            //				}
459:            //				catch (DatabaseException e)
460:            //				{
461:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
462:            //				}
463:            //			}
464:            //		}
465:            //	}
466:            //
467:            //	public void testTransactionTimeoutRollback()
468:            //	{
469:            //		DbConnection connection = null;
470:            //		DbPreparedStatement prepared_statement_create = null;
471:            //		DbPreparedStatement prepared_statement_insert = null;
472:            //		DbPreparedStatement prepared_statement_select = null;
473:            //		DbPreparedStatement prepared_statement_drop = null;
474:            //		try
475:            //		{
476:            //			connection = mDatasource.getConnection();
477:            //			prepared_statement_create = connection.getPreparedStatement("CREATE TABLE tbltest (id INTEGER, stringcol VARCHAR(255))");
478:            //			prepared_statement_drop = connection.getPreparedStatement("DROP TABLE tbltest");
479:            //			prepared_statement_create.executeUpdate();
480:            //
481:            //			prepared_statement_insert = connection.getPreparedStatement("INSERT INTO tbltest VALUES (232, 'somestring')");
482:            //			prepared_statement_select = connection.getPreparedStatement("SELECT * FROM tbltest");
483:            //
484:            //			if (connection.supportsTransactions() &&
485:            //				false == mDatasource.getAliasedDriver().equals("com.mysql.jdbc.Driver"))
486:            //			{
487:            //				assertTrue(true == connection.beginTransaction());
488:            //				prepared_statement_insert.executeUpdate();
489:            //				try
490:            //				{
491:            //					Thread.sleep(RifeConfig.Database.getTransactionTimeout()*1000+100);
492:            //				}
493:            //				catch (InterruptedException e)
494:            //				{
495:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
496:            //				}
497:            //
498:            //				try
499:            //				{
500:            //					connection.rollback();
501:            //					fail();
502:            //				}
503:            //				catch (TransactionTimedOutException e)
504:            //				{
505:            //					assertTrue(true);
506:            //				}
507:            //
508:            //				assertTrue(false == connection.commit());
509:            //				try
510:            //				{
511:            //					prepared_statement_select.executeQuery();
512:            //					assertTrue(false == prepared_statement_select.hasResultRows());
513:            //				}
514:            //				catch (DatabaseException e)
515:            //				{
516:            //					assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
517:            //				}
518:            //			}
519:            //			else
520:            //			{
521:            //				// FIXME: write tests with non transactional database
522:            //			}
523:            //		}
524:            //		catch (DatabaseException e)
525:            //		{
526:            //            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
527:            //		}
528:            //		finally
529:            //		{
530:            //			if (null != connection)
531:            //			{
532:            //				try
533:            //				{
534:            //					prepared_statement_drop.executeUpdate();
535:            //					connection.close();
536:            //				}
537:            //				catch (DatabaseException e)
538:            //				{
539:            //		            assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
540:            //				}
541:            //			}
542:            //		}
543:            //	}
544:
545:            class ThreadImpl extends Thread {
546:                private DbConnection mConnection = null;
547:
548:                public ThreadImpl(DbConnection connection) {
549:                    mConnection = connection;
550:                }
551:
552:                public void run() {
553:                    try {
554:                        assertTrue(false == mConnection
555:                                .isTransactionValidForThread());
556:                        assertTrue(false == mConnection.beginTransaction());
557:                        assertTrue(false == mConnection.commit());
558:                        assertTrue(false == mConnection.rollback());
559:                    } catch (DatabaseException e) {
560:                        assertTrue(ExceptionUtils.getExceptionStackTrace(e),
561:                                false);
562:                    } finally {
563:                        synchronized (this) {
564:                            this.notifyAll();
565:                        }
566:                    }
567:                }
568:            }
569:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.