001: package org.geotools.feature.iso.xpath;
002:
003: import junit.framework.TestCase;
004:
005: import org.apache.commons.jxpath.JXPathContext;
006: import org.apache.commons.jxpath.JXPathIntrospector;
007: import org.geotools.feature.iso.AttributeBuilder;
008: import org.geotools.feature.iso.AttributeFactoryImpl;
009: import org.geotools.feature.iso.FeatureImpl;
010: import org.geotools.feature.iso.TypeBuilder;
011: import org.geotools.feature.iso.type.TypeFactoryImpl;
012: import org.geotools.feature.type.TypeName;
013: import org.geotools.util.GTContainer;
014: import org.opengis.feature.Attribute;
015: import org.opengis.feature.Feature;
016: import org.opengis.feature.type.AttributeType;
017: import org.opengis.feature.type.FeatureType;
018: import org.opengis.feature.type.TypeFactory;
019: import org.picocontainer.MutablePicoContainer;
020: import org.picocontainer.defaults.DefaultPicoContainer;
021:
022: public class AttributePropertyHandlerTest extends TestCase {
023:
024: MutablePicoContainer container;
025:
026: TypeBuilder tb;
027:
028: AttributeBuilder fb;
029:
030: FeatureType type;
031:
032: Feature f;
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036:
037: container = GTContainer.simple();
038: container = new DefaultPicoContainer();
039: container
040: .registerComponentImplementation(AttributeFactoryImpl.class);
041: container
042: .registerComponentImplementation(TypeFactoryImpl.class);
043:
044: container.registerComponentImplementation(TypeBuilder.class);
045: container
046: .registerComponentImplementation(AttributeBuilder.class);
047:
048: tb = (TypeBuilder) container
049: .getComponentInstanceOfType(TypeBuilder.class);
050: fb = (AttributeBuilder) container
051: .getComponentInstanceOfType(AttributeBuilder.class);
052:
053: TypeFactory tf = tb.getTypeFactory();
054: AttributeType integerType = tf.createAttributeType(
055: new TypeName("integerType"), Integer.class, false,
056: false, null, null, null);
057: AttributeType stringType = tf.createAttributeType(new TypeName(
058: "stringType"), String.class, false, false, null, null,
059: null);
060: AttributeType doubleType = tf.createAttributeType(new TypeName(
061: "doubleType"), Double.class, false, false, null, null,
062: null);
063: tb.addBinding(Integer.class, integerType);
064: tb.addBinding(String.class, stringType);
065: tb.addBinding(Double.class, doubleType);
066:
067: tb.init();
068: tb.attribute("id", Integer.class);
069: tb.attribute("name", String.class);
070: tb.setName("type");
071: type = tb.feature();
072:
073: fb.init();
074: fb.setType(type);
075: fb.add(new Integer(1), "id");
076: fb.add("foo", "name");
077: f = (Feature) fb.build("f1");
078: }
079:
080: public void testSimpleFeature() {
081:
082: JXPathIntrospector.registerDynamicClass(FeatureImpl.class,
083: AttributePropertyHandler.class);
084:
085: JXPathContext context = JXPathContext.newContext(f);
086:
087: Attribute id = (Attribute) context.getValue("id");
088: assertNotNull(id);
089: assertEquals(new Integer(1), id.getValue());
090:
091: // Object id = context.getValue("id");
092: // assertNotNull(id);
093: // assertEquals(new Integer(1), id);
094:
095: Attribute name = (Attribute) context.getValue("name");
096: assertNotNull(name);
097: assertEquals("foo", name.getValue());
098:
099: // Object name = context.getValue("name");
100: // assertNotNull(name);
101: // assertEquals("foo", name);
102: }
103:
104: public void testNonSimpleFeature() {
105: tb.init();
106:
107: tb.attribute("xid", Double.class);
108: tb.attribute("feature", type);
109: tb.name("ctype");
110: FeatureType ctype = tb.feature();
111:
112: fb.init();
113: fb.setType(ctype);
114: fb.add(new Double(1), "xid");
115: fb.add(f.getValue(), "feature");
116:
117: Feature cf = (Feature) fb.build("f1");
118:
119: JXPathIntrospector.registerDynamicClass(FeatureImpl.class,
120: AttributePropertyHandler.class);
121:
122: JXPathContext context = JXPathContext.newContext(cf);
123:
124: Attribute xid = (Attribute) context.getValue("xid");
125: assertNotNull(xid);
126: assertEquals(new Double(1), xid.getValue());
127:
128: // Object xid = context.getValue("xid");
129: // assertNotNull(xid);
130: // assertEquals(new Double(1), xid);
131:
132: Attribute id = (Attribute) context.getValue("feature/id");
133: assertNotNull(id);
134: assertEquals(new Integer(1), id.getValue());
135:
136: // Object id = context.getValue("feature/id");
137: // assertNotNull(id);
138: // assertEquals(new Integer(1), id);
139:
140: Attribute name = (Attribute) context.getValue("feature/name");
141: assertNotNull(name);
142: assertEquals("foo", name.getValue());
143:
144: // Object name = context.getValue("feature/name");
145: // assertNotNull(name);
146: // assertEquals("foo", name);
147: }
148:
149: }
|