001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, 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: package org.geotools.data.mysql;
017:
018: import java.io.IOException;
019: import java.util.PropertyResourceBundle;
020: import java.util.logging.Logger;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.geotools.data.DataSourceException;
027: import org.geotools.data.jdbc.datasource.ManageableDataSource;
028:
029: import com.vividsolutions.jts.geom.Geometry;
030: import com.vividsolutions.jts.geom.GeometryFactory;
031: import com.vividsolutions.jts.io.ParseException;
032: import com.vividsolutions.jts.io.WKTReader;
033:
034: public class MysqlGeomColTestSuite extends TestCase {
035:
036: /** Standard logging instance */
037: private static final Logger LOGGER = org.geotools.util.logging.Logging
038: .getLogger("org.geotools.defaultcore");
039:
040: private MysqlGeomColumn gCol;
041:
042: private static final String TEST_CATALOG = "US";
043:
044: private static final String TEST_SCHEMA = "NY";
045:
046: private static final String TEST_F_TABLE = "STREET_LAMP";
047:
048: private static final String TEST_G_TABLE = "STREET_LAMP_LOC";
049:
050: private static final String TEST_COL_NAME = "GEOMETRY";
051:
052: private static final String TEST_WKT_GEOM = "POINT (4 7)";
053:
054: /** Factory for producing geometries (from JTS). */
055: private static GeometryFactory geometryFactory = new GeometryFactory();
056:
057: /** Well Known Text reader (from JTS). */
058: private static WKTReader geometryReader = new WKTReader(
059: geometryFactory);
060:
061: private Geometry testGeo;
062:
063: private ManageableDataSource pool;
064:
065: public MysqlGeomColTestSuite(String testName) {
066: super (testName);
067: }
068:
069: public static void main(String[] args) {
070: junit.textui.TestRunner.run(suite());
071: }
072:
073: public static Test suite() {
074: LOGGER.info("starting suite...");
075: TestSuite suite = new TestSuite(MysqlGeomColTestSuite.class);
076: LOGGER.info("made suite...");
077: return suite;
078: }
079:
080: public void setUp() throws IOException {
081: PropertyResourceBundle resource;
082: resource = new PropertyResourceBundle(this .getClass()
083: .getResourceAsStream("fixture.properties"));
084:
085: String namespace = resource.getString("namespace");
086: String host = resource.getString("host");
087: int port = Integer.parseInt(resource.getString("port"));
088: String database = resource.getString("database");
089:
090: String user = resource.getString("user");
091: String password = resource.getString("passwd");
092:
093: if (namespace.equals("http://www.geotools.org/data/postgis")) {
094: throw new IllegalStateException(
095: "The fixture.properties file needs to be configured for your own database");
096: }
097:
098: pool = MySQLDataStoreFactory.getDefaultDataSource(host, user,
099: password, port, database, 10, 2, false);
100:
101: LOGGER.info("creating MysqlConnection connection...");
102: //this will eventually be a world accessible mysql database
103: //for now it is just on localhost
104: LOGGER.info("created new db connection");
105: gCol = new MysqlGeomColumn();
106: geometryFactory = new GeometryFactory();
107: geometryReader = new WKTReader(geometryFactory);
108: try {
109: testGeo = geometryReader.read(TEST_WKT_GEOM);
110: } catch (ParseException e) {
111: LOGGER.info("Failed to parse " + e.getMessage());
112: }
113:
114: LOGGER.info("created new datasource");
115: }
116:
117: protected void tearDown() throws Exception {
118: pool.close();
119: }
120:
121: public void testFeatureSetters() {
122: gCol.setFeaTableCat(TEST_CATALOG);
123: assertEquals(gCol.getFeaTableCat(), TEST_CATALOG);
124: gCol.setFeaTableSchema(TEST_SCHEMA);
125: assertEquals(gCol.getFeaTableSchema(), TEST_SCHEMA);
126: gCol.setFeaTableName(TEST_F_TABLE);
127: assertEquals(gCol.getFeaTableName(), TEST_F_TABLE);
128: gCol.setGeomColName(TEST_COL_NAME);
129: assertEquals(gCol.getGeomColName(), TEST_COL_NAME);
130: }
131:
132: public void testGeomSetters() {
133: gCol.setGeomTableCat(TEST_CATALOG);
134: assertEquals(gCol.getGeomTableCat(), TEST_CATALOG);
135: gCol.setGeomTableSchema(TEST_SCHEMA);
136: assertEquals(gCol.getGeomTableSchema(), TEST_SCHEMA);
137: gCol.setGeomTableName(TEST_G_TABLE);
138: assertEquals(gCol.getGeomTableName(), TEST_G_TABLE);
139: }
140:
141: public void testTypeSetters() {
142: gCol.setStorageType(MysqlGeomColumn.WKB_STORAGE_TYPE);
143: assertEquals(gCol.getStorageType(),
144: MysqlGeomColumn.WKB_STORAGE_TYPE);
145: gCol.setGeomType(2);
146: assertEquals(gCol.getGeomType(), 2);
147: }
148:
149: public void testData() {
150: try {
151: testGeo = geometryReader.read(TEST_WKT_GEOM);
152: gCol.populateData(4, TEST_WKT_GEOM);
153: gCol.populateData(3, "POINT (24 53)");
154: assertTrue(gCol.getGeometry(4).equalsExact(testGeo));
155: assertNull(gCol.getGeometry(25));
156: } catch (ParseException e) {
157: LOGGER.info("Failed to parse " + e.getMessage());
158: } catch (DataSourceException e) {
159: LOGGER.info("caught datasource exception");
160: fail("datasource exception");
161: }
162: }
163:
164: public void testRemoveData() {
165: try {
166: gCol.populateData(15, TEST_WKT_GEOM);
167: gCol.removeData(15);
168: assertNull(gCol.getGeometry(15));
169: } catch (DataSourceException e) {
170: LOGGER.info("caught datasource exception");
171: fail("datasource exception");
172: }
173: }
174:
175: // public void testConnectionConstructor(){
176: //
177: //
178: // try{
179: // Connection dbCon = pool.getConnection();
180: // gCol = new MysqlGeomColumn(dbCon, TEST_F_TABLE);
181: // dbCon.close();
182: //
183: // } catch (SQLException e) {
184: // LOGGER.info("sql error " + e.getMessage());
185: // LOGGER.info("sql state: " + e.getSQLState());
186: // fail("sql error");
187: // } catch (SchemaException e) {
188: // LOGGER.info("schema error: " + e.getMessage());
189: // fail("schema error");
190: // }
191: //
192: // assertEquals(gCol.getFeaTableCat(), TEST_CATALOG);
193: // assertEquals(gCol.getFeaTableSchema(), TEST_SCHEMA);
194: // assertEquals(gCol.getFeaTableName(), TEST_F_TABLE);
195: // assertEquals(gCol.getGeomColName(), TEST_COL_NAME);
196: // assertEquals(gCol.getGeomTableCat(), TEST_CATALOG);
197: // assertEquals(gCol.getGeomTableSchema(), TEST_SCHEMA);
198: // assertEquals(gCol.getGeomTableName(), TEST_G_TABLE);
199: // assertEquals(gCol.getStorageType(), MysqlGeomColumn.WKB_STORAGE_TYPE);
200: // assertEquals(gCol.getGeomType(), 1);
201: // try {
202: // assertTrue(gCol.getGeometry(49).equalsExact(testGeo));
203: // } catch (DataSourceException e){
204: // LOGGER.info("caught datasource exception");
205: // fail("datasource exception");
206: // }
207: //
208: //
209: // }
210:
211: }
|