001: package org.geotools.xml;
002:
003: import java.net.URI;
004: import java.util.Date;
005:
006: import junit.framework.TestCase;
007:
008: import org.geotools.feature.AttributeTypeFactory;
009: import org.geotools.feature.DefaultFeatureTypeFactory;
010: import org.geotools.feature.Feature;
011: import org.geotools.feature.FeatureType;
012: import org.geotools.feature.FeatureTypeBuilder;
013:
014: import org.geotools.filter.expression.PropertyAccessor;
015:
016: import com.vividsolutions.jts.geom.Coordinate;
017: import com.vividsolutions.jts.geom.Geometry;
018: import com.vividsolutions.jts.geom.GeometryFactory;
019: import com.vividsolutions.jts.geom.Point;
020:
021: public class XPathPropertyAcessorTest extends TestCase {
022:
023: FeatureType type;
024: Feature target;
025:
026: protected void setUp() throws Exception {
027: FeatureTypeBuilder typeBuilder = new DefaultFeatureTypeFactory();
028: typeBuilder.setName("test");
029: typeBuilder
030: .setNamespace(new URI("http://www.geotools.org/test"));
031:
032: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
033: "name", String.class));
034: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
035: "description", String.class));
036: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
037: "geometry", Geometry.class));
038:
039: type = typeBuilder.getFeatureType();
040:
041: target = type.create(
042: new Object[] {
043: "theName",
044: "theDescription",
045: new GeometryFactory()
046: .createPoint(new Coordinate(0, 0)) },
047: "fid");
048:
049: }
050:
051: public void testSimpleXpath() {
052: PropertyAccessor accessor = accessor("name");
053: Object o = accessor.get(target, "name", null);
054: assertNotNull(o);
055: assertEquals("theName", o);
056:
057: accessor = accessor("description");
058: o = accessor.get(target, "description", null);
059: assertNotNull(o);
060: assertEquals("theDescription", o);
061:
062: accessor = accessor("geometry");
063: o = accessor.get(target, "geometry", null);
064: assertNotNull(o);
065: assertTrue(o instanceof Point);
066:
067: accessor = accessor("foo");
068: o = accessor.get(target, "foo", null);
069: assertNull(o);
070: }
071:
072: public void testSimpleXpathType() {
073: PropertyAccessor accessor = accessor("name");
074: Object o = accessor.get(type, "name", null);
075: assertNotNull(o);
076: assertEquals(type.getAttributeType("name"), o);
077:
078: accessor = accessor("description");
079: o = accessor.get(type, "description", null);
080: assertNotNull(o);
081: assertEquals(type.getAttributeType("description"), o);
082:
083: accessor = accessor("geometry");
084: o = accessor.get(type, "geometry", null);
085: assertNotNull(o);
086: assertEquals(type.getAttributeType("geometry"), o);
087:
088: accessor = accessor("foo");
089: o = accessor.get(type, "foo", null);
090: assertNull(o);
091: }
092:
093: public void testSimpleXpathWithNamespace() {
094: PropertyAccessor accessor = accessor("gml:name");
095: Object o = accessor.get(target, "gml:name", null);
096: assertNotNull(o);
097: assertEquals("theName", o);
098:
099: accessor = accessor("gml:description");
100: o = accessor.get(target, "gml:description", null);
101: assertNotNull(o);
102: assertEquals("theDescription", o);
103:
104: accessor = accessor("test:geometry");
105: o = accessor.get(target, "test:geometry", null);
106: assertNotNull(o);
107: assertTrue(o instanceof Point);
108: }
109:
110: public void testSimpleXpathWithNamespaceType() {
111: PropertyAccessor accessor = accessor("gml:name");
112: Object o = accessor.get(type, "gml:name", null);
113: assertNotNull(o);
114: assertEquals(type.getAttributeType("name"), o);
115:
116: accessor = accessor("gml:description");
117: o = accessor.get(type, "gml:description", null);
118: assertNotNull(o);
119: assertEquals(type.getAttributeType("description"), o);
120:
121: accessor = accessor("test:geometry");
122: o = accessor.get(type, "test:geometry", null);
123: assertNotNull(o);
124: assertEquals(type.getAttributeType("geometry"), o);
125: }
126:
127: public void testPath() {
128: PropertyAccessor accessor = accessor("//name");
129: Object o = accessor.get(target, "//name", null);
130: assertNotNull(o);
131: assertEquals("theName", o);
132:
133: accessor = accessor("//description");
134: o = accessor.get(target, "//description", null);
135: assertNotNull(o);
136: assertEquals("theDescription", o);
137:
138: accessor = accessor("//geometry");
139: o = accessor.get(target, "//geometry", null);
140: assertNotNull(o);
141: assertTrue(o instanceof Point);
142: }
143:
144: public void testPathType() {
145: PropertyAccessor accessor = accessor("//name");
146: Object o = accessor.get(type, "//name", null);
147: assertNotNull(o);
148: assertEquals(type.getAttributeType("name"), o);
149:
150: accessor = accessor("//description");
151: o = accessor.get(type, "//description", null);
152: assertNotNull(o);
153: assertEquals(type.getAttributeType("description"), o);
154:
155: accessor = accessor("//geometry");
156: o = accessor.get(type, "//geometry", null);
157: assertNotNull(o);
158: assertEquals(type.getAttributeType("geometry"), o);
159: }
160:
161: public void testPathWithNamespace() {
162: PropertyAccessor accessor = accessor("//gml:name");
163: Object o = accessor.get(target, "//gml:name", null);
164: assertNotNull(o);
165: assertEquals("theName", o);
166:
167: accessor = accessor("//gml:description");
168: o = accessor.get(target, "//gml:description", null);
169: assertNotNull(o);
170: assertEquals("theDescription", o);
171:
172: accessor = accessor("//test:geometry");
173: o = accessor.get(target, "//test:geometry", null);
174: assertNotNull(o);
175: assertTrue(o instanceof Point);
176: }
177:
178: public void testPathWithNamespaceType() {
179: PropertyAccessor accessor = accessor("//gml:name");
180: Object o = accessor.get(type, "//gml:name", null);
181: assertNotNull(o);
182: assertEquals(type.getAttributeType("name"), o);
183:
184: accessor = accessor("//gml:description");
185: o = accessor.get(type, "//gml:description", null);
186: assertNotNull(o);
187: assertEquals(type.getAttributeType("description"), o);
188:
189: accessor = accessor("//test:geometry");
190: o = accessor.get(type, "//test:geometry", null);
191: assertNotNull(o);
192: assertEquals(type.getAttributeType("geometry"), o);
193: }
194:
195: public void testIndex() {
196:
197: PropertyAccessor accessor = accessor("*[1]");
198: Object o = accessor.get(target, "*[1]", null);
199: assertNotNull(o);
200: assertEquals("theName", o);
201:
202: accessor = accessor("*[2]");
203: o = accessor.get(target, "*[2]", null);
204: assertNotNull(o);
205: assertEquals("theDescription", o);
206:
207: accessor = accessor("*[3]");
208: o = accessor.get(target, "*[3]", null);
209: assertNotNull(o);
210: assertTrue(o instanceof Point);
211: }
212:
213: public void testIndexType() {
214:
215: PropertyAccessor accessor = accessor("*[1]");
216: Object o = accessor.get(type, "*[1]", null);
217: assertNotNull(o);
218: assertEquals(type.getAttributeType("name"), o);
219:
220: accessor = accessor("*[2]");
221: o = accessor.get(type, "*[2]", null);
222: assertNotNull(o);
223: assertEquals(type.getAttributeType("description"), o);
224:
225: accessor = accessor("*[3]");
226: o = accessor.get(type, "*[3]", null);
227: assertNotNull(o);
228: assertEquals(type.getAttributeType("geometry"), o);
229: }
230:
231: public void testPosition() {
232: PropertyAccessor accessor = accessor("*[position()=1]");
233: Object o = accessor.get(target, "*[position()=1]", null);
234: assertEquals("theName", o);
235:
236: accessor = accessor("*[position()=2]");
237: o = accessor.get(target, "*[position()=2]", null);
238: assertNotNull(o);
239: assertEquals("theDescription", o);
240:
241: accessor = accessor("*[position()=3]");
242: o = accessor.get(target, "*[position()=3]", null);
243: assertNotNull(o);
244: assertTrue(o instanceof Point);
245: }
246:
247: public void testPositionType() {
248: PropertyAccessor accessor = accessor("*[position()=1]");
249: Object o = accessor.get(type, "*[position()=1]", null);
250: assertEquals(type.getAttributeType("name"), o);
251:
252: accessor = accessor("*[position()=2]");
253: o = accessor.get(type, "*[position()=2]", null);
254: assertNotNull(o);
255: assertEquals(type.getAttributeType("description"), o);
256:
257: accessor = accessor("*[position()=3]");
258: o = accessor.get(type, "*[position()=3]", null);
259: assertNotNull(o);
260: assertEquals(type.getAttributeType("geometry"), o);
261: }
262:
263: public void testId() {
264: PropertyAccessor accessor = accessor("./@gml:id");
265: Object o = accessor.get(target, "./@gml:id", null);
266: assertEquals("fid", o);
267: }
268:
269: public void testEmptyXpath() {
270: assertFalse(accessor("").canHandle(target, "", null));
271: }
272:
273: PropertyAccessor accessor(String xpath) {
274:
275: return new XPathPropertyAccessorFactory()
276: .createPropertyAccessor(Feature.class, xpath, null,
277: null);
278: }
279:
280: }
|