001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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 July 18, 2003, 7:13 PM
017: */
018:
019: package org.geotools.filter;
020:
021: import org.geotools.filter.expression.AddImpl;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: *
029: * @author jamesm
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/filter/FilterVisitorTest.java $
031: */
032: public class FilterVisitorTest extends TestCase implements
033: FilterVisitor {
034: int checkcode;
035:
036: public FilterVisitorTest(String testName) {
037: super (testName);
038: }
039:
040: /**
041: * Main for test runner.
042: */
043: public static void main(String[] args) {
044: org.geotools.util.logging.Logging.GEOTOOLS
045: .forceMonolineConsoleOutput();
046: junit.textui.TestRunner.run(suite());
047: }
048:
049: /**
050: * Required suite builder.
051: * @return A test suite for this unit test.
052: */
053: public static Test suite() {
054:
055: TestSuite suite = new TestSuite(FilterVisitorTest.class);
056: return suite;
057: }
058:
059: /**
060: * This should never be called. This can only happen if a subclass of
061: * AbstractFilter failes to implement its own version of
062: * accept(FilterVisitor);
063: *
064: * @param filter The filter to visit
065: */
066: public void visit(Filter filter) {
067: fail("should never end up here, overloaded method should have been called instead");
068: }
069:
070: public void visit(BetweenFilter filter) {
071: DefaultExpression left = (DefaultExpression) filter
072: .getLeftValue();
073: DefaultExpression right = (DefaultExpression) filter
074: .getRightValue();
075: DefaultExpression mid = (DefaultExpression) filter
076: .getMiddleValue();
077: }
078:
079: public void visit(LikeFilter filter) {
080:
081: }
082:
083: /**
084: * Writes the SQL for the Logic Filter.
085: *
086: * @param filter the logic statement to be turned into SQL.
087: */
088: public void visit(LogicFilter filter) {
089:
090: java.util.Iterator list = filter.getFilterIterator();
091:
092: if (filter.getFilterType() == AbstractFilter.LOGIC_NOT) {
093:
094: Filters.accept((org.opengis.filter.Filter) list.next(),
095: this );
096:
097: } else { //AND or OR
098:
099: while (list.hasNext()) {
100: Filters.accept((org.opengis.filter.Filter) list.next(),
101: this );
102: }
103:
104: }
105:
106: }
107:
108: public void visit(CompareFilter filter) {
109:
110: DefaultExpression left = (DefaultExpression) filter
111: .getLeftValue();
112: DefaultExpression right = (DefaultExpression) filter
113: .getRightValue();
114:
115: left.accept(this );
116:
117: right.accept(this );
118:
119: }
120:
121: public void visit(GeometryFilter filter) {
122:
123: }
124:
125: public void visit(NullFilter filter) {
126:
127: DefaultExpression expr = (DefaultExpression) filter
128: .getNullCheckValue();
129:
130: expr.accept(this );
131:
132: }
133:
134: public void visit(FidFilter filter) {
135: checkcode = 9;
136: }
137:
138: public void visit(AttributeExpression expression) {
139:
140: }
141:
142: /**
143: * Writes the SQL for the attribute Expression.
144: *
145: * @param expression the attribute to turn to SQL.
146: */
147: public void visit(Expression expression) {
148:
149: }
150:
151: public void visit(LiteralExpression expression) {
152: checkcode += 1;
153: }
154:
155: public void visit(MathExpression expression) {
156: checkcode += 3;
157: ((DefaultExpression) expression.getLeftValue()).accept(this );
158: ((DefaultExpression) expression.getRightValue()).accept(this );
159: }
160:
161: public void visit(FunctionExpression expression) {
162: checkcode += 6;
163: }
164:
165: public void testVisitLiteral() {
166: checkcode = 0;
167: LiteralExpression exp = new LiteralExpressionImpl(4);
168: exp.accept(this );
169: assertEquals(checkcode, 1);
170:
171: }
172:
173: public void testVisitMathExpression() throws IllegalFilterException {
174: checkcode = 0;
175:
176: Expression testAttribute1 = new LiteralExpressionImpl(
177: new Integer(4));
178: Expression testAttribute2 = new LiteralExpressionImpl(
179: new Integer(2));
180:
181: // Test addition
182: MathExpressionImpl mathTest = new AddImpl(null, null);
183: mathTest.addLeftValue(testAttribute1);
184: mathTest.addRightValue(testAttribute2);
185:
186: mathTest.accept(this );
187:
188: assertEquals(5, checkcode);
189: }
190:
191: public void testVisitFidFilter() throws IllegalFilterException {
192: checkcode = 0;
193: FidFilter ff = new FidFilterImpl();
194: ff.accept(this );
195: assertEquals(9, checkcode);
196: }
197:
198: public void testFunctionExpression() {
199: checkcode = 0;
200:
201: FunctionExpression min = FilterFactoryFinder
202: .createFilterFactory().createFunctionExpression("min");
203: min.accept(this );
204: assertEquals(6, checkcode);
205: }
206:
207: }
|