Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0926d40757d69bbe671d1e674b5e661c87ac953a (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
/****************************************************************************
 * Copyright (c) 2010 Composent, Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *   Composent, Inc. - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.tests.filetransfer;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.Map;

import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.util.Proxy;
import org.eclipse.ecf.filetransfer.IFileRangeSpecification;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IncomingFileTransferException;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent;
import org.eclipse.ecf.filetransfer.identity.IFileID;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransfer;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransferFactory;
import org.eclipse.ecf.provider.filetransfer.IFileTransferProtocolToFactoryMapper;

public class URIProtocolFactoryRetrieveTest extends AbstractRetrieveTestCase {

	private static final String FOO_URI = "foo:bar";

	private IFileTransferProtocolToFactoryMapper mapper;
	private IRetrieveFileTransferFactory retrieveFactory;
	private URI uri;
	
	protected void setUp() throws Exception {
		super.setUp();
		mapper = getProtocolToFactoryMapper();
		retrieveFactory = createRetrieveFactory();
		uri = new URI(FOO_URI);
	}
	
	protected void tearDown() throws Exception {
		mapper = null;
		retrieveFactory = null;
		uri = null;
		super.tearDown();
	}
	
	public static IFileTransferProtocolToFactoryMapper getProtocolToFactoryMapper() {
		return Activator.getDefault().getProtocolToFactoryMapper();
	}

	private boolean setRetrieveFileTransferFactory() {
		return mapper.setRetrieveFileTransferFactory(uri.getScheme(), retrieveFactory.getClass().getName(), retrieveFactory, 0, true);
	}
	
	private boolean removeRetrieveFileTransferFactory() {
		return mapper.removeRetrieveFileTransferFactory(retrieveFactory.getClass().getName());
	}
	
	public void testSetRetrieveFileTransferFactory() throws Exception {
		assertTrue(setRetrieveFileTransferFactory());
		removeRetrieveFileTransferFactory();
	}
	
	public void testURIProtocolFactoryRetrieve() throws Exception {
		
		setRetrieveFileTransferFactory();
		// Call sendRetrieveRequest on retrieveAdapter
		retrieveAdapter.sendRetrieveRequest(createFileID(uri), createFileTransferListener(), null);
		// no waiting for events necessary, as it should be using the dummy IRetrieveFileTransfer
		// instance below
		
		removeRetrieveFileTransferFactory();
		
	}

	public void testURIProtocolFactoryRetrieveFail() throws Exception {
		// Call sendRetrieveRequest on retrieveAdapter...this should fail as the 
		// protocol factory hasn't been set
		retrieveAdapter.sendRetrieveRequest(createFileID(uri), createFileTransferListener(), null);
		waitForDone(1000);
		assertTrue(doneEvents.size() > 0);
		IIncomingFileTransferReceiveDoneEvent doneEvent = (IIncomingFileTransferReceiveDoneEvent) doneEvents.get(0);
		Exception e = doneEvent.getException();
		assertNotNull(e);
		assertTrue(e instanceof MalformedURLException);
	}

	private IRetrieveFileTransferFactory createRetrieveFactory() {
		return new IRetrieveFileTransferFactory() {

			public IRetrieveFileTransfer newInstance() {
				return new IRetrieveFileTransfer() {

					public void sendRetrieveRequest(IFileID remoteFileID,
							IFileTransferListener transferListener, Map options)
							throws IncomingFileTransferException {
						System.out.println("sendRetrieveRequest("+remoteFileID+","+transferListener+","+options);
					}

					public void sendRetrieveRequest(IFileID remoteFileID,
							IFileRangeSpecification rangeSpecification,
							IFileTransferListener transferListener, Map options)
							throws IncomingFileTransferException {
						System.out.println("sendRetrieveRequest("+remoteFileID+","+transferListener+","+options);
					}

					public Namespace getRetrieveNamespace() {
						return null;
					}

					public void setConnectContextForAuthentication(
							IConnectContext connectContext) {
					}

					public void setProxy(Proxy proxy) {
					}

					public Object getAdapter(Class adapter) {
						return null;
					}};
			}};
	}
}

Back to the top