Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6b5f26d84cd38bbbaddb0f22fa6d99f84307bd82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package org.eclipse.ptp.core.tests;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.tests.harness.CoreTest;
import org.eclipse.ptp.remote.core.IRemoteConnection;
import org.eclipse.ptp.remote.core.IRemoteConnectionManager;
import org.eclipse.ptp.remote.core.IRemoteFileManager;
import org.eclipse.ptp.remote.core.IRemoteServices;
import org.eclipse.ptp.remote.core.PTPRemoteCorePlugin;
import org.eclipse.ptp.remote.core.exception.RemoteConnectionException;

public class FileStoreTests extends CoreTest {
	private static final String CONNECTION_NAME = "test_connection";
	private static final String USERNAME = "user";
	private static final String PASSWORD = "password";
	private static final String HOST = "localhost";
	private static final String PATH1 = "/home/user/sftp_test";
	private static final String PATH2 = PATH1 + "/.file1";
	private static final String TEST_STRING = "a string containing fairly *()(*&^$%## random text";

	private IRemoteServices fRemoteServices;
	private IRemoteConnection fRemoteConnection;
	private IRemoteFileManager fRemoteFileManager;
	
	public void testFileStore() {
		URI	path1Uri = fRemoteFileManager.toURI(PATH1);
		URI	path2Uri = fRemoteFileManager.toURI(PATH2);
		assertNotNull(path1Uri);
		assertNotNull(path2Uri);
		
		IFileStore store1 = null;
		IFileStore store2 = null;
		
		try {
			store1 = EFS.getStore(path1Uri);
			store2 = EFS.getStore(path2Uri);
		} catch (CoreException e) {
			fail("2.0", e);
		}
		
		for (int i = 0; i < 5; i++) {
			assertFalse(store1.fetchInfo().exists());
			try {
				store1.mkdir(EFS.NONE, null);
			} catch (CoreException e) {
				fail("3.0", e);
			}
			assertTrue(store1.fetchInfo().exists());
			
			assertFalse(store2.fetchInfo().exists());
			try {
				OutputStream stream = store2.openOutputStream(EFS.NONE, null);
				assertNotNull(stream);
				BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(stream));
				buf.write(TEST_STRING);
				buf.close();
			} catch (Exception e) {
				fail("4.0", e);
			}
			assertTrue(store2.fetchInfo().exists());
			
			try {
				InputStream stream = store2.openInputStream(EFS.NONE, null);
				assertNotNull(stream);
				BufferedReader buf = new BufferedReader(new InputStreamReader(stream));
				String line = buf.readLine().trim();
				assertTrue(line.equals(TEST_STRING));
				buf.close();
			} catch (Exception e) {
				fail("5.0", e);
			}
	
			try {
				store2.delete(EFS.NONE, null);
			} catch (CoreException e) {
				fail("6.0", e);
			}
			assertFalse(store2.fetchInfo().exists());
	
			try {
				store1.delete(EFS.NONE, null);
			} catch (CoreException e) {
				fail("7.0", e);
			}
			assertFalse(store1.fetchInfo().exists());
		}

	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		fRemoteServices = PTPRemoteCorePlugin.getDefault().getRemoteServices("org.eclipse.ptp.remote.RemoteTools");
		assertNotNull(fRemoteServices);
		
		IRemoteConnectionManager connMgr = fRemoteServices.getConnectionManager();
		assertNotNull(connMgr);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("ptp.localhost-selection", "false");
		map.put("ptp.login-username", USERNAME);
		map.put("ptp.login-password", PASSWORD);
		map.put("ptp.connection-address", HOST);
		map.put("ptp.connection-port", "22");
		map.put("ptp.key-path", "");
		map.put("ptp.key-passphrase", "");
		map.put("ptp.is-passwd-auth", "true");
		map.put("ptp.connection-timeout", "5");
		map.put("ptp.cipher-type", "default");
		
		try {
			fRemoteConnection = connMgr.newConnection(CONNECTION_NAME, map);
		} catch (RemoteConnectionException e) {
			fail(e.getLocalizedMessage());
		}
		assertNotNull(fRemoteConnection);
		
		fRemoteFileManager = fRemoteServices.getFileManager(fRemoteConnection);
		assertNotNull(fRemoteFileManager);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		fRemoteConnection.close();
		IRemoteConnectionManager connMgr = fRemoteServices.getConnectionManager();
		assertNotNull(connMgr);
		connMgr.removeConnection(fRemoteConnection);
	}
	
}

Back to the top