@Test
 public void testDeleteBlobFail() {
   String blobName = "test-delete-blob-fail";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   try {
     storage.delete(BUCKET, blob.name(), Storage.BlobSourceOption.generationMatch(-1L));
     fail("StorageException was expected");
   } catch (StorageException ex) {
     // expected
   }
   assertTrue(storage.delete(BUCKET, blob.name()));
 }
  private void testDelete(StorageProvider provider) throws IOException {
    Storage storage = provider.store(new ByteArrayInputStream(createData(512)));

    storage.delete();

    // getInputStream has to throw an IllegalStateException
    try {
      storage.getInputStream();
      Assert.fail();
    } catch (IllegalStateException expected) {
    }

    // invoking delete a second time should not have any effect
    storage.delete();
  }
示例#3
0
 public void secureDelete() {
   int rw = tblItems.getSelectedRow();
   if (rw == -1) {
     JOptionPane.showMessageDialog(frm, "No item selected", "Error", JOptionPane.ERROR_MESSAGE);
     return;
   }
   int idx = tblItems.convertRowIndexToModel(rw);
   if (JOptionPane.showConfirmDialog(
           frm,
           "Delete " + store.plainName(idx) + "?",
           "Confirm Delete",
           JOptionPane.YES_NO_OPTION)
       != JOptionPane.YES_OPTION) return;
   File del = store.delete(idx);
   store.fireTableDataChanged();
   if (del != null) {
     if (del.delete()) {
       // successful
       needsSave = true;
     } else {
       System.err.println("Delete " + del.getAbsolutePath() + " failed");
     }
   }
   updateStatus();
 }
 public void delete() {
   if (head != null) {
     head = null;
     tail.delete();
     tail = null;
   }
 }
 @Test
 public void testListBlobsSelectedFields() {
   String[] blobNames = {
     "test-list-blobs-selected-fields-blob1", "test-list-blobs-selected-fields-blob2"
   };
   ImmutableMap metadata = ImmutableMap.of("k", "v");
   BlobInfo blob1 =
       BlobInfo.builder(BUCKET, blobNames[0]).contentType(CONTENT_TYPE).metadata(metadata).build();
   BlobInfo blob2 =
       BlobInfo.builder(BUCKET, blobNames[1]).contentType(CONTENT_TYPE).metadata(metadata).build();
   assertNotNull(storage.create(blob1));
   assertNotNull(storage.create(blob2));
   Page<BlobInfo> page =
       storage.list(
           BUCKET,
           Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"),
           Storage.BlobListOption.fields(BlobField.METADATA));
   int index = 0;
   for (BlobInfo remoteBlob : page.values()) {
     assertEquals(BUCKET, remoteBlob.bucket());
     assertEquals(blobNames[index++], remoteBlob.name());
     assertEquals(metadata, remoteBlob.metadata());
     assertNull(remoteBlob.contentType());
   }
   assertTrue(storage.delete(BUCKET, blobNames[0]));
   assertTrue(storage.delete(BUCKET, blobNames[1]));
 }
 @Test
 public void testReadAndWriteCaptureChannels() throws IOException {
   String blobName = "test-read-and-write-capture-channels-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   byte[] stringBytes;
   BlobWriteChannel writer = storage.writer(blob);
   stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
   writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
   RestorableState<BlobWriteChannel> writerState = writer.capture();
   BlobWriteChannel secondWriter = writerState.restore();
   secondWriter.write(ByteBuffer.wrap(stringBytes));
   secondWriter.close();
   ByteBuffer readBytes;
   ByteBuffer readStringBytes;
   BlobReadChannel reader = storage.reader(blob.blobId());
   reader.chunkSize(BLOB_BYTE_CONTENT.length);
   readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
   reader.read(readBytes);
   RestorableState<BlobReadChannel> readerState = reader.capture();
   BlobReadChannel secondReader = readerState.restore();
   readStringBytes = ByteBuffer.allocate(stringBytes.length);
   secondReader.read(readStringBytes);
   reader.close();
   secondReader.close();
   assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
   assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
   assertTrue(storage.delete(BUCKET, blobName));
 }
 @Test
 public void testComposeBlobFail() {
   String sourceBlobName1 = "test-compose-blob-fail-source-1";
   String sourceBlobName2 = "test-compose-blob-fail-source-2";
   BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
   BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
   assertNotNull(storage.create(sourceBlob1));
   assertNotNull(storage.create(sourceBlob2));
   String targetBlobName = "test-compose-blob-fail-target";
   BlobInfo targetBlob = BlobInfo.builder(BUCKET, targetBlobName).build();
   Storage.ComposeRequest req =
       Storage.ComposeRequest.builder()
           .addSource(sourceBlobName1, -1L)
           .addSource(sourceBlobName2, -1L)
           .target(targetBlob)
           .build();
   try {
     storage.compose(req);
     fail("StorageException was expected");
   } catch (StorageException ex) {
     // expected
   }
   assertTrue(storage.delete(BUCKET, sourceBlobName1));
   assertTrue(storage.delete(BUCKET, sourceBlobName2));
 }
 @Test
 public void testGetBlobEmptySelectedFields() {
   String blobName = "test-get-empty-selected-fields-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).contentType(CONTENT_TYPE).build();
   assertNotNull(storage.create(blob));
   BlobInfo remoteBlob = storage.get(blob.blobId(), Storage.BlobGetOption.fields());
   assertEquals(blob.blobId(), remoteBlob.blobId());
   assertNull(remoteBlob.contentType());
   assertTrue(storage.delete(BUCKET, blobName));
 }
 @Test
 public void testUpdateBlob() {
   String blobName = "test-update-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   BlobInfo updatedBlob = storage.update(blob.toBuilder().contentType(CONTENT_TYPE).build());
   assertNotNull(updatedBlob);
   assertEquals(blob.blobId(), updatedBlob.blobId());
   assertEquals(CONTENT_TYPE, updatedBlob.contentType());
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#10
0
 @Test
 public void testCreateEmptyBlob() {
   String blobName = "test-create-empty-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   BlobInfo remoteBlob = storage.create(blob);
   assertNotNull(remoteBlob);
   assertEquals(blob.blobId(), remoteBlob.blobId());
   byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
   assertArrayEquals(new byte[0], readBytes);
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#11
0
 @Test
 public void testDeleteBlobsFail() {
   String sourceBlobName1 = "test-delete-blobs-fail-1";
   String sourceBlobName2 = "test-delete-blobs-fail-2";
   BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
   BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
   assertNotNull(storage.create(sourceBlob1));
   List<Boolean> deleteStatus = storage.delete(sourceBlob1.blobId(), sourceBlob2.blobId());
   assertTrue(deleteStatus.get(0));
   assertTrue(!deleteStatus.get(1));
 }
示例#12
0
 @Test
 public void testGetBlobsFail() {
   String sourceBlobName1 = "test-get-blobs-fail-1";
   String sourceBlobName2 = "test-get-blobs-fail-2";
   BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
   BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
   assertNotNull(storage.create(sourceBlob1));
   List<BlobInfo> remoteBlobs = storage.get(sourceBlob1.blobId(), sourceBlob2.blobId());
   assertEquals(sourceBlob1.blobId(), remoteBlobs.get(0).blobId());
   assertNull(remoteBlobs.get(1));
   assertTrue(storage.delete(BUCKET, sourceBlobName1));
 }
示例#13
0
 @Test
 public void testCreateBlobStream() throws UnsupportedEncodingException {
   String blobName = "test-create-blob-stream";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).contentType(CONTENT_TYPE).build();
   ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8));
   BlobInfo remoteBlob = storage.create(blob, stream);
   assertNotNull(remoteBlob);
   assertEquals(blob.blobId(), remoteBlob.blobId());
   assertEquals(blob.contentType(), remoteBlob.contentType());
   byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
   assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#14
0
 @Test
 public void testWriteChannelExistingBlob() throws IOException {
   String blobName = "test-write-channel-existing-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   BlobInfo remoteBlob = storage.create(blob);
   byte[] stringBytes;
   try (BlobWriteChannel writer = storage.writer(remoteBlob)) {
     stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
     writer.write(ByteBuffer.wrap(stringBytes));
   }
   assertArrayEquals(stringBytes, storage.readAllBytes(blob.blobId()));
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#15
0
 @Test
 public void testReadChannelFail() throws IOException {
   String blobName = "test-read-channel-blob-fail";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   try (BlobReadChannel reader =
       storage.reader(blob.blobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) {
     reader.read(ByteBuffer.allocate(42));
     fail("StorageException was expected");
   } catch (StorageException ex) {
     // expected
   }
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#16
0
 @Test
 public void testGetSignedUrl() throws IOException {
   String blobName = "test-get-signed-url-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob, BLOB_BYTE_CONTENT));
   URL url = storage.signUrl(blob, 1, TimeUnit.HOURS);
   URLConnection connection = url.openConnection();
   byte[] readBytes = new byte[BLOB_BYTE_CONTENT.length];
   try (InputStream responseStream = connection.getInputStream()) {
     assertEquals(BLOB_BYTE_CONTENT.length, responseStream.read(readBytes));
     assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
     assertTrue(storage.delete(BUCKET, blobName));
   }
 }
示例#17
0
 @Test
 public void testUpdateBlobMergeMetadata() {
   String blobName = "test-update-blob-merge-metadata";
   ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a");
   ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b");
   ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a", "k2", "b");
   BlobInfo blob =
       BlobInfo.builder(BUCKET, blobName).contentType(CONTENT_TYPE).metadata(metadata).build();
   assertNotNull(storage.create(blob));
   BlobInfo updatedBlob = storage.update(blob.toBuilder().metadata(newMetadata).build());
   assertNotNull(updatedBlob);
   assertEquals(blob.blobId(), updatedBlob.blobId());
   assertEquals(expectedMetadata, updatedBlob.metadata());
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#18
0
 @Test
 public void testUpdateBlobFail() {
   String blobName = "test-update-blob-fail";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   try {
     storage.update(
         blob.toBuilder().contentType(CONTENT_TYPE).generation(-1L).build(),
         Storage.BlobTargetOption.generationMatch());
     fail("StorageException was expected");
   } catch (StorageException ex) {
     // expected
   }
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#19
0
 @Test
 public void testPostSignedUrl() throws IOException {
   String blobName = "test-post-signed-url-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   URL url =
       storage.signUrl(blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST));
   URLConnection connection = url.openConnection();
   connection.setDoOutput(true);
   connection.connect();
   BlobInfo remoteBlob = storage.get(BUCKET, blobName);
   assertNotNull(remoteBlob);
   assertEquals(blob.blobId(), remoteBlob.blobId());
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#20
0
 @Test
 public void testComposeBlob() {
   String sourceBlobName1 = "test-compose-blob-source-1";
   String sourceBlobName2 = "test-compose-blob-source-2";
   BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
   BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
   assertNotNull(storage.create(sourceBlob1, BLOB_BYTE_CONTENT));
   assertNotNull(storage.create(sourceBlob2, BLOB_BYTE_CONTENT));
   String targetBlobName = "test-compose-blob-target";
   BlobInfo targetBlob = BlobInfo.builder(BUCKET, targetBlobName).build();
   Storage.ComposeRequest req =
       Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob);
   BlobInfo remoteBlob = storage.compose(req);
   assertNotNull(remoteBlob);
   assertEquals(targetBlob.blobId(), remoteBlob.blobId());
   byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName);
   byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2);
   System.arraycopy(
       BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, BLOB_BYTE_CONTENT.length);
   assertArrayEquals(composedBytes, readBytes);
   assertTrue(storage.delete(BUCKET, sourceBlobName1));
   assertTrue(storage.delete(BUCKET, sourceBlobName2));
   assertTrue(storage.delete(BUCKET, targetBlobName));
 }
示例#21
0
 @Test
 public void testGetBlobSelectedFields() {
   String blobName = "test-get-selected-fields-blob";
   BlobInfo blob =
       BlobInfo.builder(BUCKET, blobName)
           .contentType(CONTENT_TYPE)
           .metadata(ImmutableMap.of("k", "v"))
           .build();
   assertNotNull(storage.create(blob));
   BlobInfo remoteBlob =
       storage.get(blob.blobId(), Storage.BlobGetOption.fields(BlobField.METADATA));
   assertEquals(blob.blobId(), remoteBlob.blobId());
   assertEquals(ImmutableMap.of("k", "v"), remoteBlob.metadata());
   assertNull(remoteBlob.contentType());
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#22
0
 @Test
 public void testUpdateBlobsFail() {
   String sourceBlobName1 = "test-update-blobs-fail-1";
   String sourceBlobName2 = "test-update-blobs-fail-2";
   BlobInfo sourceBlob1 = BlobInfo.builder(BUCKET, sourceBlobName1).build();
   BlobInfo sourceBlob2 = BlobInfo.builder(BUCKET, sourceBlobName2).build();
   BlobInfo remoteBlob1 = storage.create(sourceBlob1);
   assertNotNull(remoteBlob1);
   List<BlobInfo> updatedBlobs =
       storage.update(
           remoteBlob1.toBuilder().contentType(CONTENT_TYPE).build(),
           sourceBlob2.toBuilder().contentType(CONTENT_TYPE).build());
   assertEquals(sourceBlob1.blobId(), updatedBlobs.get(0).blobId());
   assertEquals(CONTENT_TYPE, updatedBlobs.get(0).contentType());
   assertNull(updatedBlobs.get(1));
   assertTrue(storage.delete(BUCKET, sourceBlobName1));
 }
示例#23
0
 @Test
 public void testCopyBlob() {
   String sourceBlobName = "test-copy-blob-source";
   BlobId source = BlobId.of(BUCKET, sourceBlobName);
   ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v");
   BlobInfo blob = BlobInfo.builder(source).contentType(CONTENT_TYPE).metadata(metadata).build();
   assertNotNull(storage.create(blob, BLOB_BYTE_CONTENT));
   String targetBlobName = "test-copy-blob-target";
   Storage.CopyRequest req = Storage.CopyRequest.of(source, BlobId.of(BUCKET, targetBlobName));
   CopyWriter copyWriter = storage.copy(req);
   assertEquals(BUCKET, copyWriter.result().bucket());
   assertEquals(targetBlobName, copyWriter.result().name());
   assertEquals(CONTENT_TYPE, copyWriter.result().contentType());
   assertEquals(metadata, copyWriter.result().metadata());
   assertTrue(copyWriter.isDone());
   assertTrue(storage.delete(BUCKET, sourceBlobName));
   assertTrue(storage.delete(BUCKET, targetBlobName));
 }
示例#24
0
 @Test
 public void testCopyBlobFail() {
   String sourceBlobName = "test-copy-blob-source-fail";
   BlobId source = BlobId.of(BUCKET, sourceBlobName);
   assertNotNull(storage.create(BlobInfo.builder(source).build(), BLOB_BYTE_CONTENT));
   String targetBlobName = "test-copy-blob-target-fail";
   BlobInfo target = BlobInfo.builder(BUCKET, targetBlobName).contentType(CONTENT_TYPE).build();
   Storage.CopyRequest req =
       Storage.CopyRequest.builder()
           .source(source)
           .sourceOptions(Storage.BlobSourceOption.generationMatch(-1L))
           .target(target)
           .build();
   try {
     storage.copy(req);
     fail("StorageException was expected");
   } catch (StorageException ex) {
     // expected
   }
   assertTrue(storage.delete(BUCKET, sourceBlobName));
 }
示例#25
0
 @Test
 public void testBatchRequestFail() {
   String blobName = "test-batch-request-blob-fail";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   assertNotNull(storage.create(blob));
   BlobInfo updatedBlob = blob.toBuilder().generation(-1L).build();
   BatchRequest batchRequest =
       BatchRequest.builder()
           .update(updatedBlob, Storage.BlobTargetOption.generationMatch())
           .delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L))
           .get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L))
           .build();
   BatchResponse updateResponse = storage.apply(batchRequest);
   assertEquals(1, updateResponse.updates().size());
   assertEquals(1, updateResponse.deletes().size());
   assertEquals(1, updateResponse.gets().size());
   assertTrue(updateResponse.updates().get(0).failed());
   assertTrue(updateResponse.gets().get(0).failed());
   assertTrue(updateResponse.deletes().get(0).failed());
   assertTrue(storage.delete(BUCKET, blobName));
 }
示例#26
0
 @Test
 public void testReadAndWriteChannels() throws IOException {
   String blobName = "test-read-and-write-channels-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   byte[] stringBytes;
   try (BlobWriteChannel writer = storage.writer(blob)) {
     stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
     writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
     writer.write(ByteBuffer.wrap(stringBytes));
   }
   ByteBuffer readBytes;
   ByteBuffer readStringBytes;
   try (BlobReadChannel reader = storage.reader(blob.blobId())) {
     readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
     readStringBytes = ByteBuffer.allocate(stringBytes.length);
     reader.read(readBytes);
     reader.read(readStringBytes);
   }
   assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
   assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
   assertTrue(storage.delete(BUCKET, blobName));
 }
  /**
   * Stores an item to all available databases
   *
   * @param item
   * @throws IOException
   */
  private void process(Item item) throws IOException {
    if (store != null) {

      for (ItemFilter filter : filters) {
        if (!filter.accept(item)) {
          return;
        }
      }

      for (Processor processor : processors) {
        processor.process(item);
      }

      if (item.getOperation() == Operation.NEW) {
        store.store(item);
      } else if (item.getOperation() == Operation.UPDATE) {
        store.update(item);
      } else if (item.getOperation() == Operation.DELETED) {
        store.delete(item.getId());
      } else {
        System.out.println(item.getOperation() + ": Not supported operation");
      }
    }
  }
示例#28
0
 @Test
 public void testDeleteNonExistingBlob() {
   String blobName = "test-delete-non-existing-blob";
   assertTrue(!storage.delete(BUCKET, blobName));
 }