1 package org.apache.maven.wagon.providers.ssh;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 protected int getTestRepositoryPort()
69 {
70 return sshServerEmbedded.getPort();
71 }
72
73 public String getTestRepositoryUrl()
74 {
75 return TestData.getTestRepositoryUrl( sshServerEmbedded.getPort() );
76 }
77
78 protected AuthenticationInfo getAuthInfo()
79 {
80 AuthenticationInfo authInfo = super.getAuthInfo();
81
82 authInfo.setUserName( "guest" );
83
84 authInfo.setPrivateKey( new File( "src/test/ssh-keys/id_rsa" ).getPath() );
85
86 return authInfo;
87 }
88
89 protected long getExpectedLastModifiedOnGet( Repository repository, Resource resource )
90 {
91 return new File( repository.getBasedir(), resource.getName() ).lastModified();
92 }
93
94 public void testConnect()
95 throws Exception
96 {
97 getWagon().connect( new Repository( "foo", getTestRepositoryUrl() ), getAuthInfo() );
98 assertTrue( true );
99 }
100
101
102 @Override
103 protected boolean supportsGetIfNewer()
104 {
105 return false;
106 }
107
108 public void testWithSpaces()
109 throws Exception
110 {
111 String dir = "foo test";
112 File spaceDirectory = new File( TestData.getRepoPath(), dir );
113 if ( spaceDirectory.exists() )
114 {
115 FileUtils.deleteDirectory( spaceDirectory );
116 }
117 spaceDirectory.mkdirs();
118
119 String subDir = "foo bar";
120 File sub = new File( spaceDirectory, subDir );
121 if ( sub.exists() )
122 {
123 FileUtils.deleteDirectory( sub );
124 }
125 sub.mkdirs();
126
127 File dummy = new File( "src/test/resources/dummy.txt" );
128 FileUtils.copyFileToDirectory( dummy, sub );
129
130 String url = getTestRepositoryUrl() + "/" + dir;
131 Repository repo = new Repository( "foo", url );
132 Wagon wagon = getWagon();
133 wagon.connect( repo, getAuthInfo() );
134 List<String> files = wagon.getFileList( subDir );
135 assertNotNull( files );
136 assertEquals( 1, files.size() );
137 assertTrue( files.contains( "dummy.txt" ) );
138
139 wagon.put( new File( "src/test/resources/dummy.txt" ), subDir + "/newdummy.txt" );
140
141 files = wagon.getFileList( subDir );
142 assertNotNull( files );
143 assertEquals( 2, files.size() );
144 assertTrue( files.contains( "dummy.txt" ) );
145 assertTrue( files.contains( "newdummy.txt" ) );
146
147 File sourceWithSpace = new File( "target/directory with spaces" );
148 if ( sourceWithSpace.exists() )
149 {
150 FileUtils.deleteDirectory( sourceWithSpace );
151 }
152 File resources = new File( "src/test/resources" );
153
154 FileUtils.copyDirectory( resources, sourceWithSpace );
155
156 wagon.putDirectory( sourceWithSpace, "target with spaces" );
157
158 files = wagon.getFileList( "target with spaces" );
159
160 assertNotNull( files );
161 assertTrue( files.contains( "dummy.txt" ) );
162 assertFalse( files.contains( "newdummy.txt" ) );
163 assertTrue( files.contains( "log4j.xml" ) );
164 }
165
166 }