001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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: */
017: package org.geotools.data.geometryless.filter;
018:
019: import org.geotools.data.geometryless.filter.SQLEncoderLocationsXY;
020:
021: import java.util.logging.Logger;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.geotools.feature.AttributeType;
028: import org.geotools.feature.AttributeTypeFactory;
029: import org.geotools.feature.Feature;
030: import org.geotools.feature.FeatureType;
031: import org.geotools.feature.FeatureTypeFactory;
032: import org.opengis.filter.Filter;
033: import org.opengis.filter.FilterFactory;
034: import org.geotools.factory.CommonFactoryFinder;
035: import org.geotools.feature.IllegalAttributeException;
036: import org.geotools.feature.SchemaException;
037:
038: import com.vividsolutions.jts.geom.Coordinate; //import com.vividsolutions.jts.geom.Envelope;
039: import com.vividsolutions.jts.geom.GeometryFactory;
040: import com.vividsolutions.jts.geom.LineString;
041:
042: /**
043: * Unit test for SQLEncoderLocationsXY. This is a complementary test suite with
044: * the filter test suite.
045: *
046: * @author Chris Holmes, TOPP
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geometryless/src/test/java/org/geotools/data/geometryless/filter/SQLEncoderLocationsXYTest.java $
048: */
049: public class SQLEncoderLocationsXYTest extends TestCase {
050: /** Standard logging instance */
051: protected static final Logger LOGGER = org.geotools.util.logging.Logging
052: .getLogger("org.geotools.filter");
053: protected static AttributeTypeFactory attFactory = AttributeTypeFactory
054: .newInstance();
055:
056: // private FilterFactory filterFactory = FilterFactoryFinder.createFilterFactory();
057: private FilterFactory filterFactory = CommonFactoryFinder
058: .getFilterFactory(null);
059:
060: /** Schema on which to preform tests */
061: protected static FeatureType testSchema = null;
062:
063: /** Schema on which to preform tests */
064: protected static Feature testFeature = null;
065:
066: /** Test suite for this test case */
067: TestSuite suite = null;
068:
069: /** folder where test data is stored.. */
070: String dataFolder = "";
071: protected boolean setup = false;
072:
073: public SQLEncoderLocationsXYTest(String testName) {
074: super (testName);
075: System.out.println("running SQLEncoderTests");
076: ;
077: dataFolder = System.getProperty("dataFolder");
078:
079: if (dataFolder == null) {
080: //then we are being run by maven
081: dataFolder = System.getProperty("basedir");
082: dataFolder += "/tests/unit/testData";
083: }
084: }
085:
086: protected void setUp() throws SchemaException,
087: IllegalAttributeException {
088: if (setup) {
089: return;
090: } else {
091: prepareFeatures();
092: }
093:
094: setup = true;
095: }
096:
097: protected void prepareFeatures() throws SchemaException,
098: IllegalAttributeException {
099: //_log.getLoggerRepository().setThreshold(Level.INFO);
100: // Create the schema attributes
101: System.out.println("creating flat feature...");
102:
103: AttributeType geometryAttribute = AttributeTypeFactory
104: .newAttributeType("testGeometry", LineString.class);
105: System.out.println("created geometry attribute");
106:
107: AttributeType booleanAttribute = AttributeTypeFactory
108: .newAttributeType("testBoolean", Boolean.class);
109:
110: AttributeType charAttribute = AttributeTypeFactory
111: .newAttributeType("testCharacter", Character.class);
112: AttributeType byteAttribute = AttributeTypeFactory
113: .newAttributeType("testByte", Byte.class);
114: AttributeType shortAttribute = AttributeTypeFactory
115: .newAttributeType("testShort", Short.class);
116: AttributeType intAttribute = AttributeTypeFactory
117: .newAttributeType("testInteger", Integer.class);
118: AttributeType longAttribute = AttributeTypeFactory
119: .newAttributeType("testLong", Long.class);
120: AttributeType floatAttribute = AttributeTypeFactory
121: .newAttributeType("testFloat", Float.class);
122: AttributeType doubleAttribute = AttributeTypeFactory
123: .newAttributeType("testDouble", Double.class);
124: AttributeType stringAttribute = AttributeTypeFactory
125: .newAttributeType("testString", String.class);
126:
127: AttributeType[] types = { geometryAttribute, booleanAttribute,
128: charAttribute, byteAttribute, shortAttribute,
129: intAttribute, longAttribute, floatAttribute,
130: doubleAttribute, stringAttribute };
131:
132: // Builds the schema
133: testSchema = FeatureTypeFactory.newFeatureType(types,
134: "testSchema");
135:
136: GeometryFactory geomFac = new GeometryFactory();
137:
138: // Creates coordinates for the linestring
139: Coordinate[] coords = new Coordinate[3];
140: coords[0] = new Coordinate(1, 2);
141: coords[1] = new Coordinate(3, 4);
142: coords[2] = new Coordinate(5, 6);
143:
144: // Builds the test feature
145: Object[] attributes = new Object[10];
146: attributes[0] = geomFac.createLineString(coords);
147: attributes[1] = new Boolean(true);
148: attributes[2] = new Character('t');
149: attributes[3] = new Byte("10");
150: attributes[4] = new Short("101");
151: attributes[5] = new Integer(1002);
152: attributes[6] = new Long(10003);
153: attributes[7] = new Float(10000.4);
154: attributes[8] = new Double(100000.5);
155: attributes[9] = "test string data";
156:
157: // Creates the feature itself
158: testFeature = testSchema.create(attributes);
159: System.out.println("...flat feature created");
160:
161: //_log.getLoggerRepository().setThreshold(Level.DEBUG);
162: }
163:
164: /**
165: * Main for test runner.
166: *
167: * @param args DOCUMENT ME!
168: */
169: public static void main(String[] args) {
170: junit.textui.TestRunner.run(suite());
171: }
172:
173: /**
174: * Required suite builder.
175: *
176: * @return A test suite for this unit test.
177: */
178: public static Test suite() {
179: TestSuite suite = new TestSuite(SQLEncoderLocationsXYTest.class);
180:
181: return suite;
182: }
183:
184: public void test1() throws Exception {
185: // GeometryFilter gf = filterFactory.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
186:
187: Filter gf = filterFactory.bbox("testGeometry", 0, 300, 0, 300,
188: "EPSG:2356");
189:
190: // GeometryFilterImpl gf = new GeometryFilterImpl(AbstractFilter.GEOMETRY_BBOX);
191: /* LiteralExpressionImpl right = new BBoxExpressionImpl(new Envelope(0,
192: 300, 0, 300));
193: gf.addRightGeometry(right);
194:
195: AttributeExpressionImpl left = new AttributeExpressionImpl(testSchema,
196: "testGeometry");
197: gf.addLeftGeometry(left);
198: */
199: SQLEncoderLocationsXY encoder = new SQLEncoderLocationsXY(
200: "long", "lat");
201: encoder.setSRID(2356);
202:
203: String out = encoder.encodeToString(gf);
204: LOGGER.info("Resulting SQL filter is \n" + out);
205:
206: //assertEquals("WHERE \"testGeometry\" && GeometryFromText('POLYGON"
207: // + " ((0 0, 0 300, 300 300, 300 0, 0 0))'" + ", 2356)", out);
208: }
209:
210: public void test2() throws Exception {
211: // GeometryFilterImpl gf = new GeometryFilterImpl(AbstractFilter.GEOMETRY_BBOX);
212: Filter gf = filterFactory.bbox("testGeometry", 0, 300, 0, 300,
213: "EPSG:2356");
214:
215: /* GeometryFilter gf = filterFactory.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
216: LiteralExpressionImpl left = new BBoxExpressionImpl(new Envelope(10,
217: 300, 10, 300));
218: gf.addLeftGeometry(left);
219: */
220:
221: SQLEncoderLocationsXY encoder = new SQLEncoderLocationsXY(
222: "long", "lat");
223: encoder.setDefaultGeometry("testGeometry");
224:
225: String out = encoder.encodeToString(gf);
226: System.out.println("Resulting SQL filter is \n" + out);
227:
228: //assertEquals(out,
229: // "WHERE GeometryFromText("
230: // + "'POLYGON ((10 10, 10 300, 300 300, 300 10, 10 10))'"
231: // + ", 2346) && \"testGeometry\"");
232: }
233:
234: public void testException() throws Exception {
235: // GeometryFilterImpl gf = new GeometryFilterImpl(AbstractFilter.GEOMETRY_BEYOND);
236:
237: Filter gf = filterFactory.bbox("testGeometry", 0, 300, 0, 300,
238: "EPSG:2356");
239:
240: /* GeometryFilter gf = filterFactory.createGeometryFilter(AbstractFilter.GEOMETRY_BEYOND);
241:
242: LiteralExpressionImpl right = new BBoxExpressionImpl(new Envelope(10,
243: 10, 300, 300));
244: gf.addRightGeometry(right);
245:
246: AttributeExpressionImpl left = new AttributeExpressionImpl(testSchema,
247: "testGeometry");
248: gf.addLeftGeometry(left);
249: */
250: try {
251: SQLEncoderLocationsXY encoder = new SQLEncoderLocationsXY(
252: "long", "lat");
253: String out = encoder.encodeToString(gf);
254: System.out.println("out is " + out);
255: } catch (Exception e) {
256: System.out.println(e.getMessage());
257:
258: // assertEquals("Filter type not supported", e.getMessage());
259: }
260: }
261: }
|