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.property;
017:
018: import java.io.BufferedWriter;
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025: import java.util.NoSuchElementException;
026:
027: import junit.framework.TestCase;
028:
029: import org.geotools.data.DataUtilities;
030: import org.geotools.data.DefaultQuery;
031: import org.geotools.data.DefaultTransaction;
032: import org.geotools.data.FeatureReader;
033: import org.geotools.data.FeatureSource;
034: import org.geotools.data.FeatureStore;
035: import org.geotools.data.Query;
036: import org.geotools.data.Transaction;
037: import org.geotools.factory.CommonFactoryFinder;
038: import org.geotools.feature.AttributeType;
039: import org.geotools.feature.Feature;
040: import org.geotools.feature.FeatureCollection;
041: import org.geotools.feature.FeatureIterator;
042: import org.geotools.feature.FeatureType;
043: import org.geotools.feature.IllegalAttributeException;
044: import org.opengis.filter.Filter;
045: import org.opengis.filter.FilterFactory2;
046:
047: /**
048: * Test functioning of PropertyDataStore.
049: *
050: * @author Jody Garnett, Refractions Research Inc.
051: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/property/src/test/java/org/geotools/data/property/PropertyDataStoreTest.java $
052: */
053: public class PropertyDataStoreTest extends TestCase {
054: PropertyDataStore store;
055:
056: static FilterFactory2 ff = (FilterFactory2) CommonFactoryFinder
057: .getFilterFactory(null);
058:
059: /**
060: * Constructor for SimpleDataStoreTest.
061: * @param arg0
062: */
063: public PropertyDataStoreTest(String arg0) {
064: super (arg0);
065: }
066:
067: protected void setUp() throws Exception {
068: File dir = new File(".", "propertyTestData");
069: dir.mkdir();
070:
071: File file = new File(dir, "road.properties");
072: if (file.exists()) {
073: file.delete();
074: }
075: BufferedWriter writer = new BufferedWriter(new FileWriter(file));
076: writer.write("_=id:Integer,name:String");
077: writer.newLine();
078: writer.write("fid1=1|jody");
079: writer.newLine();
080: writer.write("fid2=2|brent");
081: writer.newLine();
082: writer.write("fid3=3|dave");
083: writer.newLine();
084: writer.write("fid4=4|justin");
085: writer.close();
086: store = new PropertyDataStore(dir);
087: super .setUp();
088: }
089:
090: protected void tearDown() throws Exception {
091: File dir = new File("propertyTestData");
092: File list[] = dir.listFiles();
093: for (int i = 0; i < list.length; i++) {
094: list[i].delete();
095: }
096: dir.delete();
097: super .tearDown();
098: }
099:
100: public void testGetTypeNames() {
101: String names[] = store.getTypeNames();
102: assertEquals(1, names.length);
103: assertEquals("road", names[0]);
104: }
105:
106: public void testGetSchema() throws IOException {
107: FeatureType type = store.getSchema("road");
108: assertNotNull(type);
109: assertEquals("road", type.getTypeName());
110: assertEquals("propertyTestData", type.getNamespace().toString());
111: assertEquals(2, type.getAttributeCount());
112:
113: AttributeType id = type.getAttributeType(0);
114: AttributeType name = type.getAttributeType(1);
115:
116: assertEquals("id", id.getName());
117: assertEquals("class java.lang.Integer", id.getType().toString());
118:
119: assertEquals("name", name.getName());
120: assertEquals("class java.lang.String", name.getType()
121: .toString());
122: }
123:
124: public void testGetFeaturesFeatureTypeFilterTransaction1()
125: throws Exception {
126: FeatureType type = store.getSchema("road");
127: Query roadQuery = new DefaultQuery("road");
128: FeatureReader reader = store.getFeatureReader(roadQuery,
129: Transaction.AUTO_COMMIT);
130: int count = 0;
131: try {
132: while (reader.hasNext()) {
133: reader.next();
134: count++;
135: }
136: } finally {
137: reader.close();
138: }
139: assertEquals(4, count);
140:
141: Filter selectFid1;
142:
143: selectFid1 = ff.id(Collections.singleton(ff.featureId("fid1")));
144: reader = store.getFeatureReader(new DefaultQuery("road",
145: selectFid1), Transaction.AUTO_COMMIT);
146: assertEquals(1, count(reader));
147:
148: Transaction transaction = new DefaultTransaction();
149: reader = store.getFeatureReader(roadQuery, transaction);
150: assertEquals(4, count(reader));
151:
152: reader = store.getFeatureReader(roadQuery, transaction);
153: List list = new ArrayList();
154: try {
155: while (reader.hasNext()) {
156: list.add(reader.next().getID());
157: }
158: } finally {
159: reader.close();
160: }
161: assertEquals("[fid1, fid2, fid3, fid4]", list.toString());
162: }
163:
164: /*
165: * Test for FeatureReader getFeatureReader(String)
166: */
167: public void testGetFeatureReaderString()
168: throws NoSuchElementException, IOException,
169: IllegalAttributeException {
170: FeatureReader reader = store.getFeatureReader("road");
171: int count = 0;
172: try {
173: while (reader.hasNext()) {
174: reader.next();
175: count++;
176: }
177: } finally {
178: reader.close();
179: }
180: assertEquals(4, count);
181: }
182:
183: private int count(FeatureReader reader) throws Exception {
184: int count = 0;
185: try {
186: while (reader.hasNext()) {
187: reader.next();
188: count++;
189: }
190: } finally {
191: reader.close();
192: }
193: return count;
194: }
195:
196: private int count(String typeName) throws Exception {
197: return count(store.getFeatureReader(typeName));
198: }
199:
200: public void testWriterSkipThrough() throws Exception {
201: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
202: .getFeatureWriter("road");
203:
204: File in = writer.read;
205: File out = writer.write;
206:
207: int count = 0;
208: while (writer.hasNext()) {
209: writer.next();
210: count++;
211: }
212: assertEquals(4, count);
213: assertTrue(in.exists());
214: assertTrue(out.exists());
215: writer.close();
216: assertTrue(in.exists());
217:
218: assertEquals(4, count("road"));
219: }
220:
221: public void testWriterChangeName() throws Exception {
222: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
223: .getFeatureWriter("road");
224:
225: int count = 0;
226: while (writer.hasNext()) {
227: Feature f = writer.next();
228: f.setAttribute(1, "name " + (count + 1));
229: writer.write();
230: count++;
231: }
232: writer.close();
233: assertEquals(4, count);
234: assertEquals(4, count("road"));
235: }
236:
237: public void testWriterChangeFirstName() throws Exception {
238: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
239: .getFeatureWriter("road");
240: Feature f;
241: f = writer.next();
242: f.setAttribute(1, "changed");
243: writer.write();
244: writer.close();
245: assertEquals(4, count("road"));
246: }
247:
248: public void testWriterChangeLastName() throws Exception {
249: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
250: .getFeatureWriter("road");
251: Feature f;
252: writer.next();
253: writer.next();
254: writer.next();
255: f = writer.next();
256: f.setAttribute(1, "changed");
257: writer.write();
258: writer.close();
259: assertEquals(4, count("road"));
260: }
261:
262: public void testWriterChangeAppend() throws Exception {
263: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
264: .getFeatureWriter("road");
265: Feature f;
266: writer.next();
267: writer.next();
268: writer.next();
269: writer.next();
270: assertFalse(writer.hasNext());
271: f = writer.next();
272: assertNotNull(f);
273: f.setAttribute(0, new Integer(-1));
274: f.setAttribute(1, "new");
275: writer.write();
276: writer.close();
277: assertEquals(5, count("road"));
278: }
279:
280: public void testWriterChangeRemoveFirst() throws Exception {
281: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
282: .getFeatureWriter("road");
283:
284: writer.next();
285: writer.remove();
286: writer.close();
287: assertEquals(3, count("road"));
288: }
289:
290: public void testWriterChangeRemoveLast() throws Exception {
291: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
292: .getFeatureWriter("road");
293:
294: writer.next();
295: writer.next();
296: writer.next();
297: writer.remove();
298: writer.close();
299: assertEquals(3, count("road"));
300: }
301:
302: public void testWriterChangeRemoveAppend() throws Exception {
303: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
304: .getFeatureWriter("road");
305: Feature f;
306: writer.next();
307: writer.next();
308: writer.next();
309: writer.next();
310:
311: assertFalse(writer.hasNext());
312: f = writer.next();
313: assertNotNull(f);
314: f.setAttribute(0, new Integer(-1));
315: f.setAttribute(1, "new");
316: writer.remove();
317: writer.close();
318: assertEquals(4, count("road"));
319: }
320:
321: public void testWriterChangeIgnoreAppend() throws Exception {
322: PropertyFeatureWriter writer = (PropertyFeatureWriter) store
323: .getFeatureWriter("road");
324: Feature f;
325: writer.next();
326: writer.next();
327: writer.next();
328: writer.next();
329: assertFalse(writer.hasNext());
330: f = writer.next();
331: assertNotNull(f);
332: f.setAttribute(0, new Integer(-1));
333: f.setAttribute(1, "new");
334: writer.close();
335: assertEquals(4, count("road"));
336: }
337:
338: public void testGetFeatureSource() throws Exception {
339: FeatureSource road = store.getFeatureSource("road");
340: FeatureCollection features = road.getFeatures();
341: FeatureIterator reader = features.features();
342: List list = new ArrayList();
343: try {
344: while (reader.hasNext()) {
345: list.add(reader.next().getID());
346: }
347: } finally {
348: reader.close();
349: }
350: assertEquals("[fid1, fid2, fid3, fid4]", list.toString());
351: assertEquals(4, road.getCount(Query.ALL));
352: assertEquals(null, road.getBounds(Query.ALL));
353: assertEquals(4, features.size());
354: assertTrue(features.getBounds().isNull());
355: assertEquals(4, features.size());
356:
357: }
358:
359: private void dir(File file) {
360: File dir;
361: if (file.isDirectory()) {
362: dir = file;
363: } else {
364: dir = file.getParentFile();
365: }
366: if (dir != null) {
367: String ls[] = dir.list();
368: System.out.println("Directory " + dir);
369: for (int i = 0; i < ls.length; i++) {
370: System.out.println(ls[i]);
371: }
372: }
373: }
374:
375: public void testTransactionIndependence() throws Exception {
376: FeatureType ROAD = store.getSchema("road");
377: Feature chrisFeature = ROAD.create(new Object[] {
378: new Integer(5), "chris" }, "fid5");
379:
380: FeatureStore roadAuto = (FeatureStore) store
381: .getFeatureSource("road");
382:
383: FeatureStore roadFromClient1 = (FeatureStore) store
384: .getFeatureSource("road");
385: Transaction transaction1 = new DefaultTransaction(
386: "Transaction Used by Client 1");
387: roadFromClient1.setTransaction(transaction1);
388:
389: FeatureStore roadFromClient2 = (FeatureStore) store
390: .getFeatureSource("road");
391: Transaction transaction2 = new DefaultTransaction(
392: "Transaction Used by Client 2");
393: roadFromClient2.setTransaction(transaction2);
394:
395: FilterFactory2 ff = (FilterFactory2) CommonFactoryFinder
396: .getFilterFactory(null);
397: Filter selectFid1 = ff.id(Collections.singleton(ff
398: .featureId("fid1")));
399: Filter selectFid2 = ff.id(Collections.singleton(ff
400: .featureId("fid2")));
401:
402: // Before we edit everything should be the same
403: assertEquals("auto before", 4, roadAuto.getFeatures().size());
404: assertEquals("client 1 before", 4, roadFromClient1
405: .getFeatures().size());
406: assertEquals("client 2 before", 4, roadFromClient2
407: .getFeatures().size());
408:
409: // Remove Feature with Fid1
410: roadFromClient1.removeFeatures(selectFid1); // road1 removes fid1 on t1
411:
412: assertEquals("auto after client 1 removes fid1", 4, roadAuto
413: .getFeatures().size());
414: assertEquals("client 1 after client 1 removes fid1", 3,
415: roadFromClient1.getFeatures().size());
416: assertEquals("client 2 after client 1 removes fid1", 4,
417: roadFromClient2.getFeatures().size());
418:
419: roadFromClient2.addFeatures(DataUtilities
420: .collection(chrisFeature)); // road2 adds fid5 on t2
421: assertEquals(
422: "auto after client 1 removes fid1 and client 2 adds fid5",
423: 4, roadAuto.getFeatures().size());
424: assertEquals(
425: "client 1 after client 1 removes fid1 and client 2 adds fid5",
426: 3, roadFromClient1.getFeatures().size());
427: assertEquals(
428: "cleint 2 after client 1 removes fid1 and client 2 adds fid5",
429: 5, roadFromClient2.getFeatures().size());
430:
431: transaction1.commit();
432: assertEquals(
433: "auto after client 1 commits removal of fid1 (client 2 has added fid5)",
434: 3, roadAuto.getFeatures().size());
435: assertEquals(
436: "client 1 after commiting removal of fid1 (client 2 has added fid5)",
437: 3, roadFromClient1.getFeatures().size());
438: assertEquals(
439: "client 2 after client 1 commits removal of fid1 (client 2 has added fid5)",
440: 4, roadFromClient2.getFeatures().size());
441:
442: transaction2.commit();
443: assertEquals(
444: "auto after client 2 commits addition of fid5 (fid1 previously removed)",
445: 4, roadAuto.getFeatures().size());
446: assertEquals(
447: "client 1 after client 2 commits addition of fid5 (fid1 previously removed)",
448: 4, roadFromClient1.getFeatures().size());
449: assertEquals(
450: "client 2 after commiting addition of fid5 (fid1 previously removed)",
451: 4, roadFromClient2.getFeatures().size());
452: }
453: }
|