Source Code Cross Referenced for CleanDatabaseTestSetup.java in  » Database-DBMS » db-derby-10.2 » org » apache » derbyTesting » junit » 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 » Database DBMS » db derby 10.2 » org.apache.derbyTesting.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *
003:         * Derby - Class org.apache.derbyTesting.functionTests.util.CleanDatabase
004:         *
005:         * Licensed to the Apache Software Foundation (ASF) under one or more
006:         * contributor license agreements.  See the NOTICE file distributed with
007:         * this work for additional information regarding copyright ownership.
008:         * The ASF licenses this file to You under the Apache License, Version 2.0
009:         * (the "License"); you may not use this file except in compliance with
010:         * the License.  You may obtain a copy of the License at
011:         *
012:         *    http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing, 
015:         * software distributed under the License is distributed on an 
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
017:         * either express or implied. See the License for the specific 
018:         * language governing permissions and limitations under the License.
019:         */
020:        package org.apache.derbyTesting.junit;
021:
022:        import java.sql.*;
023:        import java.util.ArrayList;
024:        import java.util.Iterator;
025:        import java.util.List;
026:
027:        import org.apache.derbyTesting.functionTests.tests.lang.TimeHandlingTest;
028:
029:        import junit.framework.Test;
030:
031:        /**
032:         * Test decorator that cleans a database on setUp and
033:         * tearDown to provide a test with a consistent empty
034:         * database as a starting point.
035:         * <P>
036:         * Tests can extend to provide a decorator that defines
037:         * some schema items and then have CleanDatabaseTestSetup
038:         * automatically clean them up by implementing the decorateSQL method.. 
039:         * As an example:
040:         * <code>
041:         return new CleanDatabaseTestSetup(suite) {
042:         protected void decorateSQL(Statement s) throws SQLException {
043:
044:         s.execute("CREATE TABLE T (I INT)");
045:         s.execute("CREATE INDEX TI ON T(I)")
046:
047:         }
048:         };
049:         * </code>
050:         * 
051:         */
052:        public class CleanDatabaseTestSetup extends BaseJDBCTestSetup {
053:
054:            /**
055:             * Decorator this test with the cleaner
056:             */
057:            public CleanDatabaseTestSetup(Test test) {
058:                super (test);
059:            }
060:
061:            /**
062:             * Clean the default database using the default connection
063:             * and calls the decorateSQL to allow sub-classes to
064:             * initialize their schema requirments.
065:             */
066:            protected void setUp() throws Exception {
067:                Connection conn = getConnection();
068:                conn.setAutoCommit(false);
069:                CleanDatabaseTestSetup.cleanDatabase(conn);
070:
071:                Statement s = conn.createStatement();
072:                decorateSQL(s);
073:
074:                s.close();
075:                conn.commit();
076:                conn.close();
077:            }
078:
079:            /**
080:             * Sub-classes can override this method to execute
081:             * SQL statements executed at setUp time once the
082:             * database has been cleaned.
083:             * Once this method returns the statement will be closed,
084:             * commit called and the connection closed. The connection
085:             * returned by s.getConnection() is the default connection
086:             * and is in auto-commit false mode.
087:             * <BR>
088:             * This implementation does nothing. Sub-classes need not call it.
089:             * @throws SQLException
090:             */
091:            protected void decorateSQL(Statement s) throws SQLException {
092:                // nothing in the default case.
093:            }
094:
095:            /**
096:             * Clean the default database using the default connection.
097:             */
098:            protected void tearDown() throws Exception {
099:                Connection conn = getConnection();
100:                conn.setAutoCommit(false);
101:                CleanDatabaseTestSetup.cleanDatabase(conn);
102:                super .tearDown();
103:            }
104:
105:            /**
106:             * Clean a complete database
107:             * @param conn Connection to be used, must not be in auto-commit mode.
108:             * @throws SQLException database error
109:             */
110:            public static void cleanDatabase(Connection conn)
111:                    throws SQLException {
112:                DatabaseMetaData dmd = conn.getMetaData();
113:
114:                // Fetch all the user schemas into a list
115:                List schemas = new ArrayList();
116:                ResultSet rs = dmd.getSchemas();
117:                while (rs.next()) {
118:
119:                    String schema = rs.getString("TABLE_SCHEM");
120:                    if (schema.startsWith("SYS"))
121:                        continue;
122:                    if (schema.equals("SQLJ"))
123:                        continue;
124:                    if (schema.equals("NULLID"))
125:                        continue;
126:
127:                    schemas.add(schema);
128:                }
129:                rs.close();
130:
131:                // DROP all the user schemas.
132:                for (Iterator i = schemas.iterator(); i.hasNext();) {
133:                    String schema = (String) i.next();
134:                    JDBC.dropSchema(dmd, schema);
135:                }
136:            }
137:
138:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.