svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / test / java / org / gvsig / fmap / dal / store / shp / TestCreate.java @ 45641
History | View | Annotate | Download (9.38 KB)
1 | 44669 | jjdelcerro | package org.gvsig.fmap.dal.store.shp; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 44669 | jjdelcerro | import java.io.File; |
4 | import java.util.Date; |
||
5 | import java.util.List; |
||
6 | 43512 | jjdelcerro | import junit.framework.TestCase; |
7 | 44669 | jjdelcerro | import static junit.framework.TestCase.assertEquals; |
8 | import org.apache.commons.lang3.StringUtils; |
||
9 | import org.gvsig.fmap.dal.DALLocator; |
||
10 | import org.gvsig.fmap.dal.DataTypes; |
||
11 | import org.gvsig.fmap.dal.DataManager; |
||
12 | import org.gvsig.fmap.dal.DataParameters; |
||
13 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
14 | import org.gvsig.fmap.dal.DataStore; |
||
15 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
16 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
17 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
18 | import org.gvsig.fmap.dal.feature.Feature; |
||
19 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
21 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
22 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
23 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
24 | import org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter; |
||
25 | import static org.gvsig.fmap.dal.store.shp.SHPStoreProvider.GEOMETRY_ATTIBUTE_NAME; |
||
26 | import org.gvsig.fmap.geom.Geometry; |
||
27 | 44098 | jjdelcerro | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
28 | 44669 | jjdelcerro | import org.slf4j.Logger; |
29 | import org.slf4j.LoggerFactory; |
||
30 | 43512 | jjdelcerro | |
31 | 44669 | jjdelcerro | public class TestCreate extends TestCase { |
32 | private static final Logger LOGGER = LoggerFactory.getLogger(TestCreate.class); |
||
33 | 43512 | jjdelcerro | |
34 | 44669 | jjdelcerro | public TestCreate(String testName) { |
35 | 43512 | jjdelcerro | super(testName);
|
36 | } |
||
37 | |||
38 | @Override
|
||
39 | protected void setUp() throws Exception { |
||
40 | super.setUp();
|
||
41 | 44098 | jjdelcerro | new DefaultLibrariesInitializer().fullInitialize();
|
42 | 43512 | jjdelcerro | } |
43 | |||
44 | @Override
|
||
45 | protected void tearDown() throws Exception { |
||
46 | super.tearDown();
|
||
47 | } |
||
48 | |||
49 | // TODO add test methods here. The name must begin with 'test'. For example:
|
||
50 | // public void testHello() {}
|
||
51 | 44139 | jjdelcerro | |
52 | 44669 | jjdelcerro | protected String getProviderName() { |
53 | return DataStore.SHAPE_PROVIDER_NAME;
|
||
54 | 43512 | jjdelcerro | } |
55 | |||
56 | 44669 | jjdelcerro | protected String getTargetFilename() { |
57 | return "testCreateTarget1.shp"; |
||
58 | 43512 | jjdelcerro | } |
59 | 44139 | jjdelcerro | |
60 | 44669 | jjdelcerro | protected FeatureStore openTargetStore1() throws Exception { |
61 | DataManager dataManager = DALLocator.getDataManager(); |
||
62 | File f = TestUtils.getResource(getTargetFilename());
|
||
63 | FeatureStore store; |
||
64 | try {
|
||
65 | store = (FeatureStore) dataManager.openStore( |
||
66 | getProviderName(), |
||
67 | "shpFile=",f,
|
||
68 | DataParameters.CRS_PARAMTER_NAME, "EPSG:4326"
|
||
69 | ); |
||
70 | } catch(ValidateDataParametersException ex) {
|
||
71 | LOGGER.warn(ex.getLocalizedMessageStack()); |
||
72 | throw ex;
|
||
73 | } |
||
74 | return store;
|
||
75 | 43512 | jjdelcerro | } |
76 | 44139 | jjdelcerro | |
77 | 44669 | jjdelcerro | protected void createFrom(FeatureStore sourceStore) throws Exception { |
78 | DataServerExplorer explorer = TestUtils.openServerExplorer(); |
||
79 | NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters( |
||
80 | getProviderName() |
||
81 | ); |
||
82 | ((FilesystemStoreParameters)params).setFile(TestUtils.getResource(getTargetFilename())); |
||
83 | params.setDynValue(DataParameters.CRS_PARAMTER_NAME, "EPSG:4326");
|
||
84 | params.setDynValue("GeometryType", Geometry.TYPES.POINT);
|
||
85 | EditableFeatureType ft = params.getDefaultFeatureType(); |
||
86 | 45641 | jjdelcerro | ft.copyFrom(sourceStore.getDefaultFeatureType()); |
87 | 44669 | jjdelcerro | explorer.add(getProviderName(), params, true);
|
88 | 43512 | jjdelcerro | } |
89 | |||
90 | 44669 | jjdelcerro | protected void checkTypes(FeatureType sourceFeatureType) throws Exception { |
91 | // DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
||
92 | // DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
||
93 | 44421 | jjdelcerro | |
94 | 44669 | jjdelcerro | FeatureStore targetStore = openTargetStore1(); |
95 | FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
||
96 | 44421 | jjdelcerro | |
97 | 44669 | jjdelcerro | assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
98 | for (int i = 0; i < sourceFeatureType.size(); i++) { |
||
99 | FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
||
100 | FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
||
101 | switch(sourceAttr.getType()) {
|
||
102 | case DataTypes.GEOMETRY:
|
||
103 | if( targetAttr.getName().equalsIgnoreCase(GEOMETRY_ATTIBUTE_NAME) ) {
|
||
104 | // Si no hay fichero DAL se le habra asignado el nombre por defecto.
|
||
105 | } else {
|
||
106 | assertEquals( |
||
107 | String.format("Field %s name mismatch", sourceAttr.getName()), |
||
108 | sourceAttr.getName(), |
||
109 | targetAttr.getName() |
||
110 | ); |
||
111 | } |
||
112 | assertEquals( |
||
113 | String.format("Field %s type mismatch", sourceAttr.getName()), |
||
114 | sourceAttr.getDataTypeName(), |
||
115 | targetAttr.getDataTypeName() |
||
116 | ); |
||
117 | assertEquals( |
||
118 | String.format("Field %s geometry type mismatch", sourceAttr.getName()), |
||
119 | sourceAttr.getGeomType().getName(), |
||
120 | targetAttr.getGeomType().getName() |
||
121 | ); |
||
122 | assertEquals( |
||
123 | String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), |
||
124 | sourceAttr.getSRS().toString(), |
||
125 | targetAttr.getSRS().toString() |
||
126 | ); |
||
127 | assertEquals( |
||
128 | String.format("Field %s size mismatch", sourceAttr.getName()), |
||
129 | sourceAttr.getSize(), |
||
130 | targetAttr.getSize() |
||
131 | ); |
||
132 | assertEquals( |
||
133 | String.format("Field %s precision mismatch", sourceAttr.getName()), |
||
134 | sourceAttr.getPrecision(), |
||
135 | targetAttr.getPrecision() |
||
136 | ); |
||
137 | assertEquals( |
||
138 | String.format("Field %s scale mismatch", sourceAttr.getName()), |
||
139 | sourceAttr.getScale(), |
||
140 | targetAttr.getScale() |
||
141 | ); |
||
142 | break;
|
||
143 | default:
|
||
144 | 44926 | jjdelcerro | // Solo comprobams que pasa con las geometrias, los otros
|
145 | // tipos de datos confiamos que los comprueben los test
|
||
146 | // del dbf.
|
||
147 | break;
|
||
148 | 44669 | jjdelcerro | } |
149 | } |
||
150 | 44421 | jjdelcerro | } |
151 | |||
152 | 44669 | jjdelcerro | protected void copyFrom(FeatureStore sourceStore, int mode) throws Exception { |
153 | FeatureStore targetStore = openTargetStore1(); |
||
154 | targetStore.edit(mode); |
||
155 | try {
|
||
156 | for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
||
157 | EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
||
158 | targetStore.insert(targetFeature); |
||
159 | } |
||
160 | } finally {
|
||
161 | targetStore.finishEditing(); |
||
162 | } |
||
163 | 43512 | jjdelcerro | } |
164 | |||
165 | 44669 | jjdelcerro | protected void checkData(FeatureStore sourceStore) throws Exception { |
166 | FeatureStore targetStore = openTargetStore1(); |
||
167 | 44139 | jjdelcerro | |
168 | 44669 | jjdelcerro | List<Feature> sourceFeatures = sourceStore.getFeatures();
|
169 | List<Feature> targetFeatures = targetStore.getFeatures();
|
||
170 | assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
||
171 | for (int i = 0; i < targetFeatures.size(); i++) { |
||
172 | Feature sourceFeature = sourceFeatures.get(i); |
||
173 | Feature targetFeature = targetFeatures.get(i); |
||
174 | for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
||
175 | switch(sourceAttr.getType()) {
|
||
176 | case DataTypes.GEOMETRY:
|
||
177 | assertEquals( |
||
178 | String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
||
179 | sourceFeature.get(sourceAttr.getName()), |
||
180 | targetFeature.get(sourceAttr.getName()) |
||
181 | ); |
||
182 | break;
|
||
183 | default:
|
||
184 | 44926 | jjdelcerro | // Solo comprobams que pasa con las geometrias, los otros
|
185 | // tipos de datos confiamos que los comprueben los test
|
||
186 | // del dbf.
|
||
187 | break;
|
||
188 | 44669 | jjdelcerro | } |
189 | } |
||
190 | } |
||
191 | 43512 | jjdelcerro | } |
192 | |||
193 | |||
194 | 44669 | jjdelcerro | public void testCreateWithDALFile() throws Exception { |
195 | FeatureStore sourceStore = TestUtils.openSourceStore1(); |
||
196 | 44384 | jjdelcerro | |
197 | 44669 | jjdelcerro | createFrom(sourceStore); |
198 | 44384 | jjdelcerro | |
199 | 44669 | jjdelcerro | checkTypes(sourceStore.getDefaultFeatureType()); |
200 | copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
||
201 | checkData(sourceStore); |
||
202 | 43512 | jjdelcerro | |
203 | 44669 | jjdelcerro | createFrom(sourceStore); |
204 | copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
||
205 | checkData(sourceStore); |
||
206 | 44139 | jjdelcerro | |
207 | 43512 | jjdelcerro | } |
208 | |||
209 | 44669 | jjdelcerro | public void testCreateWithoutDALFile() throws Exception { |
210 | FeatureStore sourceStore = TestUtils.openSourceStore1(); |
||
211 | 44098 | jjdelcerro | |
212 | 44669 | jjdelcerro | createFrom(sourceStore); |
213 | TestUtils.removeDALFile(getTargetFilename()); |
||
214 | 44098 | jjdelcerro | |
215 | 44669 | jjdelcerro | checkTypes(sourceStore.getDefaultFeatureType()); |
216 | copyFrom(sourceStore, FeatureStore.MODE_APPEND); |
||
217 | TestUtils.removeDALFile(getTargetFilename()); |
||
218 | checkData(sourceStore); |
||
219 | 44098 | jjdelcerro | |
220 | 44669 | jjdelcerro | createFrom(sourceStore); |
221 | TestUtils.removeDALFile(getTargetFilename()); |
||
222 | copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT); |
||
223 | TestUtils.removeDALFile(getTargetFilename()); |
||
224 | checkData(sourceStore); |
||
225 | 44139 | jjdelcerro | |
226 | 44098 | jjdelcerro | } |
227 | 43939 | jjdelcerro | } |