Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23ce5f7b1f55b0060b13a48b0633da914e777c57 (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
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/

package org.eclipse.ecf.tests.provider.filetransfer.xmpp;

import java.io.File;
import java.io.FileOutputStream;

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.identity.Namespace;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IIncomingFileTransferRequestListener;
import org.eclipse.ecf.filetransfer.ISendFileTransferContainerAdapter;
import org.eclipse.ecf.filetransfer.events.IFileTransferEvent;
import org.eclipse.ecf.filetransfer.events.IFileTransferRequestEvent;
import org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent;
import org.eclipse.ecf.filetransfer.identity.FileCreateException;
import org.eclipse.ecf.filetransfer.identity.FileIDFactory;
import org.eclipse.ecf.filetransfer.identity.IFileID;
import org.eclipse.ecf.tests.ContainerAbstractTestCase;

/**
 * 
 */
public class XMPPOutgoingTest extends ContainerAbstractTestCase {

	private static final String TESTSRCPATH = "test.src";
	private static final String TESTSRCFILE = TESTSRCPATH + "/test.txt";

	private static final String TESTTARGETPATH = "test.target";

	static final String XMPP_CONTAINER = "ecf.xmpp.smack";

	protected ISendFileTransferContainerAdapter adapter0, adapter1 = null;

	protected String getClientContainerName() {
		return XMPP_CONTAINER;
	}

	protected ISendFileTransferContainerAdapter getOutgoingFileTransfer(int client) {
		final IContainer c = getClient(client);
		if (c != null)
			return (ISendFileTransferContainerAdapter) c.getAdapter(ISendFileTransferContainerAdapter.class);
		else
			return null;
	}

	protected IFileTransferListener getFileTransferListener(final String prefix) {
		return new IFileTransferListener() {
			public void handleTransferEvent(IFileTransferEvent event) {
				System.out.println(prefix + ".handleTransferEvent(" + event + ")");
				if (event instanceof IIncomingFileTransferReceiveDoneEvent) {

				}
			}
		};
	}

	File incomingDirectory = null;
	File incomingFile = null;

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		if (incomingFile != null)
			incomingFile.delete();
		incomingFile = null;
		if (incomingDirectory != null)
			incomingDirectory.delete();
		incomingDirectory = null;
	}

	protected IIncomingFileTransferRequestListener requestListener = new IIncomingFileTransferRequestListener() {

		public void handleFileTransferRequest(IFileTransferRequestEvent event) {
			System.out.println("receiver.handleFileTransferRequest(" + event + ")");
			incomingDirectory = new File(TESTTARGETPATH);
			incomingDirectory.mkdirs();
			incomingFile = new File(incomingDirectory, event.getFileTransferInfo().getFile().getName());
			try {
				FileOutputStream fos = new FileOutputStream(incomingFile);
				event.accept(fos, receiverTransferListener);
				//event.accept(f);
			} catch (Exception e) {
				e.printStackTrace(System.err);
				fail("exception calling accept for receive file transfer");
			}
		}

	};

	protected IFileTransferListener senderTransferListener = getFileTransferListener("sender");
	protected IFileTransferListener receiverTransferListener = getFileTransferListener("receiver");

	protected ID getServerConnectID(int client) {
		final IContainer container = getClient(client);
		final Namespace connectNamespace = container.getConnectNamespace();
		final String username = getUsername(client);
		try {
			return IDFactory.getDefault().createID(connectNamespace, username);
		} catch (final IDCreateException e) {
			e.printStackTrace(System.err);
			fail("Could not create server connect ID");
			return null;
		}
	}

	protected IFileID createFileID(ISendFileTransferContainerAdapter adapter, ID clientID, String filename) throws FileCreateException {
		return FileIDFactory.getDefault().createFileID(adapter.getOutgoingNamespace(), new Object[] {clientID, filename});
	}

	public void testTwoClientsToSendAndReceive() throws Exception {
		// Setup two clients.  Client 0 is the receiver, client 1 is the sender
		setClientCount(2);
		clients = createClients();
		adapter0 = getOutgoingFileTransfer(0);
		adapter0.addListener(requestListener);
		adapter1 = getOutgoingFileTransfer(1);
		for (int i = 0; i < 2; i++) {
			connectClient(i);
		}

		final IFileID targetID = createFileID(adapter1, getServerConnectID(0), TESTSRCFILE);
		adapter1.sendOutgoingRequest(targetID, new File(TESTSRCFILE), senderTransferListener, null);

		sleep(20000);

		disconnectClients();

	}

}

Back to the top