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