KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ssh > ScpTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs.optional.ssh;
19
20 import junit.framework.TestCase;
21
22 import java.io.*;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.condition.FilesMatch;
29 import org.apache.tools.ant.types.FileSet;
30 import org.apache.tools.ant.types.selectors.FilenameSelector;
31
32 /**
33  * This is a unit test for the Scp task in Ant. It must be
34  * configured with command line options in order for it to work.
35  * Here are the options:
36  *
37  * scp.tmp This is a local path to a temporary
38  * directory for this task to use.
39  * scp.host This is the remote location of the form:
40  * "user:password@host:/path/to/directory"
41  * scp.port The port of the listening SSH service.
42  * Defaults to 22. (optional)
43  * scp.known.hosts The file containing the public keys of known
44  * hosts. Must be a SSH2 version file, but
45  * supports RSA and DSA keys. If it is not present
46  * this task setTrust() to true. (optional)
47  */

48 public class ScpTest extends TestCase {
49
50     private File tempDir = new File( System.getProperty("scp.tmp") );
51     private String JavaDoc sshHostUri = System.getProperty("scp.host");
52     private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) );
53     private String JavaDoc knownHosts = System.getProperty("scp.known.hosts");
54
55     private List JavaDoc cleanUpList = new ArrayList JavaDoc();
56
57     public ScpTest(String JavaDoc testname) {
58         super(testname);
59     }
60
61     protected void setUp() {
62         cleanUpList.clear();
63     }
64
65     protected void tearDown() {
66         for( Iterator JavaDoc i = cleanUpList.iterator(); i.hasNext(); ) {
67             File file = (File) i.next();
68             file.delete();
69         }
70     }
71
72     public void testSingleFileUploadAndDownload() throws IOException {
73         File uploadFile = createTemporaryFile();
74
75         Scp scpTask = createTask();
76         scpTask.setFile( uploadFile.getPath() );
77         scpTask.setTodir( sshHostUri );
78         scpTask.execute();
79
80         File testFile = new File( tempDir.getPath() + File.separator +
81                 "download-testSingleFileUploadAndDownload.test" );
82         addCleanup( testFile );
83         assertTrue( "Assert that the testFile does not exist.",
84                 !testFile.exists() );
85
86         scpTask.setFile( sshHostUri + "/" + uploadFile.getName() );
87         scpTask.setTodir( testFile.getPath() );
88         scpTask.execute();
89
90         assertTrue( "Assert that the testFile exists.", testFile.exists() );
91         compareFiles( uploadFile, testFile );
92     }
93
94     public void testMultiUploadAndDownload() throws IOException {
95         List JavaDoc uploadList = new ArrayList JavaDoc();
96         for( int i = 0; i < 5; i++ ) {
97             uploadList.add( createTemporaryFile() );
98         }
99
100         Scp scp = createTask();
101         FilenameSelector selector = new FilenameSelector();
102         selector.setName( "scp*" );
103         FileSet fileset = new FileSet();
104         fileset.setDir( tempDir );
105         fileset.addFilename( selector );
106         scp.addFileset( fileset );
107         scp.setTodir( sshHostUri );
108         scp.execute();
109
110         File multi = new File( tempDir, "multi" );
111         multi.mkdir();
112         addCleanup( multi );
113
114         Scp scp2 = createTask();
115         scp2.setFile( sshHostUri + "/scp*" );
116         scp2.setTodir( multi.getPath() );
117         scp2.execute();
118
119         FilesMatch match = new FilesMatch();
120         for( Iterator JavaDoc i = uploadList.iterator(); i.hasNext(); ) {
121             File f = (File)i.next();
122             match.setFile1( f );
123             File f2 = new File( multi, f.getName() );
124             match.setFile2( f2 );
125             assertTrue("Assert file '" + f.getPath() + "' and file '" +
126                     f2.getPath() + "'", match.eval() );
127         }
128     }
129
130     public void addCleanup( File file ) {
131         cleanUpList.add( file );
132     }
133
134     private void compareFiles(File src, File dest) {
135         FilesMatch match = new FilesMatch();
136         match.setFile1( src );
137         match.setFile2( dest );
138
139         assertTrue( "Assert files are equal.", match.eval() );
140     }
141
142     private File createTemporaryFile() throws IOException {
143         File uploadFile;
144         uploadFile = File.createTempFile( "scp", "test", tempDir );
145         FileWriter writer = new FileWriter( uploadFile );
146         writer.write("Can you hear me now?\n");
147         writer.close();
148         addCleanup( uploadFile );
149         return uploadFile;
150     }
151
152     private Scp createTask() {
153         Scp scp = new Scp();
154         Project p = new Project();
155         p.init();
156         scp.setProject( p );
157         if( knownHosts != null ) {
158             scp.setKnownhosts( knownHosts );
159         } else {
160             scp.setTrust( true );
161         }
162         scp.setPort( port );
163         return scp;
164     }
165 }
166
Popular Tags