svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / ResourceManagerTest.java @ 22967
History | View | Annotate | Download (11 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 IVER T.I. S.A. {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.data.feature.file; |
29 |
|
30 |
import java.security.KeyException; |
31 |
import java.util.Comparator; |
32 |
import java.util.Iterator; |
33 |
import java.util.SortedSet; |
34 |
import java.util.TreeSet; |
35 |
|
36 |
import junit.framework.TestCase; |
37 |
|
38 |
import org.gvsig.exceptions.BaseException; |
39 |
import org.gvsig.fmap.data.CloseException; |
40 |
import org.gvsig.fmap.data.DataCollection; |
41 |
import org.gvsig.fmap.data.DataException; |
42 |
import org.gvsig.fmap.data.DataManager; |
43 |
import org.gvsig.fmap.data.DataStore; |
44 |
import org.gvsig.fmap.data.Resource; |
45 |
import org.gvsig.fmap.data.ResourceManager; |
46 |
import org.gvsig.fmap.data.ResourceNotification; |
47 |
import org.gvsig.fmap.data.feature.file.shp.SHPTest; |
48 |
import org.gvsig.util.observer.Observable; |
49 |
import org.gvsig.util.observer.Observer; |
50 |
|
51 |
public class ResourceManagerTest extends TestCase { |
52 |
|
53 |
private DataManager dataManager = DataManager.getManager();
|
54 |
|
55 |
public class ResourceCloser implements Observer { |
56 |
private int maxOpened = 3; |
57 |
|
58 |
public void update(Observable observable, Object notification) { |
59 |
ResourceNotification rNotification; |
60 |
if (notification instanceof ResourceNotification) { |
61 |
rNotification = (ResourceNotification) notification; |
62 |
} else {
|
63 |
return;
|
64 |
} |
65 |
if (rNotification.getType().equals(ResourceNotification.OPENED)) {
|
66 |
try {
|
67 |
this.ajustResources();
|
68 |
} catch (CloseException e) {
|
69 |
e.printStackTrace(); |
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
public void closeAndDisposeAll() throws KeyException, DataException { |
77 |
ResourceManager rManager = ResourceManager.getResourceManager(); |
78 |
|
79 |
Iterator iter = rManager.iterator();
|
80 |
String name, key;
|
81 |
while (iter.hasNext()) {
|
82 |
Resource res = (Resource) iter.next(); |
83 |
name = res.getName(); |
84 |
key = res.getKey(); |
85 |
if (res.isOpen()) {
|
86 |
res.close(); |
87 |
System.out.println("** closed: " + name + "(" + key + ")"); |
88 |
} |
89 |
|
90 |
rManager.remove(res); |
91 |
System.out.println("** removed: " + name + "(" + key + ")"); |
92 |
|
93 |
iter = rManager.iterator(); |
94 |
} |
95 |
|
96 |
System.out.println("** Closed and Disposed all resources"); |
97 |
} |
98 |
|
99 |
|
100 |
public void ajustResources() throws CloseException { |
101 |
SortedSet resources = this.getResourcesOrdered(); |
102 |
if (resources.isEmpty() || resources.size() < this.maxOpened) { |
103 |
return;
|
104 |
} |
105 |
|
106 |
int opened = this.getOpenedResources(); |
107 |
int toClose = opened - this.maxOpened; |
108 |
if (toClose < 1) { |
109 |
return;
|
110 |
} |
111 |
System.out.println("** Resources to close: " + toClose); |
112 |
Iterator iter = resources.iterator();
|
113 |
while (iter.hasNext() && toClose > 0) { |
114 |
Resource res = (Resource) iter.next(); |
115 |
if (res.isOpen()) {
|
116 |
res.close(); |
117 |
toClose--; |
118 |
System.out.println("** closed: " + res.getName() + "(" |
119 |
+ res.getKey() + ")");
|
120 |
} |
121 |
|
122 |
} |
123 |
} |
124 |
|
125 |
public Comparator getResourceComparator() { |
126 |
Comparator resourceComparator = new Comparator(){ |
127 |
|
128 |
public int compare(Object arg0, Object arg1) { |
129 |
Resource res0=(Resource) arg0; |
130 |
Resource res1=(Resource) arg1; |
131 |
if (res0.isOpen() != res1.isOpen()){
|
132 |
if (res0.isOpen()){
|
133 |
return -1; |
134 |
} else{
|
135 |
return 1; |
136 |
} |
137 |
} else{
|
138 |
if (res0.getLastTimeUsed().before(res1.getLastTimeUsed())){
|
139 |
return -1; |
140 |
} else{
|
141 |
return 1; |
142 |
} |
143 |
} |
144 |
} |
145 |
}; |
146 |
|
147 |
return resourceComparator;
|
148 |
} |
149 |
|
150 |
public SortedSet getResourcesOrdered() { |
151 |
SortedSet set = new TreeSet(this.getResourceComparator()); |
152 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
153 |
Iterator iter = resManager.iterator();
|
154 |
|
155 |
while (iter.hasNext()) {
|
156 |
set.add(iter.next()); |
157 |
} |
158 |
return set;
|
159 |
} |
160 |
|
161 |
|
162 |
public void setMaxOpened(int maxOpened) { |
163 |
this.maxOpened = maxOpened;
|
164 |
} |
165 |
|
166 |
public int getMaxOpened() { |
167 |
return maxOpened;
|
168 |
} |
169 |
|
170 |
public int getOpenedResources() { |
171 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
172 |
Iterator iter = resManager.iterator();
|
173 |
int opened = 0; |
174 |
|
175 |
while (iter.hasNext()) {
|
176 |
if (((Resource) iter.next()).isOpen()) {
|
177 |
opened++; |
178 |
} |
179 |
} |
180 |
|
181 |
return opened;
|
182 |
} |
183 |
|
184 |
public void sys_out_resources() { |
185 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
186 |
Iterator iter = resManager.iterator();
|
187 |
while (iter.hasNext()) {
|
188 |
Resource res = (Resource) iter.next(); |
189 |
System.out.print("- Resource: " + res.getName() + "(" |
190 |
+ res.getKey() + ") ");
|
191 |
if (res.isOpen()) {
|
192 |
System.out.println(" [Open]"); |
193 |
} else {
|
194 |
System.out.println(" [Close]"); |
195 |
} |
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
|
201 |
} |
202 |
|
203 |
|
204 |
public void test_manual() { |
205 |
ResourceCloser closer = new ResourceCloser();
|
206 |
|
207 |
//Nos aseguramos que no hay recursos abiertos
|
208 |
try {
|
209 |
closer.closeAndDisposeAll(); |
210 |
} catch (Exception e1) { |
211 |
e1.printStackTrace(); |
212 |
fail(); |
213 |
} |
214 |
|
215 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
216 |
org.gvsig.fmap.data.feature.file.shp.Register.selfRegister(); |
217 |
org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister(); |
218 |
org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister(); |
219 |
|
220 |
DataStore shp = null;
|
221 |
DataStore shp_bis = null;
|
222 |
DataStore shpNull = null;
|
223 |
// DataStore dbf = null;
|
224 |
|
225 |
|
226 |
// Abrimos y cerrarmos uno (ojo los shp son dos recursos el dbf y el shp)
|
227 |
try {
|
228 |
shp = dataManager.createDataStore(SHPTest |
229 |
.getParams(SHPTest.fileNormal)); |
230 |
shp.getMetadata(); |
231 |
} catch (BaseException e) {
|
232 |
e.printStackTrace(); |
233 |
fail(); |
234 |
} |
235 |
assertEquals(2, closer.getOpenedResources());
|
236 |
try {
|
237 |
shp.dispose(); |
238 |
} catch (CloseException e) {
|
239 |
e.printStackTrace(); |
240 |
fail(); |
241 |
} |
242 |
assertEquals(0, closer.getOpenedResources());
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
// Abrimos dos veces el mismo y comprobamos que solo
|
248 |
// hay un recurso
|
249 |
try {
|
250 |
shp = dataManager.createDataStore(SHPTest |
251 |
.getParams(SHPTest.fileNormal)); |
252 |
shp.getMetadata(); |
253 |
} catch (BaseException e) {
|
254 |
e.printStackTrace(); |
255 |
fail(); |
256 |
} |
257 |
|
258 |
assertEquals(2, closer.getOpenedResources());
|
259 |
try {
|
260 |
shp_bis = dataManager.createDataStore(SHPTest |
261 |
.getParams(SHPTest.fileNormal)); |
262 |
shp_bis.getMetadata(); |
263 |
} catch (BaseException e) {
|
264 |
e.printStackTrace(); |
265 |
fail(); |
266 |
} |
267 |
assertEquals(2, closer.getOpenedResources());
|
268 |
|
269 |
// Abrimos un shp mas, llamamos al 'ajuste del closer' y comprobamos si deja solo 2
|
270 |
try {
|
271 |
shpNull = dataManager.createDataStore(SHPTest |
272 |
.getParams(SHPTest.fileGeometryNull)); |
273 |
shpNull.getMetadata(); |
274 |
} catch (BaseException e) {
|
275 |
e.printStackTrace(); |
276 |
fail(); |
277 |
} |
278 |
assertEquals(4, closer.getOpenedResources());
|
279 |
|
280 |
closer.setMaxOpened(2);
|
281 |
try {
|
282 |
closer.ajustResources(); |
283 |
} catch (CloseException e) {
|
284 |
e.printStackTrace(); |
285 |
fail(); |
286 |
} |
287 |
assertEquals(2, closer.getOpenedResources());
|
288 |
|
289 |
//comprobamos que todos se puede volver a usar sin
|
290 |
//problemas
|
291 |
|
292 |
DataCollection coll; |
293 |
try {
|
294 |
coll = shp.getDataCollection(); |
295 |
coll.size(); |
296 |
coll.iterator().next(); |
297 |
coll.dispose(); |
298 |
|
299 |
coll = shp_bis.getDataCollection(); |
300 |
coll.size(); |
301 |
coll.iterator().next(); |
302 |
coll.dispose(); |
303 |
|
304 |
coll = shpNull.getDataCollection(); |
305 |
coll.size(); |
306 |
coll.iterator().next(); |
307 |
coll.dispose(); |
308 |
|
309 |
} catch (BaseException e) {
|
310 |
e.printStackTrace(); |
311 |
fail(); |
312 |
} |
313 |
|
314 |
//ahora deberian estar todos abiertos (4)
|
315 |
closer.sys_out_resources(); |
316 |
assertEquals(4, closer.getOpenedResources());
|
317 |
|
318 |
//volvemos a comprobar que funciona
|
319 |
closer.setMaxOpened(2);
|
320 |
try {
|
321 |
closer.ajustResources(); |
322 |
} catch (CloseException e) {
|
323 |
e.printStackTrace(); |
324 |
fail(); |
325 |
} |
326 |
assertEquals(2, closer.getOpenedResources());
|
327 |
|
328 |
try {
|
329 |
shp.dispose(); |
330 |
shp_bis.dispose(); |
331 |
shpNull.dispose(); |
332 |
} catch (CloseException e) {
|
333 |
e.printStackTrace(); |
334 |
fail(); |
335 |
} |
336 |
|
337 |
|
338 |
} |
339 |
|
340 |
|
341 |
|
342 |
public void test_auto() { |
343 |
ResourceCloser closer = new ResourceCloser();
|
344 |
|
345 |
//Nos aseguramos que no hay recursos abiertos
|
346 |
try {
|
347 |
closer.closeAndDisposeAll(); |
348 |
} catch (Exception e1) { |
349 |
e1.printStackTrace(); |
350 |
fail(); |
351 |
} |
352 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
353 |
org.gvsig.fmap.data.feature.file.shp.Register.selfRegister(); |
354 |
org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister(); |
355 |
org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister(); |
356 |
|
357 |
DataStore shp = null;
|
358 |
DataStore shp_bis = null;
|
359 |
DataStore shpNull = null;
|
360 |
//DataStore dbf = null;
|
361 |
|
362 |
// A?adimos el 'closer' como observador para que no deje abierta
|
363 |
// nunca mas de 2 fuentes
|
364 |
ResourceManager rManager = ResourceManager.getResourceManager(); |
365 |
closer.setMaxOpened(2);
|
366 |
rManager.addObserver(closer); |
367 |
|
368 |
// Abrimos y cerrarmos uno (ojo los shp son dos recursos el dbf y el shp)
|
369 |
try {
|
370 |
shp = dataManager.createDataStore(SHPTest |
371 |
.getParams(SHPTest.fileNormal)); |
372 |
shp.getMetadata(); |
373 |
} catch (BaseException e) {
|
374 |
e.printStackTrace(); |
375 |
fail(); |
376 |
} |
377 |
assertEquals(2, closer.getOpenedResources());
|
378 |
try {
|
379 |
shp.dispose(); |
380 |
} catch (CloseException e) {
|
381 |
e.printStackTrace(); |
382 |
fail(); |
383 |
} |
384 |
assertEquals(0, closer.getOpenedResources());
|
385 |
|
386 |
// Abrimos dos veces el mismo y comprobamos que solo
|
387 |
// hay un recurso
|
388 |
try {
|
389 |
shp = dataManager.createDataStore(SHPTest |
390 |
.getParams(SHPTest.fileNormal)); |
391 |
shp.getMetadata(); |
392 |
} catch (BaseException e) {
|
393 |
e.printStackTrace(); |
394 |
fail(); |
395 |
} |
396 |
|
397 |
assertEquals(2, closer.getOpenedResources());
|
398 |
try {
|
399 |
shp_bis = dataManager.createDataStore(SHPTest |
400 |
.getParams(SHPTest.fileNormal)); |
401 |
shp_bis.getMetadata(); |
402 |
} catch (BaseException e) {
|
403 |
e.printStackTrace(); |
404 |
fail(); |
405 |
} |
406 |
assertEquals(2, closer.getOpenedResources());
|
407 |
|
408 |
// Abrimos un shp mas y comprobamos si deja solo 2
|
409 |
try {
|
410 |
shpNull = dataManager.createDataStore(SHPTest |
411 |
.getParams(SHPTest.fileGeometryNull)); |
412 |
shpNull.getMetadata(); |
413 |
} catch (BaseException e) {
|
414 |
e.printStackTrace(); |
415 |
fail(); |
416 |
} |
417 |
assertEquals(2, closer.getOpenedResources());
|
418 |
|
419 |
//comprobamos que todos se puede volver a usar sin
|
420 |
//problemas
|
421 |
|
422 |
DataCollection coll; |
423 |
try {
|
424 |
coll = shp.getDataCollection(); |
425 |
coll.size(); |
426 |
coll.iterator().next(); |
427 |
coll.dispose(); |
428 |
assertEquals(2, closer.getOpenedResources());
|
429 |
|
430 |
coll = shp_bis.getDataCollection(); |
431 |
coll.size(); |
432 |
coll.iterator().next(); |
433 |
coll.dispose(); |
434 |
assertEquals(2, closer.getOpenedResources());
|
435 |
|
436 |
coll = shpNull.getDataCollection(); |
437 |
coll.size(); |
438 |
coll.iterator().next(); |
439 |
coll.dispose(); |
440 |
assertEquals(2, closer.getOpenedResources());
|
441 |
|
442 |
} catch (BaseException e) {
|
443 |
e.printStackTrace(); |
444 |
fail(); |
445 |
} |
446 |
|
447 |
assertEquals(2, closer.getOpenedResources());
|
448 |
|
449 |
} |
450 |
} |