gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.point / src / test / java / org / gvsig / vectorediting / lib / prov / point / PointTest.java @ 322
History | View | Annotate | Download (10.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 gvSIG Association
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.vectorediting.lib.prov.point; |
25 |
|
26 |
import java.io.File; |
27 |
import java.net.URL; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.fmap.crs.CRSFactory; |
31 |
import org.gvsig.fmap.dal.DALLocator; |
32 |
import org.gvsig.fmap.dal.DataManager; |
33 |
import org.gvsig.fmap.dal.DataStoreParameters; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.geom.Geometry; |
36 |
import org.gvsig.fmap.geom.GeometryLocator; |
37 |
import org.gvsig.fmap.geom.GeometryManager; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.mapcontext.MapContext; |
40 |
import org.gvsig.fmap.mapcontext.ViewPort; |
41 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
42 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
43 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
44 |
import org.gvsig.vectorediting.lib.api.EditingService; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
46 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
47 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException; |
49 |
import org.gvsig.vectorediting.lib.impl.DefaultEditingManager; |
50 |
import org.gvsig.vectorediting.lib.prov.point.PointEditingProviderFactory; |
51 |
|
52 |
/**
|
53 |
*
|
54 |
* Insert point provider test.
|
55 |
*
|
56 |
* This test uses src/test/resources/cartography
|
57 |
*
|
58 |
* @author llmarques
|
59 |
*
|
60 |
*/
|
61 |
public class PointTest extends AbstractLibraryAutoInitTestCase { |
62 |
|
63 |
GeometryManager geoManager; |
64 |
DataManager dataManager; |
65 |
EditingManager editingManager; |
66 |
EditingService service; |
67 |
FeatureStore featureStoreTest; |
68 |
MapContext mapContextTest; |
69 |
|
70 |
@Override
|
71 |
protected void doSetUp() throws Exception { |
72 |
|
73 |
editingManager = new DefaultEditingManager();
|
74 |
|
75 |
geoManager = GeometryLocator.getGeometryManager(); |
76 |
|
77 |
try {
|
78 |
featureStoreTest = openTestFeatureStore(); |
79 |
} catch (Exception e) { |
80 |
fail("Unexpected exception opening test feature store: "
|
81 |
+ e.getMessage()); |
82 |
} |
83 |
|
84 |
mapContextTest = |
85 |
new MapContext(new ViewPort(featureStoreTest |
86 |
.getDefaultFeatureType().getDefaultSRS())); |
87 |
|
88 |
service = |
89 |
editingManager.getEditingService( |
90 |
PointEditingProviderFactory.PROVIDER_NAME, featureStoreTest, |
91 |
mapContextTest); |
92 |
|
93 |
service.start(); |
94 |
} |
95 |
|
96 |
public void testFinish() { |
97 |
|
98 |
// Try to finish without values.
|
99 |
try {
|
100 |
|
101 |
service.finish(); |
102 |
fail("No exception has been thrown");
|
103 |
|
104 |
} catch (NullPointerException e) { |
105 |
// Test OK, got expected exception
|
106 |
} catch (FinishServiceException e) {
|
107 |
fail("Unexpected exception finishing service: " + e.getMessage());
|
108 |
} |
109 |
|
110 |
// Try to finish with valid points
|
111 |
Point point;
|
112 |
try {
|
113 |
|
114 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
115 |
service.setValue(point); |
116 |
|
117 |
Geometry geom = service.finish(); |
118 |
|
119 |
assertNotNull("geometry is null", geom);
|
120 |
assertTrue("geometry instanceof Point == false",
|
121 |
geom instanceof Point); |
122 |
assertEquals("geometry type != POINT", geom.getType(),
|
123 |
Geometry.TYPES.POINT); |
124 |
assertTrue(((Point) geom).getX() == 0); |
125 |
assertTrue(((Point) geom).getY() == 0); |
126 |
|
127 |
} catch (Exception e) { |
128 |
fail("Unexpected exception finish with valid values: "
|
129 |
+ e.getMessage()); |
130 |
} |
131 |
|
132 |
// Try to finish with null featureStore
|
133 |
try {
|
134 |
|
135 |
service = |
136 |
editingManager.getEditingService( |
137 |
PointEditingProviderFactory.PROVIDER_NAME, null,
|
138 |
mapContextTest); |
139 |
|
140 |
service.start(); |
141 |
|
142 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
143 |
service.setValue(point); |
144 |
|
145 |
service.finish(); |
146 |
fail("No exception has been thrown");
|
147 |
} catch (FinishServiceException e) {
|
148 |
// Test OK, got expected exception
|
149 |
} catch (Exception e) { |
150 |
fail("Unexpected exception finishing with null feature store: "
|
151 |
+ e.getMessage()); |
152 |
} |
153 |
|
154 |
} |
155 |
|
156 |
public void testfinishAndStore() { |
157 |
|
158 |
// Try to finish and store without values
|
159 |
try {
|
160 |
service.finishAndStore(); |
161 |
} catch (NullPointerException e) { |
162 |
// Test ok, got expected exception
|
163 |
} catch (FinishServiceException e) {
|
164 |
fail("Unexpected exception ending and storing service: "
|
165 |
+ e.getMessage()); |
166 |
} |
167 |
|
168 |
// Try to finish and store with values
|
169 |
Point point;
|
170 |
try {
|
171 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
172 |
service.setValue(point); |
173 |
|
174 |
featureStoreTest.edit(); |
175 |
service.finishAndStore(); |
176 |
featureStoreTest.finishEditing(); |
177 |
|
178 |
} catch (Exception e) { |
179 |
fail("Unexpected exception ending and storing service: "
|
180 |
+ e.getMessage()); |
181 |
} |
182 |
|
183 |
// Try to finish and store with null featureStore
|
184 |
try {
|
185 |
|
186 |
service = |
187 |
editingManager.getEditingService( |
188 |
PointEditingProviderFactory.PROVIDER_NAME, null,
|
189 |
mapContextTest); |
190 |
|
191 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
192 |
service.setValue(point); |
193 |
|
194 |
service.finishAndStore(); |
195 |
fail("No exception has been thrown");
|
196 |
} catch (FinishServiceException e) {
|
197 |
// Test OK, got expected exception
|
198 |
} catch (Exception e) { |
199 |
fail("Unexpected exception ending and storing service: "
|
200 |
+ e.getMessage()); |
201 |
} |
202 |
|
203 |
} |
204 |
|
205 |
public void testgetDrawingStatus() { |
206 |
|
207 |
Point point;
|
208 |
try {
|
209 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
210 |
DrawingStatus ds = service.getDrawingStatus(point); |
211 |
assertNull("drawingStatus != null", ds);
|
212 |
} catch (Exception e) { |
213 |
fail("Unexpected exception getting drawing status: "
|
214 |
+ e.getMessage()); |
215 |
} |
216 |
|
217 |
point = null;
|
218 |
try {
|
219 |
DrawingStatus ds = service.getDrawingStatus(point); |
220 |
assertNull("drawingStatus != null", ds);
|
221 |
} catch (Exception e) { |
222 |
fail("Unexpected exception getting drawing status: "
|
223 |
+ e.getMessage()); |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
public void testgetName() { |
229 |
|
230 |
assertEquals(PointEditingProviderFactory.PROVIDER_NAME, |
231 |
service.getName()); |
232 |
|
233 |
} |
234 |
|
235 |
public void testgetParameters() { |
236 |
|
237 |
List<EditingServiceParameter> parameters = service.getParameters();
|
238 |
|
239 |
assertNotNull(parameters); |
240 |
assertTrue(parameters.size() == 1);
|
241 |
|
242 |
EditingServiceParameter parameter = parameters.get(0);
|
243 |
|
244 |
assertTrue(parameter.getTypes().size() == 1);
|
245 |
assertTrue(parameter.getTypes().iterator().next() == EditingServiceParameter.TYPE.POSITION); |
246 |
} |
247 |
|
248 |
public void testNext() { |
249 |
|
250 |
assertNotNull(service.next()); |
251 |
|
252 |
List<EditingServiceParameter> parameters = service.getParameters();
|
253 |
assertEquals(parameters.get(0), service.next());
|
254 |
|
255 |
Point point;
|
256 |
try {
|
257 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
258 |
service.setValue(point); |
259 |
} catch (Exception e) { |
260 |
fail("Unexpected exception getting next parameter: "
|
261 |
+ e.getMessage()); |
262 |
} |
263 |
|
264 |
assertNull(service.next()); |
265 |
|
266 |
} |
267 |
|
268 |
public void testsetValue() { |
269 |
|
270 |
// Try to set valid point
|
271 |
Point point;
|
272 |
try {
|
273 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
274 |
service.setValue(point); |
275 |
} catch (Exception e) { |
276 |
fail("Unexpected exception setting value: " + e.getMessage());
|
277 |
} |
278 |
|
279 |
assertNull("Point has not been set.", service.next());
|
280 |
|
281 |
// Try to set invalid point
|
282 |
point = null;
|
283 |
try {
|
284 |
service.stop(); |
285 |
service.start(); |
286 |
service.setValue(point); |
287 |
} catch (InvalidEntryException e) {
|
288 |
// Test ok, got expected exception
|
289 |
} catch (Exception e) { |
290 |
fail("Unexpected exception setting value: " + e.getMessage());
|
291 |
} |
292 |
|
293 |
assertNotNull("Invalid point has been set", service.next());
|
294 |
|
295 |
} |
296 |
|
297 |
public void testStart() { |
298 |
service = |
299 |
editingManager.getEditingService( |
300 |
PointEditingProviderFactory.PROVIDER_NAME, featureStoreTest, |
301 |
mapContextTest); |
302 |
|
303 |
try {
|
304 |
service.start(); |
305 |
} catch (VectorEditingException e) {
|
306 |
fail("Unexpected exception starting service: " + e.getMessage());
|
307 |
} |
308 |
} |
309 |
|
310 |
public void testStop() { |
311 |
service = |
312 |
editingManager.getEditingService( |
313 |
PointEditingProviderFactory.PROVIDER_NAME, featureStoreTest, |
314 |
mapContextTest); |
315 |
|
316 |
try {
|
317 |
service.start(); |
318 |
service.stop(); |
319 |
} catch (VectorEditingException e) {
|
320 |
fail("Unexpected exception starting or stopping service: "
|
321 |
+ e.getMessage()); |
322 |
} |
323 |
} |
324 |
|
325 |
private FeatureStore openTestFeatureStore() throws Exception { |
326 |
|
327 |
URL shapeURL =
|
328 |
this.getClass().getResource("/cartography/puertos_andalucia.shp"); |
329 |
if (shapeURL == null) { |
330 |
throw new IllegalStateException("Can't locate cartography resource"); |
331 |
} |
332 |
File shapeFile = new File(shapeURL.getFile()); |
333 |
if (!shapeFile.exists()) {
|
334 |
throw new IllegalStateException("cartography resource not exists"); |
335 |
} |
336 |
|
337 |
dataManager = DALLocator.getDataManager(); |
338 |
|
339 |
DataStoreParameters params = dataManager.createStoreParameters("Shape");
|
340 |
|
341 |
params.setDynValue("shpfile", shapeFile);
|
342 |
params.setDynValue("CRS", CRSFactory.getCRS("EPSG:23030")); |
343 |
params.setDynValue("useNullGeometry", false); |
344 |
params.validate(); |
345 |
|
346 |
return (FeatureStore) dataManager.openStore("Shape", params); |
347 |
|
348 |
} |
349 |
|
350 |
} |