public void testAsyncProtocolFileTransferCancel() throws AsyncTransferException, InterruptedException, FileNotFoundException { ClassPathResource theFile = new ClassPathResource("chabernac/protocol/asyncfiletransfer/mars_1k_color.jpg"); RefuseTransferListener theAcceptListener = new RefuseTransferListener(); myTransferProtocol2.addTransferListener(theAcceptListener); AbstractTransferState theSendState = myTransferProtocl1.startFileTransfer(theFile.getFile(), "2", 256, 5); theSendState.waitForState(State.CANCELLED, 5, TimeUnit.SECONDS); assertEquals(State.CANCELLED, theSendState.getState()); theAcceptListener.getTransferState().waitForState(State.CANCELLED, 1, TimeUnit.SECONDS); assertEquals(State.CANCELLED, theAcceptListener.getTransferState().getState()); }
@Override public void newTransfer(AbstractTransferState aTransfer) { try { myTransferState = aTransfer; myLatch.countDown(); aTransfer.cancel(); } catch (StateChangeException e) { LOGGER.error("An error occured while starting transfer", e); } }
public void testAsyncProtocolFileTransferHappyPath() throws AsyncTransferException, InterruptedException, IOException { ClassPathResource theFile = new ClassPathResource("chabernac/protocol/asyncfiletransfer/mars_1k_color.jpg"); File theReceivingFile = new File("test.jpg"); if (theReceivingFile.exists()) theReceivingFile.delete(); InputStream theInput1 = null; InputStream theInput2 = null; try { AcceptTransferListener theAcceptListener = new AcceptTransferListener(theReceivingFile); myTransferProtocol2.addTransferListener(theAcceptListener); AbstractTransferState theSendState = myTransferProtocl1.startFileTransfer(theFile.getFile(), "2", 256, 5); theSendState.addPacketTransferListener(new PacketTransferVisualizerFrame()); assertTrue(theSendState.waitForState(State.DONE, 20, TimeUnit.SECONDS)); assertTrue( theAcceptListener.getTransferState().waitForState(State.DONE, 1, TimeUnit.SECONDS)); // test if the files are equal theInput1 = new FileInputStream(theFile.getFile()); theInput2 = new FileInputStream(theReceivingFile); int theByte; while ((theByte = theInput1.read()) != -1) { assertEquals(theByte, theInput2.read()); } } finally { if (theReceivingFile.exists()) theReceivingFile.delete(); } }