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 / Bug15597Test.java @ 40435
History | View | Annotate | Download (6.24 KB)
1 | 40435 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
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 | */
|
||
22 | package org.gvsig.fmap.dal.store.shp; |
||
23 | |||
24 | import java.io.File; |
||
25 | |||
26 | import junit.framework.TestCase; |
||
27 | |||
28 | import org.gvsig.fmap.dal.DALLocator; |
||
29 | import org.gvsig.fmap.dal.DataManager; |
||
30 | import org.gvsig.fmap.dal.DataTypes; |
||
31 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
32 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
33 | import org.gvsig.fmap.dal.feature.Feature; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
35 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
36 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
37 | import org.gvsig.fmap.geom.Geometry; |
||
38 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
39 | import org.gvsig.fmap.geom.GeometryLocator; |
||
40 | import org.gvsig.fmap.geom.GeometryManager; |
||
41 | import org.gvsig.fmap.geom.type.GeometryType; |
||
42 | import org.gvsig.tools.dispose.DisposableIterator; |
||
43 | import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
||
44 | |||
45 | /**
|
||
46 | * Code to test bug gvsig-desktop#15597. This is based on the code provided by
|
||
47 | * the bug reporter (see ticket), thanks to him!!
|
||
48 | *
|
||
49 | * @author gvSIG Team
|
||
50 | * @version $Id$
|
||
51 | */
|
||
52 | public class Bug15597Test extends TestCase { |
||
53 | private static final String TEMP_PATH = System.getProperty("java.io.tmpdir") + File.separator; |
||
54 | |||
55 | public void testBug15642() throws Exception { |
||
56 | new DefaultLibrariesInitializer().fullInitialize();
|
||
57 | DataManager manager = DALLocator.getDataManager(); |
||
58 | GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
||
59 | |||
60 | //Create a store
|
||
61 | NewFeatureStoreParameters destParams = |
||
62 | (NewFeatureStoreParameters) manager.createNewStoreParameters( |
||
63 | "FilesystemExplorer", "Shape"); |
||
64 | |||
65 | //Create a feature type with two fields: "geom" and "field1"
|
||
66 | EditableFeatureType type = destParams.getDefaultFeatureType(); |
||
67 | GeometryType geometryType = |
||
68 | geometryManager.getGeometryType(Geometry.TYPES.POINT, |
||
69 | Geometry.SUBTYPES.GEOM2D); |
||
70 | type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
|
||
71 | .setGeometryType(geometryType); |
||
72 | type.setDefaultGeometryAttributeName("geom");
|
||
73 | type.add("field1", DataTypes.STRING).setSize(2); |
||
74 | |||
75 | destParams.setDynValue("shpfile", TEMP_PATH + "mySHP.shp"); |
||
76 | destParams.setDynValue("crs", "EPSG:23030"); |
||
77 | destParams.setDefaultFeatureType(type); |
||
78 | |||
79 | manager.newStore("FilesystemExplorer", "Shape", destParams, true); |
||
80 | FeatureStore store = |
||
81 | (FeatureStore) manager.openStore("Shape", destParams);
|
||
82 | |||
83 | |||
84 | //Edit the store and add a new Feature.
|
||
85 | store.edit(); |
||
86 | |||
87 | EditableFeature feature1 = store.createNewFeature().getEditable(); |
||
88 | feature1.setGeometry(type.getDefaultGeometryAttributeIndex(), |
||
89 | geometryManager.createPoint(0, 0, SUBTYPES.GEOM2D)); |
||
90 | feature1.set("field1", "Hi"); |
||
91 | |||
92 | store.insert(feature1); |
||
93 | |||
94 | //Finish the edition
|
||
95 | store.finishEditing(); |
||
96 | |||
97 | //Edit the feature type and add a new field: "field2"
|
||
98 | store.edit(); |
||
99 | |||
100 | EditableFeatureType type2 = |
||
101 | store.getDefaultFeatureType().getEditable(); |
||
102 | type2.add("field2", DataTypes.STRING).setSize(10); |
||
103 | store.update(type2); |
||
104 | |||
105 | assertEquals(store.getDefaultFeatureType().getAttributeDescriptors().length, 3);
|
||
106 | |||
107 | //Add a second feature
|
||
108 | EditableFeature feature2 = store.createNewFeature().getEditable(); |
||
109 | feature2.setGeometry(type2.getDefaultGeometryAttributeIndex(), |
||
110 | geometryManager.createPoint(1, 1, SUBTYPES.GEOM2D)); |
||
111 | feature2.set("field1", "Hi"); |
||
112 | feature2.set("field2", "Max"); |
||
113 | |||
114 | store.insert(feature2); |
||
115 | |||
116 | //The edition is not finished. Check if all the features have the two fields
|
||
117 | FeatureSet featureSet = store.getFeatureSet(); |
||
118 | DisposableIterator it = featureSet.fastIterator(); |
||
119 | |||
120 | it.hasNext(); |
||
121 | Feature feature = (Feature)it.next(); |
||
122 | assertNotNull(feature.getDefaultGeometry()); |
||
123 | assertEquals("Hi", feature.get("field1")); |
||
124 | assertNull(feature.get("field2"));
|
||
125 | |||
126 | it.hasNext(); |
||
127 | feature = (Feature)it.next(); |
||
128 | assertNotNull(feature.getDefaultGeometry()); |
||
129 | assertEquals("Hi", feature.get("field1")); |
||
130 | assertEquals("Max", feature.get("field2")); |
||
131 | |||
132 | it.dispose(); |
||
133 | store.finishEditing(); |
||
134 | |||
135 | //Edit the feature type and remove the field: "field1"
|
||
136 | store.edit(); |
||
137 | |||
138 | EditableFeatureType type3 = |
||
139 | store.getDefaultFeatureType().getEditable(); |
||
140 | type3.remove("field1");
|
||
141 | store.update(type3); |
||
142 | |||
143 | assertEquals(store.getDefaultFeatureType().getAttributeDescriptors().length, 2);
|
||
144 | |||
145 | //Finish the edition
|
||
146 | store.finishEditing(); |
||
147 | |||
148 | //Check if all the features have one field
|
||
149 | featureSet = store.getFeatureSet(); |
||
150 | it = featureSet.fastIterator(); |
||
151 | |||
152 | it.hasNext(); |
||
153 | feature = (Feature)it.next(); |
||
154 | assertNotNull(feature.getDefaultGeometry()); |
||
155 | assertEquals("", feature.get("field2")); |
||
156 | |||
157 | it.hasNext(); |
||
158 | feature = (Feature)it.next(); |
||
159 | assertNotNull(feature.getDefaultGeometry()); |
||
160 | assertEquals("Max", feature.get("field2")); |
||
161 | |||
162 | it.dispose(); |
||
163 | store.dispose(); |
||
164 | } |
||
165 | } |