gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circlecr / src / test / java / org / gvsig / vectorediting / lib / prov / circlecr / CircumferenceCRTest.java @ 322
History | View | Annotate | Download (6.83 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 |
|
25 |
package org.gvsig.vectorediting.lib.prov.circlecr; |
26 |
|
27 |
import java.io.File; |
28 |
import java.net.URL; |
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.aggregate.MultiCurve; |
39 |
import org.gvsig.fmap.geom.primitive.Arc; |
40 |
import org.gvsig.fmap.geom.primitive.Point; |
41 |
import org.gvsig.fmap.mapcontext.MapContext; |
42 |
import org.gvsig.fmap.mapcontext.ViewPort; |
43 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
44 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
45 |
import org.gvsig.vectorediting.lib.api.EditingService; |
46 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
47 |
import org.gvsig.vectorediting.lib.impl.DefaultEditingManager; |
48 |
|
49 |
/**
|
50 |
* Circumference provider test
|
51 |
*
|
52 |
* This test uses src/test/resources/cartography
|
53 |
*
|
54 |
* @author llmarques
|
55 |
*
|
56 |
*/
|
57 |
public class CircumferenceCRTest extends AbstractLibraryAutoInitTestCase { |
58 |
|
59 |
GeometryManager geoManager; |
60 |
DataManager dataManager; |
61 |
EditingManager editingManager; |
62 |
EditingService service; |
63 |
FeatureStore featureStoreTest; |
64 |
MapContext mapContextTest; |
65 |
|
66 |
@Override
|
67 |
protected void doSetUp() throws Exception { |
68 |
|
69 |
editingManager = new DefaultEditingManager();
|
70 |
|
71 |
geoManager = GeometryLocator.getGeometryManager(); |
72 |
|
73 |
try {
|
74 |
featureStoreTest = openFeatureStoretest("hidro_andalucia.shp");
|
75 |
} catch (Exception e) { |
76 |
fail("Unexpected exception" + e.getMessage());
|
77 |
} |
78 |
|
79 |
mapContextTest = |
80 |
new MapContext(new ViewPort(featureStoreTest |
81 |
.getDefaultFeatureType().getDefaultSRS())); |
82 |
|
83 |
service = |
84 |
editingManager.getEditingService( |
85 |
CircumferenceCREditingProviderFactory.PROVIDER_NAME, |
86 |
featureStoreTest, mapContextTest); |
87 |
|
88 |
service.start(); |
89 |
} |
90 |
|
91 |
public void testFinish() { |
92 |
// Try to finish without values
|
93 |
try {
|
94 |
service.finish(); |
95 |
} catch (NullPointerException e) { |
96 |
// test ok, got expected exception
|
97 |
} catch (Exception e) { |
98 |
fail("Unexpected exception ending service: " + e.getMessage());
|
99 |
} |
100 |
|
101 |
// Try to finish with only center value
|
102 |
Point center = null; |
103 |
try {
|
104 |
center = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
105 |
|
106 |
service.setValue(center); |
107 |
service.finish(); |
108 |
fail("No exception has been thrown");
|
109 |
|
110 |
} catch (NullPointerException e) { |
111 |
// Test ok, got expected exception
|
112 |
} catch (Exception e) { |
113 |
fail("Unexpected exception ending service: " + e.getMessage());
|
114 |
} |
115 |
|
116 |
// Restart service
|
117 |
try {
|
118 |
service.stop(); |
119 |
service.start(); |
120 |
} catch (Exception e) { |
121 |
fail("Unexpected exception restarting service: " + e.getMessage());
|
122 |
} |
123 |
|
124 |
// Try to finish with valid points
|
125 |
Double radius;
|
126 |
try {
|
127 |
radius = 10.0;
|
128 |
|
129 |
service.setValue(center); |
130 |
service.setValue(radius); |
131 |
|
132 |
Geometry geom = service.finish(); |
133 |
|
134 |
assertNotNull("geometry is null", geom);
|
135 |
|
136 |
assertEquals("geometry type != MULTICURVE", geom.getType(),
|
137 |
Geometry.TYPES.MULTICURVE); |
138 |
MultiCurve multicurve = (MultiCurve) geom; |
139 |
|
140 |
assertTrue(multicurve.getCurveAt(0) instanceof Arc); |
141 |
Arc arc = (Arc) multicurve.getCurveAt(0);
|
142 |
|
143 |
assertTrue(arc.getCenterPoint().getX() == center.getX()); |
144 |
assertTrue(arc.getCenterPoint().getY() == center.getY()); |
145 |
|
146 |
Point initPoint = arc.getInitPoint();
|
147 |
assertTrue(initPoint.distance(center) == radius); |
148 |
|
149 |
Point endPoint = arc.getEndPoint();
|
150 |
assertTrue(endPoint.distance(center) == radius); |
151 |
|
152 |
} catch (Exception e) { |
153 |
fail("Unexpected exception ending service. " + e.getMessage());
|
154 |
} |
155 |
|
156 |
// Try to finish with null featureStore
|
157 |
try {
|
158 |
|
159 |
service = |
160 |
editingManager.getEditingService( |
161 |
CircumferenceCREditingProviderFactory.PROVIDER_NAME, null,
|
162 |
mapContextTest); |
163 |
|
164 |
service.start(); |
165 |
|
166 |
center = geoManager.createPoint(0, 0, Geometry.SUBTYPES.GEOM2D); |
167 |
radius = 10.0;
|
168 |
|
169 |
service.setValue(center); |
170 |
service.setValue(radius); |
171 |
|
172 |
service.finish(); |
173 |
fail("No exception has been thrown");
|
174 |
} catch (FinishServiceException e) {
|
175 |
// Test OK, got expected exception
|
176 |
} catch (Exception e) { |
177 |
fail("Unexpected exception finishing with null feature store: "
|
178 |
+ e.getMessage()); |
179 |
} |
180 |
|
181 |
} |
182 |
|
183 |
public void testgetName() { |
184 |
assertEquals(CircumferenceCREditingProviderFactory.PROVIDER_NAME, |
185 |
service.getName()); |
186 |
} |
187 |
|
188 |
private FeatureStore openFeatureStoretest(String resourceName) |
189 |
throws Exception { |
190 |
|
191 |
URL shapeURL =
|
192 |
this.getClass().getResource("/cartography/" + resourceName); |
193 |
if (shapeURL == null) { |
194 |
throw new IllegalStateException("Can't locate cartography resource"); |
195 |
} |
196 |
File shapeFile = new File(shapeURL.getFile()); |
197 |
if (!shapeFile.exists()) {
|
198 |
throw new IllegalStateException("cartography resource not exists"); |
199 |
} |
200 |
|
201 |
dataManager = DALLocator.getDataManager(); |
202 |
|
203 |
DataStoreParameters params = dataManager.createStoreParameters("Shape");
|
204 |
|
205 |
params.setDynValue("shpfile", shapeFile);
|
206 |
params.setDynValue("CRS", CRSFactory.getCRS("EPSG:23030")); |
207 |
params.setDynValue("useNullGeometry", false); |
208 |
params.validate(); |
209 |
|
210 |
return (FeatureStore) dataManager.openStore("Shape", params); |
211 |
|
212 |
} |
213 |
|
214 |
} |