@Test public void sendFileShouldSendFileIfEverythingValidatedOK() throws CommandException { when(networkService.isNetworkUp()).thenReturn(true); controller.getChatState().setLoggedOn(true); final FileToSend file = mock(FileToSend.class); when(file.getName()).thenReturn("file.txt"); final User user = mock(User.class); controller.sendFile(user, file); verify(networkMessages).sendFile(user, file); }
/** * Tests sendFile(). * * Expects: 14394329!SENDFILE#Christian:(1234)[80800]{37563645}a_file.txt * * @throws CommandException In case the message could not be sent. */ @Test public void testSendFileMessage() throws CommandException { final int userCode = 1234; final long fileLength = 80800L; final String fileName = "a_file.txt"; final FileToSend file = mock(FileToSend.class); when(file.getName()).thenReturn(fileName); when(file.length()).thenReturn(fileLength); final int fileHash = file.hashCode(); // Cannot be mocked it seems final String info = "(" + userCode + ")" + "[" + fileLength + "]" + "{" + fileHash + "}" + fileName; final User user = new User("TestUser", userCode); messages.sendFile(user, file); verify(service).sendMessageToAllUsers(createMessage("SENDFILE") + info); }
/** * Sends a message over the network to notify another user that the * application user wants to send a file. * * @param user The user asked to receive a file. * @param file The file to send. * @throws CommandException If the specified user is the application user, * or there is no connection to the network, * or the application user is away, * or the specified user is away, * or the file name is too long. */ public void sendFile(final User user, final FileToSend file) throws CommandException { Validate.notNull(user, "User can not be null"); Validate.notNull(file, "File can not be null"); if (user.isMe()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.isMe")); } else if (!isConnected()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.notConnected")); } else if (me.isAway()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.meIsAway")); } else if (user.isAway()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.userIsAway")); } else if (Tools.getBytes(file.getName()) > Constants.MESSAGE_MAX_BYTES) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.messageTooLong", Constants.MESSAGE_MAX_BYTES)); } else { networkMessages.sendFile(user, file); } }
/** * Sends a message over the network to notify another user that the * application user wants to send a file. * * @param user The user asked to receive a file. * @param file The file to send. * @throws CommandException If the specified user is the application user, * or there is no connection to the network, * or the application user is away, * or the specified user is away, * or the file name is too long. */ public void sendFile(final User user, final FileToSend file) throws CommandException { Validate.notNull(user, "User can not be null"); Validate.notNull(file, "File can not be null"); if (user.isMe()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.isMe")); } else if (!isConnected()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.notConnected")); } else if (me.isAway()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.meIsAway")); } else if (user.isAway()) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.userIsAway")); } else if (Tools.getBytes(file.getName()) > Constants.MESSAGE_MAX_BYTES) { throw new CommandException(coreMessages.getMessage("core.sendFile.error.messageTooLong", Constants.MESSAGE_MAX_BYTES)); } else { networkMessages.sendFile(user, file); } }