001: /*
002: * Geotools2 - 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: */
017: package org.geotools.arcsde.data;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.HashSet;
022: import java.util.List;
023:
024: import junit.framework.TestCase;
025:
026: import org.geotools.data.DefaultQuery;
027: import org.geotools.data.FeatureReader;
028: import org.geotools.data.Query;
029: import org.geotools.factory.CommonFactoryFinder;
030: import org.geotools.feature.FeatureType;
031: import org.opengis.filter.FilterFactory;
032: import org.opengis.filter.Id;
033:
034: import com.esri.sde.sdk.client.SeConnection;
035: import com.vividsolutions.jts.geom.Envelope;
036:
037: /**
038: * DOCUMENT ME!
039: *
040: * @author Gabriel Roldan
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/test/java/org/geotools/arcsde/data/ArcSDEQueryTest.java $
042: * @version $Revision: 1.9 $
043: */
044: public class ArcSDEQueryTest extends TestCase {
045:
046: private TestData testData;
047:
048: /**
049: * do not access it directly, use {@link #getQueryAll()}
050: */
051: private ArcSDEQuery queryAll;
052:
053: /**
054: * do not access it directly, use {@link #getQueryFiltered()}
055: */
056: private ArcSDEQuery queryFiltered;
057:
058: private ArcSDEDataStore dstore;
059:
060: private String typeName;
061:
062: private Query filteringQuery;
063:
064: private FilterFactory ff = CommonFactoryFinder
065: .getFilterFactory(null);
066:
067: private FeatureType ftype;
068:
069: private static final int FILTERING_COUNT = 3;
070:
071: /**
072: * Constructor for ArcSDEQueryTest.
073: *
074: * @param arg0
075: */
076: public ArcSDEQueryTest(String name) {
077: super (name);
078: }
079:
080: /**
081: * loads {@code test-data/testparams.properties} into a Properties object, wich is
082: * used to obtain test tables names and is used as parameter to find the DataStore
083: *
084: * @throws Exception DOCUMENT ME!
085: */
086: protected void setUp() throws Exception {
087: super .setUp();
088: this .testData = new TestData();
089: this .testData.setUp();
090: dstore = testData.getDataStore();
091: typeName = testData.getLine_table();
092: this .ftype = dstore.getSchema(typeName);
093:
094: //grab some fids
095: FeatureReader reader = dstore.getFeatureReader(typeName);
096: List fids = new ArrayList();
097: for (int i = 0; i < FILTERING_COUNT; i++) {
098: fids.add(ff.featureId(reader.next().getID()));
099: }
100: reader.close();
101: Id filter = ff.id(new HashSet(fids));
102: filteringQuery = new DefaultQuery(typeName, filter);
103: }
104:
105: private ArcSDEQuery getQueryAll() throws IOException {
106: this .queryAll = ArcSDEQuery.createQuery(dstore, ftype,
107: Query.ALL);
108: return this .queryAll;
109: }
110:
111: private ArcSDEQuery getQueryFiltered() throws IOException {
112: this .queryFiltered = ArcSDEQuery.createQuery(dstore,
113: filteringQuery);
114: return this .queryFiltered;
115: }
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @throws Exception DOCUMENT ME!
121: */
122: protected void tearDown() throws Exception {
123: super .tearDown();
124: try {
125: this .queryAll.close();
126: } catch (Exception e) {
127: //no-op
128: }
129: try {
130: this .queryFiltered.close();
131: } catch (Exception e) {
132: //no-op
133: }
134: this .queryAll = null;
135: this .queryFiltered = null;
136: testData.tearDown(true, true);
137: testData = null;
138: }
139:
140: /**
141: * DOCUMENT ME!
142: */
143: public void testClose() throws IOException {
144: ArcSDEQuery queryAll = getQueryAll();
145: assertNotNull(queryAll.connection);
146:
147: queryAll.execute();
148:
149: assertNotNull(queryAll.connection);
150:
151: //should nevel do this, just to assert it is
152: //not closed by returned to the pool
153: SeConnection conn = queryAll.connection;
154:
155: queryAll.close();
156:
157: assertNull(queryAll.connection);
158: assertFalse(conn.isClosed());
159: }
160:
161: /**
162: * DOCUMENT ME!
163: */
164: public void testFetch() throws IOException {
165: ArcSDEQuery queryAll = getQueryAll();
166: try {
167: queryAll.fetch();
168: fail("fetch without calling execute");
169: } catch (IllegalStateException e) {
170: //ok
171: }
172:
173: queryAll.execute();
174: assertNotNull(queryAll.fetch());
175:
176: queryAll.close();
177: try {
178: queryAll.fetch();
179: fail("fetch after close!");
180: } catch (IllegalStateException e) {
181: //ok
182: }
183: }
184:
185: /**
186: * DOCUMENT ME!
187: */
188: public void testCalculateResultCount() throws Exception {
189: FeatureReader reader = dstore.getFeatureReader(typeName);
190: int readed = 0;
191: while (reader.hasNext()) {
192: reader.next();
193: readed++;
194: }
195:
196: int calculated = getQueryAll().calculateResultCount();
197: assertEquals(readed, calculated);
198:
199: calculated = getQueryFiltered().calculateResultCount();
200: assertEquals(FILTERING_COUNT, calculated);
201: }
202:
203: /**
204: * DOCUMENT ME!
205: */
206: public void testCalculateQueryExtent() throws Exception {
207: FeatureReader reader = dstore.getFeatureReader(typeName);
208: Envelope real = new Envelope();
209: while (reader.hasNext()) {
210: real.expandToInclude(reader.next().getBounds());
211: }
212:
213: Envelope e = getQueryAll().calculateQueryExtent();
214: assertNotNull(e);
215: assertEquals(real, e);
216:
217: reader.close();
218:
219: reader = dstore.getFeatureReader(typeName, filteringQuery);
220: real = new Envelope();
221: while (reader.hasNext()) {
222: real.expandToInclude(reader.next().getBounds());
223: }
224:
225: e = getQueryFiltered().calculateQueryExtent();
226: assertNotNull(e);
227: assertEquals(real, e);
228: reader.close();
229: }
230:
231: }
|