/
MemoryFeatureCollectionTest.java
166 lines (156 loc) · 5.32 KB
/
MemoryFeatureCollectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2003-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.data.memory;
import java.io.Closeable;
import java.util.Arrays;
import java.util.Iterator;
import org.geotools.data.DataTestCase;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.collection.FilteredIterator;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
/**
*
*
* @source $URL$
*/
public class MemoryFeatureCollectionTest extends DataTestCase {
private MemoryFeatureCollection roads;
public MemoryFeatureCollectionTest( String test) {
super(test);
}
protected void setUp() throws Exception {
super.setUp();
roads = new MemoryFeatureCollection( roadType );
roads.addAll( Arrays.asList( roadFeatures ) );
}
public void testAdd(){
MemoryFeatureCollection rivers = new MemoryFeatureCollection( riverType );
for( int i=0; i<riverFeatures.length;i++){
rivers.add( riverFeatures[i] );
}
assertEquals( riverFeatures.length, rivers.size() );
}
public void testAddAll(){
MemoryFeatureCollection rivers = new MemoryFeatureCollection( riverType );
rivers.addAll( Arrays.asList( riverFeatures ) );
}
public void testSize(){
assertEquals( roadFeatures.length, roads.size() );
}
public void testResources(){
Object[] array = roads.toArray();
assertEquals( roads.size(), array.length );
SimpleFeatureIterator i = roads.features();
try {
assertTrue( i.hasNext() );
}
finally {
i.close();
}
try {
assertFalse( i.hasNext() );
fail("should be closed");
}
catch( IllegalStateException closed ){
}
i=roads.features();
try {
assertTrue( i.hasNext() );
}
finally {
i.close();
}
try {
assertFalse( i.hasNext() );
fail("should be closed");
}
catch( IllegalStateException closed ){
}
}
public void testBounds() {
MemoryFeatureCollection rivers = new MemoryFeatureCollection(riverType);
ReferencedEnvelope expected = new ReferencedEnvelope();
for (int i = 0; i < riverFeatures.length; i++) {
rivers.add(riverFeatures[i]);
expected.include(riverFeatures[i].getBounds());
}
assertEquals(riverFeatures.length, rivers.size());
// Should not throw an UnsupportedOperationException
assertNotNull(rivers.getBounds());
assertEquals( expected, rivers.getBounds() );
}
/**
* This feature collection is still implementing Collection so we best check it works
*/
public void testIterator() throws Exception {
int count=0;
Iterator<SimpleFeature> it = roads.iterator();
try {
while( it.hasNext() ){
@SuppressWarnings("unused")
SimpleFeature feature = it.next();
count++;
}
} finally {
if( it instanceof Closeable){
((Closeable)it).close();
}
}
assertEquals( roads.size(), count );
count=0;
FilteredIterator<SimpleFeature> filteredIterator = new FilteredIterator<SimpleFeature>( roads, rd12Filter );
try {
while( filteredIterator.hasNext() ){
@SuppressWarnings("unused")
SimpleFeature feature = filteredIterator.next();
count++;
}
} finally {
filteredIterator.close();
}
assertEquals( expected( rd12Filter) , count );
}
public void testSubCollection(){
int count = 0;
SimpleFeatureIterator it = roads.features();
try {
while( it.hasNext() ){
SimpleFeature feature = it.next();
if( rd12Filter.evaluate( feature )){
count++;
}
}
} finally {
it.close();
}
SimpleFeatureCollection sub = roads.subCollection( rd12Filter );
assertEquals( count, sub.size() );
}
public void testSubSubCollection(){
SimpleFeatureCollection sub = roads.subCollection( rd12Filter );
SimpleFeatureCollection subsub = sub.subCollection( rd1Filter );
assertEquals( 1, subsub.size() );
}
public void XtestSort(){
// FeatureList fList = roads.sort(SortBy.NATURAL_ORDER);
// for (Object obj : fList) {
// System.out.println(obj);
// }
}
}