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: * Created on May 11, 2005, 9:21 PM
017: */
018: package org.geotools.filter.function;
019:
020: import java.util.HashSet;
021:
022: import org.geotools.filter.Expression;
023: import org.geotools.filter.FilterFactory;
024: import org.geotools.filter.FilterFactoryFinder;
025: import org.geotools.filter.FunctionExpression;
026:
027: /**
028: * Do aggregate functions actually work?
029: *
030: * @author Cory Horner, Refractions Research
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/filter/function/Collection_FunctionsTest.java $
032: */
033: public class Collection_FunctionsTest extends FunctionTestSupport {
034:
035: /** Creates a new instance of Collection_MinFunctionTest */
036: public Collection_FunctionsTest(String testName) {
037: super (testName);
038: }
039:
040: public void testInstance() {
041: FunctionExpression cmin = FilterFactoryFinder
042: .createFilterFactory().createFunctionExpression(
043: "Collection_Min");
044: assertNotNull(cmin);
045: }
046:
047: public void testAverage() throws Exception {
048: performNumberTest("Collection_Average", new Double(33.375));
049: }
050:
051: //FunctionTestSupport doesn't have a geometry, so no bounds test for you :(
052: // public void testBounds() throws Exception {
053: // performNumberTest("Collection_Bounds", new Integer(4));
054: // }
055:
056: public void testCount() throws Exception {
057: performNumberTest("Collection_Count", new Integer(8));
058: }
059:
060: public void testMin() throws Exception {
061: performNumberTest("Collection_Min", new Integer(4));
062: }
063:
064: public void testMedian() throws Exception {
065: performNumberTest("Collection_Median", new Double(24.5));
066: }
067:
068: public void testMax() throws Exception {
069: performNumberTest("Collection_Max", new Integer(90));
070: }
071:
072: public void testSum() throws Exception {
073: performNumberTest("Collection_Sum", new Integer(267));
074: }
075:
076: public void testUnique() throws Exception {
077: HashSet result = new HashSet(8);
078: result.add(new Integer(90));
079: result.add(new Integer(4));
080: result.add(new Integer(8));
081: result.add(new Integer(43));
082: result.add(new Integer(61));
083: result.add(new Integer(20));
084: result.add(new Integer(29));
085: result.add(new Integer(12));
086: performObjectTest("Collection_Unique", result);
087: }
088:
089: /**
090: * Tests a function class of org.geotools.filter.function.Collection_*Function
091: *
092: * Example: performTest("Collection_Min", 4);
093: */
094: public void performNumberTest(String functionName,
095: Object expectedValue) throws Exception {
096: Expression exp = (Expression) builder.parse(dataType, "foo");
097: FunctionExpression func = fac
098: .createFunctionExpression(functionName);
099: func.setArgs(new Expression[] { exp });
100: Object obj = func.getValue(featureCollection);
101: Number result = (Number) obj;
102: Number expected = (Number) expectedValue;
103: assertEquals(expected.doubleValue(), result.doubleValue(), 0);
104: }
105:
106: public void performObjectTest(String functionName,
107: Object expectedValue) throws Exception {
108: Expression exp = (Expression) builder.parse(dataType, "foo");
109: FunctionExpression func = fac
110: .createFunctionExpression(functionName);
111: func.setArgs(new Expression[] { exp });
112: Object result = func.getValue(featureCollection);
113: assertEquals(expectedValue, result);
114: }
115:
116: }
|