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 | 46101 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2; |
---|---|---|---|
2 | 45472 | jjdelcerro | |
3 | import java.io.File; |
||
4 | import java.net.URL; |
||
5 | import java.util.ArrayList; |
||
6 | 46101 | jjdelcerro | import java.util.HashMap; |
7 | 45472 | jjdelcerro | import java.util.List; |
8 | 46101 | jjdelcerro | import java.util.Map; |
9 | import java.util.Properties; |
||
10 | import org.apache.commons.collections4.CollectionUtils; |
||
11 | 45472 | jjdelcerro | import org.apache.commons.io.FileUtils; |
12 | 46101 | jjdelcerro | import org.apache.commons.io.FilenameUtils; |
13 | import org.apache.commons.io.IOUtils; |
||
14 | import org.apache.commons.lang3.BooleanUtils; |
||
15 | 45472 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
16 | 46101 | jjdelcerro | import org.apache.commons.lang3.math.NumberUtils; |
17 | 46507 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionUtils; |
18 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
19 | import org.gvsig.fmap.dal.DataManager; |
||
20 | import org.gvsig.fmap.dal.DataStore; |
||
21 | 46101 | jjdelcerro | import static org.gvsig.fmap.dal.DataStore.H2SPATIAL_PROVIDER_NAME; |
22 | 47606 | fdiaz | import org.gvsig.fmap.dal.DataTransaction; |
23 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.DatabaseWorkspaceManager; |
24 | 46101 | jjdelcerro | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
25 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
26 | 46507 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
27 | 46101 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
28 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
29 | 46507 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
30 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
31 | 46101 | jjdelcerro | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
32 | 45472 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.DefaultFeature; |
33 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
34 | 46507 | jjdelcerro | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureAttributeEmulatorExpression; |
35 | 46101 | jjdelcerro | 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 | 45472 | jjdelcerro | import org.slf4j.Logger; |
40 | import org.slf4j.LoggerFactory; |
||
41 | |||
42 | 46101 | jjdelcerro | public abstract class AbstractTestUtils { |
43 | 45472 | jjdelcerro | |
44 | 46101 | jjdelcerro | public static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestUtils.class); |
45 | 45472 | jjdelcerro | |
46 | 46101 | jjdelcerro | 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 | 46112 | omartinez | URL url = this.getClass().getResource("/"); |
56 | if (url == null) { |
||
57 | url = AbstractTestUtils.class.getResource("/");
|
||
58 | } |
||
59 | 46101 | jjdelcerro | 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 | 46112 | omartinez | URL url = this.getClass().getResource(pathname); |
71 | if (url == null) { |
||
72 | url = AbstractTestUtils.class.getResource(pathname); |
||
73 | } |
||
74 | 46101 | jjdelcerro | 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 | 45472 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
109 | 46101 | jjdelcerro | JDBCServerExplorerParameters conn = (JDBCServerExplorerParameters) dataManager.createServerExplorerParameters(H2SPATIAL_PROVIDER_NAME); |
110 | 45472 | jjdelcerro | |
111 | 46101 | jjdelcerro | File dbfile = this.getResource(String.format( |
112 | 45472 | jjdelcerro | "test-dbs/%s-%d-%03d",
|
113 | dbname, |
||
114 | 46101 | jjdelcerro | System.currentTimeMillis(),
|
115 | 45472 | jjdelcerro | dbcounter++ |
116 | 46101 | jjdelcerro | ) |
117 | 45472 | jjdelcerro | ); |
118 | // System.out.println("#### dbfile: " + dbfile.getAbsolutePath());
|
||
119 | FileUtils.forceMkdir(dbfile.getParentFile()); |
||
120 | 46101 | jjdelcerro | ((HasAFile) conn).setFile(dbfile); |
121 | 45472 | jjdelcerro | return conn;
|
122 | } |
||
123 | 46101 | jjdelcerro | |
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 | 46806 | jjdelcerro | x.end_of_item = null;
|
134 | 46101 | jjdelcerro | 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 | 46507 | jjdelcerro | public boolean replace_nl_by_space = false; |
147 | public boolean trim_end = false; |
||
148 | |||
149 | 46101 | jjdelcerro | |
150 | 46106 | jjdelcerro | public String startLineComment = "-- "; |
151 | 46806 | jjdelcerro | public String end_of_item = ";"; |
152 | 46106 | jjdelcerro | |
153 | 46101 | jjdelcerro | 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 | 46507 | jjdelcerro | boolean local_replace_nl_by_space = false; |
190 | 46806 | jjdelcerro | boolean local_trim_end = false; |
191 | 46101 | jjdelcerro | 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 | 46806 | jjdelcerro | } else if (line.toLowerCase().startsWith(startLineComment + "end-of-item")) { |
207 | CommandLine cmd = new CommandLine(line, startLineComment);
|
||
208 | end_of_item = cmd.get(1);
|
||
209 | 46101 | jjdelcerro | } 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 | 46507 | jjdelcerro | } 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 | 46101 | jjdelcerro | } 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 | 46507 | jjdelcerro | local_replace_nl_by_space = replace_nl_by_space; |
230 | local_trim_end = trim_end; |
||
231 | 46101 | jjdelcerro | } 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 | 46507 | jjdelcerro | } 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 | 46101 | jjdelcerro | } 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 | 46507 | jjdelcerro | if( local_trim_end ) {
|
266 | s = StringUtils.stripEnd(s, " ");
|
||
267 | } |
||
268 | 46101 | jjdelcerro | |
269 | List<String> value = expecteds.get(currentItem); |
||
270 | if( value == null ) { |
||
271 | value = new ArrayList<>(); |
||
272 | expecteds.put(currentItem, value); |
||
273 | } |
||
274 | 46806 | jjdelcerro | if( StringUtils.isNotBlank(s) ) {
|
275 | value.add(s); |
||
276 | } |
||
277 | 46101 | jjdelcerro | 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 | 46507 | jjdelcerro | if( local_replace_nl_by_space ) {
|
286 | line = line.replace('\n', ' '); |
||
287 | } |
||
288 | 46101 | jjdelcerro | if (localRemoveNL) {
|
289 | line = StringUtils.remove(line, "\n");
|
||
290 | line = StringUtils.remove(line, "\r");
|
||
291 | } |
||
292 | 46806 | jjdelcerro | 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 | 46101 | jjdelcerro | } |
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 | 47581 | fdiaz | 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 | 46101 | jjdelcerro | 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 | 47581 | fdiaz | 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 | 46101 | jjdelcerro | 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 | 46106 | jjdelcerro | if( resourcesStorage.allowRemove() ) {
|
361 | resourcesStorage.remove(resourceName); |
||
362 | } |
||
363 | 46101 | jjdelcerro | } |
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 | 45472 | jjdelcerro | |
374 | 46101 | jjdelcerro | 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 | 45472 | jjdelcerro | } |
384 | |||
385 | 46101 | jjdelcerro | public void info_jdbc(String label, JDBCServerExplorer explorer) { |
386 | info_jdbc(label, explorer.getParameters()); |
||
387 | 45472 | jjdelcerro | } |
388 | |||
389 | 46101 | jjdelcerro | public void info_jdbc(JDBCServerExplorer explorer) { |
390 | JDBCServerExplorerParameters params = explorer.getParameters(); |
||
391 | info_jdbc(params.getDBName(), params); |
||
392 | 45472 | jjdelcerro | } |
393 | |||
394 | 46101 | jjdelcerro | protected boolean getPropertyBoolean(String name) { |
395 | Properties props = this.getProperties(); |
||
396 | return BooleanUtils.toBoolean(props.getProperty(name));
|
||
397 | 45472 | jjdelcerro | } |
398 | 46101 | jjdelcerro | |
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 | 45472 | jjdelcerro | |
421 | 46101 | jjdelcerro | public boolean isTheDatabaseAvailable() { |
422 | return true; |
||
423 | 45472 | jjdelcerro | } |
424 | |||
425 | 46101 | jjdelcerro | public FeatureStore openCSVStore(String pathname) throws Exception { |
426 | 45472 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
427 | 46101 | jjdelcerro | File f = getResourceAsFile(pathname);
|
428 | 45472 | jjdelcerro | 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 | 46050 | omartinez | |
437 | 46517 | fdiaz | public FeatureStore openCountriesStore() throws Exception { |
438 | return this.openCSVStore("/org/gvsig/fmap/dal/store/jdbc2/countries.csv"); |
||
439 | } |
||
440 | |||
441 | 46101 | jjdelcerro | 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 | 46050 | omartinez | DataManager dataManager = DALLocator.getDataManager(); |
453 | 46101 | jjdelcerro | 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 | 46050 | omartinez | return store;
|
464 | } |
||
465 | 45472 | jjdelcerro | |
466 | 46101 | jjdelcerro | 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 | 47606 | fdiaz | 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 | 45472 | jjdelcerro | } |
506 | } |
||
507 | |||
508 | 46101 | jjdelcerro | 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 | 45472 | jjdelcerro | } |
517 | |||
518 | 47456 | jjdelcerro | 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 | 46105 | omartinez | public void drop_tables(JDBCServerExplorer explorer, String...tables) throws Exception { |
529 | 46101 | jjdelcerro | for (String table : tables) { |
530 | String sql = "DROP TABLE IF EXISTS \""+table+"\""; |
||
531 | explorer.execute(sql); |
||
532 | 46105 | omartinez | removeResource(explorer, table, "dal");
|
533 | 46101 | jjdelcerro | } |
534 | 45472 | jjdelcerro | } |
535 | |||
536 | 46209 | jjdelcerro | public DatabaseWorkspaceManager initWorkspace(String name) throws Exception { |
537 | 46105 | omartinez | 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 | 46209 | jjdelcerro | return ws;
|
548 | 46105 | omartinez | } |
549 | |||
550 | 46112 | omartinez | 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 | 46101 | jjdelcerro | // public abstract String getExpectedResourcesPrefix(); // Ex. "h2spatial"
|
573 | 45472 | jjdelcerro | |
574 | 46101 | jjdelcerro | 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 | 46507 | jjdelcerro | |
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 | 47456 | jjdelcerro | 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 | 46507 | jjdelcerro | |
615 | 45472 | jjdelcerro | } |