View Javadoc

1   package org.apache.maven.wagon.providers.webdav;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5    * agreements. See the NOTICE file distributed with this work for additional information regarding
6    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance with the License. You may obtain a
8    * copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software distributed under the License
13   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14   * or implied. See the License for the specific language governing permissions and limitations under
15   * the License.
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   * WebDAV Wagon Test
37   * 
38   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
39   * 
40   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
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       * Tests the maven 2.0.x way to define a webdav URL without SSL.
87       */
88      public void testGetURLDavHttp()
89      {
90          assertURL( "dav:http://localhost:10007/dav/", "http://localhost:10007/dav/" );
91      }
92  
93      /**
94       * Tests the maven 2.0.x way to define a webdav URL with SSL.
95       */
96      public void testGetURLDavHttps()
97      {
98          assertURL( "dav:https://localhost:10007/dav/", "https://localhost:10007/dav/" );
99      }
100 
101     /**
102      * Tests the URI spec way of defining a webdav URL without SSL.
103      */
104     public void testGetURLDavUri()
105     {
106         assertURL( "dav://localhost:10007/dav/", "http://localhost:10007/dav/" );
107     }
108 
109     /**
110      * Tests the URI spec way of defining a webdav URL with SSL.
111      */
112     public void testGetURLDavUriWithSsl()
113     {
114         assertURL( "davs://localhost:10007/dav/", "https://localhost:10007/dav/" );
115     }
116 
117     /**
118      * Tests the URI spec way of defining a webdav URL without SSL.
119      */
120     public void testGetURLDavPlusHttp()
121     {
122         assertURL( "dav+https://localhost:10007/dav/", "https://localhost:10007/dav/" );
123     }
124 
125     /**
126      * Tests the URI spec way of defining a webdav URL with SSL.
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             // check basedir also doesn't exist and will need to be created
147             dir = new File( dir, testRepository.getBasedir() );
148             assertFalse( dir.exists() );
149             
150             // test leading /
151             assertFalse( new File( dir, "foo" ).exists() );
152             wagon.mkdirs( "/foo" );
153             assertTrue( new File( dir, "foo" ).exists() );
154             
155             // test trailing /
156             assertFalse( new File( dir, "bar" ).exists() );
157             wagon.mkdirs( "bar/" );
158             assertTrue( new File( dir, "bar" ).exists() );
159             
160             // test when already exists
161             wagon.mkdirs( "bar" );
162             
163             // test several parts
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             // test additional part and trailing /
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         // WAGON-244
184         setupRepositories();
185 
186         setupWagonTestingFixtures();
187 
188         // reconstruct with no basedir
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             // check basedir also doesn't exist and will need to be created
200             dir = new File( dir, testRepository.getBasedir() );
201             assertTrue( dir.exists() );
202             
203             // test leading /
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      * Make sure wagon webdav can detect remote directory
223      * @throws Exception
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 }