1 package org.apache.maven.wagon.providers.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.wagon.FileTestUtils;
23 import org.apache.maven.wagon.TransferFailedException;
24 import org.apache.maven.wagon.Wagon;
25 import org.apache.maven.wagon.repository.Repository;
26 import org.apache.maven.wagon.shared.http.HttpConfiguration;
27 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
28 import org.mortbay.jetty.servlet.ServletHolder;
29
30 import java.io.File;
31 import java.util.Random;
32
33
34
35
36 public class HttpWagonTimeoutTest
37 extends HttpWagonHttpServerTestCase
38 {
39 protected void setUp()
40 throws Exception
41 {
42 super.setUp();
43 ServletHolder servlets = new ServletHolder( new WaitForeverServlet() );
44 context.addServlet( servlets, "/*" );
45 startServer();
46 }
47
48 public void testGetTimeout()
49 throws Exception
50 {
51 Exception thrown = null;
52
53 try
54 {
55 Wagon wagon = getWagon();
56 wagon.setTimeout( 1000 );
57
58 Repository testRepository = new Repository();
59 testRepository.setUrl( "http://localhost:" + httpServerPort );
60
61 wagon.connect( testRepository );
62
63 File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
64 destFile.deleteOnExit();
65
66 wagon.get( "/timeoutfile", destFile );
67
68 wagon.disconnect();
69 }
70 catch ( Exception e )
71 {
72 thrown = e;
73 }
74 finally
75 {
76 stopServer();
77 }
78
79 assertNotNull( thrown );
80 assertEquals( TransferFailedException.class, thrown.getClass() );
81 }
82
83 public void testResourceExits()
84 throws Exception
85 {
86 Exception thrown = null;
87
88 try
89 {
90 Wagon wagon = getWagon();
91 wagon.setTimeout( 1000 );
92
93 Repository testRepository = new Repository();
94 testRepository.setUrl( "http://localhost:" + httpServerPort );
95
96 wagon.connect( testRepository );
97
98 wagon.resourceExists( "/timeoutfile" );
99
100 wagon.disconnect();
101 }
102 catch ( Exception e )
103 {
104 thrown = e;
105 }
106 finally
107 {
108 stopServer();
109 }
110
111 assertNotNull( thrown );
112 assertEquals( TransferFailedException.class, thrown.getClass() );
113 }
114
115 public void testGetFileList()
116 throws Exception
117 {
118 Exception thrown = null;
119
120 try
121 {
122 Wagon wagon = getWagon();
123 wagon.setTimeout( 1000 );
124
125 Repository testRepository = new Repository();
126 testRepository.setUrl( "http://localhost:" + httpServerPort );
127
128 wagon.connect( testRepository );
129
130 wagon.getFileList( "/timeoutfile" );
131
132 wagon.disconnect();
133 }
134 catch ( Exception e )
135 {
136 thrown = e;
137 }
138 finally
139 {
140 stopServer();
141 }
142
143 assertNotNull( thrown );
144 assertEquals( TransferFailedException.class, thrown.getClass() );
145 }
146
147 public void testPutTimeout()
148 throws Exception
149 {
150 Exception thrown = null;
151
152 try
153 {
154 Wagon wagon = getWagon();
155 wagon.setTimeout( 1000 );
156
157 Repository testRepository = new Repository();
158 testRepository.setUrl( "http://localhost:" + httpServerPort );
159
160 wagon.connect( testRepository );
161
162 File destFile = File.createTempFile( "Hello", null );
163 destFile.deleteOnExit();
164
165 wagon.put( destFile, "/timeoutfile" );
166
167 wagon.disconnect();
168 }
169 catch ( Exception e )
170 {
171 thrown = e;
172 }
173 finally
174 {
175 stopServer();
176 }
177
178 assertNotNull( thrown );
179 assertEquals( TransferFailedException.class, thrown.getClass() );
180 }
181
182 public void testConnectionTimeout()
183 throws Exception
184 {
185 Exception thrown = null;
186
187 try
188 {
189 HttpWagon wagon = (HttpWagon) getWagon();
190 wagon.setHttpConfiguration(
191 new HttpConfiguration().setAll( new HttpMethodConfiguration().setConnectionTimeout( 500 ) ) );
192
193 Repository testRepository = new Repository();
194 Random random = new Random( );
195 testRepository.setUrl( "http://localhost:" + random.nextInt( 2048 ));
196
197 wagon.connect( testRepository );
198
199 long start = System.currentTimeMillis();
200 wagon.getFileList( "/foobar" );
201 long end = System.currentTimeMillis();
202
203
204 assertTrue( (end - start) >= 500 && (end - start) < 1000 );
205
206 }
207 catch ( Exception e )
208 {
209 thrown = e;
210 }
211 finally
212 {
213 stopServer();
214 }
215
216 assertNotNull( thrown );
217 assertEquals( TransferFailedException.class, thrown.getClass() );
218 }
219
220 }