1 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 ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 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 48 public class ScpTest extends TestCase { 49 50 private File tempDir = new File( System.getProperty("scp.tmp") ); 51 private String sshHostUri = System.getProperty("scp.host"); 52 private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) ); 53 private String knownHosts = System.getProperty("scp.known.hosts"); 54 55 private List cleanUpList = new ArrayList (); 56 57 public ScpTest(String testname) { 58 super(testname); 59 } 60 61 protected void setUp() { 62 cleanUpList.clear(); 63 } 64 65 protected void tearDown() { 66 for( Iterator 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 uploadList = new ArrayList (); 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 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 |