001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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;
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.complex.config;
018:
019: import java.net.URL;
020: import java.util.List;
021: import java.util.Map;
022:
023: import junit.framework.TestCase;
024:
025: import org.eclipse.xsd.XSDComplexTypeDefinition;
026: import org.eclipse.xsd.XSDElementDeclaration;
027: import org.eclipse.xsd.XSDTypeDefinition;
028: import org.geotools.feature.iso.Types;
029: import org.geotools.gml3.bindings.GML;
030: import org.geotools.xs.bindings.XS;
031: import org.opengis.feature.simple.SimpleFeatureType;
032: import org.opengis.feature.type.AttributeDescriptor;
033: import org.opengis.feature.type.AttributeType;
034: import org.opengis.feature.type.ComplexType;
035: import org.opengis.feature.type.FeatureType;
036: import org.opengis.feature.type.GeometryType;
037: import org.opengis.feature.type.Name;
038:
039: import com.vividsolutions.jts.geom.Geometry;
040: import com.vividsolutions.jts.geom.Point;
041:
042: /**
043: *
044: * @author Gabriel Roldan, Axios Engineering
045: * @version $Id: EmfAppSchemaReaderTest.java 28577 2008-01-03 15:44:29Z groldan $
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/test/java/org/geotools/data/complex/config/EmfAppSchemaReaderTest.java $
047: * @since 2.4
048: */
049: public class EmfAppSchemaReaderTest extends TestCase {
050:
051: /**
052: * Namespace URI of parsed types
053: */
054: private static final String NS_URI = "http://online.socialchange.net.au";
055:
056: private EmfAppSchemaReader schemaLoader;
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060: schemaLoader = EmfAppSchemaReader.newInstance();
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: schemaLoader = null;
066: }
067:
068: public void testParseSimpleFeatureType() throws Exception {
069: String res = "/test-data/simpleFeature.xsd";
070: URL resource = getClass().getResource(res);
071:
072: schemaLoader.parse(resource);
073:
074: Map parsedTypes = schemaLoader.getTypeRegistry();
075: assertNotNull(parsedTypes);
076:
077: Name typeName = Types.typeName(NS_URI, "simpleFeatureType");
078: AttributeType type = (AttributeType) parsedTypes.get(typeName);
079: assertNotNull(type);
080: assertTrue(type.getClass().getName(),
081: type instanceof SimpleFeatureType);
082: assertTrue(type.getUserData(XSDTypeDefinition.class) instanceof XSDComplexTypeDefinition);
083:
084: SimpleFeatureType ft = (SimpleFeatureType) type;
085: String local = ft.getName().getLocalPart();
086: String uri = ft.getName().getNamespaceURI();
087: assertEquals("simpleFeatureType", local);
088: assertEquals(NS_URI, uri);
089:
090: List/* <AttributeType> */attributes = (List) ft
091: .getProperties();
092: assertEquals(8, attributes.size());
093: AttributeDescriptor descriptor;
094:
095: descriptor = (AttributeDescriptor) attributes.get(5);
096: Name name = Types.typeName(NS_URI, "the_geom");
097: typeName = Types
098: .typeName(GML.NAMESPACE, "GeometryPropertyType");
099: assertTrue(descriptor.type() instanceof GeometryType);
100:
101: assertSimpleAttribute(descriptor, name, typeName,
102: Geometry.class, 1, 1);
103:
104: descriptor = (AttributeDescriptor) attributes.get(6);
105: name = Types.typeName(NS_URI, "stringAtt");
106: typeName = Types.typeName(XS.NAMESPACE, XS.STRING
107: .getLocalPart());
108:
109: assertSimpleAttribute(descriptor, name, typeName, String.class,
110: 1, 1);
111:
112: descriptor = (AttributeDescriptor) attributes.get(7);
113: name = Types.typeName(NS_URI, "intAtt");
114: typeName = Types.typeName(XS.NAMESPACE, XS.INT.getLocalPart());
115: assertSimpleAttribute(descriptor, name, typeName,
116: Integer.class, 1, 1);
117: }
118:
119: private void assertSimpleAttribute(AttributeDescriptor descriptor,
120: Name name, Name typeName, Class binding, int minOccurs,
121: int maxOccurs) {
122: AttributeType type;
123: assertEquals(name, descriptor.getName());
124: assertEquals(minOccurs, descriptor.getMinOccurs());
125: assertEquals(maxOccurs, descriptor.getMaxOccurs());
126: assertTrue(descriptor.getUserData(XSDElementDeclaration.class) instanceof XSDElementDeclaration);
127:
128: type = (AttributeType) descriptor.type();
129: assertNotNull(type);
130: assertFalse(type instanceof ComplexType);
131: assertEquals(typeName, type.getName());
132: assertEquals(binding, type.getBinding());
133: // they're prebuilt types, does not contains the emf information
134: // assertTrue(type.getUserData(EmfAppSchemaReader.EMF_USERDATA_KEY)
135: // instanceof XSDTypeDefinition);
136: }
137:
138: public void testComplexFeatureType() throws Exception {
139: String res = "/test-data/complexFeature.xsd";
140: URL resource = getClass().getResource(res);
141: schemaLoader.parse(resource);
142:
143: Map typeRegistry = schemaLoader.getTypeRegistry();
144: Map descriptorRegistry = schemaLoader.getDescriptorRegistry();
145:
146: assertNotNull(typeRegistry);
147:
148: Name typeName = Types.typeName(NS_URI, "wq_plus_Type");
149: AttributeType type = (AttributeType) typeRegistry.get(typeName);
150: assertTrue(type instanceof FeatureType);
151: assertFalse(type instanceof SimpleFeatureType);
152: assertEquals(typeName, type.getName());
153: assertTrue(type.getUserData(XSDTypeDefinition.class) instanceof XSDComplexTypeDefinition);
154:
155: FeatureType wq_plus_Type = (FeatureType) type;
156:
157: assertNotNull(wq_plus_Type.getDefaultGeometry());
158: assertNotNull(wq_plus_Type.getSuper());
159: typeName = Types.typeName(GML.NAMESPACE,
160: GML.AbstractFeatureType.getLocalPart());
161: assertEquals(typeName, wq_plus_Type.getSuper().getName());
162: assertNotNull(wq_plus_Type.getProperties());
163: assertEquals(8, wq_plus_Type.getProperties().size());
164:
165: Name name = Types.typeName(NS_URI, "wq_plus");
166: AttributeDescriptor wqPlusDescriptor = (AttributeDescriptor) descriptorRegistry
167: .get(name);
168: assertNotNull(wqPlusDescriptor);
169: assertEquals(name, wqPlusDescriptor.getName());
170: assertSame(wq_plus_Type, wqPlusDescriptor.type());
171: assertTrue(wqPlusDescriptor
172: .getUserData(XSDElementDeclaration.class) instanceof XSDElementDeclaration);
173:
174: typeName = Types.typeName(NS_URI, "measurementType");
175: type = (AttributeType) typeRegistry.get(typeName);
176: assertTrue(type instanceof ComplexType);
177: assertFalse(type instanceof FeatureType);
178: assertTrue(type.getUserData(XSDTypeDefinition.class) instanceof XSDComplexTypeDefinition);
179:
180: ComplexType measurementType = (ComplexType) type;
181: assertEquals(typeName, measurementType.getName());
182: assertTrue(measurementType.isIdentified());
183: assertFalse(measurementType.isAbstract());
184: assertEquals(2, measurementType.getProperties().size());
185:
186: name = Types.typeName(NS_URI, "measurement");
187: AttributeDescriptor descriptor;
188: descriptor = (AttributeDescriptor) Types.descriptor(
189: wq_plus_Type, name);
190: assertNotNull(descriptor);
191: assertEquals(name, descriptor.getName());
192: assertNotNull(descriptor.type());
193: assertSame(measurementType, descriptor.type());
194: assertEquals(0, descriptor.getMinOccurs());
195: assertEquals(Integer.MAX_VALUE, descriptor.getMaxOccurs());
196: assertTrue(descriptor.getUserData(XSDElementDeclaration.class) instanceof XSDElementDeclaration);
197:
198: name = Types.typeName(NS_URI, "result");
199: descriptor = (AttributeDescriptor) Types.descriptor(
200: measurementType, name);
201: typeName = Types
202: .typeName(XS.NAMESPACE, XS.FLOAT.getLocalPart());
203: assertSimpleAttribute(descriptor, name, typeName, Float.class,
204: 1, 1);
205:
206: name = Types.typeName(NS_URI, "determinand_description");
207: descriptor = (AttributeDescriptor) Types.descriptor(
208: measurementType, name);
209: typeName = Types.typeName(XS.NAMESPACE, XS.STRING
210: .getLocalPart());
211: assertSimpleAttribute(descriptor, name, typeName, String.class,
212: 1, 1);
213:
214: name = Types.typeName(NS_URI, "the_geom");
215: descriptor = (AttributeDescriptor) Types.descriptor(
216: wq_plus_Type, name);
217: typeName = Types.typeName(GML.NAMESPACE, GML.PointPropertyType
218: .getLocalPart());
219: assertSimpleAttribute(descriptor, name, typeName, Point.class,
220: 1, 1);
221:
222: name = Types.typeName(NS_URI, "sitename");
223: descriptor = (AttributeDescriptor) Types.descriptor(
224: wq_plus_Type, name);
225: typeName = Types.typeName(XS.NAMESPACE, XS.STRING
226: .getLocalPart());
227: assertSimpleAttribute(descriptor, name, typeName, String.class,
228: 1, Integer.MAX_VALUE);
229:
230: }
231:
232: public void testSimpleAttributeFromComplexDeclaration()
233: throws Exception {
234: String res = "/test-data/complexFeature.xsd";
235: URL resource = getClass().getResource(res);
236: schemaLoader.parse(resource);
237:
238: Map registry = schemaLoader.getTypeRegistry();
239:
240: Name tcl = Types.typeName(NS_URI, "TypedCategoryListType");
241: AttributeType typedCategoryListType = (AttributeType) registry
242: .get(tcl);
243: assertNotNull(typedCategoryListType);
244: assertFalse(typedCategoryListType instanceof ComplexType);
245:
246: AttributeType super Type = typedCategoryListType.getSuper();
247: assertNotNull(super Type);
248: Name super Name = super Type.getName();
249: assertEquals(XS.STRING.getNamespaceURI(), super Name
250: .getNamespaceURI());
251: assertEquals(XS.STRING.getLocalPart(), super Name.getLocalPart());
252:
253: assertNotNull(typedCategoryListType
254: .getUserData(XSDTypeDefinition.class));
255: }
256: }
|