View Javadoc

1   package org.apache.maven.wagon.providers.ssh;
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.maven.wagon.StreamingWagonTestCase;
23  import org.apache.maven.wagon.Wagon;
24  import org.apache.maven.wagon.authentication.AuthenticationInfo;
25  import org.apache.maven.wagon.repository.Repository;
26  import org.apache.maven.wagon.resource.Resource;
27  import org.codehaus.plexus.util.FileUtils;
28  
29  import java.io.File;
30  import java.util.Arrays;
31  import java.util.List;
32  
33  /**
34   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
35   *
36   */
37  public abstract class AbstractEmbeddedScpWagonWithKeyTest
38      extends StreamingWagonTestCase
39  {
40  
41      SshServerEmbedded sshServerEmbedded;
42  
43  
44      @Override
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          String sshKeyResource = "ssh-keys/id_rsa.pub";
51  
52          sshServerEmbedded = new SshServerEmbedded( getProtocol(), Arrays.asList( sshKeyResource ), true );
53  
54          sshServerEmbedded.start();
55          System.out.println( "sshd on port " + sshServerEmbedded.getPort() );
56      }
57  
58      @Override
59      protected void tearDownWagonTestingFixtures()
60          throws Exception
61      {
62  
63          sshServerEmbedded.stop();
64      }
65  
66      protected abstract String getProtocol();
67  
68      @Override
69      protected int getTestRepositoryPort()
70      {
71          return sshServerEmbedded.getPort();
72      }
73  
74  
75      public String getTestRepositoryUrl()
76      {
77          return TestData.getTestRepositoryUrl( sshServerEmbedded.getPort() );
78      }
79  
80      protected AuthenticationInfo getAuthInfo()
81      {
82          AuthenticationInfo authInfo = super.getAuthInfo();
83          // user : guest/guest123 -  passphrase : toto01
84          authInfo.setUserName( "guest" );
85          //authInfo.setPassword( TestData.getUserPassword() );
86          authInfo.setPrivateKey( new File( "src/test/ssh-keys/id_rsa" ).getPath() );
87  
88          return authInfo;
89      }
90  
91      protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
92      {
93          return new File( repository.getBasedir(), resource.getName() ).lastModified();
94      }
95  
96      public void testConnect()
97          throws Exception
98      {
99          getWagon().connect( new Repository( "foo", getTestRepositoryUrl() ), getAuthInfo() );
100         assertTrue( true );
101     }
102 
103 
104     @Override
105     protected boolean supportsGetIfNewer()
106     {
107         return false;
108     }
109 
110     public void testWithSpaces()
111         throws Exception
112     {
113         String dir = "foo   test";
114         File spaceDirectory = new File( TestData.getRepoPath(), dir );
115         if ( spaceDirectory.exists() )
116         {
117             FileUtils.deleteDirectory( spaceDirectory );
118         }
119         spaceDirectory.mkdirs();
120 
121         String subDir = "foo bar";
122         File sub = new File( spaceDirectory, subDir );
123         if ( sub.exists() )
124         {
125             FileUtils.deleteDirectory( sub );
126         }
127         sub.mkdirs();
128 
129         File dummy = new File( "src/test/resources/dummy.txt" );
130         FileUtils.copyFileToDirectory( dummy, sub );
131 
132         String url = getTestRepositoryUrl() + "/" + dir;
133         Repository repo = new Repository( "foo", url );
134         Wagon wagon = getWagon();
135         wagon.connect( repo, getAuthInfo() );
136         List<String> files = wagon.getFileList( subDir );
137         assertNotNull( files );
138         assertEquals( 1, files.size() );
139         assertTrue( files.contains( "dummy.txt" ) );
140 
141         wagon.put( new File( "src/test/resources/dummy.txt" ), subDir + "/newdummy.txt" );
142 
143         files = wagon.getFileList( subDir );
144         assertNotNull( files );
145         assertEquals( 2, files.size() );
146         assertTrue( files.contains( "dummy.txt" ) );
147         assertTrue( files.contains( "newdummy.txt" ) );
148 
149         File sourceWithSpace = new File( "target/directory with spaces" );
150         if ( sourceWithSpace.exists() )
151         {
152             FileUtils.deleteDirectory( sourceWithSpace );
153         }
154         File resources = new File( "src/test/resources" );
155 
156         FileUtils.copyDirectory( resources, sourceWithSpace );
157 
158         wagon.putDirectory( sourceWithSpace, "target with spaces" );
159 
160         files = wagon.getFileList( "target with spaces" );
161 
162         assertNotNull( files );
163         assertTrue( files.contains( "dummy.txt" ) );
164         assertFalse( files.contains( "newdummy.txt" ) );
165         assertTrue( files.contains( "log4j.xml" ) );
166     }
167 
168 }