svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.h2spatial / org.gvsig.h2spatial.h2gis132 / org.gvsig.h2spatial.h2gis132.provider / src / test / java / org / gvsig / fmap / dal / store / h2 / TestUtils.java @ 46517
History | View | Annotate | Download (6.21 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.gvsig.fmap.dal.store.jdbc2.spi.FakeConnectionProvider; |
21 |
import org.slf4j.Logger; |
22 |
import org.slf4j.LoggerFactory; |
23 |
|
24 |
public class TestUtils { |
25 |
|
26 |
public static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class); |
27 |
|
28 |
public static final String PROVIDER_NAME = H2SpatialLibrary.NAME; |
29 |
|
30 |
private static int dbcounter = 1; |
31 |
|
32 |
public static H2SpatialConnectionParameters buildDBConnection(String dbname) throws Exception { |
33 |
DataManager dataManager = DALLocator.getDataManager(); |
34 |
H2SpatialConnectionParameters conn = (H2SpatialConnectionParameters) |
35 |
dataManager.createServerExplorerParameters(H2SpatialLibrary.NAME); |
36 |
|
37 |
File dbfile = TestUtils.getResource(String.format( |
38 |
"test-dbs/%s-%d-%03d",
|
39 |
dbname, |
40 |
System.currentTimeMillis(),
|
41 |
dbcounter++ |
42 |
) |
43 |
); |
44 |
// System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
45 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
46 |
conn.setFile(dbfile); |
47 |
return conn;
|
48 |
} |
49 |
|
50 |
public static JDBCServerExplorer openServerExplorer(String dbname) throws Exception { |
51 |
DataManager dataManager = DALLocator.getDataManager(); |
52 |
H2SpatialConnectionParameters conn = buildDBConnection(dbname); |
53 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
54 |
PROVIDER_NAME, conn |
55 |
); |
56 |
return explorer;
|
57 |
} |
58 |
|
59 |
public static File getTargetFolder() throws Exception { |
60 |
URL url = TestUtils.class.getResource("/"); |
61 |
File x = new File(url.toURI()); |
62 |
File target = x.getParentFile();
|
63 |
return target;
|
64 |
} |
65 |
|
66 |
public static File getResource(String name) throws Exception { |
67 |
File x = new File(getTargetFolder(), name); |
68 |
return x;
|
69 |
} |
70 |
|
71 |
public static File getResourceAsFile(String pathname) throws Exception { |
72 |
URL url = TestUtils.class.getResource(pathname);
|
73 |
File x = new File(url.toURI()); |
74 |
return x;
|
75 |
} |
76 |
|
77 |
public static void removeDALResource(String dbname, String name) throws Exception { |
78 |
H2SpatialConnectionParameters connection = buildDBConnection(dbname); |
79 |
DatabaseWorkspaceManager workspace = DALLocator.getDataManager().createDatabaseWorkspaceManager( |
80 |
(DataServerExplorerParameters) connection |
81 |
); |
82 |
// ResourcesStorage resources = workspace.getResourcesStorage(name);
|
83 |
// resources.remove("dal");
|
84 |
} |
85 |
|
86 |
public static FeatureStore openCountriesStore() throws Exception { |
87 |
DataManager dataManager = DALLocator.getDataManager(); |
88 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/countries.csv"); |
89 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
90 |
DataStore.CSV_PROVIDER_NAME, |
91 |
"file=",f,
|
92 |
"automaticTypesDetection=", false, |
93 |
"locale=","en" |
94 |
); |
95 |
return store;
|
96 |
} |
97 |
|
98 |
public static FeatureStore openSourceStore1() throws Exception { |
99 |
DataManager dataManager = DALLocator.getDataManager(); |
100 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/testCreateSource1.csv"); |
101 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
102 |
DataStore.CSV_PROVIDER_NAME, |
103 |
"file=",f,
|
104 |
"automaticTypesDetection=", false, |
105 |
"locale=","en" |
106 |
); |
107 |
return store;
|
108 |
} |
109 |
|
110 |
public static FeatureStore openStoreTest(String baseFile) throws Exception { |
111 |
DataManager dataManager = DALLocator.getDataManager(); |
112 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/"+baseFile); |
113 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
114 |
DataStore.CSV_PROVIDER_NAME, |
115 |
"file=",f,
|
116 |
"automaticTypesDetection=", false, |
117 |
"locale=","en" |
118 |
); |
119 |
dataManager.getStoresRepository().add(store.getName(), store.getParameters()); |
120 |
return store;
|
121 |
} |
122 |
|
123 |
public static FeatureStore openSourceStore2() throws Exception { |
124 |
DataManager dataManager = DALLocator.getDataManager(); |
125 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/testCreateSource2.csv"); |
126 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
127 |
DataStore.CSV_PROVIDER_NAME, |
128 |
"file=",f,
|
129 |
"automaticTypesDetection=", false, |
130 |
"locale=","en" |
131 |
); |
132 |
return store;
|
133 |
} |
134 |
|
135 |
|
136 |
public static List<String> getSQLs(String name) throws Exception { |
137 |
File f = getResourceAsFile("/org/gvsig/fmap/dal/store/h2/"+name); |
138 |
List<String> SQLs = new ArrayList<>(); |
139 |
List<String> lines = FileUtils.readLines(f); |
140 |
StringBuilder sb = new StringBuilder(); |
141 |
for (String line : lines) { |
142 |
line = StringUtils.stripStart(line, null);
|
143 |
if( line.startsWith("--") ) { |
144 |
continue;
|
145 |
} |
146 |
if( line.endsWith(";") ) { |
147 |
sb.append(line.substring(0, line.length()-1)); |
148 |
SQLs.add(sb.toString()); |
149 |
sb.setLength(0);
|
150 |
} else {
|
151 |
sb.append(line); |
152 |
} |
153 |
} |
154 |
return SQLs;
|
155 |
} |
156 |
|
157 |
public static FeatureProvider getFeatureProvider(Feature feature) { |
158 |
return ((DefaultFeature)feature).getData();
|
159 |
} |
160 |
|
161 |
public static H2SpatialHelper createJDBCHelper() throws Exception { |
162 |
H2SpatialConnectionParameters params = buildDBConnection("fake");
|
163 |
H2SpatialHelper helper = new H2SpatialHelper(params, new FakeConnectionProvider()); |
164 |
return helper;
|
165 |
} |
166 |
|
167 |
|
168 |
} |