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 @ 47606
History | View | Annotate | Download (25.7 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.expressionevaluator.ExpressionUtils; |
18 |
import org.gvsig.fmap.dal.DALLocator; |
19 |
import org.gvsig.fmap.dal.DataManager; |
20 |
import org.gvsig.fmap.dal.DataStore; |
21 |
import static org.gvsig.fmap.dal.DataStore.H2SPATIAL_PROVIDER_NAME; |
22 |
import org.gvsig.fmap.dal.DataTransaction; |
23 |
import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
24 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
25 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
26 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
30 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
31 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
32 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
33 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
34 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
35 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
36 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
37 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
38 |
import org.gvsig.tools.util.HasAFile; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
public abstract class AbstractTestUtils { |
43 |
|
44 |
public static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestUtils.class); |
45 |
|
46 |
protected static int dbcounter = 1; |
47 |
|
48 |
protected Properties properties = null; |
49 |
|
50 |
public AbstractTestUtils() {
|
51 |
|
52 |
} |
53 |
|
54 |
public File getTargetFolder() throws Exception { |
55 |
URL url = this.getClass().getResource("/"); |
56 |
if (url == null) { |
57 |
url = AbstractTestUtils.class.getResource("/");
|
58 |
} |
59 |
File x = new File(url.toURI()); |
60 |
File target = x.getParentFile();
|
61 |
return target;
|
62 |
} |
63 |
|
64 |
public File getResource(String name) throws Exception { |
65 |
File x = new File(getTargetFolder(), name); |
66 |
return x;
|
67 |
} |
68 |
|
69 |
public File getResourceAsFile(String pathname) throws Exception { |
70 |
URL url = this.getClass().getResource(pathname); |
71 |
if (url == null) { |
72 |
url = AbstractTestUtils.class.getResource(pathname); |
73 |
} |
74 |
if( StringUtils.equalsIgnoreCase(url.getProtocol(),"file") ) { |
75 |
File x = new File(url.toURI()); |
76 |
return x;
|
77 |
} |
78 |
File tmp = new File( this.getTargetFolder(), FilenameUtils.getName(pathname)); |
79 |
IOUtils.copy(url, tmp); |
80 |
return tmp;
|
81 |
} |
82 |
|
83 |
public File getFile(File name) throws Exception { |
84 |
File f = this.getResource(String.format( |
85 |
"%s-%d-%03d",
|
86 |
name.getPath(), |
87 |
System.currentTimeMillis(),
|
88 |
dbcounter++ |
89 |
) |
90 |
); |
91 |
FileUtils.forceMkdir(f.getParentFile()); |
92 |
return f;
|
93 |
} |
94 |
|
95 |
public File getFolder(File name) throws Exception { |
96 |
File f = this.getResource(String.format( |
97 |
"%s-%d-%03d",
|
98 |
name.getPath(), |
99 |
System.currentTimeMillis(),
|
100 |
dbcounter++ |
101 |
) |
102 |
); |
103 |
FileUtils.forceMkdir(f); |
104 |
return f;
|
105 |
} |
106 |
|
107 |
public JDBCServerExplorerParameters getH2SpatialServerExplorerParameters(String dbname) throws Exception { |
108 |
DataManager dataManager = DALLocator.getDataManager(); |
109 |
JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(H2SPATIAL_PROVIDER_NAME); |
110 |
|
111 |
File dbfile = this.getResource(String.format( |
112 |
"test-dbs/%s-%d-%03d",
|
113 |
dbname, |
114 |
System.currentTimeMillis(),
|
115 |
dbcounter++ |
116 |
) |
117 |
); |
118 |
// System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
119 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
120 |
((HasAFile) conn).setFile(dbfile); |
121 |
return conn;
|
122 |
} |
123 |
|
124 |
public FeatureProvider getFeatureProvider(Feature feature) {
|
125 |
return ((DefaultFeature) feature).getData();
|
126 |
} |
127 |
|
128 |
public Expecteds getExpecteds(String name) throws Exception { |
129 |
Expecteds x = new Expecteds();
|
130 |
x.normaliceSpaces = false;
|
131 |
x.removeNL = true;
|
132 |
x.stripStart = true;
|
133 |
x.end_of_item = null;
|
134 |
File f = getResourceAsFile(getExpectedsPath() + "/" + name); |
135 |
x.parse(f); |
136 |
return x;
|
137 |
} |
138 |
|
139 |
public static class Expecteds { |
140 |
|
141 |
private Map<String, List<String>> expecteds = new HashMap<>(); |
142 |
|
143 |
public boolean normaliceSpaces = false; |
144 |
public boolean stripStart = false; |
145 |
public boolean removeNL = false; |
146 |
public boolean replace_nl_by_space = false; |
147 |
public boolean trim_end = false; |
148 |
|
149 |
|
150 |
public String startLineComment = "-- "; |
151 |
public String end_of_item = ";"; |
152 |
|
153 |
private static class CommandLine { |
154 |
|
155 |
private final String[] cmd; |
156 |
|
157 |
private CommandLine(String line, String prefix) { |
158 |
line = StringUtils.substring(line, prefix.length()); |
159 |
line = StringUtils.normalizeSpace(line); |
160 |
this.cmd = StringUtils.split(line);
|
161 |
} |
162 |
|
163 |
public boolean isTrue(int argn, boolean defaultValue) { |
164 |
if (argn >= this.cmd.length) { |
165 |
return defaultValue;
|
166 |
} |
167 |
return BooleanUtils.toBoolean(this.cmd[argn]); |
168 |
} |
169 |
|
170 |
public String get(int argn) { |
171 |
return this.cmd[argn]; |
172 |
} |
173 |
|
174 |
public int size() { |
175 |
return this.cmd.length; |
176 |
} |
177 |
} |
178 |
|
179 |
public Expecteds() throws Exception { |
180 |
|
181 |
} |
182 |
|
183 |
public void parse(File f) throws Exception { |
184 |
final int SEARCHING_ITEM = 0; |
185 |
final int READING_ITEM = 1; |
186 |
boolean localNormaliceSpaces = false; |
187 |
boolean localStripStart = false; |
188 |
boolean localRemoveNL = false; |
189 |
boolean local_replace_nl_by_space = false; |
190 |
boolean local_trim_end = false; |
191 |
String name = f.getName();
|
192 |
|
193 |
List<String> lines = FileUtils.readLines(f); |
194 |
|
195 |
StringBuilder sb = null; |
196 |
String currentItem = null; |
197 |
int lineno = 1; |
198 |
int state = SEARCHING_ITEM;
|
199 |
for (String line : lines) { |
200 |
line = line + "\n";
|
201 |
switch (state) {
|
202 |
case SEARCHING_ITEM:
|
203 |
if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) { |
204 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
205 |
normaliceSpaces = cmd.isTrue(1, true); |
206 |
} else if (line.toLowerCase().startsWith(startLineComment + "end-of-item")) { |
207 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
208 |
end_of_item = cmd.get(1);
|
209 |
} else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) { |
210 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
211 |
stripStart = cmd.isTrue(1, true); |
212 |
} else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) { |
213 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
214 |
removeNL = cmd.isTrue(1, true); |
215 |
} else if (line.toLowerCase().startsWith(startLineComment + "replace-nl-by-space")) { |
216 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
217 |
replace_nl_by_space = cmd.isTrue(1, true); |
218 |
} else if (line.toLowerCase().startsWith(startLineComment + "trim-end")) { |
219 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
220 |
trim_end = cmd.isTrue(1, true); |
221 |
} else if (line.toLowerCase().startsWith(startLineComment + "begin ")) { |
222 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
223 |
currentItem = cmd.get(1);
|
224 |
sb = new StringBuilder(); |
225 |
state = READING_ITEM; |
226 |
localNormaliceSpaces = normaliceSpaces; |
227 |
localStripStart = stripStart; |
228 |
localRemoveNL = removeNL; |
229 |
local_replace_nl_by_space = replace_nl_by_space; |
230 |
local_trim_end = trim_end; |
231 |
} else if (line.toLowerCase().startsWith(startLineComment + "rem ")) { |
232 |
// do nothing skip comment
|
233 |
} else if (!StringUtils.isBlank(line)) { |
234 |
throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + "."); |
235 |
} |
236 |
break;
|
237 |
case READING_ITEM:
|
238 |
if (line.toLowerCase().startsWith(startLineComment + "normalize-spaces")) { |
239 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
240 |
localNormaliceSpaces = cmd.isTrue(1, true); |
241 |
} else if (line.toLowerCase().startsWith(startLineComment + "strip-start")) { |
242 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
243 |
localStripStart = cmd.isTrue(1, true); |
244 |
} else if (line.toLowerCase().startsWith(startLineComment + "remove-nl")) { |
245 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
246 |
localRemoveNL = cmd.isTrue(1, true); |
247 |
} else if (line.toLowerCase().startsWith(startLineComment + "replace-nl-by-space")) { |
248 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
249 |
local_replace_nl_by_space = cmd.isTrue(1, true); |
250 |
} else if (line.toLowerCase().startsWith(startLineComment + "trim-end")) { |
251 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
252 |
local_trim_end = cmd.isTrue(1, true); |
253 |
} else if (line.toLowerCase().startsWith(startLineComment + "end ")) { |
254 |
CommandLine cmd = new CommandLine(line, startLineComment);
|
255 |
if (!StringUtils.equals(currentItem, cmd.get(1)) ) { |
256 |
throw new IllegalStateException("Syntax error at '" + name + "', line " + lineno + ", expected '" + startLineComment + "end " + currentItem + "', and found " + line + "."); |
257 |
} |
258 |
String s = sb.toString();
|
259 |
if (s.endsWith("\n")) { |
260 |
s = s.substring(0, s.length() - 1); |
261 |
} |
262 |
if (localNormaliceSpaces) {
|
263 |
s = StringUtils.normalizeSpace(s); |
264 |
} |
265 |
if( local_trim_end ) {
|
266 |
s = StringUtils.stripEnd(s, " ");
|
267 |
} |
268 |
|
269 |
List<String> value = expecteds.get(currentItem); |
270 |
if( value == null ) { |
271 |
value = new ArrayList<>(); |
272 |
expecteds.put(currentItem, value); |
273 |
} |
274 |
if( StringUtils.isNotBlank(s) ) {
|
275 |
value.add(s); |
276 |
} |
277 |
state = SEARCHING_ITEM; |
278 |
|
279 |
} else if (line.toLowerCase().startsWith(startLineComment + "rem ")) { |
280 |
// do nothing skip comment
|
281 |
} else {
|
282 |
if (localStripStart) {
|
283 |
line = StringUtils.stripStart(line, null);
|
284 |
} |
285 |
if( local_replace_nl_by_space ) {
|
286 |
line = line.replace('\n', ' '); |
287 |
} |
288 |
if (localRemoveNL) {
|
289 |
line = StringUtils.remove(line, "\n");
|
290 |
line = StringUtils.remove(line, "\r");
|
291 |
} |
292 |
if( this.end_of_item!=null ) { |
293 |
String s = StringUtils.stripEnd(line, null); |
294 |
if( s!=null && s.endsWith(this.end_of_item) ) { |
295 |
List<String> value = expecteds.get(currentItem); |
296 |
if( value == null ) { |
297 |
value = new ArrayList<>(); |
298 |
expecteds.put(currentItem, value); |
299 |
} |
300 |
s = s.substring(0, s.length() - this.end_of_item.length()); |
301 |
value.add(s); |
302 |
line = null;
|
303 |
} |
304 |
} |
305 |
if( StringUtils.isNotBlank(line) ) {
|
306 |
sb.append(line); |
307 |
} |
308 |
|
309 |
} |
310 |
break;
|
311 |
} |
312 |
lineno++; |
313 |
} |
314 |
if (state != SEARCHING_ITEM) {
|
315 |
throw new IllegalStateException("Syntax error at '" + name + "', expected '" + startLineComment + "end " + currentItem + "' and found EOF ."); |
316 |
} |
317 |
} |
318 |
|
319 |
public boolean match(String name, int i, String value) { |
320 |
String exp = this.get(name, i); |
321 |
return FilenameUtils.wildcardMatch(value, exp);
|
322 |
} |
323 |
|
324 |
public String get(String name) { |
325 |
List<String> value = this.expecteds.get(name); |
326 |
if( CollectionUtils.isEmpty(value) ) {
|
327 |
return null; |
328 |
} |
329 |
return value.get(0); |
330 |
} |
331 |
|
332 |
public List<String> getAll(String name) { |
333 |
List<String> value = this.expecteds.get(name); |
334 |
if( CollectionUtils.isEmpty(value) ) {
|
335 |
return null; |
336 |
} |
337 |
return value;
|
338 |
} |
339 |
|
340 |
public String get(String name, int index) { |
341 |
List<String> value = this.expecteds.get(name); |
342 |
if( CollectionUtils.isEmpty(value) ) {
|
343 |
return null; |
344 |
} |
345 |
return value.get(index);
|
346 |
} |
347 |
|
348 |
public int size(String name) { |
349 |
List<String> value = this.expecteds.get(name); |
350 |
if( CollectionUtils.isEmpty(value) ) {
|
351 |
return 0; |
352 |
} |
353 |
return value.size();
|
354 |
} |
355 |
} |
356 |
|
357 |
public void removeResource(JDBCServerExplorer explorer, String storeName, String resourceName) throws Exception { |
358 |
JDBCStoreParameters storeParams = explorer.get(storeName); |
359 |
ResourcesStorage resourcesStorage = explorer.getResourcesStorage(storeParams); |
360 |
if( resourcesStorage.allowRemove() ) {
|
361 |
resourcesStorage.remove(resourceName); |
362 |
} |
363 |
} |
364 |
|
365 |
public void info_h2sql(String label, File file) { |
366 |
System.out.println(""); |
367 |
System.out.println("#h2console "+label+": "+file.getName()); |
368 |
System.out.println("cd '"+file.getParent()+"'; h2sql -d '"+file.getName()+"' -user SA"); |
369 |
System.out.println("jdbc:h2:"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL"); |
370 |
System.out.println("jdbc:h2:tcp://127.0.1.1:9123/"+file.getAbsolutePath()+";MODE=PostgreSQL;SCHEMA=PUBLIC;ALLOW_LITERALS=ALL"); |
371 |
System.out.println(""); |
372 |
} |
373 |
|
374 |
public void info_jdbc(String label, JDBCServerExplorerParameters parameters) { |
375 |
if( StringUtils.equalsIgnoreCase(parameters.getProviderName(), FeatureStore.H2SPATIAL_PROVIDER_NAME) ) {
|
376 |
info_h2sql(label, ((HasAFile)parameters).getFile()); |
377 |
return;
|
378 |
} |
379 |
System.out.println(""); |
380 |
System.out.println("# Connection "+label); |
381 |
System.out.println(parameters.getUrl());
|
382 |
System.out.println(""); |
383 |
} |
384 |
|
385 |
public void info_jdbc(String label, JDBCServerExplorer explorer) { |
386 |
info_jdbc(label, explorer.getParameters()); |
387 |
} |
388 |
|
389 |
public void info_jdbc(JDBCServerExplorer explorer) { |
390 |
JDBCServerExplorerParameters params = explorer.getParameters(); |
391 |
info_jdbc(params.getDBName(), params); |
392 |
} |
393 |
|
394 |
protected boolean getPropertyBoolean(String name) { |
395 |
Properties props = this.getProperties(); |
396 |
return BooleanUtils.toBoolean(props.getProperty(name));
|
397 |
} |
398 |
|
399 |
protected int getPropertyInt(String name, int defaultValue) { |
400 |
Properties props = this.getProperties(); |
401 |
return NumberUtils.toInt(props.getProperty(name), defaultValue);
|
402 |
} |
403 |
|
404 |
protected String getProperty(String name) { |
405 |
Properties props = this.getProperties(); |
406 |
return props.getProperty(name);
|
407 |
} |
408 |
|
409 |
protected Properties getProperties() { |
410 |
if( this.properties == null ) { |
411 |
try {
|
412 |
Properties props = new Properties(); |
413 |
this.properties = props;
|
414 |
} catch(Exception ex) { |
415 |
throw new RuntimeException(ex); |
416 |
} |
417 |
} |
418 |
return this.properties; |
419 |
} |
420 |
|
421 |
public boolean isTheDatabaseAvailable() { |
422 |
return true; |
423 |
} |
424 |
|
425 |
public FeatureStore openCSVStore(String pathname) throws Exception { |
426 |
DataManager dataManager = DALLocator.getDataManager(); |
427 |
File f = getResourceAsFile(pathname);
|
428 |
FeatureStore store = (FeatureStore) dataManager.openStore( |
429 |
DataStore.CSV_PROVIDER_NAME, |
430 |
"file=",f,
|
431 |
"automaticTypesDetection=", false, |
432 |
"locale=","en" |
433 |
); |
434 |
return store;
|
435 |
} |
436 |
|
437 |
public FeatureStore openCountriesStore() throws Exception { |
438 |
return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/countries.csv"); |
439 |
} |
440 |
|
441 |
public FeatureStore openSourceStore1() throws Exception { |
442 |
return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource1.csv"); |
443 |
} |
444 |
|
445 |
public FeatureStore openSourceStore2() throws Exception { |
446 |
return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/testCreateSource2.csv"); |
447 |
} |
448 |
|
449 |
public FeatureStore openStore(JDBCServerExplorer explorer, String name) throws Exception { |
450 |
JDBCStoreParameters params = explorer.get(name); |
451 |
|
452 |
DataManager dataManager = DALLocator.getDataManager(); |
453 |
FeatureStore store; |
454 |
try {
|
455 |
store = (FeatureStore) dataManager.openStore( |
456 |
getProviderName(), |
457 |
params |
458 |
); |
459 |
} catch(ValidateDataParametersException ex) {
|
460 |
LOGGER.warn(ex.getLocalizedMessageStack()); |
461 |
throw ex;
|
462 |
} |
463 |
return store;
|
464 |
} |
465 |
|
466 |
public void create_table_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore) throws Exception { |
467 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) targetExplorer.getAddParameters( |
468 |
targetName |
469 |
); |
470 |
EditableFeatureType ft = params.getDefaultFeatureType(); |
471 |
ft.addAll(sourceStore.getDefaultFeatureType()); |
472 |
targetExplorer.add(getProviderName(), params, true);
|
473 |
} |
474 |
|
475 |
public void insert_into_from(JDBCServerExplorer targetExplorer, String targetName, FeatureStore sourceStore, int mode) throws Exception { |
476 |
FeatureStore targetStore = openStore(targetExplorer, targetName); |
477 |
switch(mode){
|
478 |
case FeatureStore.MODE_FULLEDIT:
|
479 |
targetStore.edit(mode); |
480 |
try {
|
481 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
482 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
483 |
targetStore.insert(targetFeature); |
484 |
} |
485 |
} finally {
|
486 |
targetStore.finishEditing(); |
487 |
} |
488 |
break;
|
489 |
default:
|
490 |
DataTransaction trans = DALLocator.getDataManager().createTransaction(); |
491 |
trans.begin(); |
492 |
trans.add(targetStore); |
493 |
targetStore.edit(mode); |
494 |
try {
|
495 |
for (Feature sourceFeature : sourceStore.getFeatureSet()) {
|
496 |
EditableFeature targetFeature = targetStore.createNewFeature(sourceFeature); |
497 |
targetStore.insert(targetFeature); |
498 |
} |
499 |
} finally {
|
500 |
targetStore.finishEditing(); |
501 |
} |
502 |
trans.commit(); |
503 |
trans.close(); |
504 |
break;
|
505 |
} |
506 |
} |
507 |
|
508 |
public JDBCServerExplorer openServerExplorer(String dbname) throws Exception { |
509 |
DataManager dataManager = DALLocator.getDataManager(); |
510 |
JDBCServerExplorerParameters params = this.getServerExplorerParameters(dbname);
|
511 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
512 |
this.getProviderName(),
|
513 |
params |
514 |
); |
515 |
return explorer;
|
516 |
} |
517 |
|
518 |
public JDBCServerExplorer openServerExplorer(File dbfile) throws Exception { |
519 |
DataManager dataManager = DALLocator.getDataManager(); |
520 |
JDBCServerExplorerParameters params = this.getServerExplorerParameters(dbfile);
|
521 |
JDBCServerExplorer explorer = (JDBCServerExplorer) dataManager.openServerExplorer( |
522 |
this.getProviderName(),
|
523 |
params |
524 |
); |
525 |
return explorer;
|
526 |
} |
527 |
|
528 |
public void drop_tables(JDBCServerExplorer explorer, String...tables) throws Exception { |
529 |
for (String table : tables) { |
530 |
String sql = "DROP TABLE IF EXISTS \""+table+"\""; |
531 |
explorer.execute(sql); |
532 |
removeResource(explorer, table, "dal");
|
533 |
} |
534 |
} |
535 |
|
536 |
public DatabaseWorkspaceManager initWorkspace(String name) throws Exception { |
537 |
JDBCServerExplorer explorer = this.openServerExplorer(name);
|
538 |
DataManager manager = DALLocator.getDataManager(); |
539 |
DatabaseWorkspaceManager ws = manager.createDatabaseWorkspaceManager(explorer.getParameters()); |
540 |
ws.connect(); |
541 |
if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION)) {
|
542 |
ws.createTable(DatabaseWorkspaceManager.TABLE_CONFIGURATION); |
543 |
} |
544 |
if (!ws.existsTable(DatabaseWorkspaceManager.TABLE_RESOURCES)) {
|
545 |
ws.createTable(DatabaseWorkspaceManager.TABLE_RESOURCES); |
546 |
} |
547 |
return ws;
|
548 |
} |
549 |
|
550 |
public List<String> getExpectedSQLs(String name) throws Exception { |
551 |
File f = getResourceAsFile(getExpectedsPath() + "/" + name); |
552 |
List<String> SQLs = new ArrayList<>(); |
553 |
List<String> lines = FileUtils.readLines(f); |
554 |
StringBuilder sb = new StringBuilder(); |
555 |
for (String line : lines) { |
556 |
line = StringUtils.stripStart(line, null);
|
557 |
if (line.startsWith("--")) { |
558 |
continue;
|
559 |
} |
560 |
if (line.endsWith(";")) { |
561 |
sb.append(line.substring(0, line.length() - 1)); |
562 |
SQLs.add(sb.toString()); |
563 |
sb.setLength(0);
|
564 |
} else {
|
565 |
sb.append(line); |
566 |
} |
567 |
} |
568 |
return SQLs;
|
569 |
} |
570 |
|
571 |
|
572 |
// public abstract String getExpectedResourcesPrefix(); // Ex. "h2spatial"
|
573 |
|
574 |
public abstract String getExpectedsPath(); |
575 |
|
576 |
public abstract JDBCHelper createJDBCHelper() throws Exception; |
577 |
|
578 |
public abstract String getProviderName(); |
579 |
|
580 |
public abstract JDBCServerExplorerParameters getServerExplorerParameters(String dbname) throws Exception; |
581 |
|
582 |
public void runSQLToCheckSyntax(String testName, FeatureStore datacsv, String testTableName, String sql) throws Exception { |
583 |
JDBCServerExplorer explorer = this.openServerExplorer(testName);
|
584 |
|
585 |
this.info_jdbc(explorer);
|
586 |
this.drop_tables(explorer, testTableName);
|
587 |
|
588 |
this.create_table_from(explorer, testTableName, datacsv);
|
589 |
this.insert_into_from(explorer, testTableName, datacsv, FeatureStore.MODE_APPEND);
|
590 |
|
591 |
explorer.execute(sql); |
592 |
} |
593 |
|
594 |
public void output_results(String testName, String sqlresult, String sqlexpected) { |
595 |
System.out.println("###### "+testName+"()"); |
596 |
System.out.println("###### SQL:" + sqlresult+";"); |
597 |
System.out.println("###### EXP:" + sqlexpected+";"); |
598 |
} |
599 |
|
600 |
public EditableFeatureAttributeDescriptor addExtraColumn(EditableFeatureType eFeatureType, FeatureQuery query, String name, int type, String exp) { |
601 |
EditableFeatureAttributeDescriptor extraColumn = query.getExtraColumn().add(name, type); |
602 |
extraColumn.setFeatureAttributeEmulator(new DefaultFeatureAttributeEmulatorExpression(eFeatureType, ExpressionUtils.createExpression(exp)));
|
603 |
return extraColumn;
|
604 |
} |
605 |
|
606 |
public JDBCServerExplorerParameters getServerExplorerParameters(File dbfile) throws Exception { |
607 |
DataManager dataManager = DALLocator.getDataManager(); |
608 |
JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(this.getProviderName());
|
609 |
// System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
610 |
FileUtils.forceMkdir(dbfile.getParentFile()); |
611 |
((HasAFile) conn).setFile(dbfile); |
612 |
return conn;
|
613 |
} |
614 |
|
615 |
} |