/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.jdbc;
import java.util.Collections;
import org.geotools.data.FeatureStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.Id;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
public abstract class JDBCPrimaryKeyTest extends JDBCTestSupport {
@Override
protected abstract JDBCPrimaryKeyTestSetup createTestSetup();
@Override
protected void connect() throws Exception {
super.connect();
dataStore.setDatabaseSchema(null);
}
public void testAutoGeneratedPrimaryKey() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("auto"));
assertEquals( 1, fs.getPrimaryKey().getColumns().size() );
assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof AutoGeneratedPrimaryKeyColumn );
assertNull( fs.getSchema().getDescriptor(fs.getPrimaryKey().getColumns().get(0).getName()));
FeatureCollection features = fs.getFeatures();
assertPrimaryKeyValues(features, 3);
addFeature(fs.getSchema(),features);
assertPrimaryKeyValues(features,4);
}
public void testSequencedPrimaryKey() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("seq"));
assertEquals( 1, fs.getPrimaryKey().getColumns().size() );
assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof SequencedPrimaryKeyColumn );
FeatureCollection features = fs.getFeatures();
assertPrimaryKeyValues(features, 3);
addFeature(fs.getSchema(),features);
assertPrimaryKeyValues(features,4);
}
public void testNonIncrementingPrimaryKey() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("noninc"));
assertEquals( 1, fs.getPrimaryKey().getColumns().size() );
assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof NonIncrementingPrimaryKeyColumn );
FeatureCollection features = fs.getFeatures();
assertPrimaryKeyValues(features, 3);
addFeature(fs.getSchema(),features);
assertPrimaryKeyValues(features,4);
}
protected void addFeature( SimpleFeatureType featureType, FeatureCollection features ) throws Exception {
SimpleFeatureBuilder b = new SimpleFeatureBuilder( featureType );
b.add("four");
b.add( new GeometryFactory().createPoint( new Coordinate(4,4) ) );
SimpleFeature f = b.buildFeature(null);
features.add( f );
//pattern match to handle the multi primary key case
assertTrue(((String)f.getUserData().get( "fid" )).matches( tname(featureType.getTypeName()) + ".4(\\..*)?"));
}
protected void assertPrimaryKeyValues( FeatureCollection features, int count ) throws Exception {
FeatureIterator i = features.features();
for ( int j = 1; j <= count; j++ ) {
assertTrue( i.hasNext() );
SimpleFeature f = (SimpleFeature) i.next();
assertEquals( tname(features.getSchema().getName().getLocalPart()) + "." + j , f.getID() );
}
features.close( i );
}
public void testMultiColumnPrimaryKey() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("multi"));
assertEquals( 2, fs.getPrimaryKey().getColumns().size() );
FeatureCollection features = fs.getFeatures();
FeatureIterator i = features.features();
String[] xyz = new String[]{"x","y","z"};
for ( int j = 1; j <= 3; j++ ) {
assertTrue( i.hasNext() );
SimpleFeature f = (SimpleFeature) i.next();
assertEquals( tname("multi") + "." + j + "." + xyz[j-1], f.getID() );
}
features.close( i );
addFeature(fs.getSchema(),features);
i = features.features();
for ( int j = 0; j < 3; j++ ) {
i.hasNext();
i.next();
}
assertTrue( i.hasNext() );
SimpleFeature f = (SimpleFeature) i.next();
assertTrue( f.getID().startsWith( tname("multi") + ".4.") );
features.close( i );
//test with a filter
FilterFactory ff = dataStore.getFilterFactory();
Id id = ff.id( Collections.singleton( ff.featureId( tname("multi") + ".1.x") ) );
features = fs.getFeatures( id );
assertEquals( 1, features.size() );
}
public void testNullPrimaryKey() throws Exception {
JDBCFeatureSource fs = (JDBCFeatureSource) dataStore.getFeatureSource(tname("nokey"));
assertFalse( fs instanceof FeatureStore );
}
public void testUniqueIndex() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("uniq"));
assertEquals( 1, fs.getPrimaryKey().getColumns().size() );
assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof NonIncrementingPrimaryKeyColumn );
assertNull( fs.getSchema().getDescriptor(fs.getPrimaryKey().getColumns().get(0).getName()));
FeatureCollection features = fs.getFeatures();
assertPrimaryKeyValues(features, 3);
addFeature(fs.getSchema(),features);
assertPrimaryKeyValues(features,4);
}
public void testExposePrimaryKeyColumns() throws Exception {
JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("noninc"));
assertEquals( 2, fs.getSchema().getAttributeCount() );
fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("noninc"));
fs.setExposePrimaryKeyColumns(true);
assertEquals( 3, fs.getSchema().getAttributeCount() );
}
}