001: package org.geotools.data.view;
002:
003: import java.io.IOException;
004:
005: import junit.framework.TestCase;
006:
007: import org.geotools.data.DataUtilities;
008: import org.geotools.data.DefaultQuery;
009: import org.geotools.data.FeatureSource;
010: import org.geotools.data.memory.MemoryDataStore;
011: import org.geotools.factory.CommonFactoryFinder;
012: import org.geotools.feature.Feature;
013: import org.geotools.feature.FeatureIterator;
014: import org.geotools.feature.FeatureType;
015: import org.geotools.feature.IllegalAttributeException;
016: import org.geotools.feature.SchemaException;
017: import org.geotools.filter.FilterFactoryFinder;
018: import org.geotools.filter.IllegalFilterException;
019: import org.opengis.filter.Filter;
020: import org.opengis.filter.FilterFactory;
021:
022: import com.vividsolutions.jts.geom.Coordinate;
023: import com.vividsolutions.jts.geom.GeometryFactory;
024:
025: public class DefaultViewTest extends TestCase {
026:
027: String typeName = "type1";
028: private MemoryDataStore ds;
029:
030: protected void setUp() throws Exception {
031: super .setUp();
032: FeatureType ft = DataUtilities.createType(typeName,
033: "geom:Point,name:String,id:int");
034: ds = new MemoryDataStore();
035: ds.addFeature(createFeatures(ft, 1));
036: ds.addFeature(createFeatures(ft, 2));
037: ds.addFeature(createFeatures(ft, 3));
038: ds.addFeature(createFeatures(ft, 4));
039: }
040:
041: private Feature createFeatures(FeatureType ft, int i)
042: throws IllegalAttributeException {
043: GeometryFactory fac = new GeometryFactory();
044: return ft.create(new Object[] {
045: fac.createPoint(new Coordinate(i, i)), "name" + i,
046: new Integer(i) });
047: }
048:
049: public void testGetFeatures() throws Exception {
050:
051: FeatureSource view = getView();
052:
053: FeatureIterator features = view.getFeatures().features();
054: int count = 0;
055: while (features.hasNext()) {
056: count++;
057: features.next();
058: }
059:
060: assertEquals(2, count);
061: }
062:
063: public void testGetFeaturesQuery() throws Exception {
064:
065: FeatureSource view = getView();
066:
067: FeatureIterator features = view.getFeatures(getQuery())
068: .features();
069: int count = 0;
070: while (features.hasNext()) {
071: count++;
072: features.next();
073: }
074:
075: assertEquals(1, count);
076: }
077:
078: public void testGetFeaturesFilter() throws Exception {
079:
080: FeatureSource view = getView();
081: Filter f = getFilter();
082: FeatureIterator features = view.getFeatures(f).features();
083: int count = 0;
084: while (features.hasNext()) {
085: count++;
086: features.next();
087: }
088:
089: assertEquals(1, count);
090: }
091:
092: public void testGetCount() throws Exception {
093: FeatureSource view = getView();
094:
095: DefaultQuery defaultQuery = getQuery();
096: int count = view.getCount(defaultQuery);
097: assertEquals(1, count);
098: }
099:
100: private DefaultQuery getQuery() throws IllegalFilterException {
101: Filter f = getFilter();
102: DefaultQuery defaultQuery = new DefaultQuery(typeName, f,
103: new String[0]);
104: return defaultQuery;
105: }
106:
107: private Filter getFilter() throws IllegalFilterException {
108: FilterFactory fac = CommonFactoryFinder.getFilterFactory(null);
109: Filter f = fac.equals(fac.property("name"), fac
110: .literal("name2"));
111: return f;
112: }
113:
114: private FeatureSource getView() throws IllegalFilterException,
115: IOException, SchemaException {
116: FilterFactory fac = CommonFactoryFinder.getFilterFactory(null);
117: Filter f = fac.less(fac.property("id"), fac.literal(3));
118:
119: FeatureSource view = ds.getView(new DefaultQuery(typeName, f));
120: return view;
121: }
122:
123: }
|