001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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.data.postgis.collection;
017:
018: import java.util.Map;
019:
020: import org.geotools.data.DataStoreFinder;
021: import org.geotools.data.DataTestCase;
022: import org.geotools.data.DefaultQuery;
023: import org.geotools.data.Query;
024: import org.geotools.data.jdbc.JDBCFeatureCollection;
025: import org.geotools.data.jdbc.JDBCFeatureSource;
026: import org.geotools.data.postgis.PostgisDataStore;
027: import org.geotools.data.postgis.PostgisTests;
028: import org.geotools.feature.FeatureType;
029: import org.geotools.feature.visitor.AverageVisitor;
030: import org.geotools.feature.visitor.CountVisitor;
031: import org.geotools.feature.visitor.MaxVisitor;
032: import org.geotools.feature.visitor.MinVisitor;
033: import org.geotools.feature.visitor.SumVisitor;
034: import org.geotools.feature.visitor.UniqueVisitor;
035: import org.geotools.filter.AttributeExpression;
036: import org.geotools.filter.Expression;
037: import org.geotools.filter.ExpressionBuilder;
038: import org.geotools.filter.FilterFactory;
039: import org.geotools.filter.FilterFactoryFinder;
040: import org.geotools.filter.FunctionExpression;
041: import org.geotools.filter.LiteralExpression;
042: import org.geotools.filter.MathExpression;
043:
044: /**
045: * This class tests PostgisFeatureCollection, with the inherited tests from
046: * JDBCFeatureCollection.
047: *
048: * @author Cory Horner, Refractions Research
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/postgis/src/test/java/org/geotools/data/postgis/collection/PostgisFeatureCollectionOnlineTest.java $
050: */
051: public class PostgisFeatureCollectionOnlineTest extends DataTestCase {
052: private PostgisDataStore dstore = null;
053: private JDBCFeatureCollection fc = null;
054: private JDBCFeatureCollection fc2 = null;
055: private FeatureType featureType = null;
056: private FeatureType featureType2 = null;
057: private FilterFactory ff = FilterFactoryFinder
058: .createFilterFactory();
059: AttributeExpression att = null;
060: AttributeExpression att2 = null;
061:
062: public PostgisFeatureCollectionOnlineTest(String test) {
063: super (test);
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068: Map params = PostgisTests
069: .getParams("demo-bc-fixture.properties");
070: dstore = (PostgisDataStore) DataStoreFinder
071: .getDataStore(params);
072:
073: featureType = dstore.getSchema("bc_voting_areas");
074:
075: JDBCFeatureSource source = new JDBCFeatureSource(dstore,
076: featureType);
077: Query query = new DefaultQuery(featureType.toString(), null);
078: fc = new PostgisFeatureCollection(source, query);
079: featureType2 = dstore.getSchema("bc_hospitals");
080:
081: JDBCFeatureSource source2 = new JDBCFeatureSource(dstore,
082: featureType2);
083: Query query2 = new DefaultQuery(featureType2.toString(), null);
084: fc2 = new PostgisFeatureCollection(source2, query2);
085: att = ff.createAttributeExpression("vregist");
086: att2 = ff.createAttributeExpression("authority");
087: }
088:
089: public void testSumCount() throws Exception {
090: SumVisitor sumVisitor = new SumVisitor(att);
091: fc.accepts(sumVisitor, null);
092: assertTrue(fc.isOptimized); //the postgis optimization was used
093: assertEquals(2209425, sumVisitor.getResult().toInt());
094:
095: //test count (we need this for the complex expression, so may as well test it here)
096: CountVisitor countVisitor = new CountVisitor();
097: fc.accepts(countVisitor, null);
098: assertEquals(7986, countVisitor.getResult().toInt());
099:
100: //test complex expression
101: LiteralExpression one = ff.createLiteralExpression(1);
102: MathExpression addExpr = ff
103: .createMathExpression(Expression.MATH_ADD);
104: addExpr.addLeftValue(one);
105: addExpr.addRightValue(att);
106:
107: SumVisitor sumVisitor2 = new SumVisitor(addExpr);
108: fc.accepts(sumVisitor2, null);
109: assertTrue(fc.isOptimized);
110: assertEquals(2217411, sumVisitor2.getResult().toInt());
111: }
112:
113: public void testMinMax() throws Exception {
114: MinVisitor minVisitor = new MinVisitor(att);
115: fc.accepts(minVisitor, null);
116: assertTrue(fc.isOptimized); //the postgis optimization was used
117: assertEquals(0, minVisitor.getResult().toInt());
118:
119: MaxVisitor maxVisitor = new MaxVisitor(att);
120: fc.accepts(maxVisitor, null);
121: assertTrue(fc.isOptimized); //the postgis optimization was used
122: assertEquals(1890, maxVisitor.getResult().toInt());
123: }
124:
125: // Optimization is currently unavailable for median!
126: // public void testMedian() throws Exception {
127: // AttributeExpression att = ff.createAttributeExpression(null, "vregist");
128: // MedianVisitor medianVisitor = new MedianVisitor(att);
129: // fc.accepts(medianVisitor);
130: // assertTrue(fc.isOptimized); //the postgis optimization was used
131: // assertEquals(67, medianVisitor.getResult().toDouble(), 0);
132: // }
133:
134: public void testAverage() throws Exception {
135: AverageVisitor averageVisitor = new AverageVisitor(att);
136: fc.accepts(averageVisitor, null);
137: assertTrue(fc.isOptimized); //the postgis optimization was used
138: assertEquals(276, averageVisitor.getResult().toInt());
139: }
140:
141: public void testUnique() throws Exception {
142: UniqueVisitor uniqueVisitor = new UniqueVisitor(att2);
143: fc2.accepts(uniqueVisitor, null);
144: assertTrue(fc2.isOptimized); //the postgis optimization was used
145: assertEquals(7, uniqueVisitor.getResult().toSet().size());
146: }
147:
148: public void testSumExpression() throws Exception {
149: ExpressionBuilder eb = new ExpressionBuilder();
150: FunctionExpression expr = (FunctionExpression) eb
151: .parser("Collection_Sum(vregist)");
152: int result = ((Number) expr.getValue(fc)).intValue();
153: assertTrue(fc.isOptimized);
154: assertEquals(result, 2209425);
155: }
156:
157: }
|