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 org.geotools.data.DataStoreFactorySpi.Param;
020: import java.io.IOException;
021: import java.util.Map;
022:
023: /**
024: * Exercise DB2DataStoreFactory.
025: *
026: * @author David Adler - IBM Corporation
027: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2DataStoreFactoryTest.java $
028: */
029: public class DB2DataStoreFactoryTest extends DB2TestCase {
030: DB2DataStoreFactory factory = new DB2DataStoreFactory();
031:
032: public void setUp() throws Exception {
033: super .setUp();
034: }
035:
036: protected void tearDown() throws Exception {
037: super .tearDown();
038: }
039:
040: public void testIsAvailable() {
041: assertTrue("isAvailable didn't return true", factory
042: .isAvailable());
043: }
044:
045: public void testCreateDataStore() {
046: // Should succeed
047: try {
048: DB2DataStore dataStore = (DB2DataStore) factory
049: .createDataStore(allParams);
050: } catch (IOException e) {
051: fail("createDataStore failed:" + e);
052: }
053:
054: // Should fail if dbtype is not "DB2"
055: try {
056: Map params = copyParams(allParams);
057: params.put("dbtype", "nodb");
058:
059: DB2DataStore dataStore = (DB2DataStore) factory
060: .createDataStore(params);
061: fail("createDataStore succeeded with invalid dbtype parameter");
062: } catch (IOException e) {
063: // We should come here as a result
064: }
065: // Should default schema to the user if schema is null
066: try {
067: Map params = copyParams(allParams);
068: params.put("tabschema", null);
069:
070: DB2DataStore dataStore = (DB2DataStore) factory
071: .createDataStore(params);
072: String schema = dataStore.getTableSchema();
073: assertEquals("DB2ADMIN", schema);
074: } catch (IOException e) {
075: fail("createDataStore failed:" + e);
076: }
077:
078: // Should default schema to the user if schema is blank
079: try {
080: Map params = copyParams(allParams);
081: params.put("tabschema", "");
082:
083: DB2DataStore dataStore = (DB2DataStore) factory
084: .createDataStore(params);
085: String schema = dataStore.getTableSchema();
086: assertEquals("DB2ADMIN", schema);
087: } catch (IOException e) {
088: fail("createDataStore failed:" + e);
089: }
090: // Should convert schema to uppercase
091: try {
092: Map params = copyParams(allParams);
093: params.put("tabschema", "sde");
094:
095: DB2DataStore dataStore = (DB2DataStore) factory
096: .createDataStore(params);
097: String schema = dataStore.getTableSchema();
098: assertEquals("SDE", schema);
099: } catch (IOException e) {
100: fail("createDataStore failed:" + e);
101: }
102: // Should leave schema in mixed case
103: try {
104: Map params = copyParams(allParams);
105: params.put("tabschema", "\"Test\"");
106:
107: DB2DataStore dataStore = (DB2DataStore) factory
108: .createDataStore(params);
109: String schema = dataStore.getTableSchema();
110: assertEquals("Test", schema);
111: } catch (IOException e) {
112: fail("createDataStore failed:" + e);
113: }
114: }
115:
116: public void testCreateNewDataStore() {
117: // Should fail
118: try {
119: DB2DataStore dataStore = (DB2DataStore) factory
120: .createNewDataStore(allParams);
121: fail("createNewDataStore didn't fail");
122: } catch (UnsupportedOperationException e) {
123: // We should come here as a result
124: }
125: }
126:
127: public void testGetDescription() {
128: assertEquals("DB2 Data Store", factory.getDescription());
129: }
130:
131: public void testGetDisplayName() {
132: assertEquals("DB2", factory.getDisplayName());
133: }
134:
135: public void testGetParametersInfo() {
136: Param[] params = factory.getParametersInfo();
137: int i = 0;
138:
139: try {
140: for (i = 0; i < params.length; i++) {
141: params[0].lookUp(allParams);
142: }
143: } catch (IOException e) {
144: // should never get here
145: fail("lookUp failed on " + params[i]);
146: }
147:
148: // test for missing parameter
149: Map paramMap = copyParams(allParams);
150: paramMap.remove("dbtype");
151:
152: try {
153: for (i = 0; i < params.length; i++) {
154: params[0].lookUp(paramMap);
155: }
156:
157: fail("Didn't fail on missing parameter dbtype");
158: } catch (IOException e) {
159: // should get here in successful case
160: }
161: }
162:
163: public void testCanProcess() {
164: Map params;
165:
166: // Make sure it succeeds for all good parameters
167: params = copyParams(allParams);
168: assertTrue("all parameters valid - should have succeeded",
169: factory.canProcess(params));
170:
171: // Should fail if "database" parameter is missing
172: params = copyParams(allParams);
173: params.remove("database");
174: assertFalse("database parameter is required", factory
175: .canProcess(params));
176:
177: // Should fail if "dbtype" parameter is not "db2"
178: params = copyParams(allParams);
179: params.put("dbtype", "nodb");
180: assertFalse("dbtype parameter is required", factory
181: .canProcess(params));
182:
183: // Should succeed if "dbname" parameter is not lowercase
184: params = copyParams(allParams);
185: params.put("dbtype", "DB2");
186: assertTrue("should succeed with uppercase DB2 as dbtype",
187: factory.canProcess(params));
188:
189: // Should fail if "host" parameter is missing
190: params = copyParams(allParams);
191: params.remove("host");
192: assertFalse("host parameter is required", factory
193: .canProcess(params));
194:
195: // Should fail if "port" parameter is missing
196: params = copyParams(allParams);
197: params.remove("port");
198: assertFalse("port parameter is required", factory
199: .canProcess(params));
200:
201: // Should succeed if "user" parameter is missing - not sure if it should be mandatory
202: params = copyParams(allParams);
203: params.remove("user");
204: assertTrue("user parameter should be optional", factory
205: .canProcess(params));
206:
207: // Should succeed if "passwd" parameter is missing - not sure if it should be mandatory
208: params = copyParams(allParams);
209: params.remove("passwd");
210: assertTrue("passwd parameter should be optional", factory
211: .canProcess(params));
212:
213: // Should fail if "user" parameter is missing - not sure if it should be mandatory
214: params = copyParams(allParams);
215: params.remove("tabschema");
216: assertTrue("tabschema parameter should be optional", factory
217: .canProcess(params));
218: }
219: }
|