001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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;
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: package org.geotools.caching;
017:
018: import com.vividsolutions.jts.geom.Envelope;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.geotools.caching.impl.InMemoryFeatureCache;
025:
026: import org.geotools.data.DataUtilities;
027: import org.geotools.data.Query;
028: import org.geotools.data.memory.MemoryDataStore;
029:
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.feature.FeatureType;
033: import org.geotools.feature.IllegalAttributeException;
034: import org.geotools.feature.SchemaException;
035:
036: import org.geotools.filter.FilterFactoryImpl;
037: import org.geotools.filter.spatial.BBOXImpl;
038:
039: import org.opengis.filter.Filter;
040: import org.opengis.filter.FilterFactory;
041: import org.opengis.filter.expression.Expression;
042:
043: import java.io.IOException;
044:
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: public class InMemoryFeatureCacheTest extends TestCase {
049: protected InMemoryFeatureCache cache;
050: protected FeatureType type;
051: protected List data;
052:
053: protected List createDataSet(int numberOfData) {
054: //System.out.println("=== Creating Data Set");
055: Generator gen = new Generator(1000, 1000);
056: type = gen.getFeatureType();
057:
058: List ret = new ArrayList();
059:
060: for (int i = 0; i < numberOfData; i++) {
061: ret.add(gen.createFeature(i));
062: }
063:
064: return ret;
065: }
066:
067: public static Test suite() {
068: return new TestSuite(InMemoryFeatureCacheTest.class);
069: }
070:
071: /*public void setName(String name) {
072: super.setName("InMemoryFeatureCache Test") ;
073: }*/
074: protected void setUp() throws Exception {
075: super .setUp();
076: data = createDataSet(100);
077:
078: MemoryDataStore ds = new MemoryDataStore();
079: ds.createSchema(type);
080: ds.addFeatures(data);
081: cache = new InMemoryFeatureCache(ds, type, 150);
082: }
083:
084: public void testInvalidTypeException() throws SchemaException {
085: try {
086: FeatureType t = DataUtilities.createType("test.notype",
087: "id:0,*geom:Geometry,dummy:java.lang.String");
088: cache = new InMemoryFeatureCache(cache.getDataStore(), t,
089: 150);
090: } catch (FeatureCacheException e) {
091: return;
092: }
093:
094: fail("FeatureCacheException not thrown");
095: }
096:
097: public void testBasicReadOperations() throws FeatureCacheException {
098: Feature f = (Feature) data.get(0);
099: Feature c = cache.peek(f.getID());
100: assertNull(c);
101: c = cache.get(f.getID());
102: assertTrue(f.equals(c));
103: c = cache.peek(f.getID());
104: assertTrue(f.equals(c));
105: c = cache.get("noexist");
106: assertNull(c);
107: }
108:
109: public void testReadOperations() throws IOException,
110: FeatureCacheException {
111: FeatureCollection fc = cache.getFeatures();
112: assertEquals(data.size(), fc.size());
113: }
114:
115: public void testPut() throws IllegalAttributeException,
116: FeatureCacheException {
117: Feature f = DataUtilities.template(type, "put");
118: Feature c = cache.peek("put");
119: assertNull(c);
120: cache.put(f);
121: c = cache.peek("put");
122: assertTrue(f.equals(c));
123: }
124:
125: public void testPutAll() {
126: }
127:
128: public void testRemove() throws FeatureCacheException {
129: Feature f = (Feature) data.get(0);
130: Feature c = cache.get(f.getID());
131: c = cache.remove(f.getID());
132: assertTrue(f.equals(c));
133: c = cache.peek(f.getID());
134: assertNull(c);
135: c = cache.get(f.getID());
136: assertTrue(f.equals(c));
137: }
138:
139: public void testSplitFilter() throws IOException,
140: FeatureCacheException {
141: Feature f = (Feature) data.get(0);
142: FilterFactory ff = new FilterFactoryImpl();
143: Envelope env = f.getBounds();
144: Filter bb = ff.bbox(type.getDefaultGeometry().getName(), env
145: .getMinX(), env.getMinY(), env.getMaxX(),
146: env.getMaxY(), type.getDefaultGeometry().getName());
147: Filter globalbb = ff.bbox(type.getDefaultGeometry().getName(),
148: 0, env.getMinY(), env.getMinX()
149: + ((env.getMaxX() - env.getMinX()) / 2), env
150: .getMinY()
151: + ((env.getMaxY() - env.getMinY()) / 2), type
152: .getDefaultGeometry().getName());
153:
154: Filter attfilter = ff.like(ff.property("dummydata"), "Id: 1*");
155: Filter filter = ff.and(globalbb, attfilter);
156: cache.getFeatures(bb);
157:
158: Filter[] split = cache.splitFilter(filter);
159:
160: /*System.out.println(split[0]);
161: System.out.println(split[1]);
162: System.out.println(split[2]);*/
163: Filter newbb = ff.bbox(type.getDefaultGeometry().getName(), 0,
164: env.getMinY(), env.getMinX(), env.getMinY()
165: + ((env.getMaxY() - env.getMinY()) / 2), type
166: .getDefaultGeometry().getName());
167: assertEquals(split[0], globalbb);
168: assertEquals(split[1], newbb);
169: assertEquals(split[2], attfilter);
170: cache.getFeatures(filter);
171: split = cache.splitFilter(filter);
172: assertEquals(split[0], globalbb);
173: assertEquals(split[1], Filter.EXCLUDE);
174: assertEquals(split[2], attfilter);
175: }
176:
177: public void testCountAndBounds() throws IOException {
178: MemoryDataStore ds = new MemoryDataStore();
179: ds.createSchema(type);
180: ds.addFeatures(data);
181: assertEquals(data.size(), cache.getCount(Query.ALL));
182: assertEquals(ds.getFeatureSource(type.getTypeName())
183: .getBounds(), cache.getBounds());
184: assertEquals(ds.getFeatureSource(type.getTypeName()).getBounds(
185: Query.ALL), cache.getBounds(Query.ALL));
186: }
187:
188: public void testEviction() throws IOException {
189: FilterFactory ff = new FilterFactoryImpl();
190: Filter all = ff.bbox(type.getDefaultGeometry().getName(), 0, 0,
191: 1000, 1000, "srs");
192: FeatureCollection fc = cache.getFeatures(all);
193: assertEquals(data.size(), fc.size());
194: fc = cache.getFeatures(all);
195: assertEquals(data.size(), fc.size());
196: cache.evict();
197: fc = cache.getFeatures(all);
198: assertEquals(data.size(), fc.size());
199: cache.evict();
200: cache.evict();
201: fc = cache.getFeatures(all);
202: assertEquals(data.size(), fc.size());
203: }
204: }
|