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;
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: package org.geotools.data.shapefile.indexed;
017:
018: import org.geotools.data.shapefile.indexed.FidIndexer;
019: import org.geotools.data.shapefile.indexed.IndexedFidReader;
020: import org.geotools.data.shapefile.indexed.IndexedFidWriter;
021: import org.geotools.data.shapefile.shp.IndexFile;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024: import java.net.MalformedURLException;
025: import java.nio.channels.FileChannel;
026:
027: public class IndexedFidWriterTest extends FIDTestCase {
028: private IndexFile indexFile;
029: private IndexedFidWriter writer;
030:
031: protected void setUp() throws Exception {
032: super .setUp();
033: }
034:
035: private void initWriter() throws IOException, MalformedURLException {
036: close();
037: indexFile = new IndexFile(FidIndexer.getReadChannel(backshx
038: .toURL()));
039:
040: FileChannel writeChannel = FidIndexer.getWriteChannel(fixFile
041: .toURL());
042: writer = new IndexedFidWriter(writeChannel,
043: new IndexedFidReader(TYPE_NAME, writeChannel));
044: }
045:
046: protected void tearDown() throws Exception {
047: close();
048: super .tearDown();
049: }
050:
051: private void close() throws IOException {
052: if ((writer != null) && !writer.isClosed()) {
053: writer.close();
054: }
055:
056: try {
057: if (indexFile != null) {
058: indexFile.close();
059: }
060: } catch (Exception e) {
061: // fine if already closed
062: }
063: }
064:
065: /*
066: * Test method for 'org.geotools.index.fid.IndexedFidWriter.hasNext()'
067: */
068: public void testHasNext() throws MalformedURLException, IOException {
069: FidIndexer.generate(backshp.toURL());
070: initWriter();
071:
072: for (int i = 1, j = indexFile.getRecordCount(); i < j; i++) {
073: assertTrue(i + "th record", writer.hasNext());
074: assertEquals((long) i, writer.next());
075: }
076: }
077:
078: /*
079: * Test method for 'org.geotools.index.fid.IndexedFidWriter.remove()'
080: */
081: public void testRemove() throws MalformedURLException, IOException {
082: FidIndexer.generate(backshp.toURL());
083: initWriter();
084: writer.next();
085: writer.remove();
086:
087: for (int i = 2, j = indexFile.getRecordCount(); i < j; i++) {
088: assertTrue(writer.hasNext());
089: assertEquals((long) i, writer.next());
090: }
091:
092: writer.write();
093: close();
094:
095: initWriter();
096:
097: for (int i = 1, j = indexFile.getRecordCount() - 1; i < j; i++) {
098: assertTrue(writer.hasNext());
099: assertEquals((long) i + 1, writer.next());
100: }
101: }
102:
103: public void testRemoveCounting() throws Exception {
104: FidIndexer.generate(backshp.toURL());
105: initWriter();
106: writer.next();
107: writer.remove();
108: writer.next();
109: writer.remove();
110: writer.next();
111: writer.remove();
112:
113: while (writer.hasNext()) {
114: writer.next();
115: writer.write();
116: }
117:
118: close();
119: IndexedFidReader reader = new IndexedFidReader(TYPE_NAME,
120: FidIndexer.getReadChannel(fixFile.toURL()));
121: try {
122: assertEquals(3, reader.getRemoves());
123: } finally {
124: reader.close();
125: }
126:
127: // remove some more features
128: initWriter();
129: writer.next();
130: writer.next();
131: writer.next();
132: writer.remove();
133: writer.next();
134: writer.remove();
135: writer.next();
136: writer.next();
137: writer.next();
138: writer.remove();
139: while (writer.hasNext()) {
140: writer.next();
141: writer.write();
142: }
143:
144: close();
145:
146: reader = new IndexedFidReader(TYPE_NAME, FidIndexer
147: .getReadChannel(fixFile.toURL()));
148: try {
149: assertEquals(6, reader.getRemoves());
150: } finally {
151: reader.close();
152: }
153:
154: }
155:
156: /*
157: * Test method for 'org.geotools.index.fid.IndexedFidWriter.write()'
158: */
159: public void testWrite() throws IOException {
160: initWriter();
161:
162: for (int i = 0; i < 5; i++) {
163: writer.next();
164: writer.write();
165: }
166:
167: close();
168: initWriter();
169:
170: for (int i = 1; i < 5; i++) {
171: assertTrue(writer.hasNext());
172: assertEquals((long) i, writer.next());
173: }
174: }
175:
176: }
|