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 protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
110 throws Exception
111 {
112 super.createDirectory( wagon, resourceToCreate, dirName );
113
114 getRepositoryDirectory().mkdirs();
115 }
116
117 protected void tearDownWagonTestingFixtures()
118 throws Exception
119 {
120 server.stop();
121 server = null;
122 }
123
124 protected String getTestRepositoryUrl()
125 {
126 return "ftp://localhost:" + getTestRepositoryPort();
127 }
128
129 public AuthenticationInfo getAuthInfo()
130 {
131 AuthenticationInfo authInfo = new AuthenticationInfo();
132
133 authInfo.setUserName( "admin" );
134
135 authInfo.setPassword( "admin" );
136
137 return authInfo;
138 }
139
140 protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
141 {
142 File file = new File( getRepositoryDirectory(), resource.getName() );
143
144
145 return ( file.lastModified() / 60000 ) * 60000;
146 }
147
148 private File getRepositoryDirectory()
149 {
150 return getTestFile( "target/test-output/local-repository" );
151 }
152
153 public void testNoPassword()
154 throws Exception
155 {
156 AuthenticationInfo authenticationInfo = new AuthenticationInfo();
157 authenticationInfo.setUserName( "me" );
158 try
159 {
160 getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
161 fail();
162 }
163 catch ( AuthenticationException e )
164 {
165 assertTrue( true );
166 }
167 }
168
169 public void testDefaultUserName()
170 throws Exception
171 {
172 AuthenticationInfo authenticationInfo = new AuthenticationInfo();
173 authenticationInfo.setPassword( "secret" );
174 try
175 {
176 getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
177 fail();
178 }
179 catch ( AuthenticationException e )
180 {
181 assertEquals( System.getProperty( "user.name" ), authenticationInfo.getUserName() );
182 }
183 }
184
185
186
187
188 public void testPutDirectoryCreation()
189 throws Exception
190 {
191 setupRepositories();
192
193 setupWagonTestingFixtures();
194
195 Wagon wagon = getWagon();
196
197 if ( wagon.supportsDirectoryCopy() )
198 {
199
200 File destDir = new File( getRepositoryDirectory(), "dirExists" );
201 FileUtils.deleteDirectory(destDir);
202 destDir.mkdirs();
203 destDir = new File( destDir, "not_yet_existing/also_not" );
204
205 File sourceDir = new File( getRepositoryDirectory(), "testDirectory" );
206
207 FileUtils.deleteDirectory(sourceDir);
208 sourceDir.mkdir();
209
210 File testRes = new File( sourceDir, "test-resource-1.txt" );
211 testRes.createNewFile();
212
213
214
215
216 testRepository.setUrl( testRepository.getUrl() + "/dirExists/not_yet_existing/also_not" );
217
218 wagon.connect( testRepository, getAuthInfo() );
219
220 wagon.putDirectory( sourceDir, "testDirectory" );
221
222 destFile = FileTestUtils.createUniqueFile(getName(), getName());
223
224 destFile.deleteOnExit();
225
226 wagon.get( "testDirectory/test-resource-1.txt", destFile );
227
228 wagon.disconnect();
229 }
230
231 tearDownWagonTestingFixtures();
232
233
234 }
235 }