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