root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / ResourceManagerTest.java @ 22958
History | View | Annotate | Download (7.37 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.util.Comparator; |
31 |
import java.util.Iterator; |
32 |
import java.util.SortedSet; |
33 |
import java.util.TreeSet; |
34 |
|
35 |
import junit.framework.TestCase; |
36 |
|
37 |
import org.gvsig.exceptions.BaseException; |
38 |
import org.gvsig.fmap.data.CloseException; |
39 |
import org.gvsig.fmap.data.DataCollection; |
40 |
import org.gvsig.fmap.data.DataManager; |
41 |
import org.gvsig.fmap.data.DataStore; |
42 |
import org.gvsig.fmap.data.Resource; |
43 |
import org.gvsig.fmap.data.ResourceManager; |
44 |
import org.gvsig.fmap.data.ResourceNotification; |
45 |
import org.gvsig.fmap.data.feature.file.shp.SHPTest; |
46 |
import org.gvsig.util.observer.Observable; |
47 |
import org.gvsig.util.observer.Observer; |
48 |
|
49 |
public class ResourceManagerTest extends TestCase { |
50 |
|
51 |
private DataManager dataManager = DataManager.getManager();
|
52 |
|
53 |
public class ResourceCloser implements Observer { |
54 |
private int maxOpened = 3; |
55 |
|
56 |
public void update(Observable observable, Object notification) { |
57 |
ResourceNotification rNotification; |
58 |
if (notification instanceof ResourceNotification) { |
59 |
rNotification = (ResourceNotification) notification; |
60 |
} else {
|
61 |
return;
|
62 |
} |
63 |
if (!rNotification.getType().equals(ResourceNotification.OPENED)) {
|
64 |
try {
|
65 |
this.ajustResources();
|
66 |
} catch (CloseException e) {
|
67 |
e.printStackTrace(); |
68 |
} |
69 |
|
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
|
75 |
public void ajustResources() throws CloseException { |
76 |
SortedSet resources = this.getResourcesOrdered(); |
77 |
if (resources.isEmpty() || resources.size() < this.maxOpened) { |
78 |
return;
|
79 |
} |
80 |
|
81 |
int opened = 0; |
82 |
Iterator iter = resources.iterator();
|
83 |
while (iter.hasNext()) {
|
84 |
if (((Resource) iter.next()).isOpen()) {
|
85 |
opened++; |
86 |
} |
87 |
} |
88 |
int toClose = opened - this.maxOpened; |
89 |
if (toClose < 1) { |
90 |
return;
|
91 |
} |
92 |
System.out.println("** Resources to close: " + toClose); |
93 |
iter = resources.iterator(); |
94 |
while (iter.hasNext() && toClose > 0) { |
95 |
Resource res = (Resource) iter.next(); |
96 |
if (res.isOpen()) {
|
97 |
res.close(); |
98 |
toClose--; |
99 |
System.out.println("** closed: " + res.getName() + "(" |
100 |
+ res.getKey() + ")");
|
101 |
} |
102 |
|
103 |
} |
104 |
} |
105 |
|
106 |
public Comparator getResourceComparator() { |
107 |
Comparator resourceComparator = new Comparator(){ |
108 |
|
109 |
public int compare(Object arg0, Object arg1) { |
110 |
Resource res0=(Resource) arg0; |
111 |
Resource res1=(Resource) arg1; |
112 |
if (res0.isOpen() != res1.isOpen()){
|
113 |
if (res0.isOpen()){
|
114 |
return -1; |
115 |
} else{
|
116 |
return 1; |
117 |
} |
118 |
} else{
|
119 |
if (res0.getLastTimeUsed().before(res1.getLastTimeUsed())){
|
120 |
return -1; |
121 |
} else{
|
122 |
return 1; |
123 |
} |
124 |
} |
125 |
} |
126 |
}; |
127 |
|
128 |
return resourceComparator;
|
129 |
} |
130 |
|
131 |
public SortedSet getResourcesOrdered() { |
132 |
SortedSet set = new TreeSet(this.getResourceComparator()); |
133 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
134 |
Iterator iter = resManager.iterator();
|
135 |
|
136 |
while (iter.hasNext()) {
|
137 |
set.add(iter.next()); |
138 |
} |
139 |
return set;
|
140 |
} |
141 |
|
142 |
|
143 |
public void setMaxOpened(int maxOpened) { |
144 |
this.maxOpened = maxOpened;
|
145 |
} |
146 |
|
147 |
public int getMaxOpened() { |
148 |
return maxOpened;
|
149 |
} |
150 |
|
151 |
public int getOpenedResources() { |
152 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
153 |
Iterator iter = resManager.iterator();
|
154 |
int opened = 0; |
155 |
|
156 |
while (iter.hasNext()) {
|
157 |
if (((Resource) iter.next()).isOpen()) {
|
158 |
opened++; |
159 |
} |
160 |
} |
161 |
|
162 |
return opened;
|
163 |
} |
164 |
|
165 |
public void sys_out_resources() { |
166 |
ResourceManager resManager = ResourceManager.getResourceManager(); |
167 |
Iterator iter = resManager.iterator();
|
168 |
int opened = 0; |
169 |
|
170 |
while (iter.hasNext()) {
|
171 |
Resource res = (Resource) iter.next(); |
172 |
System.out.print("- Resource: " + res.getName() + "(" |
173 |
+ res.getKey() + ") ");
|
174 |
if (res.isOpen()) {
|
175 |
System.out.println(" [Open]"); |
176 |
} else {
|
177 |
System.out.println(" [Close]"); |
178 |
} |
179 |
} |
180 |
|
181 |
} |
182 |
|
183 |
|
184 |
} |
185 |
|
186 |
|
187 |
public void test_manual() { |
188 |
ResourceCloser closer = new ResourceCloser();
|
189 |
|
190 |
|
191 |
org.gvsig.fmap.data.feature.file.dbf.Register.selfRegister(); |
192 |
org.gvsig.fmap.data.feature.file.shp.Register.selfRegister(); |
193 |
org.gvsig.fmap.data.feature.file.dxf.Register.selfRegister(); |
194 |
org.gvsig.fmap.data.feature.file.dgn.Register.selfRegister(); |
195 |
|
196 |
DataStore shp = null;
|
197 |
DataStore shp_bis = null;
|
198 |
DataStore shpNull = null;
|
199 |
DataStore dbf = null;
|
200 |
|
201 |
|
202 |
// Abrimos y cerrarmos uno (ojo los shp son dos recursos el dbf y el shp)
|
203 |
try {
|
204 |
shp = dataManager.createDataStore(SHPTest |
205 |
.getParams(SHPTest.fileNormal)); |
206 |
shp.getMetadata(); |
207 |
} catch (BaseException e) {
|
208 |
e.printStackTrace(); |
209 |
fail(); |
210 |
} |
211 |
assertEquals(2, closer.getOpenedResources());
|
212 |
try {
|
213 |
shp.dispose(); |
214 |
} catch (CloseException e) {
|
215 |
e.printStackTrace(); |
216 |
fail(); |
217 |
} |
218 |
assertEquals(0, closer.getOpenedResources());
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
// Abrimos dos veces el mismo y comprobamos que solo
|
224 |
// hay un recurso
|
225 |
try {
|
226 |
shp = dataManager.createDataStore(SHPTest |
227 |
.getParams(SHPTest.fileNormal)); |
228 |
shp.getMetadata(); |
229 |
} catch (BaseException e) {
|
230 |
e.printStackTrace(); |
231 |
fail(); |
232 |
} |
233 |
|
234 |
assertEquals(2, closer.getOpenedResources());
|
235 |
try {
|
236 |
shp_bis = dataManager.createDataStore(SHPTest |
237 |
.getParams(SHPTest.fileNormal)); |
238 |
shp_bis.getMetadata(); |
239 |
} catch (BaseException e) {
|
240 |
e.printStackTrace(); |
241 |
fail(); |
242 |
} |
243 |
assertEquals(2, closer.getOpenedResources());
|
244 |
|
245 |
// Abrimos un shp mas, llamamos al 'ajuste del closer' y comprobamos si deja solo 2
|
246 |
try {
|
247 |
shpNull = dataManager.createDataStore(SHPTest |
248 |
.getParams(SHPTest.fileGeometryNull)); |
249 |
shpNull.getMetadata(); |
250 |
} catch (BaseException e) {
|
251 |
e.printStackTrace(); |
252 |
fail(); |
253 |
} |
254 |
assertEquals(4, closer.getOpenedResources());
|
255 |
|
256 |
closer.setMaxOpened(2);
|
257 |
try {
|
258 |
closer.ajustResources(); |
259 |
} catch (CloseException e) {
|
260 |
e.printStackTrace(); |
261 |
fail(); |
262 |
} |
263 |
assertEquals(2, closer.getOpenedResources());
|
264 |
|
265 |
//comprobamos que todos se puede volver a usar sin
|
266 |
//problemas
|
267 |
|
268 |
DataCollection coll; |
269 |
try {
|
270 |
coll = shp.getDataCollection(); |
271 |
coll.size(); |
272 |
coll.iterator().next(); |
273 |
coll.dispose(); |
274 |
|
275 |
coll = shp_bis.getDataCollection(); |
276 |
coll.size(); |
277 |
coll.iterator().next(); |
278 |
coll.dispose(); |
279 |
|
280 |
coll = shpNull.getDataCollection(); |
281 |
coll.size(); |
282 |
coll.iterator().next(); |
283 |
coll.dispose(); |
284 |
|
285 |
} catch (BaseException e) {
|
286 |
e.printStackTrace(); |
287 |
fail(); |
288 |
} |
289 |
|
290 |
//ahora deberian estar todos abiertos (4)
|
291 |
closer.sys_out_resources(); |
292 |
assertEquals(4, closer.getOpenedResources());
|
293 |
|
294 |
//volvemos a comprobar que funciona
|
295 |
closer.setMaxOpened(2);
|
296 |
try {
|
297 |
closer.ajustResources(); |
298 |
} catch (CloseException e) {
|
299 |
e.printStackTrace(); |
300 |
fail(); |
301 |
} |
302 |
assertEquals(2, closer.getOpenedResources());
|
303 |
|
304 |
|
305 |
} |
306 |
|
307 |
|
308 |
|
309 |
} |