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 @ 46101

History | View | Annotate | Download (17.5 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.DataServerExplorerParameters;
20
import org.gvsig.fmap.dal.DataStore;
21
import static org.gvsig.fmap.dal.DataStore.H2SPATIAL_PROVIDER_NAME;
22
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
23
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.FIELD_RESOURCES_NAME;
24
import static org.gvsig.fmap.dal.DatabaseWorkspaceManager.TABLE_RESOURCES_NAME;
25
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
26
import org.gvsig.fmap.dal.feature.EditableFeature;
27
import org.gvsig.fmap.dal.feature.EditableFeatureType;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
31
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
32
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
33
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters;
34
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
35
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
36
import org.gvsig.tools.util.HasAFile;
37
import org.hibernate.dialect.H2Dialect;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
public abstract class AbstractTestUtils {
42

    
43
    public static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestUtils.class);
44

    
45
    protected static int dbcounter = 1;
46

    
47
    protected Properties properties = null;
48

    
49
    public AbstractTestUtils() {
50

    
51
    }
52

    
53
    public File getTargetFolder() throws Exception {
54
        URL url = AbstractTestUtils.class.getResource("/");
55
        File x = new File(url.toURI());
56
        File target = x.getParentFile();
57
        return target;
58
    }
59

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

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

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

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

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

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

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

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

    
131
    public static class Expecteds {
132

    
133
        private Map<String, List<String>> expecteds = new HashMap<>();
134

    
135
        public boolean normaliceSpaces = false;
136
        public boolean stripStart = false;
137
        public boolean removeNL = false;
138

    
139
        private static class CommandLine {
140

    
141
            private final String[] cmd;
142

    
143
            private CommandLine(String line, String prefix) {
144
                line = StringUtils.substring(line, prefix.length());
145
                line = StringUtils.normalizeSpace(line);
146
                this.cmd = StringUtils.split(line);
147
            }
148

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

    
165
        public Expecteds() throws Exception {
166
            
167
        }
168
        
169
        public void parse(File f) throws Exception {
170
            final int SEARCHING_ITEM = 0;
171
            final int READING_ITEM = 1;
172
            final String startLineComment = "-- ";
173
            boolean localNormaliceSpaces = false;
174
            boolean localStripStart = false;
175
            boolean localRemoveNL = false;
176
            String name = f.getName();
177

    
178
            List<String> lines = FileUtils.readLines(f);
179

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

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

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

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

    
288
    public void removeResource(JDBCServerExplorer explorer, String storeName, String resourceName) throws Exception {
289
        JDBCStoreParameters storeParams = explorer.get(storeName);
290
        ResourcesStorage resourcesStorage = explorer.getResourcesStorage(storeParams);
291
        resourcesStorage.remove(resourceName);
292
    }
293

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

    
350
    public boolean isTheDatabaseAvailable() {
351
        return true;
352
    }
353

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

    
370
    public FeatureStore openSourceStore2() throws Exception {
371
        return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource2.csv");
372
    }
373

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

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

    
423
    public void drop_tables(JDBCServerExplorer explorer, String...tables) {
424
        for (String table : tables) {
425
            String sql = "DROP TABLE IF EXISTS \""+table+"\"";
426
            explorer.execute(sql);
427
        }
428
    }
429
    
430
//    public abstract String getExpectedResourcesPrefix(); // Ex. "h2spatial"
431

    
432
    public abstract String getExpectedsPath();
433
    
434
    public abstract JDBCHelper createJDBCHelper() throws Exception;
435

    
436
    public abstract String getProviderName();
437
    
438
    public abstract JDBCServerExplorerParameters getServerExplorerParameters(String dbname) throws Exception;
439
}