1 package org.apache.maven.wagon.providers.ftp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.ftpserver.FtpServer;
23 import org.apache.ftpserver.FtpServerFactory;
24 import org.apache.ftpserver.ftplet.Authority;
25 import org.apache.ftpserver.ftplet.UserManager;
26 import org.apache.ftpserver.listener.ListenerFactory;
27 import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
28 import org.apache.ftpserver.usermanager.impl.BaseUser;
29 import org.apache.ftpserver.usermanager.impl.WritePermission;
30 import org.apache.maven.wagon.FileTestUtils;
31 import org.apache.maven.wagon.StreamingWagonTestCase;
32 import org.apache.maven.wagon.Wagon;
33 import org.apache.maven.wagon.authentication.AuthenticationException;
34 import org.apache.maven.wagon.authentication.AuthenticationInfo;
35 import org.apache.maven.wagon.repository.Repository;
36 import org.apache.maven.wagon.resource.Resource;
37 import org.codehaus.plexus.util.FileUtils;
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.List;
42
43
44
45
46
47 public class FtpWagonTest
48 extends StreamingWagonTestCase
49 {
50 static private FtpServer server;
51
52 protected String getProtocol()
53 {
54 return "ftp";
55 }
56
57 @Override
58 protected int getTestRepositoryPort() {
59 return 10023;
60 }
61
62 protected void setupWagonTestingFixtures()
63 throws Exception
64 {
65 File ftpHomeDir = getRepositoryDirectory();
66 if ( !ftpHomeDir.exists() )
67 {
68 ftpHomeDir.mkdirs();
69 }
70
71 if (server == null)
72 {
73 FtpServerFactory serverFactory = new FtpServerFactory();
74
75 ListenerFactory factory = new ListenerFactory();
76
77
78 factory.setPort(getTestRepositoryPort());
79
80
81 serverFactory.addListener("default", factory.createListener());
82
83 PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
84 UserManager um = userManagerFactory.createUserManager();
85
86 BaseUser user = new BaseUser();
87 user.setName("admin");
88 user.setPassword("admin");
89
90 List<Authority> authorities = new ArrayList<Authority>();
91 authorities.add( new WritePermission() );
92
93 user.setAuthorities( authorities );
94
95 user.setHomeDirectory( ftpHomeDir.getAbsolutePath() );
96
97
98 um.save(user);
99
100 serverFactory.setUserManager( um );
101
102 server = serverFactory.createServer();
103
104
105 server.start();
106
107 }
108 }
109
110 protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
111 throws Exception
112 {
113 super.createDirectory( wagon, resourceToCreate, dirName );
114
115 getRepositoryDirectory().mkdirs();
116 }
117
118 protected void tearDownWagonTestingFixtures()
119 throws Exception
120 {
121 server.stop();
122 server = null;
123 }
124
125 protected String getTestRepositoryUrl()
126 {
127 return "ftp://localhost:" + getTestRepositoryPort();
128 }
129
130 public AuthenticationInfo getAuthInfo()
131 {
132 AuthenticationInfo authInfo = new AuthenticationInfo();
133
134 authInfo.setUserName( "admin" );
135
136 authInfo.setPassword( "admin" );
137
138 return authInfo;
139 }
140
141 protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
142 {
143 File file = new File( getRepositoryDirectory(), resource.getName() );
144
145
146 return ( file.lastModified() / 60000 ) * 60000;
147 }
148
149 private File getRepositoryDirectory()
150 {
151 return getTestFile( "target/test-output/local-repository" );
152 }
153
154 public void testNoPassword()
155 throws Exception
156 {
157 AuthenticationInfo authenticationInfo = new AuthenticationInfo();
158 authenticationInfo.setUserName( "me" );
159 try
160 {
161 getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
162 fail();
163 }
164 catch ( AuthenticationException e )
165 {
166 assertTrue( true );
167 }
168 }
169
170 public void testDefaultUserName()
171 throws Exception
172 {
173 AuthenticationInfo authenticationInfo = new AuthenticationInfo();
174 authenticationInfo.setPassword( "secret" );
175 try
176 {
177 getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
178 fail();
179 }
180 catch ( AuthenticationException e )
181 {
182 assertEquals( System.getProperty( "user.name" ), authenticationInfo.getUserName() );
183 }
184 }
185
186
187
188
189 public void testPutDirectoryCreation()
190 throws Exception
191 {
192 setupRepositories();
193
194 setupWagonTestingFixtures();
195
196 Wagon wagon = getWagon();
197
198 if ( wagon.supportsDirectoryCopy() )
199 {
200
201 File destDir = new File( getRepositoryDirectory(), "dirExists" );
202 FileUtils.deleteDirectory(destDir);
203 destDir.mkdirs();
204 destDir = new File( destDir, "not_yet_existing/also_not" );
205
206 File sourceDir = new File( getRepositoryDirectory(), "testDirectory" );
207
208 FileUtils.deleteDirectory(sourceDir);
209 sourceDir.mkdir();
210
211 File testRes = new File( sourceDir, "test-resource-1.txt" );
212 testRes.createNewFile();
213
214
215
216
217 testRepository.setUrl( testRepository.getUrl() + "/dirExists/not_yet_existing/also_not" );
218
219 wagon.connect( testRepository, getAuthInfo() );
220
221 wagon.putDirectory( sourceDir, "testDirectory" );
222
223 destFile = FileTestUtils.createUniqueFile(getName(), getName());
224
225 destFile.deleteOnExit();
226
227 wagon.get( "testDirectory/test-resource-1.txt", destFile );
228
229 wagon.disconnect();
230 }
231
232 tearDownWagonTestingFixtures();
233
234
235 }
236 }