@Test
  public void testDownloadPages() throws StorageException, URISyntaxException, IOException {
    int blobLengthToUse = 8 * 512;
    byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512);

    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);
    blobRef.create(blobLengthToUse);

    // Upload one page (page 0)
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    blobRef.uploadPages(inputStream, 0, 512);

    // Upload pages 2-4
    inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512);
    blobRef.uploadPages(inputStream, 2 * 512, 3 * 512);

    // Now, we expect the first 512 bytes of the blob to be the first 512 bytes of the random buffer
    // (page 0)
    // the next 512 bytes should be 0 (page 1)
    // The next 3 * 512 bytes should be equal to bytes (512 -> 4 * 512) of the random buffer (pages
    // 2-4)
    // The next 3 * 512 bytes should be 0 (pages 5-7)

    ArrayList<PageRange> actualPageRanges = blobRef.downloadPageRanges();
    ArrayList<PageRange> expectedPageRanges = new ArrayList<PageRange>();
    expectedPageRanges.add(new PageRange(0, 512 - 1));
    expectedPageRanges.add(new PageRange(2 * 512, 2 * 512 + 3 * 512 - 1));

    assertEquals(expectedPageRanges.size(), actualPageRanges.size());
    for (int i = 0; i < expectedPageRanges.size(); i++) {
      assertEquals(
          expectedPageRanges.get(i).getStartOffset(), actualPageRanges.get(i).getStartOffset());
      assertEquals(
          expectedPageRanges.get(i).getEndOffset(), actualPageRanges.get(i).getEndOffset());
    }
  }
  @Test
  public void testClearPages() throws URISyntaxException, StorageException, IOException {
    int blobLengthToUse = 8 * 512;
    byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512);

    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);
    blobRef.create(blobLengthToUse);
    assertNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload one page (page 0)
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    blobRef.uploadPages(inputStream, 0, blobLengthToUse);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    try {
      blobRef.clearPages(0, 256);
      fail("Did not throw expected exception on non-512-byte-aligned length");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_BLOB_LENGTH, ex.getMessage());
    }

    try {
      blobRef.clearPages(3 * 256, 3 * 512);
      fail("Did not throw expected exception on non-512-byte-aligned offset");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_START_OFFSET, ex.getMessage());
    }

    blobRef.clearPages(3 * 512, 2 * 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    byte[] result = new byte[blobLengthToUse];
    blobRef.downloadToByteArray(result, 0);

    int i = 0;

    for (; i < 3 * 512; i++) {
      assertEquals(buffer[i], result[i]);
    }

    for (; i < 5 * 512; i++) {
      assertEquals(0, result[i]);
    }

    for (; i < 8 * 512; i++) {
      assertEquals(buffer[i], result[i]);
    }
  }
  @Test
  public void testUploadPages() throws URISyntaxException, StorageException, IOException {
    int blobLengthToUse = 8 * 512;
    byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512);

    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName);
    blobRef.create(blobLengthToUse);
    assertNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload one page (page 0)
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    blobRef.uploadPages(inputStream, 0, 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Upload pages 2-4
    inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512);
    blobRef.uploadPages(inputStream, 2 * 512, 3 * 512);
    assertNotNull(blobRef.getProperties().getPageBlobSequenceNumber());

    // Now, we expect the first 512 bytes of the blob to be the first 512 bytes of the random buffer
    // (page 0)
    // the next 512 bytes should be 0 (page 1)
    // The next 3 * 512 bytes should be equal to bytes (512 -> 4 * 512) of the random buffer (pages
    // 2-4)
    // The next 3 * 512 bytes should be 0 (pages 5-7)

    byte[] result = new byte[blobLengthToUse];
    blobRef.downloadToByteArray(result, 0);

    for (int i = 0; i < 512; i++) {
      assertEquals(buffer[i], result[i]);
    }

    for (int i = 0; i < 512; i++) {
      assertEquals(0, result[i + 512]);
    }

    for (int i = 0; i < 3 * 512; i++) {
      assertEquals(buffer[i + 512], result[i + 2 * 512]);
    }

    for (int i = 0; i < 3 * 512; i++) {
      assertEquals(0, result[i + 5 * 512]);
    }

    inputStream = new ByteArrayInputStream(buffer);

    try {
      blobRef.uploadPages(inputStream, 0, 256);
      fail("Did not throw expected exception on non-512-byte-aligned length");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_BLOB_LENGTH, ex.getMessage());
    }

    try {
      blobRef.uploadPages(inputStream, 3 * 256, 3 * 512);
      fail("Did not throw expected exception on non-512-byte-aligned offset");
    } catch (IllegalArgumentException ex) {
      assertEquals(SR.INVALID_PAGE_START_OFFSET, ex.getMessage());
    }
  }