001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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; either
009: * version 2.1 of the License, or (at your option) any later version.
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.wfs;
017:
018: import java.io.IOException;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.NoSuchElementException;
022: import java.util.Set;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import junit.framework.TestCase;
027:
028: import org.geotools.data.DataStore;
029: import org.geotools.data.DefaultTransaction;
030: import org.geotools.data.FeatureStore;
031: import org.geotools.data.Transaction;
032: import org.geotools.factory.CommonFactoryFinder;
033: import org.geotools.factory.FactoryConfigurationError;
034: import org.geotools.feature.AttributeType;
035: import org.geotools.feature.FeatureCollection;
036: import org.geotools.feature.FeatureIterator;
037: import org.geotools.feature.FeatureType;
038: import org.geotools.feature.IllegalAttributeException;
039: import org.geotools.filter.IllegalFilterException;
040: import org.opengis.filter.Filter;
041: import org.opengis.filter.FilterFactory;
042: import org.opengis.filter.Id;
043: import org.opengis.filter.identity.FeatureId;
044:
045: /**
046: * <p>
047: * Needs two featureTypes on the server, with the same data types ... will add the second set to the first set.
048: * </p>
049: * @author dzwiers
050: *
051: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/test/java/org/geotools/data/wfs/WFSDataStoreWriteOnlineTest.java $
052: */
053: public class WFSDataStoreWriteOnlineTest extends TestCase {
054: public void testEmpty() throws NoSuchElementException, IOException,
055: IllegalAttributeException, FactoryConfigurationError {
056: // URL u = new URL("http://localhost:8080/geoserver/wfs");
057: // WFSDataStore ds = getDataStore(u);
058: // FeatureType ft = ds.getSchema("states");
059: // Feature f = ds.getFeatureReader("states").next();
060: // doDelete(ds,ft,FilterFactoryFinder.createFilterFactory().createFidFilter(f.getID()));
061: // FeatureCollection fc = DefaultFeatureCollections.newCollection();
062: // fc.add(f);
063: // doInsert(ds,ft,(new CollectionDataStore(fc)).getFeatureReader("states"));
064: }
065:
066: public WFSDataStoreWriteOnlineTest() {
067: Logger.global.setLevel(Level.SEVERE);
068: }
069:
070: public static Id doInsert(DataStore ds, FeatureType ft,
071: FeatureCollection insert) throws NoSuchElementException,
072: IOException, IllegalAttributeException {
073: Transaction t = new DefaultTransaction();
074: WFSFeatureStore fs = (WFSFeatureStore) ds.getFeatureSource(ft
075: .getTypeName());
076: fs.setTransaction(t);
077: System.out.println("Insert Read 1");
078: FeatureIterator fr = fs.getFeatures().features();
079: int count1 = 0;
080: while (fr.hasNext()) {
081: count1++;
082: fr.next();
083: }
084: fr.close();
085: System.out.println("Insert Add Features");
086: Set fids1 = fs.addFeatures(insert);
087:
088: System.out.println("Insert Read 2");
089: fr = fs.getFeatures().features();
090: int count2 = 0;
091: while (fr.hasNext()) {
092: count2++;
093: fr.next();
094: }
095: fr.close();
096: assertEquals(count1 + insert.size(), count2);
097:
098: FilterFactory fac = CommonFactoryFinder.getFilterFactory(null);
099: Set featureIds = new HashSet();
100: for (Iterator it = fids1.iterator(); it.hasNext();) {
101: String fid = (String) it.next();
102: FeatureId id = fac.featureId(fid);
103: featureIds.add(id);
104: }
105: Id fidfilter = fac.id(featureIds);
106:
107: System.out.println("Remove Inserted Features");
108: fs.removeFeatures(fidfilter);
109:
110: System.out.println("Insert Read 3");
111: fr = fs.getFeatures().features();
112: count2 = 0;
113: while (fr.hasNext()) {
114: count2++;
115: fr.next();
116: }
117: fr.close();
118: assertEquals(count1, count2);
119:
120: System.out.println("Insert Add Features");
121: fs.addFeatures(insert);
122:
123: System.out.println("Insert Read 2");
124: fr = fs.getFeatures().features();
125: count2 = 0;
126: while (fr.hasNext()) {
127: count2++;
128: fr.next();
129: }
130: fr.close();
131: assertEquals(count1 + insert.size(), count2);
132:
133: System.out.println("Insert Commit");
134: t.commit();
135:
136: System.out.println("Insert Read 3");
137: fr = fs.getFeatures().features();
138: int count3 = 0;
139: while (fr.hasNext()) {
140: count3++;
141: fr.next();
142: }
143: fr.close();
144: assertEquals(count2, count3);
145:
146: WFSTransactionState ts = (WFSTransactionState) t.getState(ds);
147: String[] fids = ts.getFids(ft.getTypeName());
148: assertNotNull(fids);
149:
150: Set ids = new HashSet();
151: for (int i = 0; i < fids.length; i++) {
152: ids.add(fac.featureId(fids[i]));
153: }
154: Id ff = fac.id(ids);
155: return ff;
156: }
157:
158: public static void doDelete(DataStore ds, FeatureType ft, Id ff)
159: throws NoSuchElementException, IllegalAttributeException,
160: IOException {
161: assertNotNull("doInsertFailed?", ff);
162: Transaction t = new DefaultTransaction();
163: FeatureStore fs = (FeatureStore) ds.getFeatureSource(ft
164: .getTypeName());
165: fs.setTransaction(t);
166:
167: System.out.println("Delete Read 1");
168: FeatureIterator fr = fs.getFeatures().features();
169: int count1 = 0;
170: while (fr.hasNext()) {
171: count1++;
172: fr.next();
173: }
174: fr.close();
175:
176: System.out.println("Delete Remove " + ff);
177: fs.removeFeatures(ff);
178:
179: System.out.println("Delete Read 2");
180: fr = fs.getFeatures().features();
181: int count2 = 0;
182: while (fr.hasNext()) {
183: count2++;
184: if (count2 < 5)
185: System.out.println("# == " + count2 + " "
186: + fr.next().getID());
187: else
188: fr.next();
189: }
190: fr.close();
191: assertTrue("Read 1 == " + count1 + " Read 2 == " + count2,
192: count2 < count1);
193:
194: System.out.println("Delete Commit");
195: t.commit();
196:
197: System.out.println("Delete Read 3");
198: fr = fs.getFeatures().features();
199: int count3 = 0;
200: while (fr.hasNext()) {
201: count3++;
202: fr.next();
203: }
204: fr.close();
205: assertTrue(count2 == count3);
206: }
207:
208: public static void doUpdate(DataStore ds, FeatureType ft,
209: String attributeToChange, Object newValue)
210: throws IllegalFilterException, FactoryConfigurationError,
211: NoSuchElementException, IOException,
212: IllegalAttributeException {
213: Transaction t = new DefaultTransaction();
214: FeatureStore fs = (FeatureStore) ds.getFeatureSource(ft
215: .getTypeName());
216: fs.setTransaction(t);
217:
218: AttributeType at = ft.getAttributeType(attributeToChange);
219: assertNotNull("Attribute " + attributeToChange
220: + " does not exist", at);
221:
222: FilterFactory filterFactory = CommonFactoryFinder
223: .getFilterFactory(null);
224: Filter f = filterFactory.equals(filterFactory.property(at
225: .getName()), filterFactory.literal(newValue));
226:
227: System.out.println("Update Read 1");
228: FeatureIterator fr = fs.getFeatures(f).features();
229:
230: int count1 = 0;
231: Object oldValue = null;
232: if (fr != null)
233: while (fr.hasNext()) {
234: count1++;
235: oldValue = fr.next().getAttribute(attributeToChange);
236: }
237:
238: fr.close();
239: System.out.println("Update Modify");
240: fs.modifyFeatures(at, newValue, Filter.INCLUDE);
241:
242: System.out.println("Update Read 2");
243: fr = fs.getFeatures(f).features();
244: int count2 = 0;
245: while (fr.hasNext()) {
246: count2++;
247: // System.out.println(fr.next());
248: fr.next();
249: }
250: fr.close();
251: //System.out.println("Read 1 == "+count1+" Read 2 == "+count2);
252: assertTrue("Read 1 == " + count1 + " Read 2 == " + count2,
253: count2 > count1);
254:
255: System.out.println("Update Commit");
256: try {
257: t.commit();
258:
259: assertTrue(((WFSTransactionState) t.getState(ds))
260: .getFids(ft.getTypeName()) != null);
261:
262: System.out.println("Update Read 3");
263: fr = fs.getFeatures(f).features();
264: int count3 = 0;
265: while (fr.hasNext()) {
266: count3++;
267: fr.next();
268: }
269: fr.close();
270: assertEquals(count2, count3);
271: } finally {
272: // cleanup
273: fs.modifyFeatures(at, oldValue, Filter.INCLUDE);
274: t.commit();
275: }
276: }
277: }
|