@Before public void setUp() throws Exception { mockBodyElement = mock(BodyElement.class); when(mockBodyElement.getName()).thenReturn("Name"); }
@Test public void testSizeShouldBeNumberOfOctetsWhenStartMoreThanOctets() throws Exception { final long size = 2000; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 1000, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Content size is less than start. Size should be zero.").isEqualTo(NUMBER_OF_OCTETS); } }
private HeaderBodyElement headerBodyElement(final MessageResult messageResult, String name, List<MessageResult.Header> lines, final int[] path, final boolean isBase) throws MailboxException { final HeaderBodyElement result = new HeaderBodyElement(name, lines); // if the size is 2 we had found not header and just want to write the empty line with CLRF terminated // so check if there is a content for it. If not we MUST NOT write the empty line in any case // as stated in rfc3501 if (result.size() == 2) { // Check if its base as this can give use a more correctly working check // to see if we need to write the newline out to the client. // This is related to IMAP-298 if (isBase) { if (messageResult.getSize() - result.size() <= 0) { // Seems like this mail has no body result.noBody(); } } else try { if (content(messageResult, name, path, isBase).size() <= 0) { // Seems like this mail has no body result.noBody(); } } catch (IOException e) { throw new MailboxException("Unable to get size of header body element", e); } } return result; } private FetchResponse.BodyElement headers(final MessageResult messageResult, String name, final int[] path, final boolean isBase) throws MailboxException {
private HeaderBodyElement headerBodyElement(MessageResult messageResult, String name, List<MessageResult.Header> lines, int[] path, boolean isBase) throws MailboxException { final HeaderBodyElement result = new HeaderBodyElement(name, lines); // if the size is 2 we had found not header and just want to write the empty line with CLRF terminated // so check if there is a content for it. If not we MUST NOT write the empty line in any case // as stated in rfc3501 if (result.size() == 2) { // Check if its base as this can give use a more correctly working check // to see if we need to write the newline out to the client. // This is related to IMAP-298 if (isBase) { if (messageResult.getSize() - result.size() <= 0) { // Seems like this mail has no body result.noBody(); } } else { try { if (content(messageResult, name, path, isBase).size() <= 0) { // Seems like this mail has no body result.noBody(); } } catch (IOException e) { throw new MailboxException("Unable to get size of header body element", e); } } } return result; }
/** * @see org.apache.james.imap.message.response.Literal#size() */ public long size() throws IOException { final long size = delegate.size(); final long lastOctet = this.numberOfOctets + firstOctet; final long result; if (firstOctet > size) { result = 0; } else if (size > lastOctet) { result = numberOfOctets; } else { result = size - firstOctet; } return result; }
@Override public InputStream getInputStream() throws IOException { return new LimitingInputStream(delegate.getInputStream(), firstOctet, size()); }
@Override public long size() throws IOException { final long size = delegate.size(); final long lastOctet = this.numberOfOctets + firstOctet; final long result; if (firstOctet > size) { result = 0; } else if (size > lastOctet) { result = numberOfOctets; } else { result = size - firstOctet; } return result; }
@Test public void testSizeShouldBeZeroWhenStartIsMoreThanSize() throws Exception { final long size = 100; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 1000, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(0); }
@Test public void testSizeShouldBeSizeWhenNumberOfOctetsMoreWhenStartIsZero() throws Exception { final long lessThanNumberOfOctets = NUMBER_OF_OCTETS - 1; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 0, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(lessThanNumberOfOctets); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(lessThanNumberOfOctets); }
@Test public void testSizeShouldBeNumberOfOctetsWhenSizeMoreWhenStartIsZero() throws Exception { final long moreThanNumberOfOctets = NUMBER_OF_OCTETS + 1; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 0, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(moreThanNumberOfOctets); assertThat(element.size()).describedAs("Size is more than number of octets so should be number of octets").isEqualTo(NUMBER_OF_OCTETS); }
public PartialFetchBodyElement(BodyElement delegate, long firstOctet, long numberOfOctets) { super(); this.delegate = delegate; this.firstOctet = firstOctet; this.numberOfOctets = numberOfOctets; name = delegate.getName() + "<" + firstOctet + ">"; }
private void encodeBodyElements(ImapResponseComposer composer, List<FetchResponse.BodyElement> elements) throws IOException { if (elements != null) { for (FetchResponse.BodyElement element : elements) { final String name = element.getName(); composer.message(name); composer.literal(element); } } }
private void encodeBodyElements(final ImapResponseComposer composer, final List<FetchResponse.BodyElement> elements) throws IOException { if (elements != null) { for (final Iterator<FetchResponse.BodyElement> it = elements.iterator(); it.hasNext();) { FetchResponse.BodyElement element = it.next(); final String name = element.getName(); composer.message(name); composer.literal(element); } } }
public PartialFetchBodyElement(final BodyElement delegate, final long firstOctet, final long numberOfOctets) { super(); this.delegate = delegate; this.firstOctet = firstOctet; this.numberOfOctets = numberOfOctets; name = delegate.getName() + "<" + firstOctet + ">"; }
@Test public void testWhenStartPlusNumberOfOctetsIsLessThanSizeSizeShouldBeNumberOfOctetsMinusStart() throws Exception { final long size = 100; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 10, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(90); }
/** * @see org.apache.james.imap.message.response.Literal#getInputStream() */ public InputStream getInputStream() throws IOException { return new LimitingInputStream(delegate.getInputStream(), firstOctet, size()); }
@Test public void testWhenStartPlusNumberOfOctetsIsMoreThanSizeSizeShouldBeSizeMinusStart() throws Exception { final long size = 60; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 10, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(50); }