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.h2 / src / test / java / org / gvsig / fmap / dal / store / h2 / TestUtils.java @ 44678

History | View | Annotate | Download (4.29 KB)

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

    
3
import java.io.File;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.List;
7
import org.apache.commons.io.FileUtils;
8
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.DataManager;
11
import org.gvsig.fmap.dal.DataServerExplorerParameters;
12
import org.gvsig.fmap.dal.DataStore;
13
import org.gvsig.fmap.dal.DatabaseWorkspaceManager;
14
import org.gvsig.fmap.dal.feature.Feature;
15
import org.gvsig.fmap.dal.feature.FeatureStore;
16
import org.gvsig.fmap.dal.feature.impl.DefaultFeature;
17
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
18
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
public class TestUtils  {
23
    
24
    public static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class);
25

    
26
    public static final String PROVIDER_NAME = H2SpatialLibrary.NAME;
27

    
28
    private static int dbcounter = 1;
29
    
30
    public static H2SpatialConnectionParameters buildDBConnection(String dbname) throws Exception {
31
        DataManager dataManager = DALLocator.getDataManager();
32
        H2SpatialConnectionParameters conn = (H2SpatialConnectionParameters) 
33
                dataManager.createServerExplorerParameters(H2SpatialLibrary.NAME);
34

    
35
        File dbfile = TestUtils.getResource(String.format(
36
                "test-dbs/%s-%d-%03d",
37
                dbname,
38
                System.currentTimeMillis(), 
39
                dbcounter++
40
            )
41
        );
42
//        System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
43
        conn.setFile(dbfile);
44
        return conn;
45
    }
46
    
47
    public static JDBCServerExplorer openServerExplorer(String dbname) throws Exception {
48
        DataManager dataManager = DALLocator.getDataManager();
49
        H2SpatialConnectionParameters conn = buildDBConnection(dbname);
50
        JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer(
51
                PROVIDER_NAME, conn
52
        );
53
        return explorer;
54
    }
55
    
56
    public static File getTargetFolder() throws Exception {
57
        URL url = TestUtils.class.getResource("/");
58
        File x = new File(url.toURI());
59
        File target = x.getParentFile();
60
        return target;
61
    }
62
    
63
    public static File getResource(String name) throws Exception {
64
        File x = new File(getTargetFolder(), name);
65
        return x;
66
    }
67
    
68
    public static File getResourceAsFile(String pathname) throws Exception {
69
        URL url = TestUtils.class.getResource(pathname);
70
        File x = new File(url.toURI());
71
        return x;
72
    }
73

    
74
    public static void removeDALResource(String dbname, String name) throws Exception {
75
        H2SpatialConnectionParameters connection = buildDBConnection(dbname);
76
        DatabaseWorkspaceManager workspace = DALLocator.getDataManager().createDatabaseWorkspaceManager(
77
                (DataServerExplorerParameters) connection
78
        );
79
//        ResourcesStorage resources = workspace.getResourcesStorage(name);
80
//        resources.remove("dal");
81
    }
82

    
83
    public static FeatureStore openSourceStore1() throws Exception {
84
        DataManager dataManager = DALLocator.getDataManager();
85
        File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/testCreateSource1.csv");
86
        FeatureStore store = (FeatureStore) dataManager.openStore(
87
                DataStore.CSV_PROVIDER_NAME, 
88
                "file=",f,
89
                "automaticTypesDetection=", false,
90
                "locale=","en"
91
        );
92
        return store;
93
    }
94
 
95

    
96
    public static List<String> getSQLs(String name) throws Exception {
97
      File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/"+name);
98
      List<String> SQLs = new ArrayList<>();
99
      List<String> lines = FileUtils.readLines(f);
100
      StringBuilder sb = new StringBuilder();
101
      for (String line : lines) {
102
        line = StringUtils.stripStart(line, null);
103
        if( line.startsWith("--") ) {
104
          continue;
105
        }
106
        if( line.endsWith(";") ) {
107
          sb.append(line.substring(0, line.length()-1));
108
          SQLs.add(sb.toString());
109
          sb.setLength(0);
110
        } else {
111
          sb.append(line);
112
        }
113
      }
114
      return SQLs;
115
    }
116
    
117
    public static FeatureProvider getFeatureProvider(Feature feature) {
118
      return ((DefaultFeature)feature).getData();
119
    }
120
}