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.util.HashMap;
019: import java.util.Map;
020:
021: import junit.framework.TestCase;
022:
023: import org.geotools.data.DataUtilities;
024: import org.geotools.data.wfs.Action.InsertAction;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureType;
027: import org.geotools.feature.IllegalAttributeException;
028: import org.geotools.filter.AbstractFilter;
029: import org.geotools.filter.AbstractFilterImpl;
030: import org.geotools.filter.CompareFilter;
031: import org.geotools.filter.FidFilter;
032: import org.geotools.filter.Filter;
033: import org.geotools.filter.FilterFactory;
034: import org.geotools.filter.FilterFactoryFinder;
035: import org.geotools.filter.FilterType;
036: import org.opengis.filter.FilterVisitor;
037: import org.opengis.filter.PropertyIsEqualTo;
038: import org.opengis.filter.expression.Expression;
039: import org.opengis.filter.spatial.Contains;
040:
041: import com.vividsolutions.jts.geom.Coordinate;
042: import com.vividsolutions.jts.geom.GeometryFactory;
043: import com.vividsolutions.jts.geom.Point;
044:
045: /**
046: * @author Jesse
047: */
048: public class ProcessEditsBeforeCommitTest extends TestCase {
049:
050: private static final PropertyIsEqualTo CUSTOM_FILTERALL;
051: private static FilterFactory filterFac = FilterFactoryFinder
052: .createFilterFactory();
053: static {
054: CUSTOM_FILTERALL = filterFac.equals(filterFac
055: .createLiteralExpression(1), filterFac
056: .createLiteralExpression(2));
057: }
058: private WFSTransactionState state;
059: private FeatureType featureType;
060: private String typename;
061: private String GEOM_ATT = "geom";
062: private String NAME_ATT = "name";
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066: state = new WFSTransactionState(null);
067: typename = "Type";
068: featureType = DataUtilities.createType(typename, GEOM_ATT
069: + ":Point," + NAME_ATT + ":String");
070: }
071:
072: private Feature createFeature(int x, int y, String name)
073: throws IllegalAttributeException {
074: GeometryFactory fac = new GeometryFactory();
075: Object p = fac.createPoint(new Coordinate(x, y));
076:
077: return featureType.create(new Object[] { p, name });
078: }
079:
080: public void testBasicInsertFIDFilterUpdateCollapse()
081: throws Exception {
082: Feature createFeature = createFeature(0, 0, NAME_ATT);
083: state.addAction(featureType.getTypeName(),
084: new Action.InsertAction(createFeature));
085: Map properties = new HashMap();
086: properties.put(NAME_ATT, "newName");
087: state.addAction(featureType.getTypeName(),
088: new Action.UpdateAction(typename, filterFac
089: .createFidFilter(createFeature.getID()),
090: properties));
091:
092: state.combineActions();
093: Action.InsertAction action = (InsertAction) state.getActions(
094: featureType.getTypeName()).get(0);
095: assertEquals(1, state.getActions(featureType.getTypeName())
096: .size());
097: assertEquals("newName", action.getFeature().getAttribute(
098: NAME_ATT));
099: assertTrue(((Point) createFeature.getAttribute(GEOM_ATT))
100: .equals((Point) action.getFeature().getAttribute(
101: GEOM_ATT)));
102: }
103:
104: public void testMultiInsertUpdateCollapse() throws Exception {
105: Feature createFeature = createFeature(0, 0, NAME_ATT);
106: state.addAction(featureType.getTypeName(),
107: new Action.InsertAction(createFeature));
108: state
109: .addAction(featureType.getTypeName(),
110: new Action.InsertAction(createFeature(10, 10,
111: NAME_ATT)));
112: Map properties = new HashMap();
113: properties.put(NAME_ATT, "newName");
114: state.addAction(featureType.getTypeName(),
115: new Action.UpdateAction(typename, filterFac
116: .createFidFilter(createFeature.getID()),
117: properties));
118:
119: state.combineActions();
120: Action.InsertAction action = (InsertAction) state.getActions(
121: featureType.getTypeName()).get(0);
122: assertTrue(state.getActions(featureType.getTypeName()).get(1) instanceof InsertAction);
123: assertEquals(2, state.getActions(featureType.getTypeName())
124: .size());
125: assertEquals("newName", action.getFeature().getAttribute(
126: NAME_ATT));
127: assertTrue(((Point) createFeature.getAttribute(GEOM_ATT))
128: .equals((Point) action.getFeature().getAttribute(
129: GEOM_ATT)));
130: }
131:
132: public void testMultiFIDFilterUpdateCollapse() throws Exception {
133: Feature createFeature = createFeature(0, 0, NAME_ATT);
134: state.addAction(featureType.getTypeName(),
135: new Action.InsertAction(createFeature));
136: Map properties = new HashMap();
137: properties.put(NAME_ATT, "newName");
138: state.addAction(featureType.getTypeName(),
139: new Action.UpdateAction(typename, filterFac
140: .createFidFilter(createFeature.getID()),
141: properties));
142: properties.put(NAME_ATT, "secondUpdate");
143: state.addAction(featureType.getTypeName(),
144: new Action.UpdateAction(typename, filterFac
145: .createFidFilter(createFeature.getID()),
146: properties));
147:
148: state.combineActions();
149: Action.InsertAction action = (InsertAction) state.getActions(
150: featureType.getTypeName()).get(0);
151: assertEquals(1, state.getActions(featureType.getTypeName())
152: .size());
153: assertEquals("secondUpdate", action.getFeature().getAttribute(
154: NAME_ATT));
155: assertTrue(((Point) createFeature.getAttribute(GEOM_ATT))
156: .equals((Point) action.getFeature().getAttribute(
157: GEOM_ATT)));
158: }
159:
160: public void testFidAndNonFidFilterUpdates() throws Exception {
161: Feature createFeature = createFeature(0, 0, NAME_ATT);
162: Map updateProperties = new HashMap();
163: updateProperties.put(NAME_ATT, "noMatch");
164: state.addAction(featureType.getTypeName(),
165: new Action.UpdateAction(typename, Filter.INCLUDE,
166: updateProperties));
167: state.addAction(featureType.getTypeName(),
168: new Action.InsertAction(createFeature));
169: updateProperties.put(NAME_ATT, "newName");
170: state.addAction(featureType.getTypeName(),
171: new Action.UpdateAction(typename, filterFac
172: .createFidFilter(createFeature.getID()),
173: updateProperties));
174: updateProperties.put(NAME_ATT, "secondUpdate");
175: CompareFilter matchFilter = filterFac
176: .createCompareFilter(FilterType.COMPARE_EQUALS);
177: matchFilter.addLeftValue(filterFac
178: .createAttributeExpression("name"));
179: matchFilter.addRightValue(filterFac
180: .createLiteralExpression("newName"));
181: state.addAction(featureType.getTypeName(),
182: new Action.UpdateAction(typename, matchFilter,
183: updateProperties));
184:
185: updateProperties.put(NAME_ATT, "secondNoMatch");
186: state.addAction(featureType.getTypeName(),
187: new Action.UpdateAction(typename, CUSTOM_FILTERALL,
188: updateProperties));
189:
190: state.combineActions();
191: Action.InsertAction action = (InsertAction) state.getActions(
192: featureType.getTypeName()).get(3);
193: assertEquals(4, state.getActions(featureType.getTypeName())
194: .size());
195: assertEquals("secondUpdate", action.getFeature().getAttribute(
196: NAME_ATT));
197: assertTrue(((Point) createFeature.getAttribute(GEOM_ATT))
198: .equals((Point) action.getFeature().getAttribute(
199: GEOM_ATT)));
200: }
201:
202: public void testDeleteAndInsert() throws Exception {
203: Feature createFeature = createFeature(0, 0, NAME_ATT);
204: Map updateProperties = new HashMap();
205: updateProperties.put(NAME_ATT, "noMatch");
206: state.addAction(featureType.getTypeName(),
207: new Action.UpdateAction(typename, Filter.INCLUDE,
208: updateProperties));
209: state.addAction(featureType.getTypeName(),
210: new Action.InsertAction(createFeature));
211: updateProperties.put(NAME_ATT, "newName");
212: state.addAction(featureType.getTypeName(),
213: new Action.UpdateAction(typename, filterFac
214: .createFidFilter(createFeature.getID()),
215: updateProperties));
216: updateProperties.put(NAME_ATT, "secondUpdate");
217: CompareFilter matchFilter = filterFac
218: .createCompareFilter(FilterType.COMPARE_EQUALS);
219: matchFilter.addLeftValue(filterFac
220: .createAttributeExpression("name"));
221: matchFilter.addRightValue(filterFac
222: .createLiteralExpression("newName"));
223: state.addAction(featureType.getTypeName(),
224: new Action.UpdateAction(typename, matchFilter,
225: updateProperties));
226:
227: updateProperties.put(NAME_ATT, "secondNoMatch");
228: state.addAction(featureType.getTypeName(),
229: new Action.UpdateAction(typename, CUSTOM_FILTERALL,
230: updateProperties));
231:
232: state.combineActions();
233: Action.InsertAction action = (InsertAction) state.getActions(
234: featureType.getTypeName()).get(3);
235: assertEquals(4, state.getActions(featureType.getTypeName())
236: .size());
237: assertEquals("secondUpdate", action.getFeature().getAttribute(
238: NAME_ATT));
239: assertTrue(((Point) createFeature.getAttribute(GEOM_ATT))
240: .equals((Point) action.getFeature().getAttribute(
241: GEOM_ATT)));
242: }
243:
244: public void testInsertAndDelete() throws Exception {
245: Feature createFeature = createFeature(0, 0, NAME_ATT);
246: state.addAction(featureType.getTypeName(),
247: new Action.InsertAction(createFeature));
248: state.addAction(featureType.getTypeName(),
249: new Action.DeleteAction(typename, filterFac
250: .createFidFilter(createFeature.getID())));
251:
252: state.combineActions();
253: assertEquals(0, state.getActions(featureType.getTypeName())
254: .size());
255: }
256:
257: public void testUpdatesDeletesAndInsertes() throws Exception {
258: Feature createFeature = createFeature(0, 0, NAME_ATT);
259: Map updateProperties = new HashMap();
260: updateProperties.put(NAME_ATT, "noMatch");
261:
262: //create delete actions
263: Action.DeleteAction deleteNone = new Action.DeleteAction(
264: typename, CUSTOM_FILTERALL);
265:
266: FidFilter createFidFilter = filterFac.createFidFilter();
267: createFidFilter.addFid(createFeature.getID());
268: createFidFilter.addFid("someotherfid");
269: Action.DeleteAction deleteInsert1 = new Action.DeleteAction(
270: typename, createFidFilter);
271:
272: //create update actions
273: Action.UpdateAction updateAll = new Action.UpdateAction(
274: typename, Filter.INCLUDE, updateProperties);
275:
276: updateProperties.put(NAME_ATT, "newName");
277: Action.UpdateAction updateInsert1 = new Action.UpdateAction(
278: typename, filterFac.createFidFilter(createFeature
279: .getID()), updateProperties);
280:
281: updateProperties.put(NAME_ATT, "secondUpdate");
282: CompareFilter matchFilter = filterFac
283: .createCompareFilter(FilterType.COMPARE_EQUALS);
284: matchFilter.addLeftValue(filterFac
285: .createAttributeExpression("name"));
286: matchFilter.addRightValue(filterFac
287: .createLiteralExpression("newName"));
288: Action.UpdateAction updateManyIncludingInsert1 = new Action.UpdateAction(
289: typename, matchFilter, updateProperties);
290:
291: updateProperties.put(NAME_ATT, "secondNoMatch");
292: Action.UpdateAction updateNone = new Action.UpdateAction(
293: typename, CUSTOM_FILTERALL, updateProperties);
294:
295: // create Insert actions
296: Action.InsertAction insertAction = new Action.InsertAction(
297: createFeature);
298: Action.InsertAction insertAction2 = new Action.InsertAction(
299: createFeature(10, 10, "name"));
300:
301: state.addAction(featureType.getTypeName(), updateAll);
302: state.addAction(featureType.getTypeName(), insertAction);
303: state.addAction(featureType.getTypeName(), updateInsert1);
304:
305: state.addAction(featureType.getTypeName(), insertAction2);
306: state.addAction(featureType.getTypeName(), deleteInsert1);
307: state.addAction(featureType.getTypeName(), deleteNone);
308: state.addAction(featureType.getTypeName(),
309: updateManyIncludingInsert1);
310:
311: state.addAction(featureType.getTypeName(), updateNone);
312:
313: state.combineActions();
314: assertEquals(6, state.getActions(featureType.getTypeName())
315: .size());
316: assertSame(updateAll, state.getActions(
317: featureType.getTypeName()).get(0));
318: assertSame(deleteInsert1, state.getActions(
319: featureType.getTypeName()).get(1));
320: assertSame(deleteNone, state.getActions(
321: featureType.getTypeName()).get(2));
322: assertSame(updateManyIncludingInsert1, state.getActions(
323: featureType.getTypeName()).get(3));
324: assertSame(updateNone, state.getActions(
325: featureType.getTypeName()).get(4));
326: assertSame(insertAction2, state.getActions(
327: featureType.getTypeName()).get(5));
328: }
329:
330: }
|