Source Code Cross Referenced for JDBCTestSupport.java in  » GIS » GeoTools-2.4.1 » org » geotools » data » 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 » GIS » GeoTools 2.4.1 » org.geotools.data.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.geotools.data.jdbc;
002:
003:        import java.io.BufferedReader;
004:        import java.io.ByteArrayInputStream;
005:        import java.io.InputStream;
006:        import java.io.InputStreamReader;
007:        import java.sql.Connection;
008:        import java.sql.SQLException;
009:        import java.sql.Statement;
010:
011:        import junit.framework.TestCase;
012:
013:        import org.apache.commons.dbcp.BasicDataSource;
014:        import org.geotools.feature.simple.SimpleFeatureFactoryImpl;
015:        import org.geotools.feature.simple.SimpleTypeFactoryImpl;
016:        import org.geotools.filter.FilterFactoryImpl;
017:        import org.h2.tools.DeleteDbFiles;
018:        import org.h2.tools.Server;
019:
020:        import com.vividsolutions.jts.geom.GeometryFactory;
021:
022:        /**
023:         * Test support class for jdbc test cases.
024:         * <p>
025:         * This test class fires up a live instance of an h2 database to provide a 
026:         * live database to work with.
027:         * </p>
028:         *
029:         * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
030:         *
031:         */
032:        public class JDBCTestSupport extends TestCase {
033:
034:            /**
035:             * data source
036:             */
037:            static BasicDataSource dataSource = new BasicDataSource();
038:            static {
039:                dataSource.setUrl("jdbc:h2:geotools");
040:                dataSource.setDriverClassName("org.h2.Driver");
041:                dataSource.setPoolPreparedStatements(false);
042:            }
043:
044:            /**
045:             * Embedded server instance, created statically to live over the life
046:             * of many test cases, with a shutdown hook to cleanup
047:             */
048:            static Server server;
049:
050:            static {
051:                Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
052:                    public void run() {
053:                        if (server != null) {
054:                            //stop the server
055:                            server.stop();
056:
057:                            //kill the files
058:                            try {
059:                                //bug with h2? if hte log file doesn't get 
060:                                // deleted the subsequent run crashes, if 
061:                                // you execute twice, then the log file gets
062:                                // deleted
063:                                DeleteDbFiles.main(null);
064:                                DeleteDbFiles.main(null);
065:                            } catch (SQLException e) {
066:                            }
067:                        }
068:                    }
069:                }));
070:            }
071:
072:            /**
073:             * Runs an sql string aginst the database.
074:             * 
075:             * @param input The sql.
076:             */
077:            static void run(String input) throws Exception {
078:                JDBCDataStore.LOGGER.info(input);
079:                run(new ByteArrayInputStream(input.getBytes()));
080:            }
081:
082:            /**
083:             * Runs an sql script against the database.
084:             * 
085:             * @param script Input stream to the sql script to run.
086:             */
087:            static void run(InputStream script) throws Exception {
088:                //load the script
089:                BufferedReader reader = new BufferedReader(
090:                        new InputStreamReader(script));
091:
092:                //connect
093:                Connection conn = dataSource.getConnection();
094:
095:                try {
096:                    Statement st = conn.createStatement();
097:
098:                    String line = null;
099:
100:                    while ((line = reader.readLine()) != null) {
101:                        st.execute(line);
102:                    }
103:
104:                    reader.close();
105:
106:                    st.close();
107:                } finally {
108:                    conn.close();
109:                }
110:            }
111:
112:            protected JDBCDataStore dataStore;
113:
114:            protected void setUp() throws Exception {
115:                super .setUp();
116:
117:                if (server == null) {
118:                    //create the server instance
119:                    server = Server.createTcpServer(new String[] {});
120:                    server.start();
121:
122:                    //spatialy enable it
123:                    run(getClass().getResourceAsStream("h2.sql"));
124:                }
125:
126:                //create the dataStore
127:                dataStore = new JDBCDataStore();
128:
129:                dataStore.setNamespaceURI("http://www.geotools.org/test");
130:                dataStore.setDataSource(dataSource);
131:                dataStore.setDatabaseSchema("geotools");
132:                dataStore.setTypeFactory(new SimpleTypeFactoryImpl());
133:                dataStore.setFeatureFactory(new SimpleFeatureFactoryImpl());
134:                dataStore.setFilterFactory(new FilterFactoryImpl());
135:                dataStore.setGeometryFactory(new GeometryFactory());
136:
137:                //create some data
138:                StringBuffer sb = new StringBuffer()
139:                        .append("CREATE SCHEMA \"geotools\";");
140:
141:                sb
142:                        .append("CREATE TABLE \"geotools\".\"ft1\" ")
143:                        .append("(\"id\" int AUTO_INCREMENT(1) primary key , ")
144:                        .append("\"geometry\" OTHER, \"intProperty\" int, ")
145:                        .append(
146:                                "\"doubleProperty\" double, \"stringProperty\" varchar);");
147:
148:                sb.append("INSERT INTO \"geotools\".\"ft1\" VALUES (").append(
149:                        "0,GeometryFromText('POINT(0 0)'), 0, 0.0,'zero');");
150:
151:                sb.append("INSERT INTO \"geotools\".\"ft1\" VALUES (").append(
152:                        "1,GeometryFromText('POINT(1 1)'), 1, 1.1,'one');");
153:
154:                sb.append("INSERT INTO \"geotools\".\"ft1\" VALUES (").append(
155:                        "2,GeometryFromText('POINT(2 2)'), 2, 2.2,'two');");
156:
157:                run(sb.toString());
158:
159:            }
160:
161:            protected void tearDown() throws Exception {
162:                super .tearDown();
163:
164:                StringBuffer sb = new StringBuffer(
165:                        "DROP TABLE \"geotools\".\"ft1\";")
166:                        .append("DROP SCHEMA \"geotools\";");
167:
168:                run(sb.toString());
169:            }
170:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.