001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-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: * Created on July 21, 2003, 5:58 PM
017: */
018:
019: package org.geotools.feature;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import com.vividsolutions.jts.geom.Coordinate;
031: import com.vividsolutions.jts.geom.Geometry;
032: import com.vividsolutions.jts.geom.GeometryCollection;
033: import com.vividsolutions.jts.geom.GeometryFactory;
034: import com.vividsolutions.jts.geom.Point;
035: import com.vividsolutions.jts.geom.PrecisionModel;
036:
037: /**
038: *
039: * @author en
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/feature/FeatureCollectionTest.java $
041: */
042: public class FeatureCollectionTest extends TestCase {
043:
044: FeatureCollection features;
045:
046: public FeatureCollectionTest(String testName) {
047: super (testName);
048: }
049:
050: public static void main(String[] args) {
051: junit.textui.TestRunner.run(suite());
052: }
053:
054: public static Test suite() {
055: TestSuite suite = new TestSuite(FeatureCollectionTest.class);
056: return suite;
057: }
058:
059: protected void setUp() throws Exception {
060: features = FeatureCollections.newCollection();
061: FeatureType dumby = FeatureTypeFactory.newFeatureType(null,
062: "Dumby");
063: for (int i = 0; i < 100; i++) {
064: features.add(dumby.create(null));
065: }
066: }
067:
068: public Collection randomPiece(Collection original) {
069: LinkedList next = new LinkedList();
070: Iterator og = original.iterator();
071: while (og.hasNext()) {
072: if (Math.random() > .5) {
073: next.add(og.next());
074: } else {
075: og.next();
076: }
077: }
078: return next;
079: }
080:
081: public void testBounds() throws Exception {
082: PrecisionModel pm = new PrecisionModel();
083: Geometry[] g = new Geometry[4];
084: GeometryFactory gf = new GeometryFactory();
085:
086: g[0] = gf.createPoint(new Coordinate(0, 0));
087: g[1] = gf.createPoint(new Coordinate(0, 10));
088: g[2] = gf.createPoint(new Coordinate(10, 0));
089: g[3] = gf.createPoint(new Coordinate(10, 10));
090:
091: GeometryCollection gc = gf.createGeometryCollection(g);
092: FeatureTypeFactory factory = FeatureTypeFactory
093: .newInstance("bounds");
094: factory.addType(AttributeTypeFactory.newAttributeType("p1",
095: Point.class));
096: FeatureType t = factory.createFeatureType();
097: FeatureCollection fc = FeatureCollections.newCollection();
098: for (int i = 0; i < g.length; i++) {
099: fc.add(t.create(new Geometry[] { g[i] }));
100: }
101: assertEquals(gc.getEnvelopeInternal(), fc.getBounds());
102: }
103:
104: public void testSetAbilities() {
105: int size = features.size();
106: features.addAll(randomPiece(features));
107: assertEquals(features.size(), size);
108: }
109:
110: public void testAddRemoveAllAbilities() throws Exception {
111: Collection half = randomPiece(features);
112: Collection otherHalf = new ArrayList(features);
113: otherHalf.removeAll(half);
114: features.removeAll(half);
115: assertTrue(features.containsAll(otherHalf));
116: assertTrue(!features.containsAll(half));
117: features.removeAll(otherHalf);
118: assertTrue(features.size() == 0);
119: features.addAll(half);
120: assertTrue(features.containsAll(half));
121: features.addAll(otherHalf);
122: assertTrue(features.containsAll(otherHalf));
123: features.retainAll(otherHalf);
124: assertTrue(features.containsAll(otherHalf));
125: assertTrue(!features.containsAll(half));
126: features.addAll(otherHalf);
127: Iterator i = features.iterator();
128: while (i.hasNext()) {
129: i.next();
130: i.remove();
131: }
132: assertEquals(features.size(), 0);
133: assertTrue(!features.remove(FeatureTypeFactory.newFeatureType(
134: null, "XXX").create(null)));
135: }
136:
137: public void testAssorted() {
138: FeatureCollection copy = FeatureCollections.newCollection();
139: copy.addAll(features);
140: copy.clear();
141: assertTrue(copy.isEmpty());
142: copy.addAll(features);
143: assertTrue(!copy.isEmpty());
144: ArrayList list = new ArrayList(features);
145: Feature[] f1 = (Feature[]) list
146: .toArray(new Feature[list.size()]);
147: Feature[] f2 = (Feature[]) features.toArray(new Feature[list
148: .size()]);
149: assertEquals(f1.length, f2.length);
150: for (int i = 0; i < f1.length; i++) {
151: assertSame(f1[i], f2[i]);
152: }
153: FeatureIterator copyIterator = copy.features();
154: FeatureIterator featuresIterator = features.features();
155: while (copyIterator.hasNext() && featuresIterator.hasNext()) {
156: assertEquals(copyIterator.next(), featuresIterator.next());
157: }
158:
159: FeatureCollection listen = FeatureCollections.newCollection();
160: ListenerProxy counter = new ListenerProxy();
161: listen.addListener(counter);
162: listen.addAll(features);
163: assertEquals(1, counter.changeEvents);
164: listen.removeListener(counter);
165: listen.removeAll(features);
166: assertEquals(1, counter.changeEvents);
167: }
168:
169: static class ListenerProxy implements CollectionListener {
170: int changeEvents = 0;
171:
172: public void collectionChanged(
173: org.geotools.feature.CollectionEvent tce) {
174: changeEvents++;
175: }
176:
177: }
178: }
|