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