001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005. All rights reserved.
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.db2;
018:
019: import java.io.IOException;
020: import java.sql.Connection;
021:
022: /**
023: * Exercise DB2SpatialCatalog.
024: *
025: * @author David Adler - IBM Corporation
026: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2SpatialCatalogTest.java $
027: */
028: public class DB2SpatialCatalogTest extends DB2TestCase {
029: private Connection conn;
030: private DB2SpatialCatalog catalog;
031:
032: /**
033: * Setup gets a database connection that will be live for the duration of
034: * all tests.
035: *
036: * @throws Exception
037: */
038: public void setUp() throws Exception {
039: super .setUp();
040: conn = getLocalConnection();
041: }
042:
043: /**
044: * Closes the database connection before we terminate.
045: *
046: * @throws Exception
047: */
048: protected void tearDown() throws Exception {
049: conn.close();
050: super .tearDown();
051: }
052:
053: /**
054: * Test various combinations of getting an instance of a DB2SpatialCatalog
055: *
056: * @throws Exception
057: */
058: public void testGetInstance() throws Exception {
059: DB2SpatialCatalog.reset(); // Force reset in case other tests may have set static variables
060:
061: // Test that an initial call with a null connection fails
062: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
063: null);
064: assertNull("Catalog not created with valid parameter supplied",
065: catalog);
066:
067: // Test that an initial call with a valid connection is successful
068: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
069: conn);
070: assertEquals("Catalog toString not expected", getDbURL() + "-"
071: + tabSchema, catalog.toString());
072:
073: // Test that an initial call with a null connection is successful
074: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
075: conn);
076: assertEquals("Catalog not found", getDbURL() + "-" + tabSchema,
077: catalog.toString());
078:
079: // Test that an initial call with a valid connection but undefined
080: // schema name is successful. Does this make sense?
081: String notFoundSchema = "WillNotBeFound";
082: catalog = DB2SpatialCatalog.getInstance(getDbURL(),
083: notFoundSchema, conn);
084: assertEquals(getDbURL() + "-" + notFoundSchema, catalog
085: .toString());
086: }
087:
088: public void testGetSRID() throws Exception {
089: // Get a catalog - this shouldn't fail
090: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
091: conn);
092:
093: int srid = catalog.getSRID(tabSchema, "Places", "Geom");
094: assertEquals(1, srid);
095:
096: try {
097: catalog.getSRID(tabSchema, "Places", "NotFound");
098: fail("getSRID should fail for undefined geometry");
099: } catch (IOException e) {
100: }
101:
102: try {
103: catalog.getSRID(tabSchema, "NotFound", "Geom");
104: fail("getSRID should fail for undefined table");
105: } catch (IOException e) {
106: }
107:
108: try {
109: catalog.getSRID("NotFound", "Places", "Geom");
110: fail("getSRID should fail for undefined table");
111: } catch (IOException e) {
112: }
113: }
114:
115: public void testGetGeomType() throws Exception {
116: // Get a catalog - this shouldn't fail
117: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
118: conn);
119:
120: String typeFound = catalog.getDB2GeometryTypeName(tabSchema,
121: "Places", "Geom");
122: assertEquals("ST_POLYGON", typeFound);
123:
124: String typeNotFound = "yes";
125:
126: try {
127: typeNotFound = catalog.getDB2GeometryTypeName(tabSchema,
128: "Places", "NotFound");
129: fail("getDB2GeometryTypeName should fail for undefined geometry");
130: } catch (IOException e) {
131: }
132:
133: try {
134: typeNotFound = catalog.getDB2GeometryTypeName(tabSchema,
135: "NotFound", "Geom");
136: fail("getDB2GeometryTypeName should fail for undefined table");
137: } catch (IOException e) {
138: }
139:
140: try {
141: typeNotFound = catalog.getDB2GeometryTypeName("NotFound",
142: "Places", "Geom");
143: fail("getDB2GeometryTypeName should fail for undefined table");
144: } catch (IOException e) {
145: }
146: }
147:
148: public void testGetTypes() throws Exception {
149: // Get a catalog - this shouldn't fail
150: catalog = DB2SpatialCatalog.getInstance(getDbURL(), tabSchema,
151: conn);
152:
153: int foundCount = 0;
154: String[] typeNames = catalog.getTypeNames();
155:
156: for (int i = 0; i < typeNames.length; i++) {
157: if (typeNames[i].equals("Places")) {
158: foundCount++;
159: }
160:
161: if (typeNames[i].equals("Roads")) {
162: foundCount++;
163: }
164: }
165:
166: assertTrue(foundCount == 2);
167: }
168: }
|