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 @ 44926
History | View | Annotate | Download (4.53 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.JDBCHelper; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
20 |
import org.slf4j.Logger; |
21 |
import org.slf4j.LoggerFactory; |
22 |
|
23 |
public class TestUtils { |
24 |
|
25 |
public static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class); |
26 |
|
27 |
public static final String PROVIDER_NAME = H2SpatialLibrary.NAME; |
28 |
|
29 |
private static int dbcounter = 1; |
30 |
|
31 |
public static H2SpatialConnectionParameters buildDBConnection(String dbname) throws Exception { |
32 |
DataManager dataManager = DALLocator.getDataManager(); |
33 |
H2SpatialConnectionParameters conn = (H2SpatialConnectionParameters) |
34 |
dataManager.createServerExplorerParameters(H2SpatialLibrary.NAME); |
35 |
|
36 |
File dbfile = TestUtils.getResource(String.format( |
37 |
"test-dbs/%s-%d-%03d",
|
38 |
dbname, |
39 |
System.currentTimeMillis(),
|
40 |
dbcounter++ |
41 |
) |
42 |
); |
43 |
// System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
44 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
45 |
conn.setFile(dbfile); |
46 |
return conn;
|
47 |
} |
48 |
|
49 |
public static JDBCServerExplorer openServerExplorer(String dbname) throws Exception { |
50 |
DataManager dataManager = DALLocator.getDataManager(); |
51 |
H2SpatialConnectionParameters conn = buildDBConnection(dbname); |
52 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
53 |
PROVIDER_NAME, conn |
54 |
); |
55 |
return explorer;
|
56 |
} |
57 |
|
58 |
public static File getTargetFolder() throws Exception { |
59 |
URL url = TestUtils.class.getResource("/"); |
60 |
File x = new File(url.toURI()); |
61 |
File target = x.getParentFile();
|
62 |
return target;
|
63 |
} |
64 |
|
65 |
public static File getResource(String name) throws Exception { |
66 |
File x = new File(getTargetFolder(), name); |
67 |
return x;
|
68 |
} |
69 |
|
70 |
public static File getResourceAsFile(String pathname) throws Exception { |
71 |
URL url = TestUtils.class.getResource(pathname);
|
72 |
File x = new File(url.toURI()); |
73 |
return x;
|
74 |
} |
75 |
|
76 |
public static void removeDALResource(String dbname, String name) throws Exception { |
77 |
H2SpatialConnectionParameters connection = buildDBConnection(dbname); |
78 |
DatabaseWorkspaceManager workspace = DALLocator.getDataManager().createDatabaseWorkspaceManager( |
79 |
(DataServerExplorerParameters) connection |
80 |
); |
81 |
// ResourcesStorage resources = workspace.getResourcesStorage(name);
|
82 |
// resources.remove("dal");
|
83 |
} |
84 |
|
85 |
public static FeatureStore openSourceStore1() throws Exception { |
86 |
DataManager dataManager = DALLocator.getDataManager(); |
87 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/testCreateSource1.csv"); |
88 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
89 |
DataStore.CSV_PROVIDER_NAME, |
90 |
"file=",f,
|
91 |
"automaticTypesDetection=", false, |
92 |
"locale=","en" |
93 |
); |
94 |
return store;
|
95 |
} |
96 |
|
97 |
|
98 |
public static List<String> getSQLs(String name) throws Exception { |
99 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/"+name); |
100 |
List<String> SQLs = new ArrayList<>(); |
101 |
List<String> lines = FileUtils.readLines(f); |
102 |
StringBuilder sb = new StringBuilder(); |
103 |
for (String line : lines) { |
104 |
line = StringUtils.stripStart(line, null);
|
105 |
if( line.startsWith("--") ) { |
106 |
continue;
|
107 |
} |
108 |
if( line.endsWith(";") ) { |
109 |
sb.append(line.substring(0, line.length()-1)); |
110 |
SQLs.add(sb.toString()); |
111 |
sb.setLength(0);
|
112 |
} else {
|
113 |
sb.append(line); |
114 |
} |
115 |
} |
116 |
return SQLs;
|
117 |
} |
118 |
|
119 |
public static FeatureProvider getFeatureProvider(Feature feature) { |
120 |
return ((DefaultFeature)feature).getData();
|
121 |
} |
122 |
|
123 |
public static JDBCHelper getJDBCHelper() { |
124 |
H2SpatialHelper helper = new H2SpatialHelper();
|
125 |
return helper;
|
126 |
} |
127 |
|
128 |
|
129 |
} |