/* * 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.Feature; 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( final FeatureCollection features, int count ) throws Exception { assertFeatureIterator(1,count,features.features(),new SimpleFeatureAssertion() { public int toIndex(SimpleFeature feature) { return Integer.parseInt(feature.getID().split("\\.",2)[1]); } public void check(int index, SimpleFeature feature) { assertEquals( tname(features.getSchema().getName().getLocalPart()) + "." + index , feature.getID() ); } }); } public void testMultiColumnPrimaryKey() throws Exception { JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("multi")); assertEquals( 2, fs.getPrimaryKey().getColumns().size() ); FeatureCollection features = fs.getFeatures(); assertMultiPrimaryKeyValues(features,3); addFeature(fs.getSchema(),features); assertMultiPrimaryKeyValues(features,4); //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() ); } void assertMultiPrimaryKeyValues( final FeatureCollection features, int count ) throws Exception { assertFeatureIterator(1,count,features.features(),new SimpleFeatureAssertion() { String[] xyz = new String[]{"x","y","z"}; public int toIndex(SimpleFeature feature) { return Integer.parseInt(feature.getID().split("\\.")[1]); } public void check(int index, SimpleFeature feature) { if(index < 4) { assertEquals( tname("multi") + "." + index + "." + xyz[index-1], feature.getID() ); } else { assertTrue( feature.getID().startsWith( tname("multi") + ".4.") ); } } }); } 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() ); } }