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.feature;
017:
018: import com.vividsolutions.jts.geom.Coordinate;
019: import com.vividsolutions.jts.geom.GeometryFactory;
020: import com.vividsolutions.jts.geom.LineString;
021: import com.vividsolutions.jts.geom.MultiLineString;
022: import com.vividsolutions.jts.geom.Point;
023: import com.vividsolutions.jts.geom.PrecisionModel;
024: import org.geotools.feature.type.ChoiceAttributeType;
025:
026: /**
027: * This is a support class which creates test features for use in testing.
028: *
029: * @author jamesm
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/feature/SampleFeatureFixtures.java $
031: */
032: public class SampleFeatureFixtures {
033: /**
034: * Feature on which to preform tests
035: */
036:
037: // private Feature testFeature = null;
038: /**
039: * Creates a new instance of SampleFeatureFixtures
040: */
041: public SampleFeatureFixtures() {
042: }
043:
044: public static Feature createFeature() {
045: try {
046: FeatureType testType = createTestType();
047: Object[] attributes = createAttributes();
048:
049: return testType.create(attributes);
050: } catch (Exception e) {
051: Error ae = new AssertionError(
052: "Sample Feature for tests has been misscoded");
053: ae.initCause(e);
054: throw ae;
055: }
056: }
057:
058: public static Feature createAddressFeature() {
059: try {
060: return createFeature();
061:
062: //FeatureType addressType = createAddressType();
063: //Object[] attributes = createComplexAttributes();
064: //return addressType.create(attributes);
065: } catch (Exception e) {
066: Error ae = new AssertionError(
067: "Sample Feature for tests has been misscoded");
068: ae.initCause(e);
069: throw ae;
070: }
071: }
072:
073: /**
074: * creates and returns an array of sample attributes.
075: *
076: */
077: public static Object[] createAttributes() {
078: Object[] attributes = new Object[10];
079: GeometryFactory gf = new GeometryFactory();
080: attributes[0] = gf.createPoint(new Coordinate(1, 2));
081: attributes[1] = new Boolean(true);
082: attributes[2] = new Character('t');
083: attributes[3] = new Byte("10");
084: attributes[4] = new Short("101");
085: attributes[5] = new Integer(1002);
086: attributes[6] = new Long(10003);
087: attributes[7] = new Float(10000.4);
088: attributes[8] = new Double(100000.5);
089: attributes[9] = "test string data";
090:
091: return attributes;
092: }
093:
094: //If we go to factories/protected constructors this won't work, will need
095: //to move to a types directory, or use the factory
096: public static AttributeType getChoiceAttrType1() {
097: return createChoiceAttrType("choiceTest1", createType1Choices());
098: }
099:
100: public static AttributeType[] createType1Choices() {
101: AttributeType[] choices = new AttributeType[3];
102: choices[0] = AttributeTypeFactory.newAttributeType("testByte",
103: Byte.class);
104: choices[1] = AttributeTypeFactory.newAttributeType(
105: "testDouble", Double.class);
106: choices[2] = AttributeTypeFactory.newAttributeType(
107: "testString", String.class);
108:
109: return choices;
110: }
111:
112: public static AttributeType getChoiceAttrType2() {
113: AttributeType[] choices = new AttributeType[2];
114: choices[0] = AttributeTypeFactory.newAttributeType(
115: "testString", String.class, false);
116: choices[1] = AttributeTypeFactory.newAttributeType("testInt",
117: Integer.class, false);
118:
119: return createChoiceAttrType("choiceTest2", choices);
120: }
121:
122: public static AttributeType createChoiceAttrType(String name,
123: AttributeType[] choices) {
124: return new ChoiceAttributeType(name, choices);
125: }
126:
127: public static AttributeType createGeomChoiceAttrType(String name,
128: GeometryAttributeType[] choices) {
129: return new ChoiceAttributeType.Geometric(name, choices);
130: }
131:
132: public static AttributeType getChoiceGeomType() {
133: GeometryAttributeType[] choices = new GeometryAttributeType[2];
134: choices[0] = (GeometryAttributeType) AttributeTypeFactory
135: .newAttributeType("testLine", LineString.class);
136: choices[1] = (GeometryAttributeType) AttributeTypeFactory
137: .newAttributeType("testMultiLine",
138: MultiLineString.class);
139:
140: return createGeomChoiceAttrType("choiceGeom", choices);
141: }
142:
143: public static FeatureType createChoiceFeatureType() {
144: FeatureTypeFactory typeFactory = FeatureTypeFactory
145: .newInstance("test");
146: typeFactory.addType(getChoiceAttrType1());
147: typeFactory.addType(getChoiceAttrType2());
148: typeFactory.addType(getChoiceGeomType());
149: typeFactory
150: .setDefaultGeometry((GeometryAttributeType) typeFactory
151: .get(2));
152:
153: try {
154: return typeFactory.getFeatureType();
155: } catch (SchemaException se) {
156: throw new RuntimeException(
157: "Programmer error making choice ftype", se);
158: }
159: }
160:
161: /**
162: * DOCUMENT ME!
163: *
164: *
165: * @throws SchemaException
166: */
167: public static FeatureType createTestType() throws SchemaException {
168: FeatureTypeFactory typeFactory = FeatureTypeFactory
169: .newInstance("test");
170: typeFactory.addType(AttributeTypeFactory.newAttributeType(
171: "testGeometry", Point.class));
172:
173: typeFactory.addType(AttributeTypeFactory.newAttributeType(
174: "testBoolean", Boolean.class));
175:
176: typeFactory.addType(AttributeTypeFactory.newAttributeType(
177: "testCharacter", Character.class));
178: typeFactory.addType(AttributeTypeFactory.newAttributeType(
179: "testByte", Byte.class));
180: typeFactory.addType(AttributeTypeFactory.newAttributeType(
181: "testShort", Short.class));
182: typeFactory.addType(AttributeTypeFactory.newAttributeType(
183: "testInteger", Integer.class));
184: typeFactory.addType(AttributeTypeFactory.newAttributeType(
185: "testLong", Long.class));
186: typeFactory.addType(AttributeTypeFactory.newAttributeType(
187: "testFloat", Float.class));
188: typeFactory.addType(AttributeTypeFactory.newAttributeType(
189: "testDouble", Double.class));
190: typeFactory.addType(AttributeTypeFactory.newAttributeType(
191: "testString", String.class));
192: typeFactory
193: .setDefaultGeometry((GeometryAttributeType) typeFactory
194: .get(0));
195:
196: return typeFactory.getFeatureType();
197: }
198: }
|