Author: bodewig Date: Wed Jul 16 02:17:42 2008 New Revision: 677206 URL: http://svn.apache.org/viewvc?rev=677206&view=rev Log: fail with a meaningful error of remoteDir is not parseable. PR 42770. Submitted by Scott Johnson
Modified: ant/core/trunk/CONTRIBUTORS ant/core/trunk/contributors.xml ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java Modified: ant/core/trunk/CONTRIBUTORS URL: http://svn.apache.org/viewvc/ant/core/trunk/CONTRIBUTORS?rev=677206&r1=677205&r2=677206&view=diff ============================================================================== Binary files - no diff available. Modified: ant/core/trunk/contributors.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/contributors.xml?rev=677206&r1=677205&r2=677206&view=diff ============================================================================== --- ant/core/trunk/contributors.xml (original) +++ ant/core/trunk/contributors.xml Wed Jul 16 02:17:42 2008 @@ -969,6 +969,10 @@ </name> <name> <first>Scott</first> + <last>Johnson</last> + </name> + <name> + <first>Scott</first> <middle>M.</middle> <last>Stirling</last> </name> Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java?rev=677206&r1=677205&r2=677206&view=diff ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java Wed Jul 16 02:17:42 2008 @@ -101,6 +101,7 @@ * @since Ant 1.6.2 */ public void setRemoteFile(String aFromUri) { + validateRemoteUri("remoteFile", aFromUri); setFromUri(aFromUri); this.isFromRemote = true; } @@ -133,10 +134,21 @@ * @since Ant 1.6.2 */ public void setRemoteTodir(String aToUri) { + validateRemoteUri("remoteToDir", aToUri); setToUri(aToUri); this.isToRemote = true; } + private static void validateRemoteUri(String type, String aToUri) { + if (!isRemoteUri(aToUri)) { + throw new BuildException(type + " '" + aToUri + "' is invalid. " + + "The 'remoteToDir' attribute must " + + "have syntax like the " + + "following: user:[EMAIL PROTECTED]:/path" + + " - the :password part is optional"); + } + } + /** * Changes the file name to the given name while receiving it, * only useful if receiving a single file. @@ -155,6 +167,7 @@ * @since Ant 1.6.2 */ public void setRemoteTofile(String aToUri) { + validateRemoteUri("remoteToFile", aToUri); setToUri(aToUri); this.isToRemote = true; } @@ -339,9 +352,11 @@ } setUsername(uri.substring(0, indexOfColon)); setPassword(uri.substring(indexOfColon + 1, indexOfAt)); - } else { + } else if (indexOfAt > -1) { // no password, will require passphrase setUsername(uri.substring(0, indexOfAt)); + } else { + throw new BuildException("no username was given. Can't authenticate."); } if (getUserInfo().getPassword() == null @@ -364,7 +379,7 @@ return remotePath; } - private boolean isRemoteUri(String uri) { + private static boolean isRemoteUri(String uri) { boolean isRemote = true; int indexOfAt = uri.indexOf('@'); if (indexOfAt < 0) { Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java?rev=677206&r1=677205&r2=677206&view=diff ============================================================================== --- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java (original) +++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java Wed Jul 16 02:17:42 2008 @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Iterator; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.FilesMatch; import org.apache.tools.ant.types.FileSet; @@ -48,7 +49,7 @@ */ public class ScpTest extends TestCase { - private File tempDir = new File( System.getProperty("scp.tmp") ); + private File tempDir; private String sshHostUri = System.getProperty("scp.host"); private int port = Integer.parseInt( System.getProperty( "scp.port", "22" ) ); private String knownHosts = System.getProperty("scp.known.hosts"); @@ -57,6 +58,9 @@ public ScpTest(String testname) { super(testname); + if (System.getProperty("scp.tmp") != null) { + tempDir = new File(System.getProperty("scp.tmp")); + } } protected void setUp() { @@ -71,8 +75,10 @@ } public void testSingleFileUploadAndDownload() throws IOException { + assertNotNull("system property scp.tmp must be set", tempDir); File uploadFile = createTemporaryFile(); + // upload Scp scpTask = createTask(); scpTask.setFile( uploadFile.getPath() ); scpTask.setTodir( sshHostUri ); @@ -84,6 +90,8 @@ assertTrue( "Assert that the testFile does not exist.", !testFile.exists() ); + // download + scpTask = createTask(); scpTask.setFile( sshHostUri + "/" + uploadFile.getName() ); scpTask.setTodir( testFile.getPath() ); scpTask.execute(); @@ -93,6 +101,7 @@ } public void testMultiUploadAndDownload() throws IOException { + assertNotNull("system property scp.tmp must be set", tempDir); List uploadList = new ArrayList(); for( int i = 0; i < 5; i++ ) { uploadList.add( createTemporaryFile() ); @@ -128,6 +137,25 @@ } } + public void testRemoteToDir() throws IOException { + Scp scpTask = createTask(); + + // first try an invalid URI + try { + scpTask.setRemoteTodir( "host:/a/path/without/an/at" ); + fail("Expected a BuildException to be thrown due to invalid" + + " remoteToDir"); + } + catch (BuildException e) + { + // expected + } + + // And this one should work + scpTask.setRemoteTodir( "user:[EMAIL PROTECTED]:/a/path/with/an/at" ); + // no exception + } + public void addCleanup( File file ) { cleanUpList.add( file ); }