features[i] = fb.buildFeature(i +"", new Object[]{new Integer(i), geoms[i]});
mm.add(features[i]);
}
DataStore ds = new MemoryDataStore(mm);
FeatureCache cache = new GridFeatureCache(ds.getFeatureSource(featureType.getTypeName()), 4, 4, MemoryStorage.createInstance());
FilterFactory ff = new FilterFactoryImpl();
Filter f1 = ff.bbox("the_geom", 0, 0, 4.4, 4.4, DefaultEngineeringCRS.CARTESIAN_2D.toString());
Envelope e1 = new Envelope(0, 4.4, 0, 4.4);
Filter f2 = ff.bbox("the_geom", 0, 4.6, 4.4, 8.5, DefaultEngineeringCRS.CARTESIAN_2D.toString());
Envelope e2 = new Envelope(0, 4.4, 4.6, 8.5);
Filter f3 = ff.bbox("the_geom", 4.6, 0, 8.5, 4.4, DefaultEngineeringCRS.CARTESIAN_2D.toString());
Envelope e3 = new Envelope(4.6, 8.5, 0, 4.4);
Filter f4 = ff.bbox("the_geom", 4.6, 4.6, 8.5, 8.5, DefaultEngineeringCRS.CARTESIAN_2D.toString());
Envelope e4 = new Envelope(4.6, 8.5, 4.6, 8.5);
//there should be two features in each region
FeatureCollection fc = cache.getFeatures(f1);
assertEquals(2, fc.size());
fc = cache.getFeatures(f2);
assertEquals(2, fc.size());
fc = cache.getFeatures(f3);
assertEquals(2, fc.size());
//at this point the cache should contain feature from area 2 & 3 and features from area 1
//should have been evicted.
assertEquals(0, ((GridFeatureCache)cache).peek(e4).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e1).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e2).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e3).size());
//lets test the last region
fc = cache.getFeatures(f4);
assertEquals(2, fc.size());
//at this point the cache should contain features from areas 3 & 4 and area 2 should
//have been evicted;
assertEquals(2, ((GridFeatureCache)cache).peek(e3).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e4).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e2).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e1).size());
//lets query region 4 again
fc = cache.getFeatures(f4);
assertEquals(2, fc.size());
//at this point the cache should contain features from areas 3 & 4 and area 2 should
//have been evicted;
assertEquals(2, ((GridFeatureCache)cache).peek(e3).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e4).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e2).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e1).size());
//now lets go back to 1
fc = cache.getFeatures(f1);
assertEquals(2, fc.size());
assertEquals(2, ((GridFeatureCache)cache).peek(e4).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e1).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e2).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e3).size());
//if we peek at 4
fc = ((GridFeatureCache)cache).peek(e4);
//now the cache should be order (least recently used to most recently used) 1, 4
//ask for 2
fc = cache.getFeatures(f2);
//cache: 4,2
assertEquals(2, ((GridFeatureCache)cache).peek(e4).size());
assertEquals(2, ((GridFeatureCache)cache).peek(e2).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e1).size());
assertEquals(0, ((GridFeatureCache)cache).peek(e3).size());