Revision 44951 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.mdb/src/test/java/org/gvsig/fmap/dal/store/mdb/TestCreate.java

View differences:

TestCreate.java
25 25
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
26 26
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
27 27
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dataTypes.DataType;
28 30
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
29 31
import org.slf4j.Logger;
30 32
import org.slf4j.LoggerFactory;
31 33

  
32 34
public class TestCreate extends TestCase {
35

  
33 36
    private static final Logger LOGGER = LoggerFactory.getLogger(TestCreate.class);
34
    
37

  
35 38
    public static final String DBNAME = "testCreate";
36
    
39

  
37 40
    public TestCreate(String testName) {
38 41
        super(testName);
39 42
    }
40
    
43

  
41 44
    @Override
42 45
    protected void setUp() throws Exception {
43 46
        super.setUp();
44 47
        new DefaultLibrariesInitializer().fullInitialize();
45 48
    }
46
    
49

  
47 50
    @Override
48 51
    protected void tearDown() throws Exception {
49 52
        super.tearDown();
......
51 54

  
52 55
    // TODO add test methods here. The name must begin with 'test'. For example:
53 56
    // public void testHello() {}
54
    
55 57
    protected String getProviderName() {
56 58
        return DataStore.MDB_PROVIDER_NAME;
57 59
    }
58
    
60

  
59 61
    protected String getTargetName() {
60 62
        return "testCreateTarget1";
61 63
    }
62
    
64

  
63 65
    protected FeatureStore openTargetStore1(JDBCServerExplorer explorer) throws Exception {
64 66
        JDBCStoreParameters params = explorer.get(getTargetName());
65
        
67

  
66 68
        DataManager dataManager = DALLocator.getDataManager();
67 69
        FeatureStore store;
68 70
        try {
69 71
            store = (FeatureStore) dataManager.openStore(
70
                    getProviderName(), 
72
                    getProviderName(),
71 73
                    params
72 74
            );
73
        } catch(ValidateDataParametersException ex) {
75
        } catch (ValidateDataParametersException ex) {
74 76
            LOGGER.warn(ex.getLocalizedMessageStack());
75 77
            throw ex;
76 78
        }
......
85 87
        ft.addAll(sourceStore.getDefaultFeatureType());
86 88
        explorer.add(getProviderName(), params, true);
87 89
    }
88
    
90

  
89 91
    protected void checkTypes(JDBCServerExplorer explorer, FeatureType sourceFeatureType) throws Exception {
90 92
//        DataType STRING_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.STRING);
91 93
//        DataType INT_TYPE = ToolsLocator.getDataTypesManager().get(DataTypes.INT);
92
        
94

  
93 95
        FeatureStore targetStore = openTargetStore1(explorer);
94 96
        FeatureType targetFeatureType = targetStore.getDefaultFeatureType();
95 97

  
96
        assertEquals("Feature type size",sourceFeatureType.size(), targetFeatureType.size());
98
        assertEquals("Feature type size", sourceFeatureType.size(), targetFeatureType.size());
97 99
        for (int i = 0; i < sourceFeatureType.size(); i++) {
98 100
            FeatureAttributeDescriptor sourceAttr = sourceFeatureType.get(i);
99 101
            FeatureAttributeDescriptor targetAttr = targetFeatureType.get(i);
100
            switch(sourceAttr.getType()) {
101
                case DataTypes.BYTE:
102
                case DataTypes.INT:
103
                case DataTypes.LONG:
104
                case DataTypes.BOOLEAN:
102
            switch (sourceAttr.getType()) {
103
                case DataTypes.DECIMAL:
104
                    if (sourceAttr.getPrecision() == 19 && sourceAttr.getScale() == 0) { // en acces es un decimal, lo tratamos como long
105
                        assertEquals(
106
                                String.format("Field %s name mismatch", sourceAttr.getName()),
107
                                sourceAttr.getName(),
108
                                targetAttr.getName()
109
                        );
110
                        assertEquals(
111
                                String.format("Field %s type mismatch", sourceAttr.getName()),
112
                                "Long",
113
                                targetAttr.getDataTypeName()
114
                        );
115
                    } else {
116
                        assertEquals(
117
                                String.format("Field %s name mismatch", sourceAttr.getName()),
118
                                sourceAttr.getName(),
119
                                targetAttr.getName()
120
                        );
121
                        assertEquals(
122
                                String.format("Field %s type mismatch", sourceAttr.getName()),
123
                                sourceAttr.getDataTypeName(),
124
                                targetAttr.getDataTypeName()
125
                        );
126
                        assertEquals(
127
                                String.format("Field %s size mismatch", sourceAttr.getName()),
128
                                sourceAttr.getSize(),
129
                                targetAttr.getSize()
130
                        );
131
                        assertEquals(
132
                                String.format("Field %s precision mismatch", sourceAttr.getName()),
133
                                sourceAttr.getPrecision(),
134
                                targetAttr.getPrecision()
135
                        );
136
                        assertEquals(
137
                                String.format("Field %s scale mismatch", sourceAttr.getName()),
138
                                sourceAttr.getScale(),
139
                                targetAttr.getScale()
140
                        );
141
                    }
142
                    break;
105 143
                case DataTypes.DATE:
106
                case DataTypes.STRING:
107 144
                case DataTypes.TIME:
108 145
                case DataTypes.TIMESTAMP:
109
                case DataTypes.DOUBLE:
146
                    assertEquals(
147
                            String.format("Field %s name mismatch", sourceAttr.getName()),
148
                            sourceAttr.getName(),
149
                            targetAttr.getName()
150
                    );
151
                    assertEquals(
152
                            String.format("Field %s type mismatch", sourceAttr.getName()),
153
                            "Timestamp",
154
                            targetAttr.getDataTypeName()
155
                    );
156
                    assertEquals(
157
                            String.format("Field %s size mismatch", sourceAttr.getName()),
158
                            0,
159
                            targetAttr.getSize()
160
                    );
161
                    assertEquals(
162
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
163
                            -1,
164
                            targetAttr.getPrecision()
165
                    );
166
                    assertEquals(
167
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
168
                            -1,
169
                            targetAttr.getScale()
170
                    );
171
                    break;
110 172
                case DataTypes.FLOAT:
111
                case DataTypes.DECIMAL:
112 173
                    assertEquals(
113
                            String.format("Field %s name mismatch", sourceAttr.getName()), 
114
                            sourceAttr.getName(), 
174
                            String.format("Field %s name mismatch", sourceAttr.getName()),
175
                            sourceAttr.getName(),
115 176
                            targetAttr.getName()
116 177
                    );
117 178
                    assertEquals(
118
                            String.format("Field %s type mismatch", sourceAttr.getName()), 
119
                            sourceAttr.getDataTypeName(), 
179
                            String.format("Field %s type mismatch", sourceAttr.getName()),
180
                            "Double",
120 181
                            targetAttr.getDataTypeName()
121 182
                    );
122 183
                    assertEquals(
123
                            String.format("Field %s size mismatch", sourceAttr.getName()), 
184
                            String.format("Field %s size mismatch", sourceAttr.getName()),
185
                            0,
186
                            targetAttr.getSize()
187
                    );
188
                    assertEquals(
189
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
190
                            16,
191
                            targetAttr.getPrecision()
192
                    );
193
                    assertEquals(
194
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
195
                            -1,
196
                            targetAttr.getScale()
197
                    );
198
                    break;
199
                case DataTypes.LONG:
200
                case DataTypes.DOUBLE:
201
                case DataTypes.BYTE:
202
                case DataTypes.INT:
203
                case DataTypes.BOOLEAN:
204
                case DataTypes.STRING:
205
//                case DataTypes.DECIMAL:
206
                    assertEquals(
207
                            String.format("Field %s name mismatch", sourceAttr.getName()),
208
                            sourceAttr.getName(),
209
                            targetAttr.getName()
210
                    );
211
                    assertEquals(
212
                            String.format("Field %s type mismatch", sourceAttr.getName()),
213
                            sourceAttr.getDataTypeName(),
214
                            targetAttr.getDataTypeName()
215
                    );
216
                    assertEquals(
217
                            String.format("Field %s size mismatch", sourceAttr.getName()),
124 218
                            sourceAttr.getSize(),
125 219
                            targetAttr.getSize()
126 220
                    );
127 221
                    assertEquals(
128
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
222
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
129 223
                            sourceAttr.getPrecision(),
130 224
                            targetAttr.getPrecision()
131 225
                    );
132 226
                    assertEquals(
133
                            String.format("Field %s scale mismatch", sourceAttr.getName()), 
227
                            String.format("Field %s scale mismatch", sourceAttr.getName()),
134 228
                            sourceAttr.getScale(),
135 229
                            targetAttr.getScale()
136 230
                    );
137 231
                    break;
138 232
                case DataTypes.GEOMETRY:
139 233
                    assertEquals(
140
                            String.format("Field %s name mismatch", sourceAttr.getName()), 
141
                            sourceAttr.getName(), 
234
                            String.format("Field %s name mismatch", sourceAttr.getName()),
235
                            sourceAttr.getName(),
142 236
                            targetAttr.getName()
143 237
                    );
144 238
                    assertEquals(
145
                            String.format("Field %s type mismatch", sourceAttr.getName()), 
146
                            sourceAttr.getDataTypeName(), 
239
                            String.format("Field %s type mismatch", sourceAttr.getName()),
240
                            sourceAttr.getDataTypeName(),
147 241
                            targetAttr.getDataTypeName()
148 242
                    );
149 243
                    assertEquals(
150
                            String.format("Field %s geometry type mismatch", sourceAttr.getName()), 
151
                            sourceAttr.getGeomType().getName(), 
244
                            String.format("Field %s geometry type mismatch", sourceAttr.getName()),
245
                            sourceAttr.getGeomType().getName(),
152 246
                            targetAttr.getGeomType().getName()
153 247
                    );
154 248
                    assertEquals(
155
                            String.format("Field %s geometry SRS mismatch", sourceAttr.getName()), 
156
                            sourceAttr.getSRS().toString(), 
249
                            String.format("Field %s geometry SRS mismatch", sourceAttr.getName()),
250
                            sourceAttr.getSRS().toString(),
157 251
                            targetAttr.getSRS().toString()
158 252
                    );
159 253
                    assertEquals(
160
                            String.format("Field %s size mismatch", sourceAttr.getName()), 
254
                            String.format("Field %s size mismatch", sourceAttr.getName()),
161 255
                            sourceAttr.getSize(),
162 256
                            targetAttr.getSize()
163 257
                    );
164 258
                    assertEquals(
165
                            String.format("Field %s precision mismatch", sourceAttr.getName()), 
259
                            String.format("Field %s precision mismatch", sourceAttr.getName()),
166 260
                            sourceAttr.getPrecision(),
167 261
                            targetAttr.getPrecision()
168 262
                    );
169 263
                    break;
170 264
                default:
171 265
                    fail(
172
                        String.format("Field %s type %d (%s) not supported.", 
173
                                targetAttr.getName(),
174
                                targetAttr.getType(),
175
                                targetAttr.getDataTypeName()
176
                        )
266
                            String.format("Field %s type %d (%s) not supported.",
267
                                    targetAttr.getName(),
268
                                    targetAttr.getType(),
269
                                    targetAttr.getDataTypeName()
270
                            )
177 271
                    );
178 272
            }
179 273
        }
180 274
    }
181
    
275

  
182 276
    protected void copyFrom(JDBCServerExplorer explorer, FeatureStore sourceStore, int mode) throws Exception {
183 277
        FeatureStore targetStore = openTargetStore1(explorer);
184 278
        targetStore.edit(mode);
......
191 285
            targetStore.finishEditing();
192 286
        }
193 287
    }
194
    
288

  
195 289
    protected void checkData(JDBCServerExplorer explorer, FeatureStore sourceStore) throws Exception {
196 290
        FeatureStore targetStore = openTargetStore1(explorer);
197 291

  
......
202 296
            Feature sourceFeature = sourceFeatures.get(i);
203 297
            Feature targetFeature = targetFeatures.get(i);
204 298
            for (FeatureAttributeDescriptor sourceAttr : sourceStore.getDefaultFeatureType()) {
205
                switch(sourceAttr.getType()) {
299
                switch (sourceAttr.getType()) {
300
                    case DataTypes.BOOLEAN:
301
                        Object expected = sourceFeature.get(sourceAttr.getName());
302
                        if (expected == null) {
303
                            expected = false; //access never have null values for boolean
304
                        }
305
                        assertEquals(
306
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
307
                                expected,
308
                                targetFeature.get(sourceAttr.getName())
309
                        );
310
                        break;
311

  
206 312
                    case DataTypes.BYTE:
207 313
                        assertEquals(
208 314
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
......
233 339
                                targetFeature.get(sourceAttr.getName())
234 340
                        );
235 341
                        break;
236
                    case DataTypes.STRING:
237
                    case DataTypes.INT:
238 342
                    case DataTypes.LONG:
343
                        Object expectedLong = sourceFeature.get(sourceAttr.getName());
344
                        if (expectedLong != null) {
345
                            expectedLong = sourceFeature.getLong(sourceAttr.getName());
346
                        }
347
                        Object targetLong = targetFeature.get(sourceAttr.getName());
348
                        if (targetLong!=null) {
349
                            targetLong = targetFeature.getLong(sourceAttr.getName());
350
                        }
351
                        assertEquals(
352
                                String.format("Feature %03d attribute %s", i, sourceAttr.getName()),
353
                                expectedLong,
354
                                targetLong
355
                        );
356
                        break;
357

  
239 358
                    case DataTypes.FLOAT:
240 359
                    case DataTypes.DOUBLE:
241 360
                    default:
242 361
                        Object sourceValue = sourceFeature.get(sourceAttr.getName());
243 362
                        Object targetValue = targetFeature.get(sourceAttr.getName());
244
                        if( sourceValue == null ) {
363
                        if (sourceValue == null) {
245 364
                            LOGGER.info(String.format("Feature %03d attribute %s is null", i, sourceAttr.getName()));
246 365
                        }
247 366
                        assertEquals(
......
253 372
            }
254 373
        }
255 374
    }
256
    
257
    
375

  
258 376
    public void testCreatePopulateAndCheckData() throws Exception {
259 377
        FeatureStore sourceStore = TestUtils.openSourceStore1();
260 378
        JDBCServerExplorer explorer = TestUtils.openServerExplorer(DBNAME);
261
        
262
        createFrom(explorer, sourceStore);        
263
        
379

  
380
        createFrom(explorer, sourceStore);
381

  
264 382
        checkTypes(explorer, sourceStore.getDefaultFeatureType());
265 383
        copyFrom(explorer, sourceStore, FeatureStore.MODE_APPEND);
266 384
        checkData(explorer, sourceStore);
267
        
385

  
268 386
        JDBCStoreParameters params = explorer.get(this.getTargetName());
269 387
        explorer.remove(params);
270
        
388

  
271 389
        createFrom(explorer, sourceStore);
272 390
        copyFrom(explorer, sourceStore, FeatureStore.MODE_FULLEDIT);
273 391
        checkData(explorer, sourceStore);
......
277 395
    public void testFilterByDate() throws Exception {
278 396
        FeatureStore sourceStore = TestUtils.openSourceStore1();
279 397
        JDBCServerExplorer explorer = TestUtils.openServerExplorer(DBNAME);
280
        
281
        createFrom(explorer, sourceStore);        
398

  
399
        createFrom(explorer, sourceStore);
282 400
        copyFrom(explorer, sourceStore, FeatureStore.MODE_APPEND);
283
      
401

  
284 402
        FeatureStore mdbStore = openTargetStore1(explorer);
285 403
        ExpressionBuilder expbuilder = ExpressionUtils.createExpressionBuilder();
286 404

  
287 405
        String filter = expbuilder.or(
288
          expbuilder.and(
289
            expbuilder.gt(
290
                    expbuilder.column("Time"), 
291
                    expbuilder.time("01:02:03")
292
            ),
293
            expbuilder.lt(
294
                    expbuilder.column("Time"), 
295
                    expbuilder.time(new Date())
296
            )
297
          ),
298
          expbuilder.and(
299
            expbuilder.gt(
300
                    expbuilder.column("Date"), 
301
                    expbuilder.date("2019-02-17")
302
            ),
303
            expbuilder.lt(
304
                    expbuilder.column("Date"), 
305
                    expbuilder.date(new Date())
306
            )
307
          )
406
                expbuilder.and(
407
                        expbuilder.gt(
408
                                expbuilder.column("Time"),
409
                                expbuilder.time("01:02:03")
410
                        ),
411
                        expbuilder.lt(
412
                                expbuilder.column("Time"),
413
                                expbuilder.time(new Date())
414
                        )
415
                ),
416
                expbuilder.and(
417
                        expbuilder.gt(
418
                                expbuilder.column("Date"),
419
                                expbuilder.date("2019-02-17")
420
                        ),
421
                        expbuilder.lt(
422
                                expbuilder.column("Date"),
423
                                expbuilder.date(new Date())
424
                        )
425
                )
308 426
        ).toString();
309 427
        FeatureQuery query = sourceStore.createFeatureQuery();
310 428
        query.addFilter(filter);

Also available in: Unified diff