1 package org.apache.maven.wagon.providers.webdav;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import it.could.webdav.DAVServlet;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.Properties;
24
25 import org.apache.maven.wagon.ResourceDoesNotExistException;
26 import org.apache.maven.wagon.StreamingWagon;
27 import org.apache.maven.wagon.Wagon;
28 import org.apache.maven.wagon.http.HttpWagonTestCase;
29 import org.apache.maven.wagon.repository.Repository;
30 import org.apache.maven.wagon.resource.Resource;
31 import org.mortbay.jetty.Server;
32 import org.mortbay.jetty.servlet.Context;
33 import org.mortbay.jetty.servlet.ServletHolder;
34
35
36
37
38
39
40
41
42 public class WebDavWagonTest
43 extends HttpWagonTestCase
44 {
45 protected String getTestRepositoryUrl()
46 throws IOException
47 {
48 return getProtocol() + "://localhost:10007/newfolder/folder2";
49 }
50
51 protected String getProtocol()
52 {
53 return "dav";
54 }
55
56 protected void createContext( Server server, File repositoryDirectory )
57 throws IOException
58 {
59 Context dav = new Context( server, "/", Context.SESSIONS );
60 ServletHolder davServletHolder = new ServletHolder( new DAVServlet() );
61 davServletHolder.setInitParameter( "rootPath", repositoryDirectory.getAbsolutePath() );
62 davServletHolder.setInitParameter( "xmlOnly", "false" );
63 dav.addServlet( davServletHolder, "/*" );
64 }
65
66 protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
67 {
68 File file = new File( getDavRepository(), resource.getName() );
69 return ( file.lastModified() / 1000 ) * 1000;
70 }
71
72
73 private File getDavRepository()
74 {
75 return getTestFile( "target/test-output/http-repository/newfolder/folder2" );
76 }
77
78 private void assertURL( String userUrl, String expectedUrl )
79 {
80 Repository repo = new Repository( "test-geturl", userUrl );
81 String actualUrl = ( new WebDavWagon() ).getURL( repo );
82 assertEquals( "WebDavWagon.getURL(" + userUrl + ")", expectedUrl, actualUrl );
83 }
84
85
86
87
88 public void testGetURLDavHttp()
89 {
90 assertURL( "dav:http://localhost:10007/dav/", "http://localhost:10007/dav/" );
91 }
92
93
94
95
96 public void testGetURLDavHttps()
97 {
98 assertURL( "dav:https://localhost:10007/dav/", "https://localhost:10007/dav/" );
99 }
100
101
102
103
104 public void testGetURLDavUri()
105 {
106 assertURL( "dav://localhost:10007/dav/", "http://localhost:10007/dav/" );
107 }
108
109
110
111
112 public void testGetURLDavUriWithSsl()
113 {
114 assertURL( "davs://localhost:10007/dav/", "https://localhost:10007/dav/" );
115 }
116
117
118
119
120 public void testGetURLDavPlusHttp()
121 {
122 assertURL( "dav+https://localhost:10007/dav/", "https://localhost:10007/dav/" );
123 }
124
125
126
127
128 public void testGetURLDavPlusHttps()
129 {
130 assertURL( "dav+https://localhost:10007/dav/", "https://localhost:10007/dav/" );
131 }
132
133 public void testMkdirs() throws Exception
134 {
135 setupRepositories();
136
137 setupWagonTestingFixtures();
138
139 WebDavWagon wagon = (WebDavWagon) getWagon();
140 wagon.connect( testRepository, getAuthInfo() );
141
142 try
143 {
144 File dir = getRepositoryDirectory();
145
146
147 dir = new File( dir, testRepository.getBasedir() );
148 assertFalse( dir.exists() );
149
150
151 assertFalse( new File( dir, "foo" ).exists() );
152 wagon.mkdirs( "/foo" );
153 assertTrue( new File( dir, "foo" ).exists() );
154
155
156 assertFalse( new File( dir, "bar" ).exists() );
157 wagon.mkdirs( "bar/" );
158 assertTrue( new File( dir, "bar" ).exists() );
159
160
161 wagon.mkdirs( "bar" );
162
163
164 assertFalse( new File( dir, "1/2/3/4" ).exists() );
165 wagon.mkdirs( "1/2/3/4" );
166 assertTrue( new File( dir, "1/2/3/4" ).exists() );
167
168
169 assertFalse( new File( dir, "1/2/3/4/5" ).exists() );
170 wagon.mkdirs( "1/2/3/4/5/" );
171 assertTrue( new File( dir, "1/2/3/4" ).exists() );
172 }
173 finally
174 {
175 wagon.disconnect();
176
177 tearDownWagonTestingFixtures();
178 }
179 }
180
181 public void testMkdirsWithNoBasedir() throws Exception
182 {
183
184 setupRepositories();
185
186 setupWagonTestingFixtures();
187
188
189 testRepository.setUrl( testRepository.getProtocol() + "://" + testRepository.getHost() + ":"
190 + testRepository.getPort() );
191
192 WebDavWagon wagon = (WebDavWagon) getWagon();
193 wagon.connect( testRepository, getAuthInfo() );
194
195 try
196 {
197 File dir = getRepositoryDirectory();
198
199
200 dir = new File( dir, testRepository.getBasedir() );
201 assertTrue( dir.exists() );
202
203
204 assertFalse( new File( dir, "foo" ).exists() );
205 wagon.mkdirs( "/foo" );
206 assertTrue( new File( dir, "foo" ).exists() );
207 }
208 finally
209 {
210 wagon.disconnect();
211
212 tearDownWagonTestingFixtures();
213 }
214 }
215
216 protected void setHttpHeaders( StreamingWagon wagon, Properties properties )
217 {
218 ( (WebDavWagon) wagon ).setHttpHeaders( properties );
219 }
220
221
222
223
224
225 public void testWagonWebDavGetFileList()
226 throws Exception
227 {
228 setupRepositories();
229
230 setupWagonTestingFixtures();
231
232 String dirName = "file-list";
233
234 String filenames[] = new String[] {
235 "test-resource.txt",
236 "test-resource.pom",
237 "test-resource b.txt",
238 "more-resources.dat" };
239
240 for ( int i = 0; i < filenames.length; i++ )
241 {
242 putFile( dirName + "/" + filenames[i], dirName + "/" + filenames[i], filenames[i] + "\n" );
243 }
244
245 String dirnames[] = new String[] {
246 "test-dir1",
247 "test-dir2"};
248
249 for ( int i = 0; i < dirnames.length; i++ )
250 {
251 new File( getDavRepository(), dirName + "/" + dirnames[i] ).mkdirs();
252 }
253
254 Wagon wagon = getWagon();
255
256 wagon.connect( testRepository, getAuthInfo() );
257
258 List list = wagon.getFileList( dirName );
259
260 assertNotNull( "file list should not be null.", list );
261 assertEquals( "file list should contain 6 items", 6, list.size() );
262
263 for ( int i = 0; i < filenames.length; i++ )
264 {
265 assertTrue( "Filename '" + filenames[i] + "' should be in list.", list.contains( filenames[i] ) );
266 }
267
268 for ( int i = 0; i < dirnames.length; i++ )
269 {
270 assertTrue( "Directory '" + dirnames[i] + "' should be in list.", list.contains( dirnames[i] + "/" ) );
271 }
272
273
274 list = wagon.getFileList( "" );
275 assertNotNull( "file list should not be null.", list );
276 assertEquals( "file list should contain 1 items", 1, list.size() );
277
278
279 list = wagon.getFileList( dirName + "/test-dir1" );
280 assertNotNull( "file list should not be null.", list );
281 assertEquals( "file list should contain 0 items", 0, list.size() );
282
283
284 try
285 {
286 list = wagon.getFileList( dirName + "/test-dir-bogus" );
287 fail( "Exception expected" );
288 }
289 catch ( ResourceDoesNotExistException e )
290 {
291
292 }
293
294 wagon.disconnect();
295
296
297 tearDownWagonTestingFixtures();
298 }
299 }