001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.data;
017:
018: import java.io.IOException;
019: import java.util.Collections;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023:
024: import org.geotools.data.memory.MemoryDataStore;
025: import org.geotools.factory.CommonFactoryFinder;
026: import org.geotools.feature.Feature;
027: import org.geotools.feature.FeatureIterator;
028: import org.geotools.feature.FeatureType;
029: import org.geotools.feature.IllegalAttributeException;
030: import org.opengis.filter.Filter;
031: import org.opengis.filter.FilterFactory;
032:
033: import com.vividsolutions.jts.geom.Coordinate;
034: import com.vividsolutions.jts.geom.Geometry;
035: import com.vividsolutions.jts.geom.GeometryFactory;
036:
037: /**
038: *
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/data/TransactionTest.java $
040: */
041: public class TransactionTest extends TestCase {
042: MemoryDataStore ds;
043: FeatureType type;
044: Geometry geom;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: type = DataUtilities.createType("default",
049: "name:String,*geom:Geometry");
050: GeometryFactory fac = new GeometryFactory();
051: geom = fac.createPoint(new Coordinate(10, 10));
052: Feature f1 = type.create(new Object[] { "original", geom });
053: ds = new MemoryDataStore(new Feature[] { f1 });
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: public void testAddFeature() throws Exception {
061: Feature f1 = type.create(new Object[] { "one", geom });
062: Feature f2 = type.create(new Object[] { "two", geom });
063:
064: FeatureStore store = (FeatureStore) ds
065: .getFeatureSource("default");
066: store.setTransaction(new DefaultTransaction());
067: store.addFeatures(DataUtilities.collection(f1));
068: store.addFeatures(DataUtilities.collection(f2));
069:
070: count(store, 3);
071: // assertEquals("Number of known feature as obtained from getCount",3, store.getCount(Query.ALL));
072: }
073:
074: public void testRemoveFeature() throws Exception {
075: Feature f1 = type.create(new Object[] { "one", geom });
076:
077: FeatureStore store = (FeatureStore) ds
078: .getFeatureSource("default");
079: store.setTransaction(new DefaultTransaction());
080: Set fid = store.addFeatures(DataUtilities.collection(f1));
081:
082: count(store, 2);
083: String next = (String) fid.iterator().next();
084: FilterFactory filterFactory = CommonFactoryFinder
085: .getFilterFactory(null);
086: Filter f = filterFactory.id(Collections.singleton(filterFactory
087: .featureId(next)));
088: store.removeFeatures(f);
089:
090: count(store, 1);
091: // assertEquals("Number of known feature as obtained from getCount",3, store.getCount(Query.ALL));
092: }
093:
094: private void count(FeatureStore store, int expected)
095: throws IOException, IllegalAttributeException {
096: int i = 0;
097: for (FeatureIterator reader = store.getFeatures().features(); reader
098: .hasNext();) {
099: reader.next();
100: i++;
101: }
102: assertEquals("Number of known feature as obtained from reader",
103: expected, i);
104: }
105: }
|