Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / shp / Bug15597Test.java @ 38083

History | View | Annotate | Download (6.3 KB)

1 36388 jpiera
/* 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.dal.feature.impl.DefaultEditableFeatureType;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42 38083 cordinyana
import org.gvsig.fmap.geom.type.GeometryType;
43 36388 jpiera
import org.gvsig.tools.dispose.DisposableIterator;
44
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
45
46
/**
47
 * Code to test bug gvsig-desktop#15597. This is based on the code provided by
48
 * the bug reporter (see ticket), thanks to him!!
49
 *
50
 * @author gvSIG Team
51
 * @version $Id$
52
 */
53
public class Bug15597Test extends TestCase {
54
    private static final String TEMP_PATH = System.getProperty("java.io.tmpdir") + File.separator;
55
56
    public void testBug15642() throws Exception {
57
        new DefaultLibrariesInitializer().fullInitialize();
58
        DataManager manager = DALLocator.getDataManager();
59 38083 cordinyana
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
60 36388 jpiera
61
        //Create a feature type with two fields: "geom" and "field1"
62
        EditableFeatureType type = new DefaultEditableFeatureType();
63 38083 cordinyana
        GeometryType geometryType =
64
            geometryManager.getGeometryType(Geometry.TYPES.POINT,
65
                Geometry.SUBTYPES.GEOM2D);
66 36388 jpiera
        type.add("geom", org.gvsig.fmap.geom.DataTypes.GEOMETRY)
67 38083 cordinyana
            .setGeometryType(geometryType);
68 36388 jpiera
        type.setDefaultGeometryAttributeName("geom");
69
        type.add("field1", DataTypes.STRING).setSize(2);
70
71
        //Create a store
72
        NewFeatureStoreParameters destParams =
73
            (NewFeatureStoreParameters) manager.createNewStoreParameters(
74
                "FilesystemExplorer", "Shape");
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
}