001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.geometryless;
018:
019: import java.io.IOException;
020: import java.nio.charset.Charset;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.PropertyResourceBundle;
024:
025: import junit.framework.TestCase;
026:
027: import org.geotools.data.DataSourceException;
028: import org.geotools.data.DataStore;
029: import org.geotools.data.DataStoreFactorySpi;
030: import org.geotools.data.FeatureSource;
031: import org.geotools.data.DataStoreFactorySpi.Param;
032: import org.geotools.feature.Feature;
033: import org.geotools.feature.FeatureCollection;
034: import org.geotools.feature.FeatureIterator;
035: import org.geotools.feature.FeatureType;
036: import org.geotools.feature.GeometryAttributeType;
037:
038: import com.vividsolutions.jts.geom.Point;
039:
040: /**
041: * Test Params used by JDBCDataStoreFactory.
042: *
043: * @auther Rob Atkinson, Social Change Online
044: * @author jgarnett, Refractions Research, Inc.
045: * @author $Author: aaime $ (last modification)
046: * @source $URL:
047: * http://svn.geotools.org/geotools/trunk/gt/modules/unsupported/geometryless/src/test/java/org/geotools/data/geometryless/JDBCDataStoreFactoryTest.java $
048: * @version $Id: JDBCDataStoreFactoryTest.java 17707 2006-01-23 03:45:14Z
049: * desruisseaux $
050: */
051: public class JDBCDataStoreFactoryTest extends TestCase {
052: static JDBCDataStoreFactory factory = new JDBCDataStoreFactory(null);
053:
054: Map local;
055:
056: /*
057: * @see TestCase#setUp()
058: */
059: protected void setUp() throws Exception {
060:
061: local = new HashMap();
062: local.put("dbtype", "jdbc");
063:
064: PropertyResourceBundle resource = new PropertyResourceBundle(
065: this .getClass().getResourceAsStream(
066: "fixture.properties"));
067:
068: String namespace = resource.getString("namespace");
069:
070: String user = resource.getString("user");
071: local.put("user", user);
072: String password = resource.getString("password");
073: local.put("passwd", password);
074:
075: String Driver = resource.getString("driver");
076: local.put("driver", Driver);
077:
078: String urlprefix = resource.getString("urlprefix");
079: local.put("urlprefix", urlprefix);
080:
081: if (namespace.equals("http://www.geotools.org/data/postgis")) {
082: throw new IllegalStateException(
083: "The fixture.properties file needs to be configured for your own database");
084: }
085:
086: super .setUp();
087:
088: }
089:
090: public void testParamCHARSET() throws Throwable {
091: Param p = JDBCDataStoreFactory.CHARSET;
092: try {
093: p.parse(null);
094: fail("expected error for parse null");
095: } catch (Exception e) {
096: }
097:
098: try {
099: p.parse("");
100: fail("expected error for parse empty");
101: } catch (Exception e) {
102: }
103: assertNotNull("parse ISO-8859-1", p.parse("ISO-8859-1"));
104:
105: assertNull("handle null", p.handle(null));
106: assertNull("handle empty", p.handle(""));
107: assertNotNull("handle ISO-8859-1", p.handle("ISO-8859-1"));
108:
109: Map map = new HashMap();
110: Charset latin1 = Charset.forName("ISO-8859-1");
111: map.put("charset", latin1);
112: assertEquals(latin1, p.lookUp(map));
113:
114: try {
115: assertNotNull("handle ISO-LATIN-1", p.handle("ISO-LATIN-1"));
116: } catch (IOException expected) {
117: }
118: System.out.println(latin1.toString());
119: System.out.println(latin1.name());
120: System.out.println(p.text(latin1));
121: assertEquals("ISO-8859-1", p.text(latin1));
122: try {
123: assertEquals("ISO-8859-1", p.text("ISO-8859-1"));
124: fail("Should not handle bare text");
125: } catch (ClassCastException expected) {
126: }
127: }
128:
129: public void testLocal() throws Exception {
130: Map map = local;
131: System.out.println("local:" + map);
132:
133: assertTrue("canProcess", factory.canProcess(map));
134: try {
135: DataStore temp = factory.createDataStore(map);
136: assertNotNull("created", temp);
137: } catch (DataSourceException expected) {
138: assertEquals("Could not get connection", expected
139: .getMessage());
140: }
141: }
142:
143: public void testRegisterViewsJDBCDS() throws Exception {
144: testRegisterViews(factory);
145: }
146:
147: public void testRegisterViewsLocationXY() throws Exception {
148: Map params = new HashMap(this .local);
149:
150: DataStoreFactorySpi factory = new LocationsXYDataStoreFactory();
151: params.put(LocationsXYDataStoreFactory.DBTYPE.key,
152: "locationsxy");
153: params.put(LocationsXYDataStoreFactory.XCOLUMN.key, "x");
154: params.put(LocationsXYDataStoreFactory.YCOLUMN.key, "y");
155: params
156: .put(LocationsXYDataStoreFactory.GEOMNAME.key,
157: "location");
158:
159: assertTrue(factory.canProcess(params));
160:
161: this .local = params;
162: DataStore ds = testRegisterViews(factory);
163: assertTrue(ds instanceof LocationsXYDataStore);
164: FeatureSource fs = ds.getFeatureSource("ViewType1");
165: assertNotNull(fs);
166: FeatureType schema = fs.getSchema();
167: assertNotNull(schema);
168: GeometryAttributeType defaultGeometry = (GeometryAttributeType) schema
169: .getDefaultGeometry();
170: assertNotNull("No default geometry: " + schema.toString(),
171: defaultGeometry);
172: assertEquals("location", defaultGeometry.getLocalName());
173:
174: FeatureCollection features = fs.getFeatures();
175: assertNotNull(features);
176: FeatureIterator iterator = features.features();
177: while (iterator.hasNext()) {
178: Feature next = iterator.next();
179: assertNotNull(next);
180: Object location = next.getAttribute("location");
181: assertNotNull(location);
182: assertTrue(location instanceof Point);
183: }
184: features.close(iterator);
185: }
186:
187: /*
188: * public void testRegisterViewsLocationXYWaterQ()throws Exception{ Map
189: * params = new HashMap(this.local);
190: *
191: * DataStoreFactorySpi factory = new LocationsXYDataStoreFactory();
192: * params.put(LocationsXYDataStoreFactory.DBTYPE.key, "locationsxy");
193: * params.put(LocationsXYDataStoreFactory.XCOLUMN.key, "x");
194: * params.put(LocationsXYDataStoreFactory.YCOLUMN.key, "y");
195: * params.put(LocationsXYDataStoreFactory.GEOMNAME.key, "location");
196: *
197: * params.put("sqlView.1.typeName", "wq_derived");
198: * params.put("sqlView.1.sqlQuery", "SELECT station_no, station_name as
199: * sitename,anzlic_no, " + " project_no, sample_collection_date ||
200: * determinand_code as id," + " sample_collection_date,
201: * determinand_description, results_value," + " longitude as x, -latitude as
202: * y FROM wq_ir_results WHERE 1=1" + " order by station_no");
203: *
204: * DataStore ds = DataStoreFinder.getDataStore(params); assertNotNull(ds);
205: * assertTrue(ds instanceof LocationsXYDataStore);
206: *
207: * FeatureSource fs = ds.getFeatureSource("wq_derived"); assertNotNull(fs);
208: * FeatureType schema = fs.getSchema(); assertNotNull(schema);
209: * assertNotNull(schema.toString(), schema.getDefaultGeometry());
210: * assertEquals("location",
211: * schema.getDefaultGeometry().getName().getLocalPart());
212: *
213: * FeatureCollection fc = fs.getFeatures(Query.ALL); assertNotNull(fc);
214: * Iterator features = fc.features(); assertNotNull(features);
215: * assertTrue(features.hasNext()); Feature f =(Feature) features.next();
216: *
217: * Filter filter = (Filter)ExpressionBuilder.parse("station_no =
218: * '41010901'"); features = fs.getFeatures(filter).features();
219: * assertNotNull(features); assertTrue(features.hasNext()); f =
220: * (Feature)features.next(); assertEquals("41010901",
221: * f.getAttribute("station_no"));
222: *
223: * Envelope bounds = fs.getBounds(); assertNotNull(bounds);
224: *
225: * bounds = fs.getBounds(new DefaultQuery("wq_derived", filter));
226: * assertNotNull(bounds); }
227: */
228:
229: private DataStore testRegisterViews(DataStoreFactorySpi dsFactory)
230: throws IOException {
231: String typeName1 = "ViewType1";
232: String typeName2 = "ViewType2";
233: String sqlDef1 = "select area, perimeter, gid, x, y from testset order by gid";
234: String sqlDef2 = "select gid, x, y from testset";
235:
236: Map params = new HashMap(this .local);
237: params.put("sqlView.1.typeName", typeName1);
238:
239: try {
240: dsFactory.createDataStore(params);
241: fail("should have failed, sql def not provided");
242: } catch (IllegalArgumentException e) {
243: // ok
244: }
245:
246: params.put("sqlView.1.sqlQuery", sqlDef1);
247:
248: params.put("sqlView.2.typeName", typeName2);
249: params.put("sqlView.2.sqlQuery", sqlDef2);
250:
251: DataStore dstore = dsFactory.createDataStore(params);
252:
253: FeatureType ft1 = dstore.getSchema(typeName1);
254: FeatureType ft2 = dstore.getSchema(typeName2);
255: assertNotNull(ft1);
256: assertNotNull(ft2);
257:
258: return dstore;
259: }
260:
261: }
|