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.index.rtree;
017:
018: import com.vividsolutions.jts.geom.Envelope;
019: import junit.framework.TestCase;
020: import org.geotools.index.Data;
021: import org.geotools.index.DataDefinition;
022: import org.geotools.index.rtree.fs.FileSystemPageStore;
023: import org.geotools.index.rtree.memory.MemoryPageStore;
024: import java.io.File;
025: import java.util.Collection;
026:
027: /**
028: * DOCUMENT ME!
029: *
030: * @author Tommaso Nolli
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/test/java/org/geotools/index/rtree/RTreeTest.java $
032: */
033: public class RTreeTest extends TestCase {
034: private static final short FILE = 0;
035: private static final short MEMORY = 1;
036: private static final short NUM_IMPLEMENTATIONS = 2;
037: private DataDefinition dd = null;
038:
039: /**
040: * Constructor for RTreeTest.
041: *
042: * @param arg0
043: */
044: public RTreeTest(String arg0) {
045: super (arg0);
046: }
047:
048: private RTree getRTree(short type) throws Exception {
049: dd = new DataDefinition("US-ASCII");
050: dd.addField(Integer.class);
051:
052: PageStore ps = null;
053:
054: switch (type) {
055: case FILE:
056:
057: File file = File.createTempFile("geotools2", ".grx");
058: file.deleteOnExit();
059: ps = new FileSystemPageStore(file, dd);
060:
061: break;
062:
063: case MEMORY:
064: ps = new MemoryPageStore(dd);
065:
066: break;
067: }
068:
069: return new RTree(ps);
070: }
071:
072: private RTree getFullRTree(short type) throws Exception {
073: RTree idx = getRTree(type);
074:
075: Data data = null;
076: Envelope env = null;
077:
078: for (int i = 0; i < 200; i += 2) {
079: env = new Envelope(i, i + 1, i, i + 1);
080: data = new Data(dd);
081: data.addValue(new Integer(i));
082: idx.insert(env, data);
083: }
084:
085: return idx;
086: }
087:
088: public void testRTree() throws Exception {
089: for (short ni = 0; ni < NUM_IMPLEMENTATIONS; ni++) {
090: this .getRTree(ni).close();
091: }
092: }
093:
094: /*
095: * Test for Collection search(Envelope)
096: */
097: public void testSearchEnvelope() throws Exception {
098: for (short ni = 0; ni < NUM_IMPLEMENTATIONS; ni++) {
099: RTree idx = this .getFullRTree(ni);
100:
101: Envelope env = new Envelope(2, 6, 2, 6);
102: Collection res = idx.search(env);
103:
104: assertEquals(res.size(), 3);
105:
106: idx.close();
107: }
108: }
109:
110: /*
111: * Test for Collection search(Filter)
112: */
113: public void testSearchFilter() {
114: // TODO Write the test
115: }
116:
117: public void testInsert() throws Exception {
118: for (short ni = 0; ni < NUM_IMPLEMENTATIONS; ni++) {
119: RTree idx = this .getFullRTree(ni);
120: idx.close();
121: }
122: }
123:
124: public void testDelete() throws Exception {
125: for (short ni = 0; ni < NUM_IMPLEMENTATIONS; ni++) {
126: RTree idx = this .getFullRTree(ni);
127:
128: Envelope env = new Envelope(4, 5, 4, 5);
129:
130: idx.delete(env);
131:
132: env = new Envelope(2, 6, 2, 6);
133:
134: Collection res = idx.search(env);
135:
136: assertEquals(res.size(), 2);
137:
138: idx.close();
139: }
140: }
141: }
|