svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / test / java / org / gvsig / remoteclient / taskplanning / retrieving / URLRetrieveTest.java @ 40769
History | View | Annotate | Download (7.79 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.remoteclient.taskplanning.retrieving; |
||
25 | |||
26 | import java.io.File; |
||
27 | |||
28 | import junit.framework.TestCase; |
||
29 | |||
30 | import org.gvsig.remoteclient.taskplanning.IQueue; |
||
31 | import org.gvsig.remoteclient.taskplanning.IRunnableTask; |
||
32 | /**
|
||
33 | *
|
||
34 | * @author jaume
|
||
35 | *
|
||
36 | */
|
||
37 | public class URLRetrieveTest extends TestCase { |
||
38 | private final static String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"tmp-andami"; |
||
39 | IQueue queue; |
||
40 | IRunnableTask task1, task2, task3, task4; |
||
41 | boolean finished1, finished2, finished3, finished4;
|
||
42 | RetrieveListener listener1 = new RetrieveListener() {
|
||
43 | public void transferEventReceived(RetrieveEvent event) { |
||
44 | int e = event.getType();
|
||
45 | printMessage("listener1", event.getType());
|
||
46 | if (e==RetrieveEvent.REQUEST_FINISHED || e == RetrieveEvent.REQUEST_FAILED || e == RetrieveEvent.REQUEST_CANCELLED )
|
||
47 | finished1 = true;
|
||
48 | } |
||
49 | }; |
||
50 | |||
51 | String protocol = URLRequest.HTTP;
|
||
52 | String host1 = "192.168.0.223"; |
||
53 | String page1 = "cgi-bin/mapserv_46.exe?map=c:\\ms4w\\Apache\\cgi-bin\\demo.map&&REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&LAYERS=comunidades&SRS=EPSG:23030&BBOX=507522.0,4154976.282477341,942309.0,4552983.717522658&WIDTH=332&HEIGHT=305&FORMAT=image/png&STYLES=&TRANSPARENT=TRUE"; |
||
54 | URLRequest request1; |
||
55 | |||
56 | String host2 = "localhost"; |
||
57 | String page2 = "aegCapabilities1.3.xml"; |
||
58 | URLRequest request2; |
||
59 | RetrieveListener listener2 = new RetrieveListener() {
|
||
60 | public void transferEventReceived(RetrieveEvent event) { |
||
61 | int e = event.getType();
|
||
62 | printMessage("listener2", event.getType());
|
||
63 | if (e==RetrieveEvent.REQUEST_FINISHED || e == RetrieveEvent.REQUEST_FAILED || e==RetrieveEvent.REQUEST_CANCELLED )
|
||
64 | finished2 = true;
|
||
65 | |||
66 | } |
||
67 | }; |
||
68 | |||
69 | String host3 = "localhost"; |
||
70 | String page3 = "avalencia.ecw"; |
||
71 | URLRequest request3; |
||
72 | RetrieveListener listener3 = new RetrieveListener() {
|
||
73 | public void transferEventReceived(RetrieveEvent event) { |
||
74 | int e = event.getType();
|
||
75 | printMessage("listener3", event.getType());
|
||
76 | if (e==RetrieveEvent.REQUEST_FINISHED || e == RetrieveEvent.REQUEST_FAILED || e == RetrieveEvent.REQUEST_CANCELLED )
|
||
77 | finished3 = true;
|
||
78 | |||
79 | } |
||
80 | }; |
||
81 | |||
82 | String host4 = "192.168.0.223"; |
||
83 | String page4 = "prueba.rar"; |
||
84 | URLRequest request4; |
||
85 | RetrieveListener listener4 = new RetrieveListener() {
|
||
86 | public void transferEventReceived(RetrieveEvent event) { |
||
87 | int e = event.getType();
|
||
88 | printMessage("listener4", event.getType());
|
||
89 | if (e==RetrieveEvent.REQUEST_FINISHED || e == RetrieveEvent.REQUEST_FAILED || e == RetrieveEvent.REQUEST_CANCELLED )
|
||
90 | finished4 = true;
|
||
91 | |||
92 | } |
||
93 | }; |
||
94 | { |
||
95 | cleanFiles(); |
||
96 | } |
||
97 | private static void cleanFiles(){ |
||
98 | try{
|
||
99 | File tempDirectory = new File(tempDirectoryPath); |
||
100 | |||
101 | File[] files = tempDirectory.listFiles(); |
||
102 | if (files!=null) { |
||
103 | for (int i = 0; i < files.length; i++) { |
||
104 | // s?lo por si en un futuro se necesitan crear directorios temporales
|
||
105 | files[i].delete(); |
||
106 | } |
||
107 | } |
||
108 | tempDirectory.delete(); |
||
109 | } catch (Exception e) { } |
||
110 | } |
||
111 | |||
112 | |||
113 | public void setUp() { |
||
114 | File tmpDir = new File(tempDirectoryPath); |
||
115 | if (!tmpDir.exists())
|
||
116 | tmpDir.mkdir(); |
||
117 | finished1 = finished2 = finished3 = finished4 = false;
|
||
118 | System.out.println("\n\n\nSetting up test.."); |
||
119 | queue = new RetrieveQueue("http://192.168.0.223/cgi-bin/mapserv_46.exe?map=c:\\ms4w\\Apache\\cgi-bin\\demo.map"); |
||
120 | |||
121 | request1 = new URLRequest();
|
||
122 | // http://192.168.0.223/cgi-bin/mapserv_46.exe?map=c:\\ms4w\\Apache\\cgi-bin\\demo.map&&REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&LAYERS=comunidades&SRS=EPSG:23030&BBOX=507522.0,4154976.282477341,942309.0,4552983.717522658&WIDTH=332&HEIGHT=305&FORMAT=image/png&STYLES=&TRANSPARENT=TRUE
|
||
123 | request1.setProtocol(protocol); |
||
124 | request1.setHost(host1); |
||
125 | request1.setFile(page1); |
||
126 | request1.setFileName(tempDirectoryPath+File.separator+"task1"); |
||
127 | request1.setRequestType(URLRequest.GET); |
||
128 | task1 = new URLRetrieveTask(request1, listener1);
|
||
129 | |||
130 | request2 = new URLRequest();
|
||
131 | // http://localhost/aegCapabilities1.3.xml
|
||
132 | request2.setProtocol(protocol); |
||
133 | request2.setHost(host2); |
||
134 | request2.setFile(page2); |
||
135 | request2.setFileName(tempDirectoryPath+File.separator+"task2"); |
||
136 | request2.setRequestType(URLRequest.GET); |
||
137 | task2 = new URLRetrieveTask(request2, listener2);
|
||
138 | |||
139 | request3 = new URLRequest();
|
||
140 | // http://localhost/avalencia.ecw
|
||
141 | request3.setProtocol(protocol); |
||
142 | request3.setHost(host3); |
||
143 | request3.setFile(page3); |
||
144 | request3.setFileName(tempDirectoryPath+File.separator+"task3"); |
||
145 | request3.setRequestType(URLRequest.GET); |
||
146 | task3 = new URLRetrieveTask(request3, listener3);
|
||
147 | |||
148 | request4 = new URLRequest();
|
||
149 | // http://192.168.0.223/prueba.rar
|
||
150 | request4.setProtocol(protocol); |
||
151 | request4.setHost(host4); |
||
152 | request4.setFile(page4); |
||
153 | request4.setFileName(tempDirectoryPath+File.separator+"task4"); |
||
154 | request4.setRequestType(URLRequest.GET); |
||
155 | |||
156 | task4 = new URLRetrieveTask(request4, listener4);
|
||
157 | |||
158 | } |
||
159 | |||
160 | public void testRetrieve() { |
||
161 | queue.put(task1); |
||
162 | queue.put(task2); |
||
163 | |||
164 | while(!queue.isEmpty()) {
|
||
165 | } |
||
166 | } |
||
167 | |||
168 | public void testCancelling() { |
||
169 | long time = System.currentTimeMillis(); |
||
170 | |||
171 | queue.put(task1); |
||
172 | queue.put(task2); |
||
173 | boolean more = true; |
||
174 | while (!queue.isEmpty()) {
|
||
175 | if (more && System.currentTimeMillis()-time > 1000) { // wait 1 seconds and cancel |
||
176 | task2.cancel(); |
||
177 | more = false;
|
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public void testRequestManager() { |
||
183 | System.out.println("tests parallel downloading from different servers"); |
||
184 | request3.setFileName("task3");
|
||
185 | // http://localhost/avalencia.ecw
|
||
186 | |||
187 | request4.setFileName("task4");
|
||
188 | // http://192.168.0.223/prueba.rar
|
||
189 | RequestManager manager = RequestManager.getInstance(); |
||
190 | manager.addURLRequest(request3, listener3); |
||
191 | manager.addURLRequest(request4, listener4); |
||
192 | manager.addURLRequest(request4, listener1); |
||
193 | while (!(finished1 && finished3 && finished4)) {
|
||
194 | |||
195 | } |
||
196 | } |
||
197 | |||
198 | public void testCocurrentTransfers() { |
||
199 | finished1 = finished2 = finished3 = finished4 = false;
|
||
200 | System.out.println("tests to merge two or more equivalent transfers into one"); |
||
201 | RequestManager manager = RequestManager.getInstance(); |
||
202 | //manager.removeURLRequest(request3);
|
||
203 | request3.setFileName("task3");
|
||
204 | |||
205 | manager.addURLRequest(request3, listener1); |
||
206 | manager.addURLRequest(request3, listener2); |
||
207 | manager.addURLRequest(request3, listener3); |
||
208 | |||
209 | manager.addURLRequest(request3, listener4); |
||
210 | while (!(finished1 && finished2 && finished3 && finished4)) {
|
||
211 | |||
212 | } |
||
213 | } |
||
214 | |||
215 | public void printMessage(String who, int code) { |
||
216 | switch (code) {
|
||
217 | case RetrieveEvent.CONNECTING:
|
||
218 | System.out.println(who+": connecting"); |
||
219 | break;
|
||
220 | case RetrieveEvent.TRANSFERRING:
|
||
221 | System.out.println(who+": transferring"); |
||
222 | break;
|
||
223 | case RetrieveEvent.REQUEST_CANCELLED:
|
||
224 | System.out.println(who+": cancelled"); |
||
225 | break;
|
||
226 | case RetrieveEvent.REQUEST_FINISHED:
|
||
227 | System.out.println(who+": finished"); |
||
228 | break;
|
||
229 | case RetrieveEvent.REQUEST_FAILED:
|
||
230 | System.err.println(who+": failed"); |
||
231 | break;
|
||
232 | case RetrieveEvent.NOT_STARTED:
|
||
233 | System.out.println(who+": not started"); |
||
234 | break;
|
||
235 | case RetrieveEvent.POSTPROCESSING:
|
||
236 | System.out.println(who+": postprocessing"); |
||
237 | break;
|
||
238 | } |
||
239 | } |
||
240 | } |