View Javadoc
1   package org.apache.maven.wagon.providers.ftp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.ftpserver.FtpServer;
24  import org.apache.ftpserver.FtpServerFactory;
25  import org.apache.ftpserver.ftplet.Authority;
26  import org.apache.ftpserver.ftplet.UserManager;
27  import org.apache.ftpserver.listener.ListenerFactory;
28  import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
29  import org.apache.ftpserver.usermanager.impl.BaseUser;
30  import org.apache.ftpserver.usermanager.impl.WritePermission;
31  import org.apache.maven.wagon.FileTestUtils;
32  import org.apache.maven.wagon.StreamingWagonTestCase;
33  import org.apache.maven.wagon.Wagon;
34  import org.apache.maven.wagon.authentication.AuthenticationException;
35  import org.apache.maven.wagon.authentication.AuthenticationInfo;
36  import org.apache.maven.wagon.repository.Repository;
37  import org.apache.maven.wagon.resource.Resource;
38  
39  
40  import java.io.File;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
46   *
47   */
48  public class FtpWagonTest
49      extends StreamingWagonTestCase
50  {
51      static private FtpServer server;
52  
53      protected String getProtocol()
54      {
55          return "ftp";
56      }
57  
58      @Override
59      protected int getTestRepositoryPort() {
60          return 10023;
61      }
62  
63      protected void setupWagonTestingFixtures()
64          throws Exception
65      {
66          File ftpHomeDir = getRepositoryDirectory();
67          if ( !ftpHomeDir.exists() )
68          {
69              ftpHomeDir.mkdirs();
70          }
71  
72          if (server == null)
73          {
74              FtpServerFactory serverFactory = new FtpServerFactory();
75  
76              ListenerFactory factory = new ListenerFactory();
77  
78              // set the port of the listener
79              factory.setPort(getTestRepositoryPort());
80  
81              // replace the default listener
82              serverFactory.addListener("default", factory.createListener());
83  
84              PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
85              UserManager um = userManagerFactory.createUserManager();
86  
87              BaseUser user = new BaseUser();
88              user.setName("admin");
89              user.setPassword("admin");
90  
91              List<Authority> authorities = new ArrayList<Authority>();
92              authorities.add( new WritePermission() );
93  
94              user.setAuthorities( authorities );
95  
96              user.setHomeDirectory( ftpHomeDir.getAbsolutePath() );
97  
98  
99              um.save(user);
100 
101             serverFactory.setUserManager( um );
102 
103             server = serverFactory.createServer();
104 
105             // start the server
106             server.start();
107 
108         }
109     }
110 
111     protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
112         throws Exception
113     {
114         super.createDirectory( wagon, resourceToCreate, dirName );
115 
116         getRepositoryDirectory().mkdirs();
117     }
118 
119     protected void tearDownWagonTestingFixtures()
120         throws Exception
121     {
122         server.stop();
123         server = null;
124     }
125 
126     protected String getTestRepositoryUrl()
127     {
128         return "ftp://localhost:" + getTestRepositoryPort();
129     }
130 
131     public AuthenticationInfo getAuthInfo()
132     {
133         AuthenticationInfo authInfo = new AuthenticationInfo();
134 
135         authInfo.setUserName( "admin" );
136 
137         authInfo.setPassword( "admin" );
138 
139         return authInfo;
140     }
141 
142     protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
143     {
144         File file = new File( getRepositoryDirectory(), resource.getName() );
145 
146         // granularity for FTP is minutes
147         return ( file.lastModified() / 60000 ) * 60000;
148     }
149 
150     private File getRepositoryDirectory()
151     {
152         return getTestFile( "target/test-output/local-repository" );
153     }
154 
155     public void testNoPassword()
156         throws Exception
157     {
158         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
159         authenticationInfo.setUserName( "me" );
160         try
161         {
162             getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
163             fail();
164         }
165         catch ( AuthenticationException e )
166         {
167             assertTrue( true );
168         }
169     }
170 
171     public void testDefaultUserName()
172         throws Exception
173     {
174         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
175         authenticationInfo.setPassword( "secret" );
176         try
177         {
178             getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
179             fail();
180         }
181         catch ( AuthenticationException e )
182         {
183             assertEquals( System.getProperty( "user.name" ), authenticationInfo.getUserName() );
184         }
185     }
186 
187     /**
188      * This is a unit test to show WAGON-265
189      */
190     public void testPutDirectoryCreation()
191         throws Exception
192     {
193         setupRepositories();
194 
195         setupWagonTestingFixtures();
196 
197         Wagon wagon = getWagon();
198 
199         if ( wagon.supportsDirectoryCopy() )
200         {
201             // do the cleanup first
202             File destDir = new File( getRepositoryDirectory(), "dirExists" );
203             FileUtils.deleteDirectory( destDir );
204             destDir.mkdirs();
205             destDir = new File( destDir, "not_yet_existing/also_not" );
206 
207             File sourceDir = new File( getRepositoryDirectory(), "testDirectory" );
208 
209             FileUtils.deleteDirectory(sourceDir);
210             sourceDir.mkdir();
211 
212             File testRes = new File( sourceDir, "test-resource-1.txt" );
213             testRes.createNewFile();
214 
215             // This is the difference to our normal use case:
216             // the directory specified in the repo string doesn't yet exist!
217 
218             testRepository.setUrl( testRepository.getUrl() + "/dirExists/not_yet_existing/also_not" );
219 
220             wagon.connect( testRepository, getAuthInfo() );
221 
222             wagon.putDirectory( sourceDir, "testDirectory" );
223 
224             destFile = FileTestUtils.createUniqueFile(getName(), getName());
225 
226             destFile.deleteOnExit();
227 
228             wagon.get( "testDirectory/test-resource-1.txt", destFile );
229 
230             wagon.disconnect();
231         }
232 
233         tearDownWagonTestingFixtures();
234 
235 
236     }
237 }