Statistics
| Revision:

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.jdbc / src / test / java / org / gvsig / fmap / dal / store / jdbc2 / AbstractTestUtils.java @ 46112

History | View | Annotate | Download (18.9 KB)

1
package org.gvsig.fmap.dal.store.jdbc2;
2

    
3
import java.io.File;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Properties;
10
import org.apache.commons.collections4.CollectionUtils;
11
import org.apache.commons.io.FileUtils;
12
import org.apache.commons.io.FilenameUtils;
13
import org.apache.commons.io.IOUtils;
14
import org.apache.commons.lang3.BooleanUtils;
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.commons.lang3.math.NumberUtils;
17
import org.gvsig.fmap.dal.DALLocator;
18
import org.gvsig.fmap.dal.DataManager;
19
import org.gvsig.fmap.dal.DataStore;
20
import static org.gvsig.fmap.dal.DataStore.H2SPATIAL_PROVIDER_NAME;
21
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
22
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
23
import org.gvsig.fmap.dal.feature.EditableFeature;
24
import org.gvsig.fmap.dal.feature.EditableFeatureType;
25
import org.gvsig.fmap.dal.feature.Feature;
26
import org.gvsig.fmap.dal.feature.FeatureStore;
27
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
28
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
29
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
30
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
31
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
32
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
33
import org.gvsig.tools.util.HasAFile;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
public abstract class AbstractTestUtils {
38

    
39
    public static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestUtils.class);
40

    
41
    protected static int dbcounter = 1;
42

    
43
    protected Properties properties = null;
44

    
45
    public AbstractTestUtils() {
46

    
47
    }
48

    
49
    public File getTargetFolder() throws Exception {
50
        URL url = this.getClass().getResource("/");
51
        if (url == null) {
52
            url = AbstractTestUtils.class.getResource("/");
53
        }
54
        File x = new File(url.toURI());
55
        File target = x.getParentFile();
56
        return target;
57
    }
58

    
59
    public File getResource(String name) throws Exception {
60
        File x = new File(getTargetFolder(), name);
61
        return x;
62
    }
63

    
64
    public File getResourceAsFile(String pathname) throws Exception {
65
        URL url = this.getClass().getResource(pathname);
66
        if (url == null) {
67
            url = AbstractTestUtils.class.getResource(pathname);
68
        }
69
        if( StringUtils.equalsIgnoreCase(url.getProtocol(),"file") ) {
70
            File x = new File(url.toURI());
71
            return x;
72
        }
73
        File tmp = new File( this.getTargetFolder(), FilenameUtils.getName(pathname));
74
        IOUtils.copy(url, tmp);
75
        return tmp;
76
    }
77

    
78
    public File getFile(File name) throws Exception {
79
        File f = this.getResource(String.format(
80
                "%s-%d-%03d",
81
                name.getPath(),
82
                System.currentTimeMillis(),
83
                dbcounter++
84
        )
85
        );
86
        FileUtils.forceMkdir(f.getParentFile());
87
        return f;
88
    }
89

    
90
    public File getFolder(File name) throws Exception {
91
        File f = this.getResource(String.format(
92
                "%s-%d-%03d",
93
                name.getPath(),
94
                System.currentTimeMillis(),
95
                dbcounter++
96
        )
97
        );
98
        FileUtils.forceMkdir(f);
99
        return f;
100
    }
101

    
102
    public JDBCServerExplorerParameters getH2SpatialServerExplorerParameters(String dbname) throws Exception {
103
        DataManager dataManager = DALLocator.getDataManager();
104
        JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(H2SPATIAL_PROVIDER_NAME);
105

    
106
        File dbfile = this.getResource(String.format(
107
                "test-dbs/%s-%d-%03d",
108
                dbname,
109
                System.currentTimeMillis(),
110
                dbcounter++
111
        )
112
        );
113
//        System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
114
        FileUtils.forceMkdir(dbfile.getParentFile());
115
        ((HasAFile) conn).setFile(dbfile);
116
        return conn;
117
    }
118

    
119
    public FeatureProvider getFeatureProvider(Feature feature) {
120
        return ((DefaultFeature) feature).getData();
121
    }
122

    
123
    public Expecteds getExpecteds(String name) throws Exception {
124
        Expecteds x = new Expecteds();
125
        x.normaliceSpaces = false;
126
        x.removeNL = true;
127
        x.stripStart = true;
128
        File f = getResourceAsFile(getExpectedsPath() + "/" + name);
129
        x.parse(f);
130
        return x;
131
    }
132

    
133
    public static class Expecteds {
134

    
135
        private Map<String, List<String>> expecteds = new HashMap<>();
136

    
137
        public boolean normaliceSpaces = false;
138
        public boolean stripStart = false;
139
        public boolean removeNL = false;
140

    
141
        public String startLineComment = "-- ";
142
        
143
        private static class CommandLine {
144

    
145
            private final String[] cmd;
146

    
147
            private CommandLine(String line, String prefix) {
148
                line = StringUtils.substring(line, prefix.length());
149
                line = StringUtils.normalizeSpace(line);
150
                this.cmd = StringUtils.split(line);
151
            }
152

    
153
            public boolean isTrue(int argn, boolean defaultValue) {
154
                if (argn >= this.cmd.length) {
155
                    return defaultValue;
156
                }
157
                return BooleanUtils.toBoolean(this.cmd[argn]);
158
            }
159
            
160
            public String get(int argn) {
161
                return this.cmd[argn];
162
            }
163
            
164
            public int size() {
165
                return this.cmd.length;
166
            }
167
        }
168

    
169
        public Expecteds() throws Exception {
170
            
171
        }
172
        
173
        public void parse(File f) throws Exception {
174
            final int SEARCHING_ITEM = 0;
175
            final int READING_ITEM = 1;
176
            boolean localNormaliceSpaces = false;
177
            boolean localStripStart = false;
178
            boolean localRemoveNL = false;
179
            String name = f.getName();
180

    
181
            List<String> lines = FileUtils.readLines(f);
182

    
183
            StringBuilder sb = null;
184
            String currentItem = null;
185
            int lineno = 1;
186
            int state = SEARCHING_ITEM;
187
            for (String line : lines) {
188
                line = line + "\n";
189
                switch (state) {
190
                    case SEARCHING_ITEM:
191
                        if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) {
192
                            CommandLine cmd = new CommandLine(line, startLineComment);
193
                            normaliceSpaces = cmd.isTrue(1, true);
194
                        } else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) {
195
                            CommandLine cmd = new CommandLine(line, startLineComment);
196
                            stripStart = cmd.isTrue(1, true);
197
                        } else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) {
198
                            CommandLine cmd = new CommandLine(line, startLineComment);
199
                            removeNL = cmd.isTrue(1, true);
200
                        } else if (line.toLowerCase().startsWith(startLineComment + "begin ")) {
201
                            CommandLine cmd = new CommandLine(line, startLineComment);
202
                            currentItem = cmd.get(1);
203
                            sb = new StringBuilder();
204
                            state = READING_ITEM;
205
                            localNormaliceSpaces = normaliceSpaces;
206
                            localStripStart = stripStart;
207
                            localRemoveNL = removeNL;
208
                        } else if (line.toLowerCase().startsWith(startLineComment + "rem ")) {
209
                            // do nothing skip comment
210
                        } else if (!StringUtils.isBlank(line)) {
211
                            throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + ".");
212
                        }
213
                        break;
214
                    case READING_ITEM:
215
                        if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) {
216
                            CommandLine cmd = new CommandLine(line, startLineComment);
217
                            localNormaliceSpaces = cmd.isTrue(1, true);
218
                        } else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) {
219
                            CommandLine cmd = new CommandLine(line, startLineComment);
220
                            localStripStart = cmd.isTrue(1, true);
221
                        } else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) {
222
                            CommandLine cmd = new CommandLine(line, startLineComment);
223
                            localRemoveNL = cmd.isTrue(1, true);
224
                        } else if (line.toLowerCase().startsWith(startLineComment + "end ")) {
225
                            CommandLine cmd = new CommandLine(line, startLineComment);
226
                            if (!StringUtils.equals(currentItem, cmd.get(1)) )  {
227
                                throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + ", expected '" + startLineComment + "end " + currentItem + "', and found " + line + ".");
228
                            }
229
                            String s = sb.toString();
230
                            if (s.endsWith("\n")) {
231
                                s = s.substring(0, s.length() - 1);
232
                            }
233
                            if (localNormaliceSpaces) {
234
                                s = StringUtils.normalizeSpace(s);
235
                            }
236
                            
237
                            List<String> value = expecteds.get(currentItem);
238
                            if( value == null ) {
239
                                value = new ArrayList<>();
240
                                expecteds.put(currentItem, value);
241
                            }
242
                            value.add(s);
243
                            state = SEARCHING_ITEM;
244

    
245
                        } else if (line.toLowerCase().startsWith(startLineComment + "rem ")) {
246
                            // do nothing skip comment
247
                        } else {
248
                            if (localStripStart) {
249
                                line = StringUtils.stripStart(line, null);
250
                            }
251
                            if (localRemoveNL) {
252
                                line = StringUtils.remove(line, "\n");
253
                                line = StringUtils.remove(line, "\r");
254
                            }
255
                            sb.append(line);
256
                        }
257
                        break;
258
                }
259
                lineno++;
260
            }
261
            if (state != SEARCHING_ITEM) {
262
                throw new IllegalStateException("Syntax error at '" + name + "', expected '" + startLineComment + "end " + currentItem + "' and found EOF .");
263
            }
264
        }
265

    
266
        public String get(String name) {
267
            List<String> value = this.expecteds.get(name);
268
            if( CollectionUtils.isEmpty(value) ) {
269
                return null;
270
            }
271
            return value.get(0);
272
        }
273

    
274
        public String get(String name, int index) {
275
            List<String> value = this.expecteds.get(name);
276
            if( CollectionUtils.isEmpty(value) ) {
277
                return null;
278
            }
279
            return value.get(index);
280
        }
281
        
282
        public int size(String name) {
283
            List<String> value = this.expecteds.get(name);
284
            if( CollectionUtils.isEmpty(value) ) {
285
                return 0;
286
            }
287
            return value.size();
288
        }
289
    }
290

    
291
    public void removeResource(JDBCServerExplorer explorer, String storeName, String resourceName) throws Exception {
292
        JDBCStoreParameters storeParams = explorer.get(storeName);
293
        ResourcesStorage resourcesStorage = explorer.getResourcesStorage(storeParams);
294
        if( resourcesStorage.allowRemove() ) {
295
            resourcesStorage.remove(resourceName);
296
        }
297
    }
298

    
299
    public void info_h2sql(String label, File file) {
300
        System.out.println("");
301
        System.out.println("#h2console "+label+": "+file.getName());
302
        System.out.println("cd '"+file.getParent()+"'; h2sql -d '"+file.getName()+"' -user SA");
303
        System.out.println("jdbc:h2:"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL");
304
        System.out.println("jdbc:h2:tcp://127.0.1.1:9123/"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL");
305
        System.out.println("");
306
    }
307
    
308
    public void info_jdbc(String label, JDBCServerExplorerParameters parameters) {
309
        if( StringUtils.equalsIgnoreCase(parameters.getProviderName(), FeatureStore.H2SPATIAL_PROVIDER_NAME) ) {
310
            info_h2sql(label, ((HasAFile)parameters).getFile());
311
            return;
312
        }
313
        System.out.println("");
314
        System.out.println("# Connection "+label);
315
        System.out.println(parameters.getUrl());
316
        System.out.println("");
317
    }
318
    
319
    public void info_jdbc(String label, JDBCServerExplorer explorer) {
320
        info_jdbc(label, explorer.getParameters());
321
    }
322
    
323
    public void info_jdbc(JDBCServerExplorer explorer) {
324
        JDBCServerExplorerParameters params = explorer.getParameters();
325
        info_jdbc(params.getDBName(), params);
326
    }
327
    
328
    protected boolean getPropertyBoolean(String name) {
329
        Properties props = this.getProperties();
330
        return BooleanUtils.toBoolean(props.getProperty(name));
331
    }
332
    
333
    protected int getPropertyInt(String name, int defaultValue) {
334
        Properties props = this.getProperties();
335
        return NumberUtils.toInt(props.getProperty(name), defaultValue);
336
    }
337
    
338
    protected String getProperty(String name) {
339
        Properties props = this.getProperties();
340
        return props.getProperty(name);
341
    }
342
    
343
    protected Properties getProperties() {
344
        if( this.properties == null ) {
345
            try {
346
                Properties props = new Properties();
347
                this.properties = props;
348
            } catch(Exception ex) {
349
                throw new RuntimeException(ex);
350
            }
351
        }
352
        return this.properties;
353
    }
354

    
355
    public boolean isTheDatabaseAvailable() {
356
        return true;
357
    }
358

    
359
    public FeatureStore openCSVStore(String pathname) throws Exception {
360
        DataManager dataManager = DALLocator.getDataManager();
361
        File f = getResourceAsFile(pathname);
362
        FeatureStore store = (FeatureStore) dataManager.openStore(
363
                DataStore.CSV_PROVIDER_NAME, 
364
                "file=",f,
365
                "automaticTypesDetection=", false,
366
                "locale=","en"
367
        );
368
        return store;
369
    }
370
    
371
    public FeatureStore openSourceStore1() throws Exception {
372
        return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource1.csv");
373
    }
374

    
375
    public FeatureStore openSourceStore2() throws Exception {
376
        return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource2.csv");
377
    }
378

    
379
    public FeatureStore openStore(JDBCServerExplorer explorer, String name) throws Exception {
380
        JDBCStoreParameters params = explorer.get(name);
381
        
382
        DataManager dataManager = DALLocator.getDataManager();
383
        FeatureStore store;
384
        try {
385
            store = (FeatureStore) dataManager.openStore(
386
                    getProviderName(), 
387
                    params
388
            );
389
        } catch(ValidateDataParametersException ex) {
390
            LOGGER.warn(ex.getLocalizedMessageStack());
391
            throw ex;
392
        }
393
        return store;
394
    }
395

    
396
    public void create_table_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore) throws Exception {
397
        NewFeatureStoreParameters params = (NewFeatureStoreParameters) targetExplorer.getAddParameters(
398
                targetName
399
        );
400
        EditableFeatureType ft = params.getDefaultFeatureType();
401
        ft.addAll(sourceStore.getDefaultFeatureType());
402
        targetExplorer.add(getProviderName(), params, true);
403
    }
404
    
405
    public void insert_into_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore, int mode) throws Exception {
406
        FeatureStore targetStore = openStore(targetExplorer, targetName);
407
        targetStore.edit(mode);
408
        try {
409
            for (Feature sourceFeature : sourceStore.getFeatureSet()) {
410
                EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature);
411
                targetStore.insert(targetFeature);
412
            }
413
        } finally {
414
            targetStore.finishEditing();
415
        }
416
    }
417
    
418
    public JDBCServerExplorer openServerExplorer(String dbname) throws Exception {        
419
        DataManager dataManager = DALLocator.getDataManager();
420
        JDBCServerExplorerParameters params = this.getServerExplorerParameters(dbname);
421
        JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
422
                this.getProviderName(), 
423
                params
424
        );
425
        return explorer;
426
    }
427

    
428
    public void drop_tables(JDBCServerExplorer explorer, String...tables) throws Exception {
429
        for (String table : tables) {
430
            String sql = "DROP TABLE IF EXISTS \""+table+"\"";
431
            explorer.execute(sql);
432
            removeResource(explorer, table, "dal");
433
        }
434
    }
435
    
436
    public void initWorkspace(String name) throws Exception {
437
        JDBCServerExplorer explorer = this.openServerExplorer(name);
438
        DataManager manager = DALLocator.getDataManager();
439
        DatabaseWorkspaceManager ws = manager.createDatabaseWorkspaceManager(explorer.getParameters());
440
        ws.connect();
441
        if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION)) {
442
            ws.createTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION);
443
        }
444
        if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_RESOURCES)) {
445
            ws.createTable(DatabaseWorkspaceManager.TABLE_RESOURCES);
446
        }
447
    }
448
    
449
    public List<String> getExpectedSQLs(String name) throws Exception {
450
        File f = getResourceAsFile(getExpectedsPath() + "/" + name);
451
        List<String> SQLs = new ArrayList<>();
452
        List<String> lines = FileUtils.readLines(f);
453
        StringBuilder sb = new StringBuilder();
454
        for (String line : lines) {
455
            line = StringUtils.stripStart(line, null);
456
            if (line.startsWith("--")) {
457
                continue;
458
            }
459
            if (line.endsWith(";")) {
460
                sb.append(line.substring(0, line.length() - 1));
461
                SQLs.add(sb.toString());
462
                sb.setLength(0);
463
            } else {
464
                sb.append(line);
465
            }
466
        }
467
        return SQLs;
468
    }
469

    
470
    
471
//    public abstract String getExpectedResourcesPrefix(); // Ex. "h2spatial"
472

    
473
    public abstract String getExpectedsPath();
474
    
475
    public abstract JDBCHelper createJDBCHelper() throws Exception;
476

    
477
    public abstract String getProviderName();
478
    
479
    public abstract JDBCServerExplorerParameters getServerExplorerParameters(String dbname) throws Exception;
480
}