1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

Added remote connection checks. Updates to unit tests.

Signed-off-by: Greg Watson <g.watson@computer.org>
This commit is contained in:
Greg Watson 2013-08-22 15:44:48 -04:00
parent 3cb6c62acb
commit 96b3b2b6e0
6 changed files with 41 additions and 13 deletions

View file

@ -13,6 +13,7 @@ import junit.framework.TestCase;
import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.remote.core.IRemoteConnection; import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionManager; import org.eclipse.remote.core.IRemoteConnectionManager;
import org.eclipse.remote.core.IRemoteFileManager; import org.eclipse.remote.core.IRemoteFileManager;
@ -25,7 +26,7 @@ public class FileStoreTests extends TestCase {
private static final String USERNAME = "user"; private static final String USERNAME = "user";
private static final String PASSWORD = "password"; private static final String PASSWORD = "password";
private static final String HOST = "localhost"; private static final String HOST = "localhost";
private static final String PATH1 = "/home/user/sftp_test"; private static final String PATH1 = "/tmp/sftp_test";
private static final String PATH2 = PATH1 + "/.file1"; private static final String PATH2 = PATH1 + "/.file1";
private static final String TEST_STRING = "a string containing fairly *()(*&^$%## random text"; private static final String TEST_STRING = "a string containing fairly *()(*&^$%## random text";
@ -51,14 +52,16 @@ public class FileStoreTests extends TestCase {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
assertFalse(store1.fetchInfo().exists()); assertFalse(store1.fetchInfo().exists());
try { try {
store1.mkdir(EFS.NONE, null); store1.mkdir(EFS.NONE, null);
} catch (CoreException e) { } catch (CoreException e) {
e.getLocalizedMessage(); e.getLocalizedMessage();
} }
assertTrue(store1.fetchInfo().exists());
assertTrue(store1.fetchInfo().exists());
assertFalse(store2.fetchInfo().exists()); assertFalse(store2.fetchInfo().exists());
try { try {
OutputStream stream = store2.openOutputStream(EFS.NONE, null); OutputStream stream = store2.openOutputStream(EFS.NONE, null);
assertNotNull(stream); assertNotNull(stream);
@ -68,6 +71,7 @@ public class FileStoreTests extends TestCase {
} catch (Exception e) { } catch (Exception e) {
e.getLocalizedMessage(); e.getLocalizedMessage();
} }
assertTrue(store2.fetchInfo().exists()); assertTrue(store2.fetchInfo().exists());
try { try {
@ -86,6 +90,7 @@ public class FileStoreTests extends TestCase {
} catch (CoreException e) { } catch (CoreException e) {
e.getLocalizedMessage(); e.getLocalizedMessage();
} }
assertFalse(store2.fetchInfo().exists()); assertFalse(store2.fetchInfo().exists());
try { try {
@ -93,6 +98,7 @@ public class FileStoreTests extends TestCase {
} catch (CoreException e) { } catch (CoreException e) {
e.getLocalizedMessage(); e.getLocalizedMessage();
} }
assertFalse(store1.fetchInfo().exists()); assertFalse(store1.fetchInfo().exists());
} }
@ -121,6 +127,13 @@ public class FileStoreTests extends TestCase {
fRemoteConnection.setUsername(USERNAME); fRemoteConnection.setUsername(USERNAME);
fRemoteConnection.setPassword(PASSWORD); fRemoteConnection.setPassword(PASSWORD);
try {
fRemoteConnection.open(new NullProgressMonitor());
} catch (RemoteConnectionException e) {
fail(e.getMessage());
}
assertTrue(fRemoteConnection.isOpen());
fRemoteFileManager = fRemoteServices.getFileManager(fRemoteConnection); fRemoteFileManager = fRemoteServices.getFileManager(fRemoteConnection);
assertNotNull(fRemoteFileManager); assertNotNull(fRemoteFileManager);
} }

View file

@ -5,7 +5,6 @@ import junit.framework.TestSuite;
import org.eclipse.remote.core.tests.ConnectionTests; import org.eclipse.remote.core.tests.ConnectionTests;
import org.eclipse.remote.core.tests.FileStoreTests; import org.eclipse.remote.core.tests.FileStoreTests;
import org.eclipse.remote.core.tests.ProcessTests;
public class RemoteCoreTestSuite { public class RemoteCoreTestSuite {
public static Test suite() { public static Test suite() {
@ -13,7 +12,7 @@ public class RemoteCoreTestSuite {
suite.addTestSuite(ConnectionTests.class); suite.addTestSuite(ConnectionTests.class);
suite.addTestSuite(FileStoreTests.class); suite.addTestSuite(FileStoreTests.class);
suite.addTestSuite(ProcessTests.class); // suite.addTestSuite(ProcessTests.class);
return suite; return suite;
} }

View file

@ -23,6 +23,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.internal.remote.jsch.core.messages.Messages;
import org.eclipse.remote.core.AbstractRemoteProcessBuilder; import org.eclipse.remote.core.AbstractRemoteProcessBuilder;
import org.eclipse.remote.core.IRemoteProcess; import org.eclipse.remote.core.IRemoteProcess;
import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.core.exception.RemoteConnectionException;
@ -109,6 +110,10 @@ public class JSchProcessBuilder extends AbstractRemoteProcessBuilder {
*/ */
@Override @Override
public IRemoteProcess start(int flags) throws IOException { public IRemoteProcess start(int flags) throws IOException {
if (!fConnection.isOpen()) {
throw new IOException(Messages.JSchProcessBuilder_Connection_is_not_open);
}
List<String> cmdArgs = command(); List<String> cmdArgs = command();
if (cmdArgs.size() < 1) { if (cmdArgs.size() < 1) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -172,7 +177,7 @@ public class JSchProcessBuilder extends AbstractRemoteProcessBuilder {
ChannelExec exec = fConnection.getExecChannel(); ChannelExec exec = fConnection.getExecChannel();
String command = buildCommand(remoteCmd, env, clearEnv); String command = buildCommand(remoteCmd, env, clearEnv);
exec.setCommand(command); exec.setCommand(command);
System.out.println("running command: " + command); System.out.println("running command: " + command); //$NON-NLS-1$
exec.setPty((flags & ALLOCATE_PTY) == ALLOCATE_PTY); exec.setPty((flags & ALLOCATE_PTY) == ALLOCATE_PTY);
exec.setXForwarding((flags & FORWARD_X11) == FORWARD_X11); exec.setXForwarding((flags & FORWARD_X11) == FORWARD_X11);
exec.connect(); exec.connect();

View file

@ -43,8 +43,6 @@ import org.eclipse.remote.core.IRemoteServices;
import org.eclipse.remote.core.RemoteServices; import org.eclipse.remote.core.RemoteServices;
import org.eclipse.remote.core.exception.RemoteConnectionException; import org.eclipse.remote.core.exception.RemoteConnectionException;
import com.jcraft.jsch.ChannelSftp;
public class JschFileStore extends FileStore { public class JschFileStore extends FileStore {
private static Map<String, JschFileStore> instanceMap = new HashMap<String, JschFileStore>(); private static Map<String, JschFileStore> instanceMap = new HashMap<String, JschFileStore>();
@ -85,6 +83,12 @@ public class JschFileStore extends FileStore {
fRemotePath = new Path(path); fRemotePath = new Path(path);
} }
private void checkConnection() throws RemoteConnectionException {
if (!fConnection.isOpen()) {
throw new RemoteConnectionException(Messages.JschFileStore_Connection_is_not_open);
}
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
@ -93,6 +97,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException { public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 10); SubMonitor subMon = SubMonitor.convert(monitor, 10);
ChildInfosCommand command = new ChildInfosCommand(fConnection, fRemotePath); ChildInfosCommand command = new ChildInfosCommand(fConnection, fRemotePath);
return command.getResult(subMon.newChild(10)); return command.getResult(subMon.newChild(10));
@ -123,6 +128,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public void delete(int options, IProgressMonitor monitor) throws CoreException { public void delete(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 20); SubMonitor subMon = SubMonitor.convert(monitor, 20);
IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10)); IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10));
if (!subMon.isCanceled() && info.exists()) { if (!subMon.isCanceled() && info.exists()) {
@ -139,6 +145,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException { public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 10); SubMonitor subMon = SubMonitor.convert(monitor, 10);
FetchInfoCommand command = new FetchInfoCommand(fConnection, fRemotePath); FetchInfoCommand command = new FetchInfoCommand(fConnection, fRemotePath);
return command.getResult(subMon.newChild(10)); return command.getResult(subMon.newChild(10));
@ -205,6 +212,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException { public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 20); SubMonitor subMon = SubMonitor.convert(monitor, 20);
IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10)); IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10));
@ -238,6 +246,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException { public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 30); SubMonitor subMon = SubMonitor.convert(monitor, 30);
IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10)); IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10));
if (!subMon.isCanceled()) { if (!subMon.isCanceled()) {
@ -263,6 +272,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException { public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 30); SubMonitor subMon = SubMonitor.convert(monitor, 30);
IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10)); IFileInfo info = fetchInfo(EFS.NONE, subMon.newChild(10));
if (!subMon.isCanceled()) { if (!subMon.isCanceled()) {
@ -285,6 +295,7 @@ public class JschFileStore extends FileStore {
*/ */
@Override @Override
public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException { public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException {
checkConnection();
SubMonitor subMon = SubMonitor.convert(monitor, 10); SubMonitor subMon = SubMonitor.convert(monitor, 10);
PutInfoCommand command = new PutInfoCommand(fConnection, info, options, fRemotePath); PutInfoCommand command = new PutInfoCommand(fConnection, info, options, fRemotePath);
command.getResult(subMon.newChild(10)); command.getResult(subMon.newChild(10));
@ -299,10 +310,4 @@ public class JschFileStore extends FileStore {
public URI toURI() { public URI toURI() {
return JSchFileSystem.getURIFor(fConnection.getName(), fRemotePath.toString()); return JSchFileSystem.getURIFor(fConnection.getName(), fRemotePath.toString());
} }
private ChannelSftp getChannel(IProgressMonitor monitor) throws RemoteConnectionException {
JSchFileSystem fileSystem = (JSchFileSystem) getFileSystem();
return fileSystem.getChannel(fConnection);
}
} }

View file

@ -38,6 +38,10 @@ public class Messages extends NLS {
public static String JSchConnectionManager_connection_with_this_name_exists; public static String JSchConnectionManager_connection_with_this_name_exists;
public static String JSchConnectionManager_cannotRemoveOpenConnection; public static String JSchConnectionManager_cannotRemoveOpenConnection;
public static String JSchConnectionManager_invalidConnectionType; public static String JSchConnectionManager_invalidConnectionType;
public static String JSchProcessBuilder_Connection_is_not_open;
public static String JschFileStore_Connection_is_not_open;
public static String JschFileStore_File_doesnt_exist; public static String JschFileStore_File_doesnt_exist;
public static String JschFileStore_Is_a_directory; public static String JschFileStore_Is_a_directory;
public static String JschFileStore_The_file_of_name_already_exists; public static String JschFileStore_The_file_of_name_already_exists;

View file

@ -25,6 +25,8 @@ JSchConnection_username_must_be_set=Username must be set before opening connecti
JSchConnectionManager_connection_with_this_name_exists=A connection with this name already exists JSchConnectionManager_connection_with_this_name_exists=A connection with this name already exists
JSchConnectionManager_cannotRemoveOpenConnection=Cannot remove an open connection JSchConnectionManager_cannotRemoveOpenConnection=Cannot remove an open connection
JSchConnectionManager_invalidConnectionType=Invalid connection type JSchConnectionManager_invalidConnectionType=Invalid connection type
JSchProcessBuilder_Connection_is_not_open=Connection is not open
JschFileStore_Connection_is_not_open=Connection is not open
JschFileStore_File_doesnt_exist=File {0} doesn't exist JschFileStore_File_doesnt_exist=File {0} doesn't exist
JschFileStore_Is_a_directory={0} is a directory JschFileStore_Is_a_directory={0} is a directory
JschFileStore_The_file_of_name_already_exists=A file of name {0} already exists JschFileStore_The_file_of_name_already_exists=A file of name {0} already exists