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.data;
017:
018: import java.io.IOException;
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.geotools.feature.Feature;
025: import org.geotools.feature.FeatureCollection;
026: import org.geotools.feature.FeatureCollections;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.feature.IllegalAttributeException;
029:
030: /**
031: * Tests the ArrayFeatureReader class
032: *
033: * @author jones
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/data/ArrayFeatureReaderTest.java $
035: */
036: public class ArrayFeatureReaderTest extends TestCase {
037: private CollectionFeatureReader arrayReader;
038: private CollectionFeatureReader collectionReader;
039: private CollectionFeatureReader featureCollectionReader;
040: private FeatureType type;
041: private Feature[] features;
042:
043: protected void setUp() throws Exception {
044: type = DataUtilities.createType("TestType", "geom:Geometry");
045: features = new Feature[] {
046: type.create(new Object[] { null }, "f1"),
047: type.create(new Object[] { null }, "f2"),
048: type.create(new Object[] { null }, "f3"),
049: type.create(new Object[] { null }, "f4"),
050: type.create(new Object[] { null }, "f5"),
051: type.create(new Object[] { null }, "f6") };
052:
053: FeatureCollection collection = FeatureCollections
054: .newCollection();
055: List list = Arrays.asList(features);
056: collection.addAll(list);
057: arrayReader = new CollectionFeatureReader(features);
058: collectionReader = new CollectionFeatureReader(list, type);
059: featureCollectionReader = new CollectionFeatureReader(
060: collection, type);
061: }
062:
063: /**
064: * Test method for 'org.geotools.data.ArrayFeatureReader.getFeatureType()'
065: */
066: public void testGetFeatureType() {
067: assertEquals(type, arrayReader.getFeatureType());
068: assertEquals(type, collectionReader.getFeatureType());
069: assertEquals(type, featureCollectionReader.getFeatureType());
070: }
071:
072: /**
073: * Test method for 'org.geotools.data.ArrayFeatureReader.next()'
074: *
075: * @throws Exception
076: */
077: public void testNext() throws Exception {
078: assertEquals(features[0], arrayReader.next());
079: assertEquals(features[1], arrayReader.next());
080: assertEquals(features[2], arrayReader.next());
081: assertEquals(features[3], arrayReader.next());
082: assertEquals(features[4], arrayReader.next());
083: assertEquals(features[5], arrayReader.next());
084:
085: assertEquals(features[0], collectionReader.next());
086: assertEquals(features[1], collectionReader.next());
087: assertEquals(features[2], collectionReader.next());
088: assertEquals(features[3], collectionReader.next());
089: assertEquals(features[4], collectionReader.next());
090: assertEquals(features[5], collectionReader.next());
091:
092: assertEquals(features[0], featureCollectionReader.next());
093: assertEquals(features[1], featureCollectionReader.next());
094: assertEquals(features[2], featureCollectionReader.next());
095: assertEquals(features[3], featureCollectionReader.next());
096: assertEquals(features[4], featureCollectionReader.next());
097: assertEquals(features[5], featureCollectionReader.next());
098: }
099:
100: /**
101: * Test method for 'org.geotools.data.ArrayFeatureReader.hasNext()'
102: *
103: * @throws Exception DOCUMENT ME!
104: */
105: public void testHasNext() throws Exception {
106: testHasNext(arrayReader);
107: testHasNext(collectionReader);
108: testHasNext(featureCollectionReader);
109: }
110:
111: private void testHasNext(FeatureReader arrayReader)
112: throws IOException, IllegalAttributeException {
113: assertTrue(arrayReader.hasNext());
114: arrayReader.next();
115: assertTrue(arrayReader.hasNext());
116: arrayReader.next();
117: assertTrue(arrayReader.hasNext());
118: arrayReader.next();
119: assertTrue(arrayReader.hasNext());
120: arrayReader.next();
121: assertTrue(arrayReader.hasNext());
122: arrayReader.next();
123: assertTrue(arrayReader.hasNext());
124: arrayReader.next();
125: assertFalse(arrayReader.hasNext());
126: }
127:
128: /**
129: * Test method for 'org.geotools.data.ArrayFeatureReader.close()'
130: *
131: * @throws Exception DOCUMENT ME!
132: */
133: public void testClose() throws Exception {
134: arrayReader.close();
135: assertFalse(arrayReader.hasNext());
136:
137: try {
138: arrayReader.next();
139: fail();
140: } catch (Exception e) {
141: // good
142: }
143:
144: collectionReader.close();
145: assertFalse(collectionReader.hasNext());
146:
147: try {
148: collectionReader.next();
149: fail();
150: } catch (Exception e) {
151: // good
152: }
153:
154: featureCollectionReader.close();
155: assertFalse(featureCollectionReader.hasNext());
156:
157: try {
158: featureCollectionReader.next();
159: fail();
160: } catch (Exception e) {
161: // good
162: }
163: }
164: }
|