List<ImapResponse> executeCommandWithIdSet(String commandPrefix, String commandSuffix, Set<Long> ids) throws IOException, MessagingException { GroupedIds groupedIds = IdGrouper.groupIds(ids); List<String> splitCommands = ImapCommandSplitter.splitCommand( commandPrefix, commandSuffix, groupedIds, getLineLengthLimit()); List<ImapResponse> responses = new ArrayList<>(); for (String splitCommand : splitCommands) { responses.addAll(executeSimpleCommand(splitCommand)); } return responses; }
@Test public void splitCommand_withEmptySuffix_shouldCreateCommandWithoutTrailingSpace() throws Exception { Set<Long> ids = createNonContiguousIdSet(1, 2, 1); GroupedIds groupedIds = new GroupedIds(ids, Collections.<IdGrouper.ContiguousIdGroup>emptyList()); List<String> commands = ImapCommandSplitter.splitCommand("UID SEARCH UID", "", groupedIds, 980); assertEquals(commands.size(), 1); assertEquals("UID SEARCH UID 1,2", commands.get(0)); }
@Test public void splitCommand_withManyNonContiguousIds_shouldSplitCommand() throws Exception { Set<Long> ids = createNonContiguousIdSet(10000, 10500, 2); GroupedIds groupedIds = new GroupedIds(ids, Collections.<IdGrouper.ContiguousIdGroup>emptyList()); List<String> commands = ImapCommandSplitter.splitCommand(COMMAND_PREFIX, COMMAND_SUFFIX, groupedIds, 980); assertEquals(commands.size(), 2); assertCommandLengthLimit(commands, 980); verifyCommandString(commands.get(0), createNonContiguousIdSet(10000, 10316, 2)); verifyCommandString(commands.get(1), createNonContiguousIdSet(10318, 10500, 2)); }
@Test public void splitCommand_withContiguousAndNonContiguousIds_shouldGroupIdsAndSplitCommand() throws Exception { Set<Long> idSet = Sets.union( createNonContiguousIdSet(10000, 10298, 2), createNonContiguousIdSet(10402, 10500, 2)); List<IdGrouper.ContiguousIdGroup> idGroups = singletonList(new IdGrouper.ContiguousIdGroup(10300L, 10400L)); GroupedIds groupedIds = new GroupedIds(idSet, idGroups); List<String> commands = ImapCommandSplitter.splitCommand(COMMAND_PREFIX, COMMAND_SUFFIX, groupedIds, 980); assertEquals(commands.size(), 2); assertCommandLengthLimit(commands, 980); verifyCommandString(commands.get(0), Sets.union( createNonContiguousIdSet(10000, 10298, 2), createNonContiguousIdSet(10402, 10418, 2))); verifyCommandString(commands.get(1), createNonContiguousIdSet(10420, 10500, 2), "10300:10400"); }