001: package org.geotools.data.postgis;
002:
003: import java.sql.Statement;
004:
005: import org.geotools.data.DefaultQuery;
006: import org.geotools.data.FeatureReader;
007: import org.geotools.data.FeatureWriter;
008: import org.geotools.data.Query;
009: import org.geotools.data.Transaction;
010: import org.geotools.data.postgis.fidmapper.PostgisFIDMapperFactory;
011: import org.geotools.feature.Feature;
012: import org.geotools.feature.FeatureType;
013:
014: /**
015: * Hits a PostGIS database with a feature reader.
016: *
017: * @author Cory Horner, Refractions Research
018: */
019: public class PostgisFeatureReaderOnlineTest extends
020: AbstractPostgisOnlineTestCase {
021:
022: protected void createTables(Statement st) throws Exception {
023: createTable1(st);
024: createTable2(st);
025: createTable3(st);
026: //advance the sequence to larger values
027: st.execute("SELECT setval('" + table1
028: + "_fid_seq', 2000000000);");
029: st.execute("SELECT setval('" + table3
030: + "_fid_seq', 6000000000);");
031: //put some data in there
032: String[] keys = new String[] { "name", "the_geom" };
033: String[] values = new String[] { "'f1'",
034: "GeomFromText('POINT(1294523.17592358 469418.897140173)',4326)" };
035: addFeatureManual(table1, keys, values);
036: values[0] = "'f2'";
037: values[1] = "GeomFromText('POINT(1281485.7108 459444.7332)',4326)";
038: addFeatureManual(table2, keys, values);
039: values[0] = "'f3'";
040: values[1] = "GeomFromText('POINT(1271185.71084336 454376.774827237)',4326)";
041: addFeatureManual(table3, keys, values);
042: }
043:
044: protected void setupGeometryColumns(Statement st) throws Exception {
045: String preSql = "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, type) VALUES ('',";
046: String postSql = ", 'the_geom', 2, 4326, 'POINT')";
047: //table1: no entry
048: //table2: geometry_columns f_table_schema = ''
049: String sql = preSql + "'', '" + table2 + "'" + postSql;
050: st.execute(sql);
051: // table3: geometry_columns f_table_schema = 'public'
052: sql = preSql + "'public', '" + table3 + "'" + postSql;
053: st.execute(sql);
054: }
055:
056: /**
057: * Make sure that both large integer and long values are acceptable and valid.
058: * @throws Exception
059: */
060: public void testReadFid() throws Exception {
061: if (((PostgisFIDMapperFactory) ds.getFIDMapperFactory())
062: .isReturningTypedFIDMapper()) {
063: assertEquals(table1 + ".2000000001", attemptRead(table1)); //int is signed :(
064: assertEquals(table3 + ".6000000001", attemptRead(table3));
065: } else {
066: assertEquals("2000000001", attemptRead(table1)); //int is signed :(
067: assertEquals("6000000001", attemptRead(table3));
068: }
069: }
070:
071: /**
072: * Adds a feature so we have something to read.
073: */
074: protected boolean addFeature(String table) throws Exception {
075: FeatureWriter writer = ds.getFeatureWriter(table,
076: Transaction.AUTO_COMMIT);
077: Feature feature;
078:
079: while (writer.hasNext()) {
080: feature = (Feature) writer.next();
081: }
082:
083: feature = (Feature) writer.next();
084: feature.setAttribute(0, "test");
085: //feature.setAttribute(1, val);
086: writer.write();
087: String id = feature.getID();
088: return id != null;
089: }
090:
091: protected void addFeatureManual(String table, String[] keys,
092: String[] values) throws Exception {
093: Statement st = ds.getDataSource().getConnection()
094: .createStatement();
095: StringBuffer sql = new StringBuffer();
096: sql.append("INSERT INTO \"");
097: sql.append(table);
098: sql.append("\" (");
099: for (int i = 0; i < keys.length; i++) {
100: if (i > 0) {
101: sql.append(",");
102: }
103: sql.append(keys[i]);
104: }
105: sql.append(") VALUES (");
106: for (int i = 0; i < values.length; i++) {
107: if (i > 0) {
108: sql.append(",");
109: }
110: sql.append(values[i]);
111: }
112: sql.append(")");
113: st.execute(sql.toString());
114: st.close();
115: }
116:
117: protected String attemptRead(String table) throws Exception {
118: //addFeature(table);
119: Query query = new DefaultQuery(table);
120: FeatureReader fr = ds.getFeatureReader(query,
121: Transaction.AUTO_COMMIT);
122: assertTrue(fr.hasNext());
123: Feature feature = fr.next();
124: String id = feature.getID();
125: fr.close();
126: return id;
127: }
128:
129: public void testGetSchema() throws Exception {
130: //test that getSchema works when a entry does not exist in the geometry_columns table
131: FeatureType schema;
132: schema = ds.getSchema(table1);
133: assertNotNull(schema);
134: //test that getSchema works when geometry_columns f_table_schema = public
135: schema = ds.getSchema(table2);
136: assertNotNull(schema);
137: //test that getSchema works when geometry_columns f_table_schema = ''
138: schema = ds.getSchema(table3);
139: assertNotNull(schema);
140: }
141: }
|