Browse Source

fail with a meaningful error of remoteDir is not parseable. PR 42770. Submitted by Scott Johnson

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@677206 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
9ffa99a029
4 changed files with 51 additions and 3 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      contributors.xml
  3. +17
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java
  4. +29
    -1
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java

+ 1
- 0
CONTRIBUTORS View File

@@ -239,6 +239,7 @@ Sam Ruby
Sandra Metz
Scott Carlson
Scott Ellsworth
Scott Johnson
Scott M. Stirling
Sean Egan
Sean P. Kane


+ 4
- 0
contributors.xml View File

@@ -967,6 +967,10 @@
<first>Scott</first>
<last>Ellsworth</last>
</name>
<name>
<first>Scott</first>
<last>Johnson</last>
</name>
<name>
<first>Scott</first>
<middle>M.</middle>


+ 17
- 2
src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java View File

@@ -101,6 +101,7 @@ public class Scp extends SSHBase {
* @since Ant 1.6.2
*/
public void setRemoteFile(String aFromUri) {
validateRemoteUri("remoteFile", aFromUri);
setFromUri(aFromUri);
this.isFromRemote = true;
}
@@ -133,10 +134,21 @@ public class Scp extends SSHBase {
* @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:password@host:/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 @@ public class Scp extends SSHBase {
* @since Ant 1.6.2
*/
public void setRemoteTofile(String aToUri) {
validateRemoteUri("remoteToFile", aToUri);
setToUri(aToUri);
this.isToRemote = true;
}
@@ -339,9 +352,11 @@ public class Scp extends SSHBase {
}
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 @@ public class Scp extends SSHBase {
return remotePath;
}

private boolean isRemoteUri(String uri) {
private static boolean isRemoteUri(String uri) {
boolean isRemote = true;
int indexOfAt = uri.indexOf('@');
if (indexOfAt < 0) {


+ 29
- 1
src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java View File

@@ -25,6 +25,7 @@ import java.util.List;
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 @@ import org.apache.tools.ant.types.selectors.FilenameSelector;
*/
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 class ScpTest extends TestCase {

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 class ScpTest extends TestCase {
}

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 @@ public class ScpTest extends TestCase {
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 class ScpTest extends TestCase {
}

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 class ScpTest extends TestCase {
}
}

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:password@host:/a/path/with/an/at" );
// no exception
}
public void addCleanup( File file ) {
cleanUpList.add( file );
}


Loading…
Cancel
Save