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 @ 46106
History | View | Annotate | Download (18 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 = AbstractTestUtils.class.getResource("/"); |
51 |
File x = new File(url.toURI()); |
52 |
File target = x.getParentFile();
|
53 |
return target;
|
54 |
} |
55 |
|
56 |
public File getResource(String name) throws Exception { |
57 |
File x = new File(getTargetFolder(), name); |
58 |
return x;
|
59 |
} |
60 |
|
61 |
public File getResourceAsFile(String pathname) throws Exception { |
62 |
URL url = AbstractTestUtils.class.getResource(pathname);
|
63 |
if( StringUtils.equalsIgnoreCase(url.getProtocol(),"file") ) { |
64 |
File x = new File(url.toURI()); |
65 |
return x;
|
66 |
} |
67 |
File tmp = new File( this.getTargetFolder(), FilenameUtils.getName(pathname)); |
68 |
IOUtils.copy(url, tmp); |
69 |
return tmp;
|
70 |
} |
71 |
|
72 |
public File getFile(File name) throws Exception { |
73 |
File f = this.getResource(String.format( |
74 |
"%s-%d-%03d",
|
75 |
name.getPath(), |
76 |
System.currentTimeMillis(),
|
77 |
dbcounter++ |
78 |
) |
79 |
); |
80 |
FileUtils.forceMkdir(f.getParentFile()); |
81 |
return f;
|
82 |
} |
83 |
|
84 |
public File getFolder(File name) throws Exception { |
85 |
File f = this.getResource(String.format( |
86 |
"%s-%d-%03d",
|
87 |
name.getPath(), |
88 |
System.currentTimeMillis(),
|
89 |
dbcounter++ |
90 |
) |
91 |
); |
92 |
FileUtils.forceMkdir(f); |
93 |
return f;
|
94 |
} |
95 |
|
96 |
public JDBCServerExplorerParameters getH2SpatialServerExplorerParameters(String dbname) throws Exception { |
97 |
DataManager dataManager = DALLocator.getDataManager(); |
98 |
JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(H2SPATIAL_PROVIDER_NAME); |
99 |
|
100 |
File dbfile = this.getResource(String.format( |
101 |
"test-dbs/%s-%d-%03d",
|
102 |
dbname, |
103 |
System.currentTimeMillis(),
|
104 |
dbcounter++ |
105 |
) |
106 |
); |
107 |
// System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
108 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
109 |
((HasAFile) conn).setFile(dbfile); |
110 |
return conn;
|
111 |
} |
112 |
|
113 |
public FeatureProvider getFeatureProvider(Feature feature) {
|
114 |
return ((DefaultFeature) feature).getData();
|
115 |
} |
116 |
|
117 |
public Expecteds getExpecteds(String name) throws Exception { |
118 |
Expecteds x = new Expecteds();
|
119 |
x.normaliceSpaces = false;
|
120 |
x.removeNL = true;
|
121 |
x.stripStart = true;
|
122 |
File f = getResourceAsFile(getExpectedsPath() + "/" + name); |
123 |
x.parse(f); |
124 |
return x;
|
125 |
} |
126 |
|
127 |
public static class Expecteds { |
128 |
|
129 |
private Map<String, List<String>> expecteds = new HashMap<>(); |
130 |
|
131 |
public boolean normaliceSpaces = false; |
132 |
public boolean stripStart = false; |
133 |
public boolean removeNL = false; |
134 |
|
135 |
public String startLineComment = "-- "; |
136 |
|
137 |
private static class CommandLine { |
138 |
|
139 |
private final String[] cmd; |
140 |
|
141 |
private CommandLine(String line, String prefix) { |
142 |
line = StringUtils.substring(line, prefix.length()); |
143 |
line = StringUtils.normalizeSpace(line); |
144 |
this.cmd = StringUtils.split(line);
|
145 |
} |
146 |
|
147 |
public boolean isTrue(int argn, boolean defaultValue) { |
148 |
if (argn >= this.cmd.length) { |
149 |
return defaultValue;
|
150 |
} |
151 |
return BooleanUtils.toBoolean(this.cmd[argn]); |
152 |
} |
153 |
|
154 |
public String get(int argn) { |
155 |
return this.cmd[argn]; |
156 |
} |
157 |
|
158 |
public int size() { |
159 |
return this.cmd.length; |
160 |
} |
161 |
} |
162 |
|
163 |
public Expecteds() throws Exception { |
164 |
|
165 |
} |
166 |
|
167 |
public void parse(File f) throws Exception { |
168 |
final int SEARCHING_ITEM = 0; |
169 |
final int READING_ITEM = 1; |
170 |
boolean localNormaliceSpaces = false; |
171 |
boolean localStripStart = false; |
172 |
boolean localRemoveNL = false; |
173 |
String name = f.getName();
|
174 |
|
175 |
List<String> lines = FileUtils.readLines(f); |
176 |
|
177 |
StringBuilder sb = null; |
178 |
String currentItem = null; |
179 |
int lineno = 1; |
180 |
int state = SEARCHING_ITEM;
|
181 |
for (String line : lines) { |
182 |
line = line + "\n";
|
183 |
switch (state) {
|
184 |
case SEARCHING_ITEM:
|
185 |
if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) { |
186 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
187 |
normaliceSpaces = cmd.isTrue(1, true); |
188 |
} else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) { |
189 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
190 |
stripStart = cmd.isTrue(1, true); |
191 |
} else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) { |
192 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
193 |
removeNL = cmd.isTrue(1, true); |
194 |
} else if (line.toLowerCase().startsWith(startLineComment + "begin ")) { |
195 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
196 |
currentItem = cmd.get(1);
|
197 |
sb = new StringBuilder(); |
198 |
state = READING_ITEM; |
199 |
localNormaliceSpaces = normaliceSpaces; |
200 |
localStripStart = stripStart; |
201 |
localRemoveNL = removeNL; |
202 |
} else if (line.toLowerCase().startsWith(startLineComment + "rem ")) { |
203 |
// do nothing skip comment
|
204 |
} else if (!StringUtils.isBlank(line)) { |
205 |
throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + "."); |
206 |
} |
207 |
break;
|
208 |
case READING_ITEM:
|
209 |
if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) { |
210 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
211 |
localNormaliceSpaces = cmd.isTrue(1, true); |
212 |
} else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) { |
213 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
214 |
localStripStart = cmd.isTrue(1, true); |
215 |
} else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) { |
216 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
217 |
localRemoveNL = cmd.isTrue(1, true); |
218 |
} else if (line.toLowerCase().startsWith(startLineComment + "end ")) { |
219 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
220 |
if (!StringUtils.equals(currentItem, cmd.get(1)) ) { |
221 |
throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + ", expected '" + startLineComment + "end " + currentItem + "', and found " + line + "."); |
222 |
} |
223 |
String s = sb.toString();
|
224 |
if (s.endsWith("\n")) { |
225 |
s = s.substring(0, s.length() - 1); |
226 |
} |
227 |
if (localNormaliceSpaces) {
|
228 |
s = StringUtils.normalizeSpace(s); |
229 |
} |
230 |
|
231 |
List<String> value = expecteds.get(currentItem); |
232 |
if( value == null ) { |
233 |
value = new ArrayList<>(); |
234 |
expecteds.put(currentItem, value); |
235 |
} |
236 |
value.add(s); |
237 |
state = SEARCHING_ITEM; |
238 |
|
239 |
} else if (line.toLowerCase().startsWith(startLineComment + "rem ")) { |
240 |
// do nothing skip comment
|
241 |
} else {
|
242 |
if (localStripStart) {
|
243 |
line = StringUtils.stripStart(line, null);
|
244 |
} |
245 |
if (localRemoveNL) {
|
246 |
line = StringUtils.remove(line, "\n");
|
247 |
line = StringUtils.remove(line, "\r");
|
248 |
} |
249 |
sb.append(line); |
250 |
} |
251 |
break;
|
252 |
} |
253 |
lineno++; |
254 |
} |
255 |
if (state != SEARCHING_ITEM) {
|
256 |
throw new IllegalStateException("Syntax error at '" + name + "', expected '" + startLineComment + "end " + currentItem + "' and found EOF ."); |
257 |
} |
258 |
} |
259 |
|
260 |
public String get(String name) { |
261 |
List<String> value = this.expecteds.get(name); |
262 |
if( CollectionUtils.isEmpty(value) ) {
|
263 |
return null; |
264 |
} |
265 |
return value.get(0); |
266 |
} |
267 |
|
268 |
public String get(String name, int index) { |
269 |
List<String> value = this.expecteds.get(name); |
270 |
if( CollectionUtils.isEmpty(value) ) {
|
271 |
return null; |
272 |
} |
273 |
return value.get(index);
|
274 |
} |
275 |
|
276 |
public int size(String name) { |
277 |
List<String> value = this.expecteds.get(name); |
278 |
if( CollectionUtils.isEmpty(value) ) {
|
279 |
return 0; |
280 |
} |
281 |
return value.size();
|
282 |
} |
283 |
} |
284 |
|
285 |
public void removeResource(JDBCServerExplorer explorer, String storeName, String resourceName) throws Exception { |
286 |
JDBCStoreParameters storeParams = explorer.get(storeName); |
287 |
ResourcesStorage resourcesStorage = explorer.getResourcesStorage(storeParams); |
288 |
if( resourcesStorage.allowRemove() ) {
|
289 |
resourcesStorage.remove(resourceName); |
290 |
} |
291 |
} |
292 |
|
293 |
public void info_h2sql(String label, File file) { |
294 |
System.out.println(""); |
295 |
System.out.println("#h2console "+label+": "+file.getName()); |
296 |
System.out.println("cd '"+file.getParent()+"'; h2sql -d '"+file.getName()+"' -user SA"); |
297 |
System.out.println("jdbc:h2:"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL"); |
298 |
System.out.println("jdbc:h2:tcp://127.0.1.1:9123/"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL"); |
299 |
System.out.println(""); |
300 |
} |
301 |
|
302 |
public void info_jdbc(String label, JDBCServerExplorerParameters parameters) { |
303 |
if( StringUtils.equalsIgnoreCase(parameters.getProviderName(), FeatureStore.H2SPATIAL_PROVIDER_NAME) ) {
|
304 |
info_h2sql(label, ((HasAFile)parameters).getFile()); |
305 |
return;
|
306 |
} |
307 |
System.out.println(""); |
308 |
System.out.println("# Connection "+label); |
309 |
System.out.println(parameters.getUrl());
|
310 |
System.out.println(""); |
311 |
} |
312 |
|
313 |
public void info_jdbc(String label, JDBCServerExplorer explorer) { |
314 |
info_jdbc(label, explorer.getParameters()); |
315 |
} |
316 |
|
317 |
public void info_jdbc(JDBCServerExplorer explorer) { |
318 |
JDBCServerExplorerParameters params = explorer.getParameters(); |
319 |
info_jdbc(params.getDBName(), params); |
320 |
} |
321 |
|
322 |
protected boolean getPropertyBoolean(String name) { |
323 |
Properties props = this.getProperties(); |
324 |
return BooleanUtils.toBoolean(props.getProperty(name));
|
325 |
} |
326 |
|
327 |
protected int getPropertyInt(String name, int defaultValue) { |
328 |
Properties props = this.getProperties(); |
329 |
return NumberUtils.toInt(props.getProperty(name), defaultValue);
|
330 |
} |
331 |
|
332 |
protected String getProperty(String name) { |
333 |
Properties props = this.getProperties(); |
334 |
return props.getProperty(name);
|
335 |
} |
336 |
|
337 |
protected Properties getProperties() { |
338 |
if( this.properties == null ) { |
339 |
try {
|
340 |
Properties props = new Properties(); |
341 |
this.properties = props;
|
342 |
} catch(Exception ex) { |
343 |
throw new RuntimeException(ex); |
344 |
} |
345 |
} |
346 |
return this.properties; |
347 |
} |
348 |
|
349 |
public boolean isTheDatabaseAvailable() { |
350 |
return true; |
351 |
} |
352 |
|
353 |
public FeatureStore openCSVStore(String pathname) throws Exception { |
354 |
DataManager dataManager = DALLocator.getDataManager(); |
355 |
File f = getResourceAsFile(pathname);
|
356 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
357 |
DataStore.CSV_PROVIDER_NAME, |
358 |
"file=",f,
|
359 |
"automaticTypesDetection=", false, |
360 |
"locale=","en" |
361 |
); |
362 |
return store;
|
363 |
} |
364 |
|
365 |
public FeatureStore openSourceStore1() throws Exception { |
366 |
return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource1.csv"); |
367 |
} |
368 |
|
369 |
public FeatureStore openSourceStore2() throws Exception { |
370 |
return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource2.csv"); |
371 |
} |
372 |
|
373 |
public FeatureStore openStore(JDBCServerExplorer explorer, String name) throws Exception { |
374 |
JDBCStoreParameters params = explorer.get(name); |
375 |
|
376 |
DataManager dataManager = DALLocator.getDataManager(); |
377 |
FeatureStore store; |
378 |
try {
|
379 |
store = (FeatureStore) dataManager.openStore( |
380 |
getProviderName(), |
381 |
params |
382 |
); |
383 |
} catch(ValidateDataParametersException ex) {
|
384 |
LOGGER.warn(ex.getLocalizedMessageStack()); |
385 |
throw ex;
|
386 |
} |
387 |
return store;
|
388 |
} |
389 |
|
390 |
public void create_table_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore) throws Exception { |
391 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) targetExplorer.getAddParameters( |
392 |
targetName |
393 |
); |
394 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
395 |
ft.addAll(sourceStore.getDefaultFeatureType()); |
396 |
targetExplorer.add(getProviderName(), params, true);
|
397 |
} |
398 |
|
399 |
public void insert_into_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore, int mode) throws Exception { |
400 |
FeatureStore targetStore = openStore(targetExplorer, targetName); |
401 |
targetStore.edit(mode); |
402 |
try {
|
403 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
404 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
405 |
targetStore.insert(targetFeature); |
406 |
} |
407 |
} finally {
|
408 |
targetStore.finishEditing(); |
409 |
} |
410 |
} |
411 |
|
412 |
public JDBCServerExplorer openServerExplorer(String dbname) throws Exception { |
413 |
DataManager dataManager = DALLocator.getDataManager(); |
414 |
JDBCServerExplorerParameters params = this.getServerExplorerParameters(dbname);
|
415 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
416 |
this.getProviderName(),
|
417 |
params |
418 |
); |
419 |
return explorer;
|
420 |
} |
421 |
|
422 |
public void drop_tables(JDBCServerExplorer explorer, String...tables) throws Exception { |
423 |
for (String table : tables) { |
424 |
String sql = "DROP TABLE IF EXISTS \""+table+"\""; |
425 |
explorer.execute(sql); |
426 |
removeResource(explorer, table, "dal");
|
427 |
} |
428 |
} |
429 |
|
430 |
public void initWorkspace(String name) throws Exception { |
431 |
JDBCServerExplorer explorer = this.openServerExplorer(name);
|
432 |
DataManager manager = DALLocator.getDataManager(); |
433 |
DatabaseWorkspaceManager ws = manager.createDatabaseWorkspaceManager(explorer.getParameters()); |
434 |
ws.connect(); |
435 |
if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION)) {
|
436 |
ws.createTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION); |
437 |
} |
438 |
if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_RESOURCES)) {
|
439 |
ws.createTable(DatabaseWorkspaceManager.TABLE_RESOURCES); |
440 |
} |
441 |
} |
442 |
|
443 |
// public abstract String getExpectedResourcesPrefix(); // Ex. "h2spatial"
|
444 |
|
445 |
public abstract String getExpectedsPath(); |
446 |
|
447 |
public abstract JDBCHelper createJDBCHelper() throws Exception; |
448 |
|
449 |
public abstract String getProviderName(); |
450 |
|
451 |
public abstract JDBCServerExplorerParameters getServerExplorerParameters(String dbname) throws Exception; |
452 |
} |