001: package org.geotools.data.jdbc;
002:
003: import java.sql.Connection;
004: import java.sql.SQLException;
005: import java.sql.Statement;
006:
007: import javax.sql.DataSource;
008:
009: import junit.framework.TestCase;
010:
011: import org.geotools.factory.CommonFactoryFinder;
012: import org.geotools.feature.DefaultFeatureCollection;
013: import org.geotools.feature.FeatureCollection;
014: import org.geotools.feature.FeatureType;
015: import org.geotools.feature.simple.SimpleFeatureBuilder;
016: import org.geotools.feature.simple.SimpleFeatureFactoryImpl;
017: import org.geotools.feature.simple.SimpleTypeBuilder;
018: import org.geotools.feature.simple.SimpleTypeFactoryImpl;
019: import org.geotools.util.CommonsConverterFactory;
020:
021: import com.vividsolutions.jts.geom.Coordinate;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import com.vividsolutions.jts.geom.LineString;
024: import com.vividsolutions.jts.geom.Point;
025: import com.vividsolutions.jts.geom.Polygon;
026:
027: /**
028: * Abstract test suite for jdbc datastores.
029: *
030: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
031: *
032: */
033: public abstract class JDBCConformanceTestSupport extends TestCase {
034:
035: /**
036: * data
037: */
038: DataSource dataSource;
039: /**
040: * datastore
041: */
042: JDBCDataStore dataStore;
043: /**
044: * feature types
045: */
046: FeatureType point, line, polygon;
047: FeatureCollection points, lines, polygons;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051:
052: //create the datasource
053: dataSource = createDataSource();
054:
055: //create the datastore
056: dataStore = createDataStore();
057: dataStore.setFeatureFactory(new SimpleFeatureFactoryImpl());
058: dataStore.setTypeFactory(new SimpleTypeFactoryImpl());
059: dataStore.setFilterFactory(CommonFactoryFinder
060: .getFilterFactory(null));
061:
062: dataStore.setDataSource(dataSource);
063:
064: //create the data
065: SimpleTypeBuilder tb = new SimpleTypeBuilder(dataStore
066: .getTypeFactory());
067:
068: tb.setName("point");
069: tb.setNamespaceURI("http://geotools.org/jdbc");
070:
071: tb.attribute("geometry", Point.class);
072: tb.attribute("intProperty", Integer.class);
073: point = tb.feature();
074:
075: tb.init();
076: tb.setName("line");
077: tb.attribute("geometry", LineString.class);
078: tb.attribute("doubleProperty", Double.class);
079: line = tb.feature();
080:
081: tb.init();
082: tb.setName("polygon");
083: tb.attribute("geometry", Polygon.class);
084: tb.attribute("stringProperty", String.class);
085: polygon = tb.feature();
086:
087: SimpleFeatureBuilder fb = new SimpleFeatureBuilder(
088: new SimpleFeatureFactoryImpl());
089: GeometryFactory gf = new GeometryFactory();
090:
091: points = new DefaultFeatureCollection(null, null) {
092: };
093:
094: fb.setType(point);
095: fb.add(gf.createPoint(new Coordinate(0, 0)));
096: fb.add(new Integer(0));
097: points.add(fb.build("fid.0"));
098:
099: fb.init();
100: fb.add(gf.createPoint(new Coordinate(1, 1)));
101: fb.add(new Integer(1));
102: points.add(fb.build("fid.1"));
103:
104: fb.init();
105: fb.add(gf.createPoint(new Coordinate(2, 2)));
106: fb.add(new Integer(2));
107: points.add(fb.build("fid.2"));
108:
109: lines = new DefaultFeatureCollection(null, null) {
110: };
111:
112: fb.setType(line);
113:
114: fb.init();
115: fb.add(gf.createLineString(new Coordinate[] {
116: new Coordinate(0, 0), new Coordinate(1, 1) }));
117: fb.add(new Double(0.0));
118: lines.add(fb.build("fid.0"));
119:
120: fb.init();
121: fb.add(gf.createLineString(new Coordinate[] {
122: new Coordinate(1, 1), new Coordinate(2, 2) }));
123: fb.add(new Double(1.1));
124: lines.add(fb.build("fid.1"));
125:
126: fb.init();
127: fb.add(gf.createLineString(new Coordinate[] {
128: new Coordinate(2, 2), new Coordinate(3, 3) }));
129: fb.add(new Double(2.2));
130: lines.add(fb.build("fid.2"));
131:
132: //create the tables
133: SQLBuilder sql = dataStore.createSQLBuilder();
134: Connection connection = dataSource.getConnection();
135: Statement st = connection.createStatement();
136: try {
137: st.execute(sql.dropTable(point));
138: } catch (SQLException e) {
139: }
140: st.execute(sql.createTable(point));
141:
142: try {
143: st.execute(sql.dropTable(line));
144: } catch (SQLException e) {
145: }
146: st.execute(sql.createTable(line));
147:
148: try {
149: st.execute(sql.dropTable(polygon));
150: } catch (SQLException e) {
151: }
152: st.execute(sql.createTable(polygon));
153: }
154:
155: protected void tearDown() throws Exception {
156: super .tearDown();
157: }
158:
159: /**
160: * Creates the data source.
161: */
162: protected abstract DataSource createDataSource();
163:
164: /**
165: * Creates an instance of the datastore.
166: */
167: protected abstract JDBCDataStore createDataStore();
168:
169: public void test() {
170:
171: }
172: }
|