svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src-test / org / gvsig / data / datastores / vectorial / db / jdbc / JDBCTest.java @ 20973
History | View | Annotate | Download (8.31 KB)
1 |
package org.gvsig.data.datastores.vectorial.db.jdbc; |
---|---|
2 |
|
3 |
import java.util.Iterator; |
4 |
|
5 |
import junit.framework.TestCase; |
6 |
|
7 |
import org.gvsig.data.CloseException; |
8 |
import org.gvsig.data.DataException; |
9 |
import org.gvsig.data.DataManager; |
10 |
import org.gvsig.data.IDataCollection; |
11 |
import org.gvsig.data.IDataStoreParameters; |
12 |
import org.gvsig.data.InitializeException; |
13 |
import org.gvsig.data.OpenException; |
14 |
import org.gvsig.data.ReadException; |
15 |
import org.gvsig.data.Resource; |
16 |
import org.gvsig.data.ResourceManager; |
17 |
import org.gvsig.data.WriteException; |
18 |
import org.gvsig.data.datastores.vectorial.file.FileResource; |
19 |
import org.gvsig.data.vectorial.FeatureStore; |
20 |
import org.gvsig.data.vectorial.IFeature; |
21 |
import org.gvsig.data.vectorial.IFeatureCollection; |
22 |
import org.gvsig.data.vectorial.IFeatureStore; |
23 |
import org.gvsig.data.vectorial.IFeatureType; |
24 |
import org.gvsig.data.vectorial.visitor.PrintlnFeaturesVisitor; |
25 |
import org.gvsig.exceptions.BaseException; |
26 |
import org.gvsig.util.observer.IObserver; |
27 |
|
28 |
public abstract class JDBCTest extends TestCase { |
29 |
|
30 |
public JDBCTest() {
|
31 |
super();
|
32 |
} |
33 |
|
34 |
protected void storeTest(IDataStoreParameters dp, String filter, String order, boolean testEdit) { |
35 |
DataManager dsm=DataManager.getManager(); |
36 |
|
37 |
|
38 |
IFeatureStore fs=null;
|
39 |
try {
|
40 |
fs = (IFeatureStore)dsm.createDataStore(dp); |
41 |
} catch (InitializeException e) {
|
42 |
e.printStackTrace(); |
43 |
fail("Exception:" + e);
|
44 |
} |
45 |
|
46 |
try {
|
47 |
fs.open(); |
48 |
} catch (OpenException e2) {
|
49 |
e2.printStackTrace(); |
50 |
fail(); |
51 |
} |
52 |
|
53 |
if (fs.isEditable() && testEdit) {
|
54 |
|
55 |
try {
|
56 |
fs.startEditing(); |
57 |
} catch (ReadException e) {
|
58 |
e.printStackTrace(); |
59 |
fail(); |
60 |
} |
61 |
try{
|
62 |
IFeature feature1 = fs.createDefaultFeature(false);
|
63 |
feature1.editing(); |
64 |
feature1.set("ID",1); |
65 |
IFeature feature2 = fs.createDefaultFeature(false);
|
66 |
feature2.editing(); |
67 |
feature2.set("ID",2); |
68 |
IFeature feature3 = fs.createDefaultFeature(false);
|
69 |
feature3.editing(); |
70 |
feature3.set("ID",3); |
71 |
|
72 |
|
73 |
fs.insert(feature1); |
74 |
fs.insert(feature2); |
75 |
feature1.editing(); |
76 |
feature1.set(1,111111); |
77 |
fs.update(feature1); |
78 |
fs.delete(feature3); |
79 |
fs.delete(feature2); |
80 |
}catch (DataException e) {
|
81 |
e.printStackTrace(); |
82 |
fail(); |
83 |
} |
84 |
} |
85 |
|
86 |
//Mostrar por consola todos los registros.
|
87 |
IFeatureType ft= fs.getDefaultFeatureType(); |
88 |
IFeatureCollection featureCollection=null;
|
89 |
// featureCollection = (IFeatureCollection)fs.getDataCollection();
|
90 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"NOMBRE = 'CALPE'",null);
|
91 |
// featureCollection = (IFeatureCollection)fs.getDataCollection(ft,"AREA > 3.2213163729E7 and AREA < 3.2213163749E7",null);
|
92 |
try {
|
93 |
featureCollection = (IFeatureCollection)fs.getDataCollection(ft,filter,order); |
94 |
} catch (ReadException e2) {
|
95 |
// TODO Auto-generated catch block
|
96 |
e2.printStackTrace(); |
97 |
} |
98 |
|
99 |
PrintlnFeaturesVisitor visitor=new PrintlnFeaturesVisitor(ft);
|
100 |
try {
|
101 |
featureCollection.accept(visitor); |
102 |
} catch (BaseException e1) {
|
103 |
e1.printStackTrace(); |
104 |
fail("Exception: "+e1);
|
105 |
} |
106 |
featureCollection.dispose(); |
107 |
|
108 |
if (fs.isEditable() && testEdit){
|
109 |
try {
|
110 |
fs.finishEditing(); |
111 |
// fs.cancelEditing();
|
112 |
} catch (WriteException e) {
|
113 |
e.printStackTrace(); |
114 |
fail("Exception: "+e);
|
115 |
} catch (ReadException e) {
|
116 |
e.printStackTrace(); |
117 |
fail("Exception: "+e);
|
118 |
} |
119 |
} |
120 |
try {
|
121 |
fs.close(); |
122 |
} catch (CloseException e) {
|
123 |
e.printStackTrace(); |
124 |
fail("Exception: "+e);
|
125 |
} |
126 |
try {
|
127 |
fs.dispose(); |
128 |
} catch (CloseException e) {
|
129 |
// TODO Auto-generated catch block
|
130 |
e.printStackTrace(); |
131 |
} |
132 |
} |
133 |
|
134 |
public static void doFileResourceTest(JDBCStoreParameters params){ |
135 |
doFileResourceTest(params,true);
|
136 |
} |
137 |
|
138 |
public static void doFileResourceTest(JDBCStoreParameters params,boolean testEdit){ |
139 |
DataManager manager = DataManager.getManager(); |
140 |
|
141 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
142 |
|
143 |
FeatureStore store=null;
|
144 |
FeatureStore store2=null;
|
145 |
FeatureStore store3=null;
|
146 |
try {
|
147 |
store = (FeatureStore)manager.createDataStore(params); |
148 |
store2 = (FeatureStore)manager.createDataStore(params); |
149 |
store3 = (FeatureStore)manager.createDataStore(params); |
150 |
} catch (InitializeException e1) {
|
151 |
e1.printStackTrace();fail(); |
152 |
} |
153 |
|
154 |
int i=0; |
155 |
Resource res = null;
|
156 |
Resource tmpRes = null;
|
157 |
Object obj = null; |
158 |
|
159 |
Iterator iter = resMan.iterator();
|
160 |
while (iter.hasNext()){
|
161 |
obj = iter.next(); |
162 |
if (obj instanceof JDBCResource){ |
163 |
tmpRes = (Resource)obj; |
164 |
if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
|
165 |
i++; |
166 |
res=tmpRes; |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
assertEquals(1, i);
|
172 |
|
173 |
assertEquals(3, res.getRefencesCount());
|
174 |
|
175 |
try {
|
176 |
store.close(); |
177 |
} catch (CloseException e1) {
|
178 |
e1.printStackTrace();fail(); |
179 |
} |
180 |
|
181 |
assertEquals(false, res.isOpen());
|
182 |
|
183 |
IDataCollection coll = null;
|
184 |
|
185 |
try {
|
186 |
coll =store.getDataCollection(); |
187 |
} catch (ReadException e1) {
|
188 |
e1.printStackTrace();fail(); |
189 |
} |
190 |
|
191 |
coll.iterator().next(); |
192 |
|
193 |
assertEquals(true, res.isOpen());
|
194 |
|
195 |
coll.dispose(); |
196 |
|
197 |
|
198 |
|
199 |
if (store.isEditable() && testEdit){
|
200 |
/*Test edition notification*/
|
201 |
|
202 |
int fCountOrg=0; |
203 |
int fCountFin=0; |
204 |
try {
|
205 |
fCountOrg = store.getDataCollection().size(); |
206 |
} catch (ReadException e2) {
|
207 |
// TODO Auto-generated catch block
|
208 |
e2.printStackTrace();fail(); |
209 |
} |
210 |
try {
|
211 |
store.startEditing(); |
212 |
} catch (ReadException e1) {
|
213 |
e1.printStackTrace();fail(); |
214 |
} |
215 |
|
216 |
try {
|
217 |
coll = store2.getDataCollection(); |
218 |
} catch (ReadException e1) {
|
219 |
e1.printStackTrace();fail(); |
220 |
} |
221 |
|
222 |
try {
|
223 |
store.finishEditing(); |
224 |
} catch (WriteException e1) {
|
225 |
e1.printStackTrace();fail(); |
226 |
} catch (ReadException e1) {
|
227 |
// TODO Auto-generated catch block
|
228 |
e1.printStackTrace();fail(); |
229 |
} |
230 |
|
231 |
try {
|
232 |
fCountFin = store.getDataCollection().size(); |
233 |
} catch (ReadException e2) {
|
234 |
// TODO Auto-generated catch block
|
235 |
e2.printStackTrace();fail(); |
236 |
} |
237 |
|
238 |
assertEquals(fCountOrg, fCountFin); |
239 |
|
240 |
|
241 |
boolean isOk = false; |
242 |
try{
|
243 |
coll.iterator().next(); |
244 |
} catch (Exception e){ |
245 |
isOk=true;
|
246 |
} |
247 |
assertTrue("Resource Changed Notification fails",isOk);
|
248 |
|
249 |
coll.dispose(); |
250 |
|
251 |
/*Test edition notification END*/
|
252 |
|
253 |
} |
254 |
|
255 |
|
256 |
JDBCExplorer explorer = null;
|
257 |
try {
|
258 |
explorer = (JDBCExplorer) store.getExplorer(); |
259 |
} catch (ReadException e) {
|
260 |
e.printStackTrace();fail(); |
261 |
} |
262 |
|
263 |
if (explorer != null){ |
264 |
assertEquals(4, res.getRefencesCount());
|
265 |
|
266 |
try {
|
267 |
explorer.list(); |
268 |
} catch (ReadException e) {
|
269 |
e.printStackTrace();fail(); |
270 |
} |
271 |
|
272 |
try {
|
273 |
explorer.dispose(); |
274 |
} catch (DataException e1) {
|
275 |
e1.printStackTrace();fail(); |
276 |
} |
277 |
|
278 |
|
279 |
assertEquals(3, res.getRefencesCount());
|
280 |
} |
281 |
try {
|
282 |
store3.dispose(); |
283 |
} catch (CloseException e1) {
|
284 |
e1.printStackTrace();fail(); |
285 |
} |
286 |
|
287 |
assertEquals(2, res.getRefencesCount());
|
288 |
|
289 |
try {
|
290 |
store2.dispose(); |
291 |
} catch (CloseException e1) {
|
292 |
e1.printStackTrace();fail(); |
293 |
} |
294 |
|
295 |
assertEquals(1, res.getRefencesCount());
|
296 |
|
297 |
try {
|
298 |
store.dispose(); |
299 |
} catch (CloseException e1) {
|
300 |
e1.printStackTrace();fail(); |
301 |
} |
302 |
|
303 |
assertEquals(0, res.getRefencesCount());
|
304 |
res = null;
|
305 |
|
306 |
i=0;
|
307 |
iter = resMan.iterator(); |
308 |
while (iter.hasNext()){
|
309 |
obj = iter.next(); |
310 |
if (obj instanceof FileResource){ |
311 |
tmpRes = (Resource)obj; |
312 |
if (((JDBCResource)tmpRes).getUrl().equals(params.getUrl())){
|
313 |
i++; |
314 |
res=tmpRes; |
315 |
} |
316 |
} |
317 |
} |
318 |
|
319 |
assertEquals(0, i);
|
320 |
|
321 |
doPrepareFileResourceTest(params); |
322 |
|
323 |
} |
324 |
|
325 |
public static void doPrepareFileResourceTest(JDBCStoreParameters params){ |
326 |
DataManager manager = DataManager.getManager(); |
327 |
|
328 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
329 |
|
330 |
String passw = params.getPassw();
|
331 |
params.setPassw("--------------");
|
332 |
|
333 |
IObserver obs = new PrepareResourceObserver(params.getUrl(), passw);
|
334 |
|
335 |
|
336 |
resMan.addObserver(obs); |
337 |
|
338 |
FeatureStore store=null;
|
339 |
try {
|
340 |
store = (FeatureStore)manager.createDataStore(params); |
341 |
} catch (InitializeException e1) {
|
342 |
e1.printStackTrace();fail(); |
343 |
} catch (Exception e2){ |
344 |
e2.printStackTrace();fail(); |
345 |
} |
346 |
|
347 |
try {
|
348 |
store.getDataCollection().iterator().next(); |
349 |
} catch (ReadException e) {
|
350 |
// TODO Auto-generated catch block
|
351 |
e.printStackTrace();fail(); |
352 |
} |
353 |
|
354 |
try {
|
355 |
store.close(); |
356 |
} catch (CloseException e) {
|
357 |
// TODO Auto-generated catch block
|
358 |
e.printStackTrace();fail(); |
359 |
} |
360 |
try {
|
361 |
store.dispose(); |
362 |
} catch (CloseException e) {
|
363 |
// TODO Auto-generated catch block
|
364 |
e.printStackTrace();fail(); |
365 |
} |
366 |
params.setPassw(passw); |
367 |
|
368 |
} |
369 |
|
370 |
} |