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.xml.ogc;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.StringBufferInputStream;
022: import java.io.StringReader;
023: import java.io.StringWriter;
024: import java.util.HashMap;
025: import java.util.Map;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031:
032: import junit.framework.Test;
033: import junit.framework.TestCase;
034: import junit.framework.TestSuite;
035:
036: import org.geotools.filter.BetweenFilter;
037: import org.geotools.filter.FidFilter;
038: import org.geotools.filter.Filter;
039: import org.geotools.filter.FilterDOMParser;
040: import org.geotools.filter.FilterFactory;
041: import org.geotools.filter.FilterFactoryFinder;
042: import org.geotools.filter.NullFilter;
043: import org.geotools.test.TestData;
044: import org.geotools.xml.DocumentFactory;
045: import org.geotools.xml.DocumentWriter;
046: import org.geotools.xml.SchemaFactory;
047: import org.geotools.xml.XMLHandlerHints;
048: import org.geotools.xml.filter.FilterSchema;
049: import org.w3c.dom.Document;
050: import org.w3c.dom.Element;
051: import org.w3c.dom.Node;
052: import org.w3c.dom.NodeList;
053:
054: /**
055: * This code uses the filter parser to generate test cases, and runs it through the encoder.
056: *
057: * TODO create the filters manually, and check the output.
058: *
059: * @author James MacGill, CCG
060: * @author Rob Hranac, TOPP
061: * @author David Zwiers
062: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/xml/src/test/java/org/geotools/xml/ogc/XMLEncoderTest.java $
063: */
064: public class XMLEncoderTest extends TestCase {
065:
066: /** Standard logging instance */
067: protected static final Logger LOGGER = org.geotools.util.logging.Logging
068: .getLogger("org.geotools.filter");
069:
070: /** Constructor with test name. */
071: String dataFolder = "";
072:
073: public XMLEncoderTest(String testName) {
074: super (testName);
075:
076: //_log.getLoggerRepository().setThreshold(Level.DEBUG);
077: LOGGER.finer("running XMLEncoderTests");
078:
079: dataFolder = System.getProperty("dataFolder");
080:
081: if (dataFolder == null) {
082: try {
083: TestData.file(this , null);
084: } catch (IOException e) {
085: LOGGER.finer("data folder is unavailable" + dataFolder);
086: }
087: }
088: if (dataFolder == null) {
089: //then we are being run by maven
090: dataFolder = System.getProperty("basedir");
091: dataFolder = "file:////" + "tests/unit/testData"; //url.toString();
092: LOGGER.finer("data folder is " + dataFolder);
093: }
094: }
095:
096: /**
097: * Main for test runner.
098: *
099: * @param args DOCUMENT ME!
100: */
101: public static void main(String[] args) {
102: junit.textui.TestRunner.run(suite());
103: }
104:
105: /**
106: * Required suite builder.
107: *
108: * @return A test suite for this unit test.
109: */
110: public static Test suite() {
111: TestSuite suite = new TestSuite(XMLEncoderTest.class);
112:
113: return suite;
114: }
115:
116: public void test1() throws Exception {
117: Filter test = parseDocument("test1.xml");
118: assertNotNull(test);
119: StringWriter output = new StringWriter();
120: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
121: output, null);
122: //System.out.println( output );
123: InputStream stream = new StringBufferInputStream(output
124: .toString());
125:
126: Object o = DocumentFactory.getInstance(stream, new HashMap(),
127: Level.FINEST);
128: assertNotNull(o);
129: assertEquals(test, o);
130: //LOGGER.fine("parsed filter is: " + test);
131: }
132:
133: public void test3a() throws Exception {
134: Filter test = parseDocument("test3a.xml");
135:
136: //LOGGER.fine("parsed filter is: " + test);
137: }
138:
139: public void test3b() throws Exception {
140: Filter test = parseDocument("test3b.xml");
141: StringWriter output = new StringWriter();
142: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
143: output, null);
144:
145: //LOGGER.fine("parsed filter is: " + test);
146: }
147:
148: public void test4() throws Exception {
149: Filter test = parseDocument("test4.xml");
150: StringWriter output = new StringWriter();
151: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
152: output, null);
153:
154: //LOGGER.fine("parsed filter is: " + test);
155: }
156:
157: public void test5() throws Exception {
158: Filter test = parseDocument("test5.xml");
159: StringWriter output = new StringWriter();
160: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
161: output, null);
162:
163: //LOGGER.fine("parsed filter is: " + test);
164: }
165:
166: public void test8() throws Exception {
167: Filter test = parseDocument("test8.xml");
168:
169: StringWriter output = new StringWriter();
170: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
171: output, null);
172:
173: //System.out.println(output);
174: //LOGGER.fine("parsed filter is: " + test);
175: }
176:
177: public void test9() throws Exception {
178: Filter test = parseDocument("test9.xml");
179: StringWriter output = new StringWriter();
180: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
181: output, null);
182:
183: //LOGGER.fine("parsed filter is: " + test);
184: }
185:
186: public void test12() throws Exception {
187: Filter test = parseDocument("test12.xml");
188: StringWriter output = new StringWriter();
189: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
190: output, null);
191:
192: // LOGGER.fine("parsed filter is: " + test);
193: }
194:
195: public void test13() throws Exception {
196: Filter test = parseDocument("test13.xml");
197: StringWriter output = new StringWriter();
198: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
199: output, null);
200:
201: //LOGGER.fine("parsed filter is: " + test);
202: }
203:
204: public void test14() throws Exception {
205: Filter test = parseDocument("test14.xml");
206: StringWriter output = new StringWriter();
207: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
208: output, null);
209:
210: //LOGGER.fine("parsed filter is: " + test);
211: }
212:
213: public void test28() throws Exception {
214: Filter test = parseDocument("test28.xml");
215: StringWriter output = new StringWriter();
216: DocumentWriter.writeFragment(test, FilterSchema.getInstance(),
217: output, null);
218:
219: //System.out.println(output);
220: // LOGGER.fine("parsedfilter is: " + test);
221: }
222:
223: public Filter parseDocument(String uri) throws Exception {
224: Filter filter = null;
225: DocumentBuilderFactory dbf = DocumentBuilderFactory
226: .newInstance();
227: DocumentBuilder db = dbf.newDocumentBuilder();
228: Document dom = db.parse(TestData.getResource(this , uri)
229: .toExternalForm());
230:
231: // LOGGER.fine("exporting " + uri);
232: // first grab a filter node
233: NodeList nodes = dom.getElementsByTagName("Filter");
234:
235: for (int j = 0; j < nodes.getLength(); j++) {
236: Element filterNode = (Element) nodes.item(j);
237: NodeList list = filterNode.getChildNodes();
238: Node child = null;
239:
240: for (int i = 0; i < list.getLength(); i++) {
241: child = list.item(i);
242:
243: //_log.getLoggerRepository().setThreshold(Level.INFO);
244: if ((child == null)
245: || (child.getNodeType() != Node.ELEMENT_NODE)) {
246: continue;
247: }
248:
249: filter = FilterDOMParser.parseFilter(child);
250:
251: StringWriter output = new StringWriter();
252: DocumentWriter.writeFragment(filter, FilterSchema
253: .getInstance(), output, null);
254:
255: // System.out.println(output);
256: }
257: }
258:
259: return filter;
260: }
261:
262: // TODO test or ( null, and( fidFilter, null ) ) filter
263: public void testStrictHintComplexFilter() throws Exception {
264: FilterFactory factory = FilterFactoryFinder
265: .createFilterFactory();
266: NullFilter null1 = factory.createNullFilter();
267: null1.nullCheckValue(factory.createAttributeExpression("name"));
268: NullFilter null2 = factory.createNullFilter();
269: null2.nullCheckValue(factory.createAttributeExpression("geom"));
270: FidFilter fidFilter = factory.createFidFilter("FID.1");
271: Filter inner = null1.and(fidFilter);
272: Filter filter = null2.or(inner);
273:
274: StringWriter output = new StringWriter();
275: XMLHandlerHints hints = new XMLHandlerHints();
276: hints.put(XMLHandlerHints.FILTER_COMPLIANCE_STRICTNESS,
277: XMLHandlerHints.VALUE_FILTER_COMPLIANCE_MEDIUM);
278: DocumentWriter.writeFragment(filter,
279: FilterSchema.getInstance(), output, hints);
280: String string = output.toString().replaceAll("\\s", "");
281: assertEquals(
282: "<Filterxmlns=\"http://www.opengis.net/ogc\"xmlns:gml=\"http://www.opengis.net/gml\">"
283: + "<PropertyIsNull><PropertyName>geom</PropertyName></PropertyIsNull>"
284: + "<FeatureIdfid=\"FID.1\"/>" + "</Filter>",
285: string);
286:
287: // Note: Round trip doesn't work in this case because request may returns more features than "filter" will accept
288: }
289:
290: public void testStrictHintOR() throws Exception {
291: FilterFactory factory = FilterFactoryFinder
292: .createFilterFactory();
293: Filter filter = factory.createFidFilter("FID.1").or(
294: factory.createFidFilter("FID.2"));
295:
296: StringWriter output = new StringWriter();
297: XMLHandlerHints hints = new XMLHandlerHints();
298: hints.put(XMLHandlerHints.FILTER_COMPLIANCE_STRICTNESS,
299: XMLHandlerHints.VALUE_FILTER_COMPLIANCE_MEDIUM);
300: DocumentWriter.writeFragment(filter,
301: FilterSchema.getInstance(), output, hints);
302: String string = output.toString().replaceAll("\\s", "");
303: // The following test fails with Java 1.6. May be caused by some iteration order dependent code.
304: if (TestData.isBaseJavaPlatform()) {
305: assertEquals(
306: "<Filterxmlns=\"http://www.opengis.net/ogc\"xmlns:gml=\"http://www.opengis.net/gml\"><FeatureIdfid=\"FID.1\"/><FeatureIdfid=\"FID.2\"/></Filter>",
307: string);
308: }
309: Filter roundTrip = (Filter) DocumentFactory.getInstance(
310: new ByteArrayInputStream(output.toString().getBytes()),
311: null, Level.OFF);
312: assertEquals(filter, roundTrip);
313: }
314:
315: }
|