IncomingTransaction { public void receive(@NonNull ChunkHeader header, T data) { if (!mTransactionId.equals(header.getTransactionId())) { throw new IllegalArgumentException("Chunk contains data from a different transaction"); } if (header.getOffset() > mOffset) { throw new IllegalArgumentException("Chunk arrived early"); } else if (header.getOffset() < mOffset) { throw new IllegalArgumentException("Duplicate chunk arrived"); } else if (mOffset + header.getSize() > mSize) { throw new IllegalArgumentException("Chunk has too much data"); } mAggregate = mAggregator.aggregate(mAggregate, data, header.getOffset()); mOffset += header.getSize(); } IncomingTransaction(TransactionToken token, T emptyAggregate, Aggregator<T> aggregator); void receive(@NonNull ChunkHeader header, T data); boolean isTransmissionComplete(); T getData();  }
@Test(expected = IllegalArgumentException.class) public void testReceiveFails_earlyChunk() { ChunkHeader header = new ChunkHeader(TRANS_ID, 10, 1); mSubject.receive(header, null); }
@Test(expected = IllegalArgumentException.class) public void testReceiveFails_duplicateChunk() { ChunkHeader chunk = new ChunkHeader(TRANS_ID, 0, 1); mSubject.receive(chunk, null); mSubject.receive(chunk, null); }
@Test(expected = IllegalArgumentException.class) public void testReceiveFails_largeChunk() { ChunkHeader chunk = new ChunkHeader(TRANS_ID, 0, TRANS_SIZE + 1); mSubject.receive(chunk, null); }