gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.polyline / src / test / java / org / gvsig / vectorediting / lib / prov / polyline / PolylineTest.java @ 144
History | View | Annotate | Download (13.7 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.vectorediting.lib.prov.polyline
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.vectorediting.lib.prov.polyline; |
8 |
|
9 |
import java.io.File; |
10 |
import java.net.URL; |
11 |
import java.util.List; |
12 |
|
13 |
import org.gvsig.fmap.crs.CRSFactory; |
14 |
import org.gvsig.fmap.dal.DALLocator; |
15 |
import org.gvsig.fmap.dal.DataManager; |
16 |
import org.gvsig.fmap.dal.DataStoreParameters; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.geom.Geometry; |
19 |
import org.gvsig.fmap.geom.GeometryLocator; |
20 |
import org.gvsig.fmap.geom.GeometryManager; |
21 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
22 |
import org.gvsig.fmap.geom.primitive.Curve; |
23 |
import org.gvsig.fmap.geom.primitive.Line; |
24 |
import org.gvsig.fmap.geom.primitive.Point; |
25 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
26 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
27 |
import org.gvsig.vectorediting.lib.api.EditingService; |
28 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
29 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
30 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
31 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
32 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
33 |
import org.gvsig.vectorediting.lib.impl.DefaultEditingManager; |
34 |
|
35 |
/**
|
36 |
* Polyline provider test
|
37 |
*
|
38 |
* This test uses src/test/resources/cartography
|
39 |
*
|
40 |
* @author lmarques
|
41 |
*
|
42 |
*/
|
43 |
public class PolylineTest extends AbstractLibraryAutoInitTestCase { |
44 |
|
45 |
DefaultEditingManager editingManager; |
46 |
GeometryManager geoManager; |
47 |
FeatureStore featureStoreTest; |
48 |
EditingService service; |
49 |
DataManager dataManager; |
50 |
|
51 |
@Override
|
52 |
protected void doSetUp() throws Exception { |
53 |
editingManager = new DefaultEditingManager();
|
54 |
|
55 |
geoManager = GeometryLocator.getGeometryManager(); |
56 |
|
57 |
try {
|
58 |
featureStoreTest = openTestFeatureStore(); |
59 |
} catch (Exception e) { |
60 |
fail("Unexpected exception opening test feature store: "
|
61 |
+ e.getMessage()); |
62 |
} |
63 |
|
64 |
service = |
65 |
editingManager.getEditingService( |
66 |
PolylineEditingProviderFactory.PROVIDER_NAME, featureStoreTest); |
67 |
|
68 |
service.start(); |
69 |
|
70 |
} |
71 |
|
72 |
public void testFinish() { |
73 |
|
74 |
// Finish without values
|
75 |
try {
|
76 |
|
77 |
service.finish(); |
78 |
fail("No exception has been thrown");
|
79 |
|
80 |
} catch (NullPointerException e) { |
81 |
// Test OK, got expected exception
|
82 |
} catch (Exception e) { |
83 |
fail("Unexpected exception finishing service: " + e.getMessage());
|
84 |
} |
85 |
|
86 |
// Finish with one value
|
87 |
Point point = null; |
88 |
try {
|
89 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
90 |
|
91 |
service.setValue(point); |
92 |
service.finish(); |
93 |
fail("No exception has been thrown");
|
94 |
} catch (NullPointerException e) { |
95 |
// Test ok, got expected exception
|
96 |
} catch (Exception e) { |
97 |
fail("Unexpected exception has been thrown endig service. "
|
98 |
+ e.getMessage()); |
99 |
} |
100 |
|
101 |
// Finish with two values mode line
|
102 |
Point point2 = null; |
103 |
try {
|
104 |
point2 = geoManager.createPoint(10, 10, Geometry.SUBTYPES.GEOM2D); |
105 |
service.setValue(point2); |
106 |
|
107 |
Geometry geom = service.finish(); |
108 |
|
109 |
assertTrue(geom instanceof MultiCurve);
|
110 |
MultiCurve multicurve = (MultiCurve) geom; |
111 |
Curve curve = multicurve.getCurveAt(0);
|
112 |
|
113 |
assertTrue(curve.getVertex(0).getX() == point.getX());
|
114 |
assertTrue(curve.getVertex(0).getY() == point.getY());
|
115 |
assertTrue(curve.getVertex(1).getX() == point2.getX());
|
116 |
assertTrue(curve.getVertex(1).getY() == point2.getY());
|
117 |
|
118 |
} catch (Exception e) { |
119 |
fail("Unexpected exception has been thrown ending service. "
|
120 |
+ e.getMessage()); |
121 |
} |
122 |
|
123 |
// Restart service
|
124 |
restartService(); |
125 |
|
126 |
// Finish with three aligned values mode arc
|
127 |
Point point3;
|
128 |
try {
|
129 |
point3 = geoManager.createPoint(20, 20, Geometry.SUBTYPES.GEOM2D); |
130 |
|
131 |
service.setValue(point); |
132 |
service.setValue(point2); |
133 |
service.setValue("A");
|
134 |
service.setValue(point3); |
135 |
|
136 |
service.finish(); |
137 |
} catch (FinishServiceException e) {
|
138 |
// Test ok, got expected exception
|
139 |
} catch (Exception e) { |
140 |
fail("Unexpected exception has been thrown ending service. "
|
141 |
+ e.getMessage()); |
142 |
} |
143 |
|
144 |
// Restart service
|
145 |
restartService(); |
146 |
|
147 |
// Finish with random values
|
148 |
try {
|
149 |
for (int i = 0; i < 200; i++) { |
150 |
point = |
151 |
geoManager.createPoint(Math.random() * 10000, |
152 |
Math.random() * 10000, Geometry.SUBTYPES.GEOM2D); |
153 |
service.setValue(point); |
154 |
if (i > 3) { |
155 |
if ((int) (Math.random() * 2) > 0) { |
156 |
service.setValue("A");
|
157 |
} else {
|
158 |
service.setValue("L");
|
159 |
} |
160 |
} |
161 |
} |
162 |
|
163 |
service.finish(); |
164 |
} catch (Exception e) { |
165 |
fail("Unexpected exception has been thrown ending service. "
|
166 |
+ e.getMessage()); |
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
public void testfinishAndStore() { |
172 |
// Finish without values
|
173 |
try {
|
174 |
|
175 |
service.finishAndStore(); |
176 |
fail("No exception has been thrown");
|
177 |
|
178 |
} catch (NullPointerException e) { |
179 |
// Test OK, got expected exception
|
180 |
} catch (Exception e) { |
181 |
fail("Unexpected exception finishing service: " + e.getMessage());
|
182 |
} |
183 |
|
184 |
// Finish with one value
|
185 |
Point point = null; |
186 |
try {
|
187 |
point = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
188 |
|
189 |
service.setValue(point); |
190 |
service.finishAndStore(); |
191 |
fail("No exception has been thrown");
|
192 |
} catch (NullPointerException e) { |
193 |
// Test ok, got expected exception
|
194 |
} catch (Exception e) { |
195 |
fail("Unexpected exception has been thrown endig service. "
|
196 |
+ e.getMessage()); |
197 |
} |
198 |
|
199 |
// Finish with two values mode line
|
200 |
Point point2 = null; |
201 |
try {
|
202 |
point2 = geoManager.createPoint(10, 10, Geometry.SUBTYPES.GEOM2D); |
203 |
service.setValue(point2); |
204 |
|
205 |
featureStoreTest.edit(); |
206 |
service.finishAndStore(); |
207 |
featureStoreTest.finishEditing(); |
208 |
|
209 |
} catch (Exception e) { |
210 |
fail("Unexpected exception has been thrown ending service. "
|
211 |
+ e.getMessage()); |
212 |
} |
213 |
|
214 |
// Restart service
|
215 |
restartService(); |
216 |
|
217 |
// Finish with three aligned values mode arc
|
218 |
Point point3;
|
219 |
try {
|
220 |
point3 = geoManager.createPoint(20, 20, Geometry.SUBTYPES.GEOM2D); |
221 |
|
222 |
service.setValue(point); |
223 |
service.setValue(point2); |
224 |
service.setValue("A");
|
225 |
service.setValue(point3); |
226 |
|
227 |
service.finishAndStore(); |
228 |
} catch (FinishServiceException e) {
|
229 |
// Test ok, got expected exception
|
230 |
} catch (Exception e) { |
231 |
fail("Unexpected exception has been thrown ending service. "
|
232 |
+ e.getMessage()); |
233 |
} |
234 |
|
235 |
// Restart service
|
236 |
restartService(); |
237 |
|
238 |
// Finish with random values
|
239 |
try {
|
240 |
for (int i = 0; i < 200; i++) { |
241 |
point = |
242 |
geoManager.createPoint(Math.random() * 10000, |
243 |
Math.random() * 10000, Geometry.SUBTYPES.GEOM2D); |
244 |
service.setValue(point); |
245 |
if (i > 3) { |
246 |
if ((int) (Math.random() * 2) > 0) { |
247 |
service.setValue("A");
|
248 |
} else {
|
249 |
service.setValue("L");
|
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
featureStoreTest.edit(); |
255 |
service.finishAndStore(); |
256 |
featureStoreTest.finishEditing(); |
257 |
} catch (Exception e) { |
258 |
fail("Unexpected exception has been thrown ending service. "
|
259 |
+ e.getMessage()); |
260 |
} |
261 |
|
262 |
} |
263 |
|
264 |
public void testgetDrawingStatus() { |
265 |
|
266 |
// Without values and mouse point null
|
267 |
try {
|
268 |
service.getDrawingStatus(null);
|
269 |
} catch (NullPointerException e) { |
270 |
// Testk ok, got expected exception
|
271 |
} catch (Exception e) { |
272 |
fail("Unexpected exception has been thrown getting drawing status. "
|
273 |
+ e.getMessage()); |
274 |
} |
275 |
|
276 |
// Without values and valid mouse point
|
277 |
Point mousePoint = null; |
278 |
try {
|
279 |
|
280 |
mousePoint = |
281 |
geoManager.createPoint(20, 50, Geometry.SUBTYPES.GEOM2D); |
282 |
DrawingStatus drawingStatus = service.getDrawingStatus(mousePoint); |
283 |
|
284 |
assertNull(drawingStatus); |
285 |
|
286 |
} catch (Exception e) { |
287 |
fail("Unexpected exception has been thrown getting drawing status. "
|
288 |
+ e.getMessage()); |
289 |
} |
290 |
|
291 |
// One valid values and valid mouse position
|
292 |
Point point1;
|
293 |
try {
|
294 |
|
295 |
point1 = |
296 |
geoManager.createPoint(20, 50, Geometry.SUBTYPES.GEOM2D); |
297 |
|
298 |
service.setValue(point1); |
299 |
DrawingStatus drawingStatus = service.getDrawingStatus(mousePoint); |
300 |
|
301 |
assertNotNull(drawingStatus); |
302 |
assertTrue(drawingStatus.getGeometries().size() == 1);
|
303 |
Geometry geometry = drawingStatus.getGeometries().get(0);
|
304 |
assertTrue(geometry instanceof Line); |
305 |
|
306 |
Line line = (Line) geometry; |
307 |
assertTrue(line.getVertex(0).getX() == point1.getX());
|
308 |
assertTrue(line.getVertex(0).getY() == point1.getY());
|
309 |
assertTrue(line.getVertex(1).getX() == point1.getX());
|
310 |
assertTrue(line.getVertex(1).getY() == point1.getY());
|
311 |
|
312 |
} catch (Exception e) { |
313 |
fail("Unexpected exception has been thrown getting drawing status. "
|
314 |
+ e.getMessage()); |
315 |
} |
316 |
|
317 |
} |
318 |
|
319 |
public void testgetName() { |
320 |
|
321 |
assertEquals(PolylineEditingProviderFactory.PROVIDER_NAME, |
322 |
service.getName()); |
323 |
|
324 |
} |
325 |
|
326 |
public void testgetParameters() { |
327 |
|
328 |
List<EditingServiceParameter> parameters = service.getParameters();
|
329 |
|
330 |
assertTrue(parameters.size() == 1);
|
331 |
assertTrue(parameters.get(0).getTypes()
|
332 |
.contains(EditingServiceParameter.TYPE.LIST_POSITIONS)); |
333 |
} |
334 |
|
335 |
public void testNext() { |
336 |
|
337 |
assertNotNull(service.next()); |
338 |
|
339 |
List<EditingServiceParameter> parameters = service.getParameters();
|
340 |
assertEquals(parameters.get(0), service.next());
|
341 |
|
342 |
for (int i = 0; i < 10; i++) { |
343 |
try {
|
344 |
Point point =
|
345 |
geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
346 |
service.setValue(point); |
347 |
|
348 |
} catch (Exception e) { |
349 |
fail("Unexpected exception creating point or setting value. "
|
350 |
+ e.getMessage()); |
351 |
} |
352 |
} |
353 |
|
354 |
assertEquals(parameters.get(0), service.next());
|
355 |
} |
356 |
|
357 |
public void testsetValue() { |
358 |
|
359 |
// Valid point
|
360 |
try {
|
361 |
Point point =
|
362 |
geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
363 |
service.setValue(point); |
364 |
} catch (Exception e) { |
365 |
fail("Unexpected exception setting value. " + e.getMessage());
|
366 |
} |
367 |
|
368 |
// Invalid value
|
369 |
try {
|
370 |
Point point = null; |
371 |
service.setValue(point); |
372 |
} catch (InvalidEntryException e) {
|
373 |
// Test ok, got expected value
|
374 |
} catch (Exception e) { |
375 |
fail("Unexpected exception setting value. " + e.getMessage());
|
376 |
} |
377 |
|
378 |
} |
379 |
|
380 |
public void testStart() { |
381 |
service = |
382 |
editingManager.getEditingService( |
383 |
PolylineEditingProviderFactory.PROVIDER_NAME, featureStoreTest); |
384 |
|
385 |
try {
|
386 |
service.start(); |
387 |
} catch (StartServiceException e) {
|
388 |
fail("Unexpected exception starting service: " + e.getMessage());
|
389 |
} |
390 |
} |
391 |
|
392 |
public void testStop() { |
393 |
service = |
394 |
editingManager.getEditingService( |
395 |
PolylineEditingProviderFactory.PROVIDER_NAME, featureStoreTest); |
396 |
|
397 |
try {
|
398 |
service.start(); |
399 |
service.stop(); |
400 |
} catch (StartServiceException e) {
|
401 |
fail("Unexpected exception starting service: " + e.getMessage());
|
402 |
} catch (StopServiceException e) {
|
403 |
fail("Unexpected exception stopping service: " + e.getMessage());
|
404 |
} |
405 |
} |
406 |
|
407 |
private FeatureStore openTestFeatureStore() throws Exception { |
408 |
|
409 |
URL shapeURL =
|
410 |
this.getClass().getResource("/cartography/hidro_andalucia.shp"); |
411 |
if (shapeURL == null) { |
412 |
throw new IllegalStateException("Can't locate cartography resource"); |
413 |
} |
414 |
File shapeFile = new File(shapeURL.getFile()); |
415 |
if (!shapeFile.exists()) {
|
416 |
throw new IllegalStateException("cartography resource not exists"); |
417 |
} |
418 |
|
419 |
dataManager = DALLocator.getDataManager(); |
420 |
|
421 |
DataStoreParameters params = dataManager.createStoreParameters("Shape");
|
422 |
|
423 |
params.setDynValue("shpfile", shapeFile);
|
424 |
params.setDynValue("CRS", CRSFactory.getCRS("EPSG:23030")); |
425 |
params.setDynValue("useNullGeometry", false); |
426 |
params.validate(); |
427 |
|
428 |
return (FeatureStore) dataManager.openStore("Shape", params); |
429 |
|
430 |
} |
431 |
|
432 |
public void restartService() { |
433 |
try {
|
434 |
service.stop(); |
435 |
service.start(); |
436 |
} catch (Exception e) { |
437 |
fail("Unexpected exception has been thrown restarting service. "
|
438 |
+ e.getMessage()); |
439 |
} |
440 |
} |
441 |
|
442 |
} |