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; 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: package org.geotools.data.jdbc;
017:
018: import java.io.StringWriter;
019: import java.util.logging.Handler;
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import junit.framework.TestCase;
024:
025: import org.geotools.factory.CommonFactoryFinder;
026: import org.geotools.feature.AttributeTypeFactory;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.feature.FeatureTypeBuilder;
029: import org.geotools.filter.SQLEncoder;
030: import org.geotools.filter.SQLEncoderException;
031: import org.opengis.filter.Filter;
032: import org.opengis.filter.FilterFactory;
033: import org.opengis.filter.IncludeFilter;
034: import org.opengis.filter.PropertyIsEqualTo;
035: import org.opengis.filter.expression.Expression;
036:
037: /**
038: * Unit test for sql encoding of filters into where statements.
039: *
040: * @author Chris Holmes, TOPP
041: * @author Saul Farber, MassGIS
042: *
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/test/java/org/geotools/data/jdbc/FilterToSQLTest.java $
044: */
045: public class FilterToSQLTest extends TestCase {
046: private FilterFactory filterFac = CommonFactoryFinder
047: .getFilterFactory(null);
048: private static Logger LOGGER = org.geotools.util.logging.Logging
049: .getLogger("org.geotools.data.jdbc");
050:
051: private FeatureType integerFType;
052: private FeatureType stringFType;
053:
054: public FilterToSQLTest(String testName) throws Exception {
055: super (testName);
056:
057: Logger log = LOGGER;
058: Level debugLevel = Level.FINE;
059: while (log != null) {
060: log.setLevel(debugLevel);
061: for (int i = 0; i < log.getHandlers().length; i++) {
062: Handler h = log.getHandlers()[i];
063: h.setLevel(debugLevel);
064: }
065: log = log.getParent();
066: }
067:
068: FeatureTypeBuilder ftBuilder = FeatureTypeBuilder
069: .newInstance("testFeatureType");
070: ftBuilder.addType(AttributeTypeFactory.newAttributeType(
071: "testAttr", Integer.class));
072: integerFType = ftBuilder.getFeatureType();
073:
074: ftBuilder = FeatureTypeBuilder.newInstance("testFeatureType");
075: ftBuilder.addType(AttributeTypeFactory.newAttributeType(
076: "testAttr", String.class));
077: stringFType = ftBuilder.getFeatureType();
078:
079: }
080:
081: public void setUp() {
082: }
083:
084: public void testIntegerContext() throws Exception {
085:
086: Expression literal = filterFac.literal(5);
087: Expression prop = filterFac.property(integerFType
088: .getAttributeTypes()[0].getName());
089: PropertyIsEqualTo filter = filterFac.equals(prop, literal);
090:
091: StringWriter output = new StringWriter();
092: FilterToSQL encoder = new FilterToSQL(output);
093: encoder.setFeatureType(integerFType);
094: encoder.encode(filter);
095:
096: LOGGER.fine("testAttr is an Integer " + filter + " -> "
097: + output.getBuffer().toString());
098: assertEquals(output.getBuffer().toString(),
099: "WHERE testAttr = 5");
100: }
101:
102: public void testStringContext() throws Exception {
103:
104: Expression literal = filterFac.literal(5);
105: Expression prop = filterFac.property(stringFType
106: .getAttributeTypes()[0].getName());
107: PropertyIsEqualTo filter = filterFac.equals(prop, literal);
108:
109: StringWriter output = new StringWriter();
110: FilterToSQL encoder = new FilterToSQL(output);
111: encoder.setFeatureType(stringFType);
112: encoder.encode(filter);
113:
114: LOGGER.fine("testAttr is a String " + filter + " -> "
115: + output.getBuffer().toString());
116: assertEquals(output.getBuffer().toString(),
117: "WHERE testAttr = '5'");
118: }
119:
120: public void testInclude() throws Exception {
121: StringWriter output = new StringWriter();
122: FilterToSQL encoder = new FilterToSQL(output);
123: encoder.encode(Filter.INCLUDE);
124: assertEquals(output.getBuffer().toString(), "WHERE TRUE");
125: }
126:
127: public void testExclude() throws Exception {
128: StringWriter output = new StringWriter();
129: FilterToSQL encoder = new FilterToSQL(output);
130: encoder.encode(Filter.EXCLUDE);
131: assertEquals(output.getBuffer().toString(), "WHERE FALSE");
132: }
133:
134: public void testIdFilter() throws Exception {
135:
136: // SOMEONE NEEDS TO WRITE AN ID FILTER!
137: // SDE doesn't use the FIDMapper classes, so
138: // I'm not sure how a real-world encoder would want them encoded
139: }
140:
141: public void testEscapeQuote() throws FilterToSQLException {
142: org.opengis.filter.FilterFactory ff = CommonFactoryFinder
143: .getFilterFactory(null);
144: PropertyIsEqualTo equals = ff.equals(ff.property("attribute"),
145: ff.literal("A'A"));
146: StringWriter output = new StringWriter();
147: FilterToSQL encoder = new FilterToSQL(output);
148: encoder.encode(equals);
149: assertEquals("WHERE attribute = 'A''A'", output.toString());
150: }
151:
152: public void testEscapeQuoteFancy() throws FilterToSQLException {
153: org.opengis.filter.FilterFactory ff = CommonFactoryFinder
154: .getFilterFactory(null);
155: Object fancyLiteral = new Object() {
156:
157: public String toString() {
158: return "A'A";
159: }
160:
161: };
162: PropertyIsEqualTo equals = ff.equals(ff.property("attribute"),
163: ff.literal(fancyLiteral));
164: StringWriter output = new StringWriter();
165: FilterToSQL encoder = new FilterToSQL(output);
166: encoder.encode(equals);
167: assertEquals("WHERE attribute = 'A''A'", output.toString());
168: }
169: }
|