<commit_before>/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.slick.protocol.generic;

import java.io.*;
import java.util.*;
import junit.framework.*;
import net.java.sip.communicator.service.fileaccess.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import org.osgi.framework.*;

/**
 * Generic test for file transfer operationset.
 *
 * @author <NAME>
 */
public abstract class TestOperationSetFileTransfer
    extends TestCase
{
    private static final Logger logger
        = Logger.getLogger(TestOperationSetFileTransfer.class);

    private FileAccessService fileAccessService = null;

    public TestOperationSetFileTransfer(String name)
    {
        super(name);
    }

    public TestOperationSetFileTransfer()
    {
    }

    protected void setUp()
        throws Exception
    {
        super.setUp();
        start();
    }

    protected void tearDown() throws Exception
    {
        stop();
        super.tearDown();
    }

    private FileAccessService getFileService()
    {
        if(fileAccessService == null)
        {
            ServiceReference ref = getContext()
                .getServiceReference(FileAccessService.class.getName());
            fileAccessService = (FileAccessService)getContext().getService(ref);
        }

        return fileAccessService;
    }

    private File getTempFileToTransfer(int size)
        throws Exception
    {
        File fileToTransfer =
            getFileService().getTemporaryFile();
        byte[] buff = new byte[size];
        Arrays.fill(buff, (byte)1);
        FileOutputStream out = new FileOutputStream(fileToTransfer);
        out.write(buff);
        out.flush();
        out.close();

        return fileToTransfer;
    }

    public void testSendAndReceive()
        throws Exception
    {
        if(!enableTestSendAndReceive())
            return;

        logger.trace("Start test : send and receive ok.");

        File fileToTransfer = getTempFileToTransfer(123456);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferStatusEventCollector receiverStatusListener =
            new FileTransferStatusEventCollector("Receiver");
        FileTransferEventCollector receiverFTListerner =
            new FileTransferEventCollector("Receiver", receiverStatusListener);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(8000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                ,FileTransferStatusChangeEvent.PREPARING
                ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();
            req.acceptFile(receiveFile);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);

            //receiver
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            receiverStatusListener.waitForEvent(30000, 3);
            assertEquals("A file transfer status changed - " +
                "preparing, inprogress and completed received on receiver side"
                         , 3, receiverStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(1);

            assertEquals("Event must be completed"
                         ,FileTransferStatusChangeEvent.IN_PROGRESS
                         ,fileTransferStatusEvent.getNewStatus());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(2);

            assertEquals("Event must be completed"
                         ,FileTransferStatusChangeEvent.COMPLETED
                         ,fileTransferStatusEvent.getNewStatus());

            // sender
            senderStatusListener.waitForEvent(4000, 2);
            assertTrue("Completed event must be received",
                senderStatusListener.contains(FileTransferStatusChangeEvent.COMPLETED));
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testSenderCancelBeforeAccepted()
        throws Exception
    {
        if(!enableTestSenderCancelBeforeAccepted())
            return;

        logger.trace("Start test : sender will cancel transfer before "
            + "receiver accept or decline it");

        File fileToTransfer = getTempFileToTransfer(1234567);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            FileTransfer ft1 = ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(2000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will cancel the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            // now cancel
            ft1.cancel();

            receiverFTListerner.waitForEvent(6000);
            senderStatusListener.waitForEvent(6000);

            // sender
            assertEquals("A file transfer status changed - cancel received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,fileTransferStatusEvent.getNewStatus());

            // the receiver must receive event refused
            assertEquals("A file transfer cancel must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            assertTrue("FileTransfer must be canceled"
                         ,receiverFTListerner.isCanceled());
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testReceiverDecline()
        throws Exception
    {
        if(!enableTestReceiverDecline())
            return;

        logger.trace("Start test : receiver will decline incoming fileTransfer");

        File fileToTransfer = getTempFileToTransfer(12345);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(6000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will decline the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            req.rejectFile();

            senderStatusListener.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);

            // receiver
            assertEquals("A file transfer rejected must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            assertTrue("FileTransfer must be rejected"
                         ,receiverFTListerner.isRejected());

            // sender
            assertEquals("A file transfer status changed - refused received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be refused"
                         ,FileTransferStatusChangeEvent.REFUSED
                         ,fileTransferStatusEvent.getNewStatus());
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testReceiverCancelsWhileTransfering()
        throws Exception
    {
        if(!enableTestReceiverCancelsWhileTransfering())
            return;

        logger.trace("Start test : receiver will cancel fileTransfer whil transfering.");

        File fileToTransfer = getTempFileToTransfer(12345678);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);

        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferStatusEventCollector receiverStatusListener =
            new FileTransferStatusEventCollector("Receiver");

        FileTransferEventCollector receiverFTListerner =
            new FileTransferEventCollector("Receiver", receiverStatusListener);

        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);
            senderStatusListener.waitForEvent(4000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();

            final FileTransfer ft2 = req.acceptFile(receiveFile);

            // wait for preapring
            receiverStatusListener.waitForEvent(16000, 1);

            assertTrue("A file transfer status changed - " +
                "preparing or and inProgress received on receiver side"
                         , receiverStatusListener.collectedEvents.size() >= 1);

            FileTransferStatusChangeEvent stat1 =
                receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,stat1.getNewStatus());

            // now wait if some protocol filres inProgress
            // jabber doesn't fire inProgress here
            // yahoo fires it
            receiverStatusListener.waitForEvent(14000);

            // wait in_progress
            senderStatusListener.waitForEvent(14000);

            receiverStatusListener.collectedEvents.clear();
            senderStatusListener.clear();

            ft2.cancel();

            // now wait for cancel
            receiverStatusListener.waitForEvent(14000);

            FileTransferStatusChangeEvent stat3 =
                receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,stat3.getNewStatus());

            receiverFTListerner.waitForEvent(14000);

            //receiver
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            // sender
            senderStatusListener.waitForEvent(14000, 2);

            assertTrue("Must contain canceled event",
                senderStatusListener.contains(FileTransferStatusChangeEvent.CANCELED));
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testSenderCancelsWhileTransfering()
        throws Exception
    {
        if(!enableTestSenderCancelsWhileTransfering())
            return;

        logger.trace("Start test : Sender will cancel fileTransfer while transfering.");

        File fileToTransfer = getTempFileToTransfer(12345678);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            FileTransfer ft1 = ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);
            senderStatusListener.waitForEvent(4000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();
            FileTransferStatusEventCollector receiverStatusListener =
                new FileTransferStatusEventCollector("Receiver");

            final FileTransfer ft2 = req.acceptFile(receiveFile);
            ft2.addStatusListener(receiverStatusListener);

            // sender
            // now wait for progress
            senderStatusListener.waitForEvent(4000);
            FileTransferStatusChangeEvent stat1 = senderStatusListener.collectedEvents.get(0);
            senderStatusListener.collectedEvents.clear();

            assertEquals("Event must be inProgress"
                         ,FileTransferStatusChangeEvent.IN_PROGRESS
                         ,stat1.getNewStatus());

            ft1.cancel();

            // now wait for cancel
            senderStatusListener.waitForEvent(4000);
            FileTransferStatusChangeEvent stat2 = senderStatusListener.collectedEvents.get(0);
            senderStatusListener.collectedEvents.clear();

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,stat2.getNewStatus());

            //receiver
            receiverFTListerner.waitForEvent(4000);
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            receiverStatusListener.waitForEvent(8000, 3);
            assertTrue("Cancel event must be received",
                receiverStatusListener.contains(FileTransferStatusChangeEvent.CANCELED));
        }
        finally
        {<commit_msg>Filetransfer tests fix: Increase interval for waiting event(when last test failed the event was received 100ms after we stopped waiting).<commit_after>/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.slick.protocol.generic;

import java.io.*;
import java.util.*;
import junit.framework.*;
import net.java.sip.communicator.service.fileaccess.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import org.osgi.framework.*;

/**
 * Generic test for file transfer operationset.
 *
 * @author <NAME>
 */
public abstract class TestOperationSetFileTransfer
    extends TestCase
{
    private static final Logger logger
        = Logger.getLogger(TestOperationSetFileTransfer.class);

    private FileAccessService fileAccessService = null;

    public TestOperationSetFileTransfer(String name)
    {
        super(name);
    }

    public TestOperationSetFileTransfer()
    {
    }

    protected void setUp()
        throws Exception
    {
        super.setUp();
        start();
    }

    protected void tearDown() throws Exception
    {
        stop();
        super.tearDown();
    }

    private FileAccessService getFileService()
    {
        if(fileAccessService == null)
        {
            ServiceReference ref = getContext()
                .getServiceReference(FileAccessService.class.getName());
            fileAccessService = (FileAccessService)getContext().getService(ref);
        }

        return fileAccessService;
    }

    private File getTempFileToTransfer(int size)
        throws Exception
    {
        File fileToTransfer =
            getFileService().getTemporaryFile();
        byte[] buff = new byte[size];
        Arrays.fill(buff, (byte)1);
        FileOutputStream out = new FileOutputStream(fileToTransfer);
        out.write(buff);
        out.flush();
        out.close();

        return fileToTransfer;
    }

    public void testSendAndReceive()
        throws Exception
    {
        if(!enableTestSendAndReceive())
            return;

        logger.trace("Start test : send and receive ok.");

        File fileToTransfer = getTempFileToTransfer(123456);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferStatusEventCollector receiverStatusListener =
            new FileTransferStatusEventCollector("Receiver");
        FileTransferEventCollector receiverFTListerner =
            new FileTransferEventCollector("Receiver", receiverStatusListener);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(8000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                ,FileTransferStatusChangeEvent.PREPARING
                ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();
            req.acceptFile(receiveFile);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);

            //receiver
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            receiverStatusListener.waitForEvent(30000, 3);
            assertEquals("A file transfer status changed - " +
                "preparing, inprogress and completed received on receiver side"
                         , 3, receiverStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(1);

            assertEquals("Event must be completed"
                         ,FileTransferStatusChangeEvent.IN_PROGRESS
                         ,fileTransferStatusEvent.getNewStatus());

            fileTransferStatusEvent
                = receiverStatusListener.collectedEvents.get(2);

            assertEquals("Event must be completed"
                         ,FileTransferStatusChangeEvent.COMPLETED
                         ,fileTransferStatusEvent.getNewStatus());

            // sender
            senderStatusListener.waitForEvent(4000, 2);
            assertTrue("Completed event must be received",
                senderStatusListener.contains(FileTransferStatusChangeEvent.COMPLETED));
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testSenderCancelBeforeAccepted()
        throws Exception
    {
        if(!enableTestSenderCancelBeforeAccepted())
            return;

        logger.trace("Start test : sender will cancel transfer before "
            + "receiver accept or decline it");

        File fileToTransfer = getTempFileToTransfer(1234567);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            FileTransfer ft1 = ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(2000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will cancel the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            // now cancel
            ft1.cancel();

            receiverFTListerner.waitForEvent(6000);
            senderStatusListener.waitForEvent(6000);

            // sender
            assertEquals("A file transfer status changed - cancel received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,fileTransferStatusEvent.getNewStatus());

            // the receiver must receive event refused
            assertEquals("A file transfer cancel must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            assertTrue("FileTransfer must be canceled"
                         ,receiverFTListerner.isCanceled());
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testReceiverDecline()
        throws Exception
    {
        if(!enableTestReceiverDecline())
            return;

        logger.trace("Start test : receiver will decline incoming fileTransfer");

        File fileToTransfer = getTempFileToTransfer(12345);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(2000);
            receiverFTListerner.waitForEvent(6000);
            senderStatusListener.waitForEvent(2000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will decline the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            req.rejectFile();

            senderStatusListener.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);

            // receiver
            assertEquals("A file transfer rejected must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            assertTrue("FileTransfer must be rejected"
                         ,receiverFTListerner.isRejected());

            // sender
            assertEquals("A file transfer status changed - refused received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be refused"
                         ,FileTransferStatusChangeEvent.REFUSED
                         ,fileTransferStatusEvent.getNewStatus());
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testReceiverCancelsWhileTransfering()
        throws Exception
    {
        if(!enableTestReceiverCancelsWhileTransfering())
            return;

        logger.trace("Start test : receiver will cancel fileTransfer whil transfering.");

        File fileToTransfer = getTempFileToTransfer(12345678);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);

        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferStatusEventCollector receiverStatusListener =
            new FileTransferStatusEventCollector("Receiver");

        FileTransferEventCollector receiverFTListerner =
            new FileTransferEventCollector("Receiver", receiverStatusListener);

        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);
            senderStatusListener.waitForEvent(4000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();

            final FileTransfer ft2 = req.acceptFile(receiveFile);

            // wait for preapring
            receiverStatusListener.waitForEvent(16000, 1);

            assertTrue("A file transfer status changed - " +
                "preparing or and inProgress received on receiver side"
                         , receiverStatusListener.collectedEvents.size() >= 1);

            FileTransferStatusChangeEvent stat1 =
                receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,stat1.getNewStatus());

            // now wait if some protocol filres inProgress
            // jabber doesn't fire inProgress here
            // yahoo fires it
            receiverStatusListener.waitForEvent(14000);

            // wait in_progress
            senderStatusListener.waitForEvent(14000);

            receiverStatusListener.collectedEvents.clear();
            senderStatusListener.clear();

            ft2.cancel();

            // now wait for cancel
            receiverStatusListener.waitForEvent(14000);

            FileTransferStatusChangeEvent stat3 =
                receiverStatusListener.collectedEvents.get(0);

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,stat3.getNewStatus());

            receiverFTListerner.waitForEvent(14000);

            //receiver
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            // sender
            senderStatusListener.waitForEvent(14000, 2);

            assertTrue("Must contain canceled event",
                senderStatusListener.contains(FileTransferStatusChangeEvent.CANCELED));
        }
        finally
        {
            ftOpSet1.removeFileTransferListener(senderFTListerner);
            ftOpSet2.addFileTransferListener(receiverFTListerner);
        }
    }

    public void testSenderCancelsWhileTransfering()
        throws Exception
    {
        if(!enableTestSenderCancelsWhileTransfering())
            return;

        logger.trace("Start test : Sender will cancel fileTransfer while transfering.");

        File fileToTransfer = getTempFileToTransfer(12345678);

        OperationSetFileTransfer ftOpSet1 = getOpSetFilTransfer1();
        OperationSetFileTransfer ftOpSet2 = getOpSetFilTransfer2();

        FileTransferStatusEventCollector senderStatusListener =
            new FileTransferStatusEventCollector("Sender");

        FileTransferEventCollector senderFTListerner =
            new FileTransferEventCollector("Sender", senderStatusListener);
        ftOpSet1.addFileTransferListener(senderFTListerner);

        FileTransferEventCollector receiverFTListerner = 
            new FileTransferEventCollector("Receiver", null);
        ftOpSet2.addFileTransferListener(receiverFTListerner);

        try
        {
            FileTransfer ft1 = ftOpSet1.sendFile(getContact1(), fileToTransfer);

            senderFTListerner.waitForEvent(4000);
            receiverFTListerner.waitForEvent(4000);
            senderStatusListener.waitForEvent(4000);

            // sender
            assertEquals("A file transfer created must be received on send side"
                         , 1, senderFTListerner.collectedEvents.size());

            FileTransferCreatedEvent fileTransferCreatedEvent
                = (FileTransferCreatedEvent)senderFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,fileToTransfer);

            assertEquals("A file transfer status changed - preparing received on send side"
                         , 1, senderStatusListener.collectedEvents.size());

            FileTransferStatusChangeEvent fileTransferStatusEvent
                = senderStatusListener.collectedEvents.get(0);

            assertEquals("Event must be preparing"
                         ,FileTransferStatusChangeEvent.PREPARING
                         ,fileTransferStatusEvent.getNewStatus());

            // receiver
            assertEquals("A file transfer request must be received on the receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            FileTransferRequestEvent fileTransferRequestEvent
                = (FileTransferRequestEvent)receiverFTListerner.collectedEvents.get(0);

            IncomingFileTransferRequest req =
                fileTransferRequestEvent.getRequest();

            assertEquals("FileTransfer file name must be the same"
                         ,req.getFileName()
                         ,fileToTransfer.getName());
            assertEquals("FileTransfer file size must be the same"
                         ,req.getFileSize()
                         ,fileToTransfer.length());

            // now we will accpet the file
            // but let first clear the event listeners
            senderFTListerner.clear();
            receiverFTListerner.clear();
            senderStatusListener.clear();

            File receiveFile = getFileService().getTemporaryFile();
            FileTransferStatusEventCollector receiverStatusListener =
                new FileTransferStatusEventCollector("Receiver");

            final FileTransfer ft2 = req.acceptFile(receiveFile);
            ft2.addStatusListener(receiverStatusListener);

            // sender
            // now wait for progress
            senderStatusListener.waitForEvent(4000);
            FileTransferStatusChangeEvent stat1 = senderStatusListener.collectedEvents.get(0);
            senderStatusListener.collectedEvents.clear();

            assertEquals("Event must be inProgress"
                         ,FileTransferStatusChangeEvent.IN_PROGRESS
                         ,stat1.getNewStatus());

            ft1.cancel();

            // now wait for cancel
            senderStatusListener.waitForEvent(4000);
            FileTransferStatusChangeEvent stat2 = senderStatusListener.collectedEvents.get(0);
            senderStatusListener.collectedEvents.clear();

            assertEquals("Event must be canceled"
                         ,FileTransferStatusChangeEvent.CANCELED
                         ,stat2.getNewStatus());

            //receiver
            receiverFTListerner.waitForEvent(4000);
            assertEquals("A file transfer created must be received on receiver side"
                         , 1, receiverFTListerner.collectedEvents.size());

            fileTransferCreatedEvent
                = (FileTransferCreatedEvent)receiverFTListerner.collectedEvents.get(0);

            assertEquals("FileTransfer file"
                         ,fileTransferCreatedEvent.getFileTransfer().getLocalFile()
                         ,receiveFile);

            receiverStatusListener.waitForEvent(12000, 3);
            assertTrue("Cancel event must be received",
                receiverStatusListener.contains(FileTransferStatusChangeEvent.CANCELED));
        }
        finally
        {
{'fraction_non_alphanumeric': 0.06933831748300441, 'fraction_numerical': 0.010921151485166657, 'mean_word_length': 2.129422558632949, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5760619', 'n_tokens_mistral': 12947, 'n_tokens_neox': 12074, 'n_words': 2405}