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: }
|