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.SchemaNotFoundException;
020: import org.geotools.data.jdbc.fidmapper.BasicFIDMapper;
021: import org.geotools.data.jdbc.fidmapper.FIDMapper;
022: import org.geotools.data.jdbc.fidmapper.FIDMapperFactory;
023: import org.geotools.data.jdbc.fidmapper.TypedFIDMapper;
024: import java.io.IOException;
025: import java.sql.Connection;
026: import java.sql.SQLException;
027:
028: /**
029: * Exercise DB2FIDMapperFactory.
030: *
031: * @author David Adler - IBM Corporation
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/test/java/org/geotools/data/db2/DB2FIDMapperFactoryTest.java $
033: */
034: public class DB2FIDMapperFactoryTest extends DB2TestCase {
035: public void testMappers() throws Exception {
036: String catalog = null;
037: String schema = "Test";
038: String tableName = null;
039: Connection conn = null;
040: FIDMapper fm;
041: String wrapperDesc = null;
042: FIDMapperFactory fmFact = new DB2FIDMapperFactory("Test");
043:
044: DB2DataStore ds = getDataStore();
045: FIDMapper fm1 = ds.getFIDMapper("FIDVCHARPRIKEY");
046: FIDMapper fm2 = new TypedFIDMapper(new BasicFIDMapper("IDCOL",
047: 12), "FIDVCHARPRIKEY");
048: fm1.equals(fm2);
049: // assertEquals(new TypedFIDMapper(new BasicFIDMapper("IDCOL", 12), "FIDVCHARPRIKEY"), ds.getFIDMapper("FIDVCHARPRIKEY"));
050:
051: conn = getLocalConnection();
052: tableName = "FIDAUTOINC";
053: fm = fmFact.getMapper(catalog, schema, tableName, conn);
054: wrapperDesc = fm.toString();
055: fm2 = ds.getFIDMapper(tableName);
056: assertEquals(
057: tableName,
058: wrapperDesc,
059: "Wrapped:class org.geotools.data.db2.DB2AutoIncrementFIDMapper:1:IDCOL:4:0:0:false:true:");
060:
061: conn = getLocalConnection();
062: tableName = "FIDCHARPRIKEY";
063: fm = fmFact.getMapper(catalog, schema, tableName, conn);
064: wrapperDesc = fm.toString();
065: assertEquals(
066: tableName,
067: wrapperDesc,
068: "Wrapped:class org.geotools.data.jdbc.fidmapper.BasicFIDMapper:1:IDCOL:12:15:0:true:false:");
069:
070: conn = getLocalConnection();
071: tableName = "FIDNOPRIKEY";
072: fm = fmFact.getMapper(catalog, schema, tableName, conn);
073: wrapperDesc = fm.toString();
074: assertEquals(tableName, wrapperDesc,
075: "Wrapped:class org.geotools.data.db2.DB2NullFIDMapper:0::false:false:");
076:
077: conn = getLocalConnection();
078: tableName = "FIDINTPRIKEY";
079: fm = fmFact.getMapper(catalog, schema, tableName, conn);
080: wrapperDesc = fm.toString();
081: assertEquals(
082: tableName,
083: wrapperDesc,
084: "Wrapped:class org.geotools.data.jdbc.fidmapper.MaxIncFIDMapper:1:IDCOL:4:0:0:true:false:");
085:
086: conn = getLocalConnection();
087: tableName = "FIDVCHARPRIKEY";
088: fm = fmFact.getMapper(catalog, schema, tableName, conn);
089: wrapperDesc = fm.toString();
090: assertEquals(
091: tableName,
092: wrapperDesc,
093: "Wrapped:class org.geotools.data.jdbc.fidmapper.BasicFIDMapper:1:IDCOL:12:17:0:true:false:");
094:
095: conn = getLocalConnection();
096: tableName = "FIDMCOLPRIKEY";
097: fm = fmFact.getMapper(catalog, schema, tableName, conn);
098: wrapperDesc = fm.toString();
099: assertEquals(
100: tableName,
101: wrapperDesc,
102: "Wrapped:class org.geotools.data.jdbc.fidmapper.MultiColumnFIDMapper:2:IDCOL1:1:11:0:true:false:");
103:
104: // Don't know why, but DefaultFIDFactory.getPkColumnInfo closes the connection
105: conn = getLocalConnection();
106:
107: try {
108: tableName = "NoTable";
109: fm = fmFact.getMapper(catalog, schema, tableName, conn);
110: fail("Didn't get exception on invalid tableName");
111: } catch (SchemaNotFoundException e) {
112: assertEquals("Unexpected exception", e.getMessage(),
113: "Feature type could not be found for NoTable");
114: } catch (IOException e) {
115: fail("Unexpected exception: " + e);
116: }
117:
118: try {
119: conn.close();
120: } catch (SQLException e1) {
121: e1.printStackTrace();
122: }
123: }
124:
125: String toString(FIDMapper fm) {
126: String mapperName = ((TypedFIDMapper) fm).getWrappedMapper()
127: .getClass().toString();
128: String colInfo = "";
129:
130: if (fm.getColumnCount() > 0) {
131: colInfo = fm.getColumnName(0) + ":" + fm.getColumnType(0)
132: + ":" + fm.getColumnSize(0) + ":"
133: + fm.getColumnDecimalDigits(0);
134: }
135:
136: String s = mapperName + ":" + fm.getColumnCount() + ":"
137: + colInfo + ":" + fm.returnFIDColumnsAsAttributes()
138: + ":" + fm.hasAutoIncrementColumns() + ":" + "";
139:
140: return s;
141: }
142: }
|