svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.h2 / src / test / java / org / gvsig / fmap / dal / store / h2 / TestCreate.java @ 44669
History | View | Annotate | Download (12.2 KB)
1 |
package org.gvsig.fmap.dal.store.h2; |
---|---|
2 |
|
3 |
import java.util.Date; |
4 |
import java.util.List; |
5 |
import junit.framework.TestCase; |
6 |
import static junit.framework.TestCase.assertEquals; |
7 |
import org.gvsig.fmap.dal.DALLocator; |
8 |
import org.gvsig.fmap.dal.DataTypes; |
9 |
import org.gvsig.fmap.dal.DataManager; |
10 |
import org.gvsig.fmap.dal.DataStore; |
11 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
12 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
13 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
14 |
import org.gvsig.fmap.dal.feature.Feature; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
19 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
21 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
public class TestCreate extends TestCase { |
26 |
private static final Logger LOGGER = LoggerFactory.getLogger(TestCreate.class); |
27 |
|
28 |
public static final String DBNAME = "testCreate"; |
29 |
|
30 |
public TestCreate(String testName) { |
31 |
super(testName);
|
32 |
} |
33 |
|
34 |
@Override
|
35 |
protected void setUp() throws Exception { |
36 |
super.setUp();
|
37 |
new DefaultLibrariesInitializer().fullInitialize();
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
protected void tearDown() throws Exception { |
42 |
super.tearDown();
|
43 |
} |
44 |
|
45 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
46 |
// public void testHello() {}
|
47 |
|
48 |
protected String getProviderName() { |
49 |
return DataStore.H2SPATIAL_PROVIDER_NAME;
|
50 |
} |
51 |
|
52 |
protected String getTargetName() { |
53 |
return "testCreateTarget1"; |
54 |
} |
55 |
|
56 |
protected FeatureStore openTargetStore1(JDBCServerExplorer explorer) throws Exception { |
57 |
JDBCStoreParameters params = explorer.get(getTargetName()); |
58 |
|
59 |
DataManager dataManager = DALLocator.getDataManager(); |
60 |
FeatureStore store; |
61 |
try {
|
62 |
store = (FeatureStore) dataManager.openStore( |
63 |
getProviderName(), |
64 |
params |
65 |
); |
66 |
} catch(ValidateDataParametersException ex) {
|
67 |
LOGGER.warn(ex.getLocalizedMessageStack()); |
68 |
throw ex;
|
69 |
} |
70 |
return store;
|
71 |
} |
72 |
|
73 |
protected void createFrom(JDBCServerExplorer explorer, FeatureStore sourceStore) throws Exception { |
74 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) explorer.getAddParameters( |
75 |
getProviderName() |
76 |
); |
77 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
78 |
ft.addAll(sourceStore.getDefaultFeatureType()); |
79 |
explorer.add(getProviderName(), params, true);
|
80 |
} |
81 |
|
82 |
protected void checkTypes(JDBCServerExplorer explorer, FeatureType sourceFeatureType) throws Exception { |
83 |
// DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
|
84 |
// DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
|
85 |
|
86 |
FeatureStore targetStore = openTargetStore1(explorer); |
87 |
FeatureType targetFeatureType = targetStore.getDefaultFeatureType(); |
88 |
|
89 |
assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
|
90 |
for (int i = 0; i < sourceFeatureType.size(); i++) { |
91 |
FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i); |
92 |
FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i); |
93 |
switch(sourceAttr.getType()) {
|
94 |
case DataTypes.BYTE:
|
95 |
case DataTypes.INT:
|
96 |
case DataTypes.LONG:
|
97 |
case DataTypes.BOOLEAN:
|
98 |
case DataTypes.DATE:
|
99 |
case DataTypes.STRING:
|
100 |
case DataTypes.TIME:
|
101 |
case DataTypes.TIMESTAMP:
|
102 |
case DataTypes.DOUBLE:
|
103 |
case DataTypes.FLOAT:
|
104 |
assertEquals( |
105 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
106 |
sourceAttr.getName(), |
107 |
targetAttr.getName() |
108 |
); |
109 |
assertEquals( |
110 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
111 |
sourceAttr.getDataTypeName(), |
112 |
targetAttr.getDataTypeName() |
113 |
); |
114 |
assertEquals( |
115 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
116 |
sourceAttr.getSize(), |
117 |
targetAttr.getSize() |
118 |
); |
119 |
assertEquals( |
120 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
121 |
sourceAttr.getPrecision(), |
122 |
targetAttr.getPrecision() |
123 |
); |
124 |
assertEquals( |
125 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
126 |
sourceAttr.getScale(), |
127 |
targetAttr.getScale() |
128 |
); |
129 |
break;
|
130 |
case DataTypes.GEOMETRY:
|
131 |
assertEquals( |
132 |
String.format("Field %s name mismatch", sourceAttr.getName()), |
133 |
sourceAttr.getName(), |
134 |
targetAttr.getName() |
135 |
); |
136 |
assertEquals( |
137 |
String.format("Field %s type mismatch", sourceAttr.getName()), |
138 |
sourceAttr.getDataTypeName(), |
139 |
targetAttr.getDataTypeName() |
140 |
); |
141 |
assertEquals( |
142 |
String.format("Field %s geometry type mismatch", sourceAttr.getName()), |
143 |
sourceAttr.getGeomType().getName(), |
144 |
targetAttr.getGeomType().getName() |
145 |
); |
146 |
assertEquals( |
147 |
String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), |
148 |
sourceAttr.getSRS().toString(), |
149 |
targetAttr.getSRS().toString() |
150 |
); |
151 |
assertEquals( |
152 |
String.format("Field %s size mismatch", sourceAttr.getName()), |
153 |
sourceAttr.getSize(), |
154 |
targetAttr.getSize() |
155 |
); |
156 |
assertEquals( |
157 |
String.format("Field %s precision mismatch", sourceAttr.getName()), |
158 |
sourceAttr.getPrecision(), |
159 |
targetAttr.getPrecision() |
160 |
); |
161 |
break;
|
162 |
default:
|
163 |
fail( |
164 |
String.format("Field %s type %d (%s) not supported.", |
165 |
targetAttr.getName(), |
166 |
targetAttr.getType(), |
167 |
targetAttr.getDataTypeName() |
168 |
) |
169 |
); |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
protected void copyFrom(JDBCServerExplorer explorer, FeatureStore sourceStore, int mode) throws Exception { |
175 |
FeatureStore targetStore = openTargetStore1(explorer); |
176 |
targetStore.edit(mode); |
177 |
try {
|
178 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
179 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
180 |
targetStore.insert(targetFeature); |
181 |
} |
182 |
} finally {
|
183 |
targetStore.finishEditing(); |
184 |
} |
185 |
} |
186 |
|
187 |
protected void checkData(JDBCServerExplorer explorer, FeatureStore sourceStore) throws Exception { |
188 |
FeatureStore targetStore = openTargetStore1(explorer); |
189 |
|
190 |
List<Feature> sourceFeatures = sourceStore.getFeatures();
|
191 |
List<Feature> targetFeatures = targetStore.getFeatures();
|
192 |
assertEquals("Count features", sourceFeatures.size(), targetFeatures.size());
|
193 |
for (int i = 0; i < targetFeatures.size(); i++) { |
194 |
Feature sourceFeature = sourceFeatures.get(i); |
195 |
Feature targetFeature = targetFeatures.get(i); |
196 |
for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
|
197 |
switch(sourceAttr.getType()) {
|
198 |
case DataTypes.BYTE:
|
199 |
assertEquals( |
200 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
201 |
sourceFeature.getInt(sourceAttr.getName()), |
202 |
targetFeature.getInt(sourceAttr.getName()) |
203 |
); |
204 |
break;
|
205 |
case DataTypes.TIMESTAMP:
|
206 |
Date sourceTimestamp = sourceFeature.getDate(sourceAttr.getName());
|
207 |
Date targetTimestamp = targetFeature.getDate(sourceAttr.getName());
|
208 |
assertEquals( |
209 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
210 |
sourceTimestamp, |
211 |
targetTimestamp |
212 |
); |
213 |
break;
|
214 |
case DataTypes.TIME:
|
215 |
assertEquals( |
216 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
217 |
sourceFeature.getDate(sourceAttr.getName()), |
218 |
targetFeature.getDate(sourceAttr.getName()) |
219 |
); |
220 |
break;
|
221 |
case DataTypes.GEOMETRY:
|
222 |
assertEquals( |
223 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
224 |
sourceFeature.get(sourceAttr.getName()), |
225 |
targetFeature.get(sourceAttr.getName()) |
226 |
); |
227 |
break;
|
228 |
case DataTypes.STRING:
|
229 |
case DataTypes.INT:
|
230 |
case DataTypes.LONG:
|
231 |
case DataTypes.FLOAT:
|
232 |
case DataTypes.DOUBLE:
|
233 |
default:
|
234 |
Object sourceValue = sourceFeature.get(sourceAttr.getName());
|
235 |
Object targetValue = targetFeature.get(sourceAttr.getName());
|
236 |
if( sourceValue == null ) { |
237 |
LOGGER.info("sourceValue is null");
|
238 |
} |
239 |
assertEquals( |
240 |
String.format("Feature %03d attribute %s", i, sourceAttr.getName()), |
241 |
sourceValue, |
242 |
targetValue |
243 |
); |
244 |
} |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
|
250 |
public void testCreateWithDALFile() throws Exception { |
251 |
FeatureStore sourceStore = TestUtils.openSourceStore1(); |
252 |
JDBCServerExplorer explorer = TestUtils.openServerExplorer(DBNAME); |
253 |
|
254 |
createFrom(explorer, sourceStore); |
255 |
|
256 |
checkTypes(explorer, sourceStore.getDefaultFeatureType()); |
257 |
copyFrom(explorer, sourceStore, FeatureStore.MODE_APPEND); |
258 |
checkData(explorer, sourceStore); |
259 |
|
260 |
createFrom(explorer, sourceStore); |
261 |
copyFrom(explorer, sourceStore, FeatureStore.MODE_FULLEDIT); |
262 |
checkData(explorer, sourceStore); |
263 |
|
264 |
} |
265 |
|
266 |
// public void testCreateWithoutDALFile() throws Exception {
|
267 |
// FeatureStore sourceStore = TestUtils.openSourceStore1();
|
268 |
//
|
269 |
// createFrom(sourceStore);
|
270 |
// TestUtils.removeDALResource(getTargetName());
|
271 |
//
|
272 |
// checkTypes(sourceStore.getDefaultFeatureType());
|
273 |
// copyFrom(sourceStore, FeatureStore.MODE_APPEND);
|
274 |
// TestUtils.removeDALResource(getTargetName());
|
275 |
// checkData(sourceStore);
|
276 |
//
|
277 |
// createFrom(sourceStore);
|
278 |
// TestUtils.removeDALResource(getTargetName());
|
279 |
// copyFrom(sourceStore, FeatureStore.MODE_FULLEDIT);
|
280 |
// TestUtils.removeDALResource(getTargetName());
|
281 |
// checkData(sourceStore);
|
282 |
//
|
283 |
// }
|
284 |
} |