Source Code Cross Referenced for JDBCMockObjectFactory.java in  » Testing » mockrunner-0.4 » com » mockrunner » mock » jdbc » 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 » Testing » mockrunner 0.4 » com.mockrunner.mock.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.mock.jdbc;
002:
003:        import java.sql.Driver;
004:        import java.sql.DriverManager;
005:        import java.sql.SQLException;
006:        import java.util.Enumeration;
007:        import java.util.HashSet;
008:        import java.util.Iterator;
009:        import java.util.Set;
010:
011:        import com.mockrunner.base.NestedApplicationException;
012:
013:        /**
014:         * Used to create all types of JDBC mock objects. 
015:         * Maintains the necessary dependencies between the mock objects.
016:         * If you use the mock objects returned by this
017:         * factory in your tests you can be sure that they are all
018:         * up to date.
019:         * Please note, that this class removes all drivers
020:         * from the JDBC <code>DriverManager</code> and registers
021:         * the {@link MockDriver}. All drivers are preserved and
022:         * can be restored with {@link #restoreDrivers}.
023:         */
024:        public class JDBCMockObjectFactory {
025:            private MockDataSource dataSource;
026:            private MockDriver driver;
027:            private MockConnection connection;
028:            private Set preservedDrivers;
029:
030:            /**
031:             * Creates a new set of mock objects.
032:             */
033:            public JDBCMockObjectFactory() {
034:                dataSource = createMockDataSource();
035:                driver = createMockDriver();
036:                connection = createMockConnection();
037:                preservedDrivers = new HashSet();
038:                setUpDependencies();
039:            }
040:
041:            private void setUpDependencies() {
042:                dataSource.setupConnection(connection);
043:                driver.setupConnection(connection);
044:                registerMockDriver();
045:            }
046:
047:            private void deregisterDrivers() {
048:                try {
049:                    Enumeration drivers = DriverManager.getDrivers();
050:                    while (drivers.hasMoreElements()) {
051:                        DriverManager.deregisterDriver((Driver) drivers
052:                                .nextElement());
053:                    }
054:                } catch (SQLException exc) {
055:                    throw new NestedApplicationException(exc);
056:                }
057:            }
058:
059:            private void deregisterMockDrivers() {
060:                try {
061:                    Enumeration drivers = DriverManager.getDrivers();
062:                    while (drivers.hasMoreElements()) {
063:                        Driver currentDriver = (Driver) drivers.nextElement();
064:                        if (currentDriver instanceof  MockDriver) {
065:                            DriverManager.deregisterDriver(currentDriver);
066:                        }
067:                    }
068:                } catch (SQLException exc) {
069:                    throw new NestedApplicationException(exc);
070:                }
071:            }
072:
073:            private void preserveDrivers() {
074:                Enumeration drivers = DriverManager.getDrivers();
075:                while (drivers.hasMoreElements()) {
076:                    Driver currentDriver = (Driver) drivers.nextElement();
077:                    if (!(currentDriver instanceof  MockDriver)) {
078:                        preservedDrivers.add(currentDriver);
079:                    }
080:                }
081:            }
082:
083:            /**
084:             * Removes all JDBC drivers from the <code>DriveManager</code> and
085:             * registers the mock driver. The removed drivers are preserved and
086:             * can be restored with {@link #restoreDrivers}.
087:             */
088:            public void registerMockDriver() {
089:                try {
090:                    preserveDrivers();
091:                    deregisterDrivers();
092:                    DriverManager.registerDriver(driver);
093:                } catch (SQLException exc) {
094:                    throw new NestedApplicationException(exc);
095:                }
096:            }
097:
098:            /**
099:             * Since <code>JDBCMockObjectFactory</code> removes all the
100:             * drivers from the <code>DriveManager</code> (so the
101:             * {@link MockDriver} is guaranteed to be the only one)
102:             * you can use this method to restore the original drivers.
103:             * Automatically called by {@link com.mockrunner.base.BaseTestCase#tearDown}.
104:             */
105:            public void restoreDrivers() {
106:                deregisterMockDrivers();
107:                try {
108:                    Iterator drivers = preservedDrivers.iterator();
109:                    while (drivers.hasNext()) {
110:                        DriverManager.registerDriver((Driver) drivers.next());
111:                    }
112:                } catch (SQLException exc) {
113:                    throw new NestedApplicationException(exc);
114:                }
115:                preservedDrivers.clear();
116:            }
117:
118:            /**
119:             * Creates the {@link com.mockrunner.mock.jdbc.MockConnection} using <code>new</code>.
120:             * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockConnection}.
121:             * @return the {@link com.mockrunner.mock.jdbc.MockConnection}
122:             */
123:            public MockConnection createMockConnection() {
124:                return new MockConnection();
125:            }
126:
127:            /**
128:             * Creates the {@link com.mockrunner.mock.jdbc.MockDriver} using <code>new</code>.
129:             * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDriver}.
130:             * @return the {@link com.mockrunner.mock.jdbc.MockDriver}
131:             */
132:            public MockDriver createMockDriver() {
133:                return new MockDriver();
134:            }
135:
136:            /**
137:             * Creates the {@link com.mockrunner.mock.jdbc.MockDataSource} using <code>new</code>.
138:             * This method can be overridden to return a subclass of {@link com.mockrunner.mock.jdbc.MockDataSource}.
139:             * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}
140:             */
141:            public MockDataSource createMockDataSource() {
142:                return new MockDataSource();
143:            }
144:
145:            /**
146:             * Returns the {@link com.mockrunner.mock.jdbc.MockDataSource}.
147:             * @return the {@link com.mockrunner.mock.jdbc.MockDataSource}
148:             */
149:            public MockDataSource getMockDataSource() {
150:                return dataSource;
151:            }
152:
153:            /**
154:             * Returns the {@link com.mockrunner.mock.jdbc.MockDriver}.
155:             * @return the {@link com.mockrunner.mock.jdbc.MockDriver}
156:             */
157:            public MockDriver getMockDriver() {
158:                return driver;
159:            }
160:
161:            /**
162:             * Returns the {@link com.mockrunner.mock.jdbc.MockConnection}.
163:             * @return the {@link com.mockrunner.mock.jdbc.MockConnection}
164:             */
165:            public MockConnection getMockConnection() {
166:                return connection;
167:            }
168:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.