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 java.io.IOException;
019: import java.nio.channels.FileChannel;
020:
021: import org.geotools.data.shapefile.indexed.FidIndexer;
022: import org.geotools.data.shapefile.indexed.IndexedFidReader;
023: import org.geotools.data.shapefile.indexed.IndexedFidWriter;
024: import org.geotools.data.shapefile.shp.IndexFile;
025:
026: public class IndexedFidReaderTest extends FIDTestCase {
027: private IndexedFidReader reader;
028:
029: private IndexFile indexFile;
030:
031: protected void setUp() throws Exception {
032: super .setUp();
033:
034: FidIndexer.generate(backshp.toURL());
035:
036: indexFile = new IndexFile(FidIndexer.getReadChannel(backshx
037: .toURL()));
038: reader = new IndexedFidReader(TYPE_NAME, FidIndexer
039: .getReadChannel(fixFile.toURL()));
040: }
041:
042: protected void tearDown() throws Exception {
043: reader.close();
044: indexFile.close();
045: super .tearDown();
046: }
047:
048: /*
049: * Test method for 'org.geotools.index.fid.IndexedFidReader.findFid(String)'
050: */
051: public void testFindFid() throws Exception {
052: long offset = reader.findFid("roads.4");
053: assertEquals(3, offset);
054:
055: offset = reader.findFid("roads.1");
056: assertEquals(0, offset);
057:
058: // test if the fid is too high
059: offset = reader.findFid("roads.10000000");
060: assertEquals(-1, offset);
061:
062: // test if the fid is negative
063: offset = reader.findFid("roads.-1");
064: assertEquals(-1, offset);
065:
066: }
067:
068: // test if FID no longer exists.
069: public void testFindDeletedFID() throws Exception {
070: reader.close();
071:
072: FileChannel channel = FidIndexer.getWriteChannel(fixFile
073: .toURL());
074: IndexedFidWriter writer = new IndexedFidWriter(channel,
075: new IndexedFidReader(TYPE_NAME, channel));
076: try {
077: writer.next();
078: writer.next();
079: writer.next();
080: writer.remove();
081: while (writer.hasNext()) {
082: writer.next();
083: }
084: } finally {
085: writer.close();
086: reader.close();
087: }
088:
089: reader = new IndexedFidReader(TYPE_NAME, FidIndexer
090: .getReadChannel(fixFile.toURL()));
091:
092: long offset = reader.findFid(TYPE_NAME + ".11");
093: assertEquals(9, offset);
094:
095: offset = reader.findFid(TYPE_NAME + ".4");
096: assertEquals(2, offset);
097:
098: offset = reader.findFid(TYPE_NAME + ".3");
099: assertEquals(-1, offset);
100:
101: }
102:
103: public void testHardToFindFid() throws Exception {
104: long offset = reader.search(5, 3, 7, 5);
105: assertEquals(4, offset);
106: }
107:
108: /*
109: * Test method for 'org.geotools.index.fid.IndexedFidReader.goTo(int)'
110: */
111: public void testGoTo() throws IOException {
112: reader.goTo(10);
113: assertEquals(TYPE_NAME + ".11", reader.next());
114: assertTrue(reader.hasNext());
115:
116: reader.goTo(15);
117: assertEquals(TYPE_NAME + ".16", reader.next());
118: assertTrue(reader.hasNext());
119:
120: reader.goTo(0);
121: assertEquals(TYPE_NAME + ".1", reader.next());
122: assertTrue(reader.hasNext());
123:
124: reader.goTo(3);
125: assertEquals(TYPE_NAME + ".4", reader.next());
126: assertTrue(reader.hasNext());
127: }
128: }
|