Source Code Cross Referenced for CQLTest.java in  » GIS » GeoTools-2.4.1 » org » geotools » filter » text » cql2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.filter.text.cql2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *    GeoTools - OpenSource mapping toolkit
0003:         *    http://geotools.org
0004:         *    (C) 2002-2006, GeoTools Project Managment Committee (PMC)
0005:         *
0006:         *    This library is free software; you can redistribute it and/or
0007:         *    modify it under the terms of the GNU Lesser General Public
0008:         *    License as published by the Free Software Foundation;
0009:         *    version 2.1 of the License.
0010:         *
0011:         *    This library is distributed in the hope that it will be useful,
0012:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014:         *    Lesser General Public License for more details.
0015:         */
0016:        package org.geotools.filter.text.cql2;
0017:
0018:        import junit.framework.TestCase;
0019:
0020:        import org.opengis.filter.Filter;
0021:        import org.opengis.filter.PropertyIsBetween;
0022:        import org.opengis.filter.PropertyIsEqualTo;
0023:        import org.opengis.filter.PropertyIsLessThan;
0024:        import org.opengis.filter.PropertyIsLike;
0025:        import org.opengis.filter.expression.Add;
0026:        import org.opengis.filter.expression.Expression;
0027:        import org.opengis.filter.expression.Literal;
0028:        import org.opengis.filter.expression.PropertyName;
0029:        import org.opengis.filter.spatial.BBOX;
0030:        import org.opengis.filter.spatial.Beyond;
0031:        import org.opengis.filter.spatial.BinarySpatialOperator;
0032:        import org.opengis.filter.spatial.Contains;
0033:        import org.opengis.filter.spatial.Crosses;
0034:        import org.opengis.filter.spatial.DWithin;
0035:        import org.opengis.filter.spatial.Disjoint;
0036:        import org.opengis.filter.spatial.DistanceBufferOperator;
0037:        import org.opengis.filter.spatial.Equals;
0038:        import org.opengis.filter.spatial.Intersects;
0039:        import org.opengis.filter.spatial.Overlaps;
0040:        import org.opengis.filter.spatial.Touches;
0041:        import org.opengis.filter.spatial.Within;
0042:
0043:        import com.vividsolutions.jts.geom.Point;
0044:
0045:        /**
0046:         * CQL Test
0047:         * 
0048:         * <p>
0049:         * Test Common CQL language
0050:         * </p>
0051:         *
0052:         * @author Mauricio Pazos (Axios Engineering)
0053:         * @since 2.4 
0054:         */
0055:        public class CQLTest extends TestCase {
0056:
0057:            public CQLTest(String testName) {
0058:                super (testName);
0059:            }
0060:
0061:            /**
0062:             * Test Comparison Predicate
0063:             * <p>
0064:             *
0065:             * <pre>
0066:             * &lt;comparison predicate &gt; ::=
0067:             *      &lt;attribute name &gt;  &lt;comp op &gt;  &lt;literal &gt;
0068:             * </pre>
0069:             *
0070:             * </p>
0071:             */
0072:            public void testComparationPredicate() throws Exception {
0073:                Filter expected;
0074:                Filter actual;
0075:                // attr1 < 5
0076:                expected = FilterSample
0077:                        .getSample(FilterSample.LESS_FILTER_SAMPLE);
0078:
0079:                actual = CQL.toFilter(FilterSample.LESS_FILTER_SAMPLE);
0080:
0081:                assertNotNull("expects filter not null", actual);
0082:
0083:                assertEquals("less than compare filter error", expected, actual);
0084:
0085:                // attr1 <= 5
0086:                expected = FilterSample
0087:                        .getSample(FilterSample.LESS_EQ_FILTER_SAMPLE);
0088:
0089:                actual = CQL.toFilter(FilterSample.LESS_EQ_FILTER_SAMPLE);
0090:
0091:                assertNotNull("expects filter not null", actual);
0092:
0093:                assertEquals("less or equal compare filter error", expected,
0094:                        actual);
0095:
0096:                // attr <> 5
0097:                expected = FilterSample.getSample(FilterSample.NOT_EQ_FILTER);
0098:
0099:                actual = CQL.toFilter(FilterSample.NOT_EQ_FILTER);
0100:
0101:                assertNotNull("expects filter not null", actual);
0102:
0103:                assertEquals("not equal compare filter error", expected, actual);
0104:
0105:                // "gmd:aa:bb.gmd:cc.gmd:dd"
0106:                final String prop = "gmd:aa:bb.gmd:cc.gmd:dd";
0107:                final String propExpected = "gmd:aa:bb/gmd:cc/gmd:dd";
0108:                actual = CQL.toFilter(prop + " < 100");
0109:
0110:                assertTrue("PropertyIsLessThan filter was expected",
0111:                        actual instanceof  PropertyIsLessThan);
0112:
0113:                PropertyIsLessThan lessFilter = (PropertyIsLessThan) actual;
0114:                Expression property = lessFilter.getExpression1();
0115:
0116:                assertEquals(propExpected, property.toString());
0117:            }
0118:
0119:            /**
0120:             * Test Comparation Predicate.
0121:             * <p>
0122:             *
0123:             * <pre>
0124:             * &lt;comparison predicate &gt; ::=
0125:             *      &lt;attrsibute name &gt;  &lt;comp op &gt;  &lt;literal &gt;
0126:             * </pre>
0127:             *
0128:             * </p>
0129:             */
0130:            public void testComparationPredicateWithSimpleExpressions()
0131:                    throws Exception {
0132:                Filter expected;
0133:                Filter actual;
0134:
0135:                expected = FilterSample
0136:                        .getSample(FilterSample.FILTER_SIMPLE_EXPR);
0137:                actual = CQL.toFilter(FilterSample.FILTER_SIMPLE_EXPR);
0138:
0139:                assertNotNull("expects filter not null", actual);
0140:
0141:                assertEquals("not equal compare filter error", expected, actual);
0142:            }
0143:
0144:            /**
0145:             * Test Existnce Predicate.
0146:             * <p>
0147:             * EXIST: evaluates as true for all record instances where the
0148:             * attribute_name is a member of the record schema. DOES-NOT-EXIST: opposite
0149:             * to EXISTS
0150:             * </p>
0151:             * <p>
0152:             *
0153:             * <pre>
0154:             *  &lt;existence_predicate &gt; ::=
0155:             *          &lt;attribute_name &gt; EXISTS
0156:             *      |   &lt;attribute_name &gt; DOES-NOT-EXIST
0157:             * </pre>
0158:             *
0159:             * </p>
0160:             */
0161:            public void testExistencePredicate() throws Exception {
0162:                Filter resultFilter;
0163:                Filter expected;
0164:                PropertyIsEqualTo eqToResultFilter;
0165:
0166:                // -------------------------------------------------------------
0167:                // <attribute_name> DOES-NOT-EXIST
0168:                // -------------------------------------------------------------
0169:                resultFilter = CQL
0170:                        .toFilter(FilterSample.ATTRIBUTE_NAME_DOES_NOT_EXIST);
0171:
0172:                assertNotNull("Filter expected", resultFilter);
0173:
0174:                expected = FilterSample
0175:                        .getSample(FilterSample.ATTRIBUTE_NAME_DOES_NOT_EXIST);
0176:
0177:                assertEquals(expected, resultFilter);
0178:
0179:                // -------------------------------------------------------------
0180:                // <attribute_name> EXISTS
0181:                // TODO Exist function must be implemented in Geotools
0182:                // -------------------------------------------------------------
0183:                resultFilter = CQL.toFilter(FilterSample.ATTRIBUTE_NAME_EXISTS);
0184:
0185:                assertNotNull("Filter expected", resultFilter);
0186:
0187:                assertTrue(resultFilter instanceof  PropertyIsEqualTo);
0188:
0189:                eqToResultFilter = (PropertyIsEqualTo) resultFilter;
0190:
0191:                expected = FilterSample
0192:                        .getSample(FilterSample.ATTRIBUTE_NAME_EXISTS);
0193:
0194:                assertEquals(expected, eqToResultFilter);
0195:
0196:                assertNotNull("implementation of function was expected",
0197:                        eqToResultFilter.getExpression1());
0198:
0199:            }
0200:
0201:            /**
0202:             * Test Null Predicate:
0203:             * <p>
0204:             *
0205:             * <pre>
0206:             * &lt;null predicate &gt; ::=  &lt;attribute name &gt; IS [ NOT ] NULL
0207:             * </pre>
0208:             *
0209:             * </p>
0210:             */
0211:            public void testNullPredicate() throws Exception {
0212:                Filter expected;
0213:                Filter resultFilter;
0214:                // -------------------------------------------------------------
0215:                // ATTR1 IS NULL
0216:                // -------------------------------------------------------------
0217:                expected = FilterSample
0218:                        .getSample(FilterSample.PROPERTY_IS_NULL);
0219:                resultFilter = CQL.toFilter(FilterSample.PROPERTY_IS_NULL);
0220:
0221:                assertNotNull("Filter expected", resultFilter);
0222:
0223:                assertEquals("PropertyIsNull filter was expected",
0224:                        resultFilter, expected);
0225:
0226:                // -------------------------------------------------------------
0227:                // ATTR1 IS NOT NULL
0228:                // -------------------------------------------------------------
0229:                expected = FilterSample
0230:                        .getSample(FilterSample.PROPERTY_IS_NOT_NULL);
0231:                resultFilter = CQL.toFilter(FilterSample.PROPERTY_IS_NOT_NULL);
0232:
0233:                assertNotNull("Filter expected", resultFilter);
0234:
0235:                assertEquals("Not PropertyIsNull filter was expected",
0236:                        resultFilter, expected);
0237:            }
0238:
0239:            public void testParenRoundtripExpression() throws Exception {
0240:                // ATTR1 > ((1 + 2) / 3)
0241:                testEqualsExpressions(FilterSample.FILTER_WITH_PAREN_ROUNDTRIP_EXPR);
0242:
0243:                // "ATTR1 < (1 + ((2 / 3) * 4))"
0244:                testEqualsExpressions(FilterSample.FILTER_WITH_NESTED_PAREN_EXPR);
0245:            }
0246:
0247:            public void testBracketRoundtripFilter() throws Exception {
0248:                // ATTR1 > [[1 + 2] / 3]
0249:                testEqualsExpressions(FilterSample.FILTER_WITH_BRACKET_ROUNDTRIP_EXPR);
0250:
0251:                // TODO more test
0252:                // roundtripFilter("[[[ 3 < 4 ] AND NOT [ 2 < 4 ]] AND [ 5 < 4 ]]");
0253:                // roundtripFilter("[3<4 AND 2<4 ] OR 5<4");
0254:                // roundtripFilter("3<4 && 2<4");
0255:            }
0256:
0257:            /**
0258:             * Test temporal predicate. This test <b>BEFORE</b> rule [*]
0259:             * <p>
0260:             *
0261:             * <pre>
0262:             * &lt;temporal predicate  &gt;::=
0263:             *      &lt;attribute_name &gt; &lt;b&gt;BEFORE&lt;/b&gt;  &lt;date-time expression &gt; [*]
0264:             *  |   &lt;attribute_name &gt; BEFORE OR DURING  &lt;period &gt;
0265:             *  |   &lt;attribute_name &gt; DURING  &lt;period &gt;
0266:             *  |   &lt;attribute_name &gt; DURING OR AFTER  &lt;period &gt;
0267:             *  |   &lt;attribute_name &gt; AFTER  &lt;date-time expression &gt;
0268:             * &lt;date-time expression &gt; ::=  &lt;date-time &gt; |  &lt;period &gt;[*]
0269:             * &lt;period &gt; ::=
0270:             *      &lt;date-time &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0271:             *  |   &lt;date-time &gt; &quot;/&quot;  &lt;duration &gt; [*]
0272:             *  |   &lt;duration &gt; &quot;/&quot;  &lt;date-time &gt; [*]
0273:             * </pre>
0274:             *
0275:             * </p>
0276:             */
0277:            public void testTemporalPredicateBefore() throws Exception {
0278:                Filter resultFilter;
0279:                Filter expected;
0280:                // -------------------------------------------------------------
0281:                // <attribute_name> BEFORE <date-time expression>
0282:                // -------------------------------------------------------------
0283:                // ATTR1 BEFORE 2006-12-31T01:30:00Z
0284:                resultFilter = CQL.toFilter(FilterSample.FILTER_BEFORE_DATE);
0285:
0286:                assertNotNull("not null expected", resultFilter);
0287:
0288:                expected = FilterSample
0289:                        .getSample(FilterSample.FILTER_BEFORE_DATE);
0290:                assertEquals("less filter ", expected, resultFilter);
0291:
0292:                // ATTR1 BEFORE 2006-11-31T01:30:00Z/2006-12-31T01:30:00Z
0293:                resultFilter = CQL
0294:                        .toFilter(FilterSample.FILTER_BEFORE_PERIOD_BETWEEN_DATES);
0295:
0296:                assertNotNull("Filter expected", resultFilter);
0297:
0298:                expected = FilterSample
0299:                        .getSample(FilterSample.FILTER_BEFORE_PERIOD_BETWEEN_DATES);
0300:
0301:                assertEquals("less than first date of period ", expected,
0302:                        resultFilter);
0303:
0304:                // ATTR1 BEFORE 2006-11-31T01:30:00Z/P30D
0305:                resultFilter = CQL
0306:                        .toFilter(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_DAYS);
0307:
0308:                assertNotNull("Filter expected", resultFilter);
0309:
0310:                expected = FilterSample
0311:                        .getSample(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_DAYS);
0312:
0313:                assertEquals("less than first date of period ", expected,
0314:                        resultFilter);
0315:
0316:                // "ATTR1 BEFORE 2006-11-31T01:30:00Z/P1Y"
0317:                resultFilter = CQL
0318:                        .toFilter(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_YEARS);
0319:
0320:                assertNotNull("Filter expected", resultFilter);
0321:
0322:                expected = FilterSample
0323:                        .getSample(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_YEARS);
0324:
0325:                assertEquals("less than first date of period ", expected,
0326:                        resultFilter);
0327:
0328:                // "ATTR1 BEFORE 2006-11-31T01:30:00Z/P12M"
0329:                resultFilter = CQL
0330:                        .toFilter(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_MONTHS);
0331:
0332:                assertNotNull("Filter expected", resultFilter);
0333:
0334:                expected = FilterSample
0335:                        .getSample(FilterSample.FILTER_BEFORE_PERIOD_DATE_AND_MONTHS);
0336:
0337:                assertEquals("less than first date of period ", expected,
0338:                        resultFilter);
0339:
0340:                // ATTR1 BEFORE P10Y10M10DT5H5M5S/2006-11-30T01:30:00Z
0341:                resultFilter = CQL
0342:                        .toFilter(FilterSample.FILTER_BEFORE_PERIOD_YMD_HMS_DATE);
0343:
0344:                assertNotNull("Filter expected", resultFilter);
0345:
0346:                expected = FilterSample
0347:                        .getSample(FilterSample.FILTER_BEFORE_PERIOD_YMD_HMS_DATE);
0348:
0349:                assertEquals("greater filter", expected, resultFilter);
0350:
0351:                // test compound attribute gmd:aa:bb.gmd:cc.gmd:dd
0352:                final String prop = "gmd:aa:bb.gmd:cc.gmd:dd";
0353:                final String propExpected = "gmd:aa:bb/gmd:cc/gmd:dd";
0354:                resultFilter = CQL.toFilter(prop
0355:                        + " BEFORE P10Y10M10DT5H5M5S/2006-11-30T01:30:00Z ");
0356:
0357:                assertTrue("PropertyIsLessThan filter was expected",
0358:                        resultFilter instanceof  PropertyIsLessThan);
0359:
0360:                PropertyIsLessThan lessFilter = (PropertyIsLessThan) resultFilter;
0361:                Expression property = lessFilter.getExpression1();
0362:
0363:                assertEquals(propExpected, property.toString());
0364:            }
0365:
0366:            /**
0367:             * Test temporal predicate. This tests <b>BEFORE or DURING</b> rule[*]
0368:             * <p>
0369:             *
0370:             * <pre>
0371:             *  &lt;temporal predicate  &gt;::=
0372:             *          &lt;attribute_name &gt; BEFORE  &lt;date-time expression &gt;
0373:             *      |   &lt;b&gt; &lt;attribute_name &gt; BEFORE OR DURING  &lt;period &gt;[*]&lt;/b&gt;
0374:             *      |   &lt;attribute_name &gt; DURING  &lt;period &gt;
0375:             *      |   &lt;attribute_name &gt; DURING OR AFTER  &lt;period &gt;
0376:             *      |   &lt;attribute_name &gt; AFTER  &lt;date-time expression &gt;
0377:             *  &lt;date-time expression &gt; ::=  &lt;date-time &gt; |  &lt;period &gt;
0378:             *  &lt;period &gt; ::=
0379:             *          &lt;date-time &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0380:             *      |   &lt;date-time &gt; &quot;/&quot;  &lt;duration &gt;[*]
0381:             *      |   &lt;duration &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0382:             * </pre>
0383:             *
0384:             * </p>
0385:             */
0386:            public void testTemporalPredicateBeforeOrDuring() throws Exception {
0387:                Filter resultFilter;
0388:                Filter expected;
0389:                // -------------------------------------------------------------
0390:                // <attribute_name> BEFORE OR DURING <period>
0391:                // -------------------------------------------------------------
0392:                // ATTR1 BEFORE OR DURING 2006-11-31T01:30:00Z/2006-12-31T01:30:00Z
0393:                resultFilter = CQL
0394:                        .toFilter(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_BETWEEN_DATES);
0395:
0396:                assertNotNull("Filter expected", resultFilter);
0397:
0398:                expected = FilterSample
0399:                        .getSample(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_BETWEEN_DATES);
0400:
0401:                assertEquals("less than or equal the last date of period ",
0402:                        expected, resultFilter);
0403:
0404:                // ATTR1 BEFORE OR DURING P10Y10M10DT5H5M5S/2006-11-30T01:30:00Z
0405:                resultFilter = CQL
0406:                        .toFilter(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_YMD_HMS_DATE);
0407:
0408:                assertNotNull("Filter expected", resultFilter);
0409:
0410:                expected = FilterSample
0411:                        .getSample(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_YMD_HMS_DATE);
0412:
0413:                assertEquals(" filter", expected, resultFilter);
0414:
0415:                // ATTR1 BEFORE OR DURING 2006-11-30T01:30:00Z/P10Y10M10DT5H5M5S
0416:                resultFilter = CQL
0417:                        .toFilter(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_DATE_YMD_HMS);
0418:
0419:                assertNotNull("Filter expected", resultFilter);
0420:
0421:                expected = FilterSample
0422:                        .getSample(FilterSample.FILTER_BEFORE_OR_DURING_PERIOD_DATE_YMD_HMS);
0423:
0424:                assertEquals(" filter", expected, resultFilter);
0425:            }
0426:
0427:            /**
0428:             * Test temporal predicate. This tests <b>DURING OR AFTER</b> rule[*]
0429:             * <p>
0430:             *
0431:             * <pre>
0432:             *  &lt;temporal predicate  &gt;::=
0433:             *          &lt;attribute_name &gt; BEFORE  &lt;date-time expression &gt;
0434:             *      |   &lt;b&gt; &lt;attribute_name &gt; BEFORE OR DURING  &lt;period &gt;&lt;/b&gt;
0435:             *      |   &lt;attribute_name &gt; DURING  &lt;period &gt;
0436:             *      |   &lt;attribute_name &gt; DURING OR AFTER  &lt;period &gt;[*]
0437:             *      |   &lt;attribute_name &gt; AFTER  &lt;date-time expression &gt;
0438:             *  &lt;date-time expression &gt; ::=  &lt;date-time &gt; |  &lt;period &gt;
0439:             *  &lt;period &gt; ::=
0440:             *          &lt;date-time &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0441:             *      |   &lt;date-time &gt; &quot;/&quot;  &lt;duration &gt;[*]
0442:             *      |   &lt;duration &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0443:             * </pre>
0444:             *
0445:             * </p>
0446:             */
0447:            public void testTemporalPredicateDuringOrAfter() throws Exception {
0448:                Filter resultFilter;
0449:                Filter expected;
0450:                // -------------------------------------------------------------
0451:                // <attribute_name> BEFORE OR DURING <period>
0452:                // -------------------------------------------------------------
0453:                // ATTR1 DURING OF AFTER 2006-11-31T01:30:00Z/2006-12-31T01:30:00Z
0454:                resultFilter = CQL
0455:                        .toFilter(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_BETWEEN_DATES);
0456:
0457:                assertNotNull("Filter expected", resultFilter);
0458:
0459:                expected = FilterSample
0460:                        .getSample(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_BETWEEN_DATES);
0461:
0462:                assertEquals("greater than or equal the first date of period ",
0463:                        expected, resultFilter);
0464:
0465:                // ATTR1 DURING OR AFTER P10Y10M10DT5H5M5S/2006-11-30T01:30:00Z
0466:                resultFilter = CQL
0467:                        .toFilter(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_YMD_HMS_DATE);
0468:
0469:                assertNotNull("Filter expected", resultFilter);
0470:
0471:                expected = FilterSample
0472:                        .getSample(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_YMD_HMS_DATE);
0473:
0474:                assertEquals(
0475:                        "greater than or equal the first date (is calculated subtract period to last date) of period",
0476:                        expected, resultFilter);
0477:
0478:                // ATTR1 DURING OR AFTER 2006-11-30T01:30:00Z/P10Y10M10DT5H5M5S
0479:                resultFilter = CQL
0480:                        .toFilter(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_DATE_YMD_HMS);
0481:
0482:                assertNotNull("Filter expected", resultFilter);
0483:
0484:                expected = FilterSample
0485:                        .getSample(FilterSample.FILTER_DURING_OR_AFTER_PERIOD_DATE_YMD_HMS);
0486:
0487:                assertEquals("greater than or equal the first date", expected,
0488:                        resultFilter);
0489:            }
0490:
0491:            /**
0492:             * Test temporal predicate. This tests <b>DURING</b> rule[*]
0493:             * <p>
0494:             *
0495:             * <pre>
0496:             *  &lt;temporal predicate  &gt;::=
0497:             *          &lt;attribute_name &gt; BEFORE  &lt;date-time expression &gt;
0498:             *      |   &lt;b&gt; &lt;attribute_name &gt; BEFORE OR DURING  &lt;period &gt;&lt;/b&gt;
0499:             *      |  &lt;attribute_name &gt; DURING  &lt;period &gt;[*]
0500:             *      |  &lt;attribute_name &gt; DURING OR AFTER  &lt;period &gt;
0501:             *      |  &lt;attribute_name &gt; AFTER  &lt;date-time expression &gt;
0502:             *  &lt;date-time expression &gt; ::=  &lt;date-time &gt; |  &lt;period &gt;
0503:             *  &lt;period &gt; ::=
0504:             *          &lt;date-time &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0505:             *      |   &lt;date-time &gt; &quot;/&quot;  &lt;duration &gt;[*]
0506:             *      |   &lt;duration &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0507:             * </pre>
0508:             *
0509:             * </p>
0510:             */
0511:            public void testTemporalPredicateDuring() throws Exception {
0512:                Filter resultFilter;
0513:                Filter expected;
0514:
0515:                // ATTR1 DURING 2006-11-30T01:30:00Z/2006-12-31T01:30:00Z
0516:                resultFilter = CQL
0517:                        .toFilter(FilterSample.FILTER_DURING_PERIOD_BETWEEN_DATES);
0518:
0519:                assertNotNull("Filter expected", resultFilter);
0520:
0521:                expected = FilterSample
0522:                        .getSample(FilterSample.FILTER_DURING_PERIOD_BETWEEN_DATES);
0523:
0524:                assertEquals("greater filter ", expected, resultFilter);
0525:
0526:                // ATTR1 DURING 2006-11-30T01:30:00Z/P10Y10M10DT5H5M5S
0527:                resultFilter = CQL
0528:                        .toFilter(FilterSample.FILTER_DURING_PERIOD_DATE_YMD_HMS);
0529:
0530:                assertNotNull("Filter expected", resultFilter);
0531:
0532:                expected = FilterSample
0533:                        .getSample(FilterSample.FILTER_DURING_PERIOD_DATE_YMD_HMS);
0534:
0535:                assertEquals("greater filter", expected, resultFilter);
0536:
0537:                // ATTR1 DURING P10Y10M10DT5H5M5S/2006-11-30T01:30:00Z
0538:                resultFilter = CQL
0539:                        .toFilter(FilterSample.FILTER_DURING_PERIOD_YMD_HMS_DATE);
0540:
0541:                assertNotNull("Filter expected", resultFilter);
0542:
0543:                expected = FilterSample
0544:                        .getSample(FilterSample.FILTER_DURING_PERIOD_YMD_HMS_DATE);
0545:
0546:                assertEquals("greater filter", expected, resultFilter);
0547:            }
0548:
0549:            /**
0550:             * Test temporal predicate. This tests <B>AFTER</B> or during rule[*]
0551:             * <p>
0552:             *
0553:             * <pre>
0554:             * &lt;temporal predicate  &gt;::=
0555:             *          &lt;attribute_name &gt; BEFORE  &lt;date-time expression &gt;
0556:             *      |   &lt;attribute_name &gt; BEFORE OR DURING  &lt;period &gt;
0557:             *      |   &lt;attribute_name &gt; DURING  &lt;period &gt;
0558:             *      |   &lt;attribute_name &gt; DURING OR AFTER  &lt;period &gt;
0559:             *      |   &lt;B&gt;  &lt;attribute_name &gt; AFTER  &lt;date-time expression &gt;[*]&lt;/B&gt;
0560:             *  &lt;date-time expression &gt; ::=  &lt;date-time &gt; |  &lt;period &gt;
0561:             *  &lt;period &gt; ::=
0562:             *          &lt;date-time &gt; &quot;/&quot;  &lt;date-time &gt;[*]
0563:             *      |   &lt;date-time &gt; &quot;/&quot;  &lt;duration &gt;  [*]
0564:             *      |  &lt;duration &gt; &quot;/&quot;  &lt;date-time &gt;  [*]
0565:             * </pre>
0566:             *
0567:             * </p>
0568:             */
0569:            public void testTemporalPredicateAfter() throws Exception {
0570:                Filter resultFilter;
0571:                Filter expected;
0572:
0573:                // -------------------------------------------------------------
0574:                // <attribute_name> AFTER <date-time expression>
0575:                // -------------------------------------------------------------
0576:                //
0577:                resultFilter = CQL.toFilter(FilterSample.FILTER_AFTER_DATE);
0578:
0579:                assertNotNull("Filter expected", resultFilter);
0580:
0581:                expected = FilterSample
0582:                        .getSample(FilterSample.FILTER_AFTER_DATE);
0583:
0584:                assertEquals("greater filter ", expected, resultFilter);
0585:
0586:                // -------------------------------------------------------------
0587:                // <attribute_name> AFTER <period>
0588:                // -------------------------------------------------------------
0589:                // ATTR1 BEFORE 2006-11-31T01:30:00Z/2006-12-31T01:30:00Z
0590:                resultFilter = CQL
0591:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_BETWEEN_DATES);
0592:
0593:                assertNotNull("Filter expected", resultFilter);
0594:
0595:                expected = FilterSample
0596:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_BETWEEN_DATES);
0597:
0598:                assertEquals("greater filter ", expected, resultFilter);
0599:
0600:                // ATTR1 AFTER 2006-11-30T01:30:00Z/P10D
0601:                resultFilter = CQL
0602:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_DAYS);
0603:
0604:                assertNotNull("Filter expected", resultFilter);
0605:
0606:                expected = FilterSample
0607:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_DAYS);
0608:
0609:                assertEquals("greater filter", expected, resultFilter);
0610:
0611:                // ATTR1 AFTER 2006-11-30T01:30:00Z/P10M
0612:                resultFilter = CQL
0613:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_MONTH);
0614:
0615:                assertNotNull("Filter expected", resultFilter);
0616:
0617:                expected = FilterSample
0618:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_MONTH);
0619:
0620:                assertEquals("greater filter", expected, resultFilter);
0621:
0622:                // ATTR1 AFTER 2006-11-30T01:30:00Z/P10Y
0623:                resultFilter = CQL
0624:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_YEARS);
0625:
0626:                assertNotNull("Filter expected", resultFilter);
0627:
0628:                expected = FilterSample
0629:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_YEARS);
0630:
0631:                assertEquals("greater filter", expected, resultFilter);
0632:
0633:                // ATTR1 AFTER 2006-11-30T01:30:00Z/P10Y10M
0634:                resultFilter = CQL
0635:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_YEARS_MONTH);
0636:
0637:                assertNotNull("Filter expected", resultFilter);
0638:
0639:                expected = FilterSample
0640:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_YEARS_MONTH);
0641:
0642:                assertEquals("greater filter", expected, resultFilter);
0643:
0644:                // ATTR1 AFTER 2006-11-30T01:30:00Z/T5H
0645:                resultFilter = CQL
0646:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_HOURS);
0647:
0648:                assertNotNull("Filter expected", resultFilter);
0649:
0650:                expected = FilterSample
0651:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_HOURS);
0652:
0653:                assertEquals("greater filter", expected, resultFilter);
0654:
0655:                // ATTR1 AFTER 2006-11-30T01:30:00Z/T5M
0656:                resultFilter = CQL
0657:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_MINUTES);
0658:
0659:                assertNotNull("FilSter expected", resultFilter);
0660:
0661:                expected = FilterSample
0662:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_MINUTES);
0663:
0664:                assertEquals("greater filter", expected, resultFilter);
0665:
0666:                // ATTR1 AFTER 2006-11-30T01:30:00Z/T5S
0667:                resultFilter = CQL
0668:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_SECONDS);
0669:
0670:                assertNotNull("Filter expected", resultFilter);
0671:
0672:                expected = FilterSample
0673:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_SECONDS);
0674:
0675:                assertEquals("greater filter", expected, resultFilter);
0676:
0677:                // ATTR1 AFTER 2006-11-30T01:30:00Z/P10Y10M10DT5H5M5S
0678:                resultFilter = CQL
0679:                        .toFilter(FilterSample.FILTER_AFTER_PERIOD_DATE_YMD_HMS);
0680:
0681:                assertNotNull("Filter expected", resultFilter);
0682:
0683:                expected = FilterSample
0684:                        .getSample(FilterSample.FILTER_AFTER_PERIOD_DATE_YMD_HMS);
0685:
0686:                assertEquals("greater filter", expected, resultFilter);
0687:            }
0688:
0689:            /**
0690:             * Test Text Predicate
0691:             * <p>
0692:             *
0693:             * <pre>
0694:             *  &lt;text predicate &gt; ::=
0695:             *      &lt;attribute name &gt; [ NOT ] LIKE  &lt;character pattern &gt;
0696:             *      For example:
0697:             *      attribute like '%contains_this%'
0698:             *      attribute like 'begins_with_this%'
0699:             *      attribute like '%ends_with_this'
0700:             *      attribute like 'd_ve' will match 'dave' or 'dove'
0701:             *      attribute not like '%will_not_contain_this%'
0702:             *      attribute not like 'will_not_begin_with_this%'
0703:             *      attribute not like '%will_not_end_with_this'
0704:             * </pre>
0705:             *
0706:             * </p>
0707:             */
0708:            public void testTextPredicate() throws Exception {
0709:                Filter resultFilter;
0710:                Filter expected;
0711:
0712:                // Like
0713:                resultFilter = CQL.toFilter(FilterSample.LIKE_FILTER);
0714:
0715:                assertNotNull("Filter expected", resultFilter);
0716:
0717:                expected = FilterSample.getSample(FilterSample.LIKE_FILTER);
0718:
0719:                assertEquals("like filter was expected", expected, resultFilter);
0720:
0721:                // not Like
0722:                resultFilter = CQL.toFilter(FilterSample.NOT_LIKE_FILTER);
0723:
0724:                assertNotNull("Filter expected", resultFilter);
0725:
0726:                expected = FilterSample.getSample(FilterSample.NOT_LIKE_FILTER);
0727:
0728:                assertEquals("like filter was expected", expected, resultFilter);
0729:            }
0730:
0731:            /**
0732:             * Test Between Predicate.
0733:             * <p>
0734:             *
0735:             * <pre>
0736:             *  This cql clause is an extension for convenience.
0737:             *  &lt;between predicate &gt; ::=
0738:             *  &lt;attribute name &gt; [ NOT ] BETWEEN  &lt;literal&amp; #62; AND  &lt; literal  &gt;
0739:             * </pre>
0740:             *
0741:             * </p>
0742:             */
0743:            public void testBetweenPredicate() throws Exception {
0744:                Filter resultFilter;
0745:                Filter expected;
0746:
0747:                // between
0748:                resultFilter = CQL.toFilter(FilterSample.BETWEEN_FILTER);
0749:
0750:                assertNotNull("Filter expected", resultFilter);
0751:
0752:                expected = FilterSample.getSample(FilterSample.BETWEEN_FILTER);
0753:
0754:                assertEquals("Between filter was expected", expected,
0755:                        resultFilter);
0756:
0757:                // not between
0758:                resultFilter = CQL.toFilter(FilterSample.NOT_BETWEEN_FILTER);
0759:
0760:                assertNotNull("Filter expected", resultFilter);
0761:
0762:                expected = FilterSample
0763:                        .getSample(FilterSample.NOT_BETWEEN_FILTER);
0764:
0765:                assertEquals("Between filter was expected", expected,
0766:                        resultFilter);
0767:
0768:                // test compound attribute gmd:aa:bb.gmd:cc.gmd:dd
0769:                final String prop = "gmd:aa:bb.gmd:cc.gmd:dd";
0770:                final String propExpected = "gmd:aa:bb/gmd:cc/gmd:dd";
0771:                resultFilter = CQL.toFilter(prop + " BETWEEN 100 AND 200 ");
0772:
0773:                assertTrue("PropertyIsBetween filter was expected",
0774:                        resultFilter instanceof  PropertyIsBetween);
0775:
0776:                PropertyIsBetween filter = (PropertyIsBetween) resultFilter;
0777:                Expression property = filter.getExpression();
0778:
0779:                assertEquals(propExpected, property.toString());
0780:            }
0781:
0782:            /**
0783:             * Test Attribute
0784:             * <p>
0785:             *
0786:             * <pre>
0787:             *  &lt;attribute name &gt; ::=
0788:             *          &lt;simple attribute name &gt;
0789:             *      |    &lt;compound attribute name &gt;
0790:             *  &lt;simple attribute name &gt; ::=  &lt;identifier &gt;
0791:             *  &lt;compound attribute name &gt; ::=  &lt;identifier &gt; &lt;period &gt; [{ &lt;identifier &gt; &lt;period &gt;}...] &lt;simple attribute name &gt;
0792:             *  &lt;identifier &gt; ::=  &lt;identifier start [ {  &lt;colon &gt; |  &lt;identifier part &gt; }... ]
0793:             *  &lt;identifier start &gt; ::=  &lt;simple Latin letter &gt;
0794:             *  &lt;identifier part &gt; ::=  &lt;simple Latin letter &gt; |  &lt;digit &gt;
0795:             * </pre>
0796:             *
0797:             * </p>
0798:             */
0799:            public void testAttribute() throws Exception {
0800:                // Simple attribute name
0801:                testAttribute("startPart");
0802:
0803:                testAttribute("startpart:part1:part2");
0804:
0805:                // Compound attribute name
0806:                testAttribute("s11:p12:p13.s21:p22.s31:p32");
0807:
0808:                testAttribute("gmd:MD_Metadata.gmd:identificationInfo.gmd:MD_DataIdentification.gmd:abstract");
0809:            }
0810:
0811:            private void testAttribute(final String attSample) throws Exception {
0812:                PropertyIsLike result;
0813:                PropertyName attResult = null;
0814:
0815:                String expected = attSample.replace('.', '/');
0816:
0817:                result = (PropertyIsLike) CQL.toFilter(attSample
0818:                        + " LIKE 'abc%'");
0819:
0820:                attResult = (PropertyName) result.getExpression();
0821:
0822:                assertEquals(expected, attResult.getPropertyName());
0823:            }
0824:
0825:            /**
0826:             * Test boolean value expressions.
0827:             * <p>
0828:             *
0829:             * <pre>
0830:             *  &lt;boolean value expression &gt; ::=
0831:             *          &lt;boolean term &gt;
0832:             *      |   &lt;boolean value expression &gt; OR  &lt;boolean term &gt;
0833:             *  &lt;boolean term &gt; ::=
0834:             *          &lt;boolean factor &gt;
0835:             *      |   &lt;boolean term &gt; AND  &lt;boolean factor&gt;
0836:             * </pre>
0837:             *
0838:             * </p>
0839:             */
0840:            public void testBooleanValueExpression() throws Exception {
0841:                Filter result;
0842:                Filter expected;
0843:
0844:                // ATTR1 < 10 AND ATTR2 < 2
0845:                result = CQL.toFilter(FilterSample.FILTER_AND);
0846:
0847:                assertNotNull("filter expected", result);
0848:
0849:                expected = FilterSample.getSample(FilterSample.FILTER_AND);
0850:
0851:                assertEquals("ATTR1 < 10 AND ATTR2 < 2 was expected", expected,
0852:                        result);
0853:
0854:                // "ATTR1 > 10 OR ATTR2 < 2"
0855:                result = CQL.toFilter(FilterSample.FILTER_OR);
0856:
0857:                assertNotNull("filter expected", result);
0858:
0859:                expected = FilterSample.getSample(FilterSample.FILTER_OR);
0860:
0861:                assertEquals("ATTR1 > 10 OR ATTR2 < 2 was expected", expected,
0862:                        result);
0863:
0864:                // ATTR1 < 10 AND ATTR2 < 2 OR ATTR3 > 10
0865:                result = CQL.toFilter(FilterSample.FILTER_OR_AND);
0866:
0867:                assertNotNull("filter expected", result);
0868:
0869:                expected = FilterSample.getSample(FilterSample.FILTER_OR_AND);
0870:
0871:                assertEquals(
0872:                        "(ATTR1 < 10 AND ATTR2 < 2 OR ATTR3 > 10) was expected",
0873:                        expected, result);
0874:
0875:                // ATTR3 < 4 AND (ATT1 > 10 OR ATT2 < 2)
0876:                result = CQL.toFilter(FilterSample.FILTER_OR_AND_PARENTHESIS);
0877:
0878:                assertNotNull("filter expected", result);
0879:
0880:                expected = FilterSample
0881:                        .getSample(FilterSample.FILTER_OR_AND_PARENTHESIS);
0882:
0883:                assertEquals(
0884:                        "ATTR3 < 4 AND (ATT1 > 10 OR ATT2 < 2) was expected",
0885:                        expected, result);
0886:
0887:                // ATTR3 < 4 AND (NOT( ATTR1 < 10 AND ATTR2 < 2))
0888:                result = CQL.toFilter(FilterSample.FILTER_AND_NOT_AND);
0889:
0890:                assertNotNull("filter expected", result);
0891:
0892:                expected = FilterSample
0893:                        .getSample(FilterSample.FILTER_AND_NOT_AND);
0894:
0895:                assertEquals(
0896:                        "ATTR3 < 4 AND (NOT( ATTR1 < 10 AND ATTR2 < 2)) was expected",
0897:                        expected, result);
0898:
0899:                // "ATTR1 < 1 AND (NOT (ATTR2 < 2)) AND ATTR3 < 3"
0900:                result = CQL.toFilter(FilterSample.FILTER_AND_NOT_COMPARASION);
0901:
0902:                assertNotNull("filter expected", result);
0903:
0904:                expected = FilterSample
0905:                        .getSample(FilterSample.FILTER_AND_NOT_COMPARASION);
0906:
0907:                assertEquals(
0908:                        "ATTR1 < 4 AND (NOT (ATTR2 < 4)) AND ATTR3 < 4 was expected",
0909:                        expected, result);
0910:            }
0911:
0912:            /**
0913:             * Test Geo Operations.
0914:             * <p>
0915:             *
0916:             * <pre>
0917:             *   &lt;routine invocation &gt; ::=
0918:             *           &lt;geoop name &gt; &lt;georoutine argument list &gt;[*]
0919:             *       |   &lt;relgeoop name &gt; &lt;relgeoop argument list &gt;
0920:             *       |   &lt;routine name &gt; &lt;argument list &gt;
0921:             *   &lt;geoop name &gt; ::=
0922:             *           EQUAL | DISJOINT | INTERSECT | TOUCH | CROSS | [*]
0923:             *           WITHIN | CONTAINS |OVERLAP | RELATE [*]
0924:             *   That rule is extended with bbox for convenience.
0925:             *   &lt;bbox argument list &gt;::=
0926:             *       &quot;(&quot;  &lt;attribute &gt; &quot;,&quot; &lt;min X &gt; &quot;,&quot; &lt;min Y &gt; &quot;,&quot; &lt;max X &gt; &quot;,&quot; &lt;max Y &gt;[&quot;,&quot;  &lt;srs &gt;] &quot;)&quot;
0927:             *       &lt;min X &gt; ::=  &lt;signed numerical literal &gt;
0928:             *       &lt;min Y &gt; ::=  &lt;signed numerical literal &gt;
0929:             *       &lt;max X &gt; ::=  &lt;signed numerical literal &gt;
0930:             *       &lt;max Y &gt; ::=  &lt;signed numerical literal &gt;
0931:             *       &lt;srs &gt; ::=
0932:             * </pre>
0933:             *
0934:             * </p>
0935:             * TODO Note: RELATE is not supported (implementation in GeoTools is
0936:             * required)
0937:             */
0938:            public void testRoutineInvocationGeoOp() throws Exception {
0939:                Filter resultFilter;
0940:
0941:                resultFilter = CQL.toFilter("DISJOINT(ATTR1, POINT(1 2))");
0942:
0943:                assertTrue("Disjoint was expected",
0944:                        resultFilter instanceof  Disjoint);
0945:
0946:                resultFilter = CQL.toFilter("INTERSECT(ATTR1, POINT(1 2))");
0947:
0948:                assertTrue("Intersects was expected",
0949:                        resultFilter instanceof  Intersects);
0950:
0951:                resultFilter = CQL.toFilter("TOUCH(ATTR1, POINT(1 2))");
0952:
0953:                assertTrue("Touches was expected",
0954:                        resultFilter instanceof  Touches);
0955:
0956:                resultFilter = CQL.toFilter("CROSS(ATTR1, POINT(1 2))");
0957:
0958:                assertTrue("Crosses was expected",
0959:                        resultFilter instanceof  Crosses);
0960:
0961:                resultFilter = CQL.toFilter("CONTAINS(ATTR1, POINT(1 2))");
0962:
0963:                assertTrue("Contains was expected",
0964:                        resultFilter instanceof  Contains);
0965:
0966:                resultFilter = CQL.toFilter("OVERLAP(ATTR1, POINT(1 2))");
0967:
0968:                assertTrue("Overlaps was expected",
0969:                        resultFilter instanceof  Overlaps);
0970:
0971:                // BBOX
0972:                resultFilter = CQL.toFilter("BBOX(ATTR1, 10.0,20.0,30.0,40.0)");
0973:                assertTrue("BBox was expected", resultFilter instanceof  BBOX);
0974:                BBOX bboxFilter = (BBOX) resultFilter;
0975:                assertEquals(bboxFilter.getMinX(), 10.0, 0);
0976:                assertEquals(bboxFilter.getMinY(), 20.0, 0);
0977:                assertEquals(bboxFilter.getMaxX(), 30.0, 0);
0978:                assertEquals(bboxFilter.getMaxY(), 40.0, 0);
0979:                assertEquals(null, bboxFilter.getSRS());
0980:
0981:                resultFilter = CQL
0982:                        .toFilter("BBOX(ATTR1, 10.0,20.0,30.0,40.0, 'EPSG:4326')");
0983:                assertTrue("BBox was expected", resultFilter instanceof  BBOX);
0984:                bboxFilter = (BBOX) resultFilter;
0985:                assertEquals("EPSG:4326", bboxFilter.getSRS());
0986:
0987:                // EQUALS
0988:                resultFilter = CQL.toFilter("EQUAL(ATTR1, POINT(1 2))");
0989:
0990:                assertTrue("not an instance of Equals",
0991:                        resultFilter instanceof  Equals);
0992:
0993:                resultFilter = CQL
0994:                        .toFilter("WITHIN(ATTR1, POLYGON((1 2, 1 10, 5 10, 1 2)) )");
0995:
0996:                assertTrue("Within was expected",
0997:                        resultFilter instanceof  Within);
0998:            }
0999:
1000:            /**
1001:             * Test RelGeo Operations [*]
1002:             * <p>
1003:             *
1004:             * <pre>
1005:             *   &lt;routine invocation &gt; ::=
1006:             *       &lt;geoop name &gt; &lt;georoutine argument list &gt;
1007:             *   |   &lt;relgeoop name &gt; &lt;relgeoop argument list &gt; [*]
1008:             *   |  &lt;routine name &gt; &lt;argument list &gt;
1009:             *   &lt;relgeoop name &gt; ::=
1010:             *       DWITHIN | BEYOND [*]
1011:             * </pre>
1012:             *
1013:             * </p>
1014:             */
1015:            public void testRoutineInvocationRelGeoOp() throws Exception {
1016:                Filter resultFilter;
1017:
1018:                // DWITHIN
1019:                resultFilter = CQL
1020:                        .toFilter("DWITHIN(ATTR1, POINT(1 2), 10, kilometers)");
1021:
1022:                assertTrue(resultFilter instanceof  DistanceBufferOperator);
1023:
1024:                // test compound attribute gmd:aa:bb.gmd:cc.gmd:dd
1025:                final String prop = "gmd:aa:bb.gmd:cc.gmd:dd";
1026:                final String propExpected = "gmd:aa:bb/gmd:cc/gmd:dd";
1027:                resultFilter = CQL.toFilter("DWITHIN(" + prop
1028:                        + ", POINT(1 2), 10, kilometers) ");
1029:
1030:                assertTrue("DistanceBufferOperator filter was expected",
1031:                        resultFilter instanceof  DWithin);
1032:
1033:                DistanceBufferOperator filter = (DWithin) resultFilter;
1034:                Expression property = filter.getExpression1();
1035:
1036:                assertEquals(propExpected, property.toString());
1037:
1038:                // Beyond
1039:                resultFilter = CQL
1040:                        .toFilter("BEYOND(ATTR1, POINT(1.0 2.0), 10.0, kilometers)");
1041:                assertTrue(resultFilter instanceof  Beyond);
1042:                Beyond beyondFilter = (Beyond) resultFilter;
1043:
1044:                assertEquals(beyondFilter.getDistance(), 10.0, 0);
1045:                assertEquals(beyondFilter.getDistanceUnits(), "kilometers");
1046:                assertEquals(beyondFilter.getExpression1().toString(), "ATTR1");
1047:
1048:                Expression geomExpression = beyondFilter.getExpression2();
1049:                assertTrue(geomExpression instanceof  Literal);
1050:                Literal literalPoint = (Literal) geomExpression;
1051:
1052:                Object pointValue = literalPoint.getValue();
1053:                assertTrue(pointValue instanceof  Point);
1054:                Point point = (Point) pointValue;
1055:                assertEquals(point.getX(), 1.0, 0);
1056:                assertEquals(point.getY(), 2.0, 0);
1057:
1058:                // syntax error test (POINTS must be POINT)
1059:                try {
1060:                    resultFilter = CQL
1061:                            .toFilter("BEYOND(ATTR1, POINTS(1.0 2.0), 10.0, kilometers)");
1062:                    fail("CQLException was expected");
1063:
1064:                } catch (CQLException e) {
1065:
1066:                    assertNotNull(
1067:                            "Syntax error was expected (should be POINT)", e
1068:                                    .getMessage());
1069:                }
1070:
1071:            }
1072:
1073:            /**
1074:             * Test RelGeo Operations [*]
1075:             * <p>
1076:             * 
1077:             * <pre>
1078:             *   &lt;routine invocation &gt; ::=
1079:             *       &lt;geoop name &gt; &lt;georoutine argument list &gt;
1080:             *   |   &lt;relgeoop name &gt; &lt;relgeoop argument list &gt;
1081:             *   |   &lt;routine name &gt; &lt;argument list &gt; [*]
1082:             *  &lt;argument list&gt; ::=    [*]
1083:             *       &lt;left paren&gt; [&lt;positional arguments&gt;] &lt;right paren&gt;
1084:             *  &lt;positional arguments&gt; ::=
1085:             *       &lt;argument&gt; [ { &lt;comma&amp;gt &lt;argument&gt; }... ]
1086:             *  &lt;argument&gt;  ::=
1087:             *       &lt;literal&gt;
1088:             *   |   &lt;attribute name&gt;
1089:             * </pre>
1090:             * 
1091:             * </p>
1092:             * 
1093:             * @throws Exception
1094:             */
1095:            public void testRoutineInvocationGeneric() throws Exception {
1096:                // TODO not implement it
1097:                // (Mauricio Comments) This case is not implemented because the filter
1098:                // model has not a
1099:                // Routine (Like functions in Expression). We could develop easily the
1100:                // parser but we can not build a filter for CQL <Routine invocation>.
1101:            }
1102:
1103:            /**
1104:             * Tests Geometry Literals
1105:             * <p>
1106:             *
1107:             * <pre>
1108:             *  &lt;geometry literal &gt; :=
1109:             *          &lt;Point Tagged Text &gt;
1110:             *      |   &lt;LineString Tagged Text &gt;
1111:             *      |   &lt;Polygon Tagged Text &gt;
1112:             *      |   &lt;MultiPoint Tagged Text &gt;
1113:             *      |   &lt;MultiLineString Tagged Text &gt;
1114:             *      |   &lt;MultiPolygon Tagged Text &gt;
1115:             *      |   &lt;GeometryCollection Tagged Text &gt;
1116:             *      |   &lt;Envelope Tagged Text &gt;
1117:             * </pre>
1118:             *
1119:             * </p>
1120:             */
1121:            public void testGeometryLiterals() throws Exception {
1122:                BinarySpatialOperator result;
1123:                Literal geom;
1124:
1125:                // Point
1126:                result = (BinarySpatialOperator) CQL
1127:                        .toFilter("CROSS(ATTR1, POINT(1 2))");
1128:
1129:                geom = (Literal) result.getExpression2();
1130:
1131:                assertNotNull(geom.getValue());
1132:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.Point);
1133:
1134:                // LineString
1135:                result = (BinarySpatialOperator) CQL
1136:                        .toFilter("CROSS(ATTR1, LINESTRING(1 2, 10 15))");
1137:
1138:                geom = (Literal) result.getExpression2();
1139:
1140:                assertNotNull(geom.getValue());
1141:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.LineString);
1142:
1143:                // Poligon
1144:                result = (BinarySpatialOperator) CQL
1145:                        .toFilter("CROSS(ATTR1, POLYGON((1 2, 15 2, 15 20, 15 21, 1 2)))");
1146:
1147:                geom = (Literal) result.getExpression2();
1148:
1149:                assertNotNull(geom.getValue());
1150:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.Polygon);
1151:
1152:                // MultiPoint
1153:                result = (BinarySpatialOperator) CQL
1154:                        .toFilter("CROSS(ATTR1, MULTIPOINT( (1 2), (15 2), (15 20), (15 21), (1 2) ))");
1155:
1156:                geom = (Literal) result.getExpression2();
1157:
1158:                assertNotNull(geom.getValue());
1159:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.MultiPoint);
1160:
1161:                // MultiLineString
1162:                result = (BinarySpatialOperator) CQL
1163:                        .toFilter("CROSS(ATTR1, MULTILINESTRING((10 10, 20 20),(15 15,30 15)) )");
1164:
1165:                geom = (Literal) result.getExpression2();
1166:
1167:                assertNotNull(geom.getValue());
1168:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.MultiLineString);
1169:
1170:                // MultiPolygon
1171:                result = (BinarySpatialOperator) CQL
1172:                        .toFilter("CROSS(ATTR1, MULTIPOLYGON( ((10 10, 10 20, 20 20, 20 15, 10 10)),((60 60, 70 70, 80 60, 60 60 )) ) )");
1173:
1174:                geom = (Literal) result.getExpression2();
1175:
1176:                assertNotNull(geom.getValue());
1177:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.MultiPolygon);
1178:
1179:                // GEOMETRYCOLLECTION
1180:                result = (BinarySpatialOperator) CQL
1181:                        .toFilter("CROSS(ATTR1, GEOMETRYCOLLECTION (POINT (10 10),POINT (30 30),LINESTRING (15 15, 20 20)) )");
1182:
1183:                geom = (Literal) result.getExpression2();
1184:
1185:                assertNotNull(geom.getValue());
1186:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.GeometryCollection);
1187:
1188:                // ENVELOPE
1189:                result = (BinarySpatialOperator) CQL
1190:                        .toFilter("CROSS(ATTR1, ENVELOPE( 10, 20, 30, 40) )");
1191:
1192:                geom = (Literal) result.getExpression2();
1193:
1194:                assertNotNull(geom.getValue());
1195:                assertTrue(geom.getValue() instanceof  com.vividsolutions.jts.geom.Polygon);
1196:            }
1197:
1198:            /**
1199:             * Test error at geometry literal
1200:             *
1201:             */
1202:            public void testGeometryLiteralsError() {
1203:                final String filterError = "WITHIN(ATTR1, POLYGON((1 2, 10 15), (10 15, 1 2)))";
1204:
1205:                try {
1206:                    CQL.toFilter(filterError);
1207:
1208:                    fail("polygon error was expected");
1209:                } catch (CQLException e) {
1210:                    String message = e.getSyntaxError();
1211:                    assertFalse("error message is expected", "".equals(message));
1212:                }
1213:            }
1214:
1215:            /**
1216:             * Some token errors 
1217:             */
1218:            public void testTokensError() {
1219:
1220:                // double quote in expression
1221:                try {
1222:                    String cqlExpression = "strConcat(A, \".\")";
1223:                    CQL.toExpression(cqlExpression);
1224:
1225:                    fail("CQLException was expected");
1226:
1227:                } catch (CQLException e) {
1228:
1229:                    assertNotNull("Token message error was expected", e
1230:                            .getMessage());
1231:                }
1232:                // double quote in predicate 
1233:                try {
1234:                    String cqlExpression = "A=\"2\"";
1235:                    CQL.toFilter(cqlExpression);
1236:
1237:                    fail("CQLException was expected");
1238:
1239:                } catch (CQLException e) {
1240:
1241:                    assertNotNull("Token message error was expected", e
1242:                            .getMessage());
1243:
1244:                }
1245:
1246:                // token identifier error
1247:                try {
1248:                    String cqlExpression = "1A=2";
1249:                    CQL.toFilter(cqlExpression);
1250:
1251:                    fail("CQLException was expected");
1252:
1253:                } catch (CQLException e) {
1254:
1255:                    assertNotNull("Token message error was expected", e
1256:                            .getMessage());
1257:                }
1258:            }
1259:
1260:            /**
1261:             * <pre>
1262:             * &lt;character string literal&gt; ::= &lt;quote&gt; [ {&lt;character representation&lt;} ]  &lt;quote&gt;
1263:             * &lt;character representation&gt; ::= 
1264:             * 			&lt;nonquote character&gt; 
1265:             * 		| 	&lt;quote symbol&gt;
1266:             * &lt;quote symbol&gt; ::=  &lt;quote&gt; &lt;quote&gt;
1267:             *  </pre>
1268:             */
1269:            public void testCharacterStringLiteral() throws Exception {
1270:
1271:                PropertyIsEqualTo eqFilter;
1272:
1273:                // empty string ''
1274:                Filter emptyFilter = CQL
1275:                        .toFilter("MAJOR_WATERSHED_SYSTEM = ''");
1276:
1277:                assertNotNull(emptyFilter);
1278:                assertTrue(emptyFilter instanceof  PropertyIsEqualTo);
1279:
1280:                eqFilter = (PropertyIsEqualTo) emptyFilter;
1281:                Expression emptyLiteral = eqFilter.getExpression2();
1282:                assertEquals("", emptyLiteral.toString());
1283:
1284:                //character string without quote
1285:                final String expectedWithout = "ab";
1286:
1287:                Filter filterWithoutQuote = CQL
1288:                        .toFilter("MAJOR_WATERSHED_SYSTEM = '"
1289:                                + expectedWithout + "'");
1290:
1291:                assertNotNull(filterWithoutQuote);
1292:                assertTrue(filterWithoutQuote instanceof  PropertyIsEqualTo);
1293:
1294:                eqFilter = (PropertyIsEqualTo) filterWithoutQuote;
1295:                Expression actualWhithoutQuote = eqFilter.getExpression2();
1296:                assertEquals(expectedWithout, actualWhithoutQuote.toString());
1297:
1298:                // <quote symbol>
1299:                final String expected = "cde'' fg";
1300:
1301:                Filter filter = CQL.toFilter("MAJOR_WATERSHED_SYSTEM = '"
1302:                        + expected + "'");
1303:
1304:                assertNotNull(filter);
1305:                assertTrue(filter instanceof  PropertyIsEqualTo);
1306:
1307:                eqFilter = (PropertyIsEqualTo) filter;
1308:                Expression actual = eqFilter.getExpression2();
1309:                assertEquals(expected.replaceAll("''", "'"), actual.toString());
1310:
1311:            }
1312:
1313:            /**
1314:             * Test for expressions
1315:             *  
1316:             * @throws Exception
1317:             */
1318:            public void testParseExpression() throws Exception {
1319:                Expression expression = CQL.toExpression("attName");
1320:                assertNotNull(expression);
1321:                assertTrue(expression instanceof  PropertyName);
1322:                assertEquals("attName", ((PropertyName) expression)
1323:                        .getPropertyName());
1324:
1325:                expression = CQL.toExpression("a + b + x.y.z");
1326:                assertNotNull(expression);
1327:                assertTrue(expression instanceof  Add);
1328:
1329:                Add add = (Add) expression;
1330:                Expression e1 = add.getExpression1();
1331:                Expression e2 = add.getExpression2();
1332:
1333:                assertTrue(e1 instanceof  Add);
1334:                assertTrue(e2 instanceof  PropertyName);
1335:                assertEquals("x/y/z", ((PropertyName) e2).getPropertyName());
1336:            }
1337:
1338:            public void testBoolean() throws Exception {
1339:                Filter filter = CQL.toFilter("attr = true");
1340:                assertNotNull(filter);
1341:                assertTrue(filter instanceof  PropertyIsEqualTo);
1342:
1343:                PropertyIsEqualTo f = (PropertyIsEqualTo) filter;
1344:                assertEquals("attr", ((PropertyName) f.getExpression1())
1345:                        .getPropertyName());
1346:                assertEquals(Boolean.TRUE, ((Literal) f.getExpression2())
1347:                        .getValue());
1348:            }
1349:
1350:            /**
1351:             * General test for cql expressions
1352:             *
1353:             * @param cqlSample
1354:             * @throws Exception
1355:             */
1356:            private void testEqualsExpressions(final String cqlSample)
1357:                    throws Exception {
1358:                Filter expected = FilterSample.getSample(cqlSample);
1359:                Filter actual = CQL.toFilter(cqlSample);
1360:
1361:                assertNotNull("expects filter not null", actual);
1362:                assertEquals("this is not the filter expected", expected,
1363:                        actual);
1364:            }
1365:
1366:            public final void testGetSyntaxError() {
1367:
1368:                try {
1369:                    final String malformedExp = "12 / ] + 4";
1370:                    CQL.toFilter(malformedExp);
1371:                    fail("expected Exception");
1372:                } catch (CQLException pe) {
1373:                    String error = pe.getSyntaxError();
1374:                    assertFalse("".equals(error));
1375:                }
1376:
1377:                try {
1378:                    String malformedGeometry = "WITHIN(ATTR1, POLYGON((1 2, 10 15), (10 15, 1 2)))";
1379:                    CQL.toFilter(malformedGeometry);
1380:
1381:                    fail("polygon error was expected");
1382:                } catch (CQLException e) {
1383:                    String message = e.getSyntaxError();
1384:                    assertFalse("error message is expected", "".equals(message));
1385:                }
1386:
1387:            }
1388:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.