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 java.io.File;
23  
24  import org.apache.ftpserver.interfaces.FtpServerInterface;
25  import org.apache.maven.wagon.FileTestUtils;
26  import org.apache.maven.wagon.StreamingWagonTestCase;
27  import org.apache.maven.wagon.Wagon;
28  import org.apache.maven.wagon.authentication.AuthenticationException;
29  import org.apache.maven.wagon.authentication.AuthenticationInfo;
30  import org.apache.maven.wagon.repository.Repository;
31  import org.apache.maven.wagon.resource.Resource;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  /**
35   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
36   * @version $Id: FtpWagonTest.java 680997 2008-07-30 12:14:49Z brett $
37   */
38  public class FtpWagonTest
39      extends StreamingWagonTestCase
40  {
41      private FtpServerInterface server;
42  
43      protected String getProtocol()
44      {
45          return "ftp";
46      }
47  
48      protected void setupWagonTestingFixtures()
49          throws Exception
50      {
51          server = (FtpServerInterface) lookup( FtpServerInterface.ROLE );
52      }
53  
54      protected void createDirectory( Wagon wagon, String resourceToCreate, String dirName )
55          throws Exception
56      {
57          super.createDirectory( wagon, resourceToCreate, dirName );
58  
59          getRepositoryDirectory().mkdirs();
60      }
61  
62      protected void tearDownWagonTestingFixtures()
63          throws Exception
64      {
65          release( server );
66      }
67  
68      protected String getTestRepositoryUrl()
69      {
70          return "ftp://localhost:10023";
71      }
72  
73      public AuthenticationInfo getAuthInfo()
74      {
75          AuthenticationInfo authInfo = new AuthenticationInfo();
76  
77          authInfo.setUserName( "admin" );
78  
79          authInfo.setPassword( "admin" );
80  
81          return authInfo;
82      }
83  
84      protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
85      {
86          File file = new File( getRepositoryDirectory(), resource.getName() );
87  
88          // granularity for FTP is minutes
89          return ( file.lastModified() / 60000 ) * 60000;
90      }
91  
92      private File getRepositoryDirectory()
93      {
94          return getTestFile( "target/test-output/local-repository" );
95      }
96  
97      public void testNoPassword()
98          throws Exception
99      {
100         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
101         authenticationInfo.setUserName( "me" );
102         try
103         {
104             getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
105             fail();
106         }
107         catch ( AuthenticationException e )
108         {
109             assertTrue( true );
110         }
111     }
112 
113     public void testDefaultUserName()
114         throws Exception
115     {
116         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
117         authenticationInfo.setPassword( "secret" );
118         try
119         {
120             getWagon().connect( new Repository( "id", getTestRepositoryUrl() ), authenticationInfo );
121             fail();
122         }
123         catch ( AuthenticationException e )
124         {
125             assertEquals( System.getProperty( "user.name" ), authenticationInfo.getUserName() );
126         }
127     }
128 }