示例#1
0
  @Test
  @Category(IntegrationTest.class)
  public void uploadAndDownloadFileSucceeds() throws IOException {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "Tamme-Lauri_tamm_suvepäeval.jpg";
    URL fileURL = this.getClass().getResource("/sample-files/" + fileName);
    String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8");
    long fileSize = new File(filePath).length();
    byte[] fileContent = readAllBytes(filePath);

    InputStream uploadStream = new FileInputStream(filePath);
    ProgressListener mockUploadListener = mock(ProgressListener.class);
    BoxFile.Info uploadedFileInfo =
        rootFolder.uploadFile(uploadStream, fileName, fileSize, mockUploadListener);
    BoxFile uploadedFile = uploadedFileInfo.getResource();

    ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
    ProgressListener mockDownloadListener = mock(ProgressListener.class);
    uploadedFile.download(downloadStream, mockDownloadListener);
    byte[] downloadedFileContent = downloadStream.toByteArray();

    assertThat(downloadedFileContent, is(equalTo(fileContent)));
    assertThat(
        rootFolder,
        hasItem(Matchers.<BoxItem.Info>hasProperty("ID", equalTo(uploadedFile.getID()))));
    verify(mockUploadListener, atLeastOnce())
        .onProgressChanged(anyLong(), longThat(is(equalTo(fileSize))));
    verify(mockDownloadListener, atLeastOnce())
        .onProgressChanged(anyLong(), longThat(is(equalTo(fileSize))));

    uploadedFile.delete();
  }
示例#2
0
  @Test
  @Category(IntegrationTest.class)
  public void promoteVersionsSucceeds() throws UnsupportedEncodingException {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String fileName = "[promoteVersionsSucceeds] Multi-version File.txt";
    String version1Content = "Version 1";
    byte[] version1Bytes = version1Content.getBytes(StandardCharsets.UTF_8);
    byte[] version2Bytes = "Version 2".getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(version1Bytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
    uploadStream = new ByteArrayInputStream(version2Bytes);
    uploadedFile.uploadVersion(uploadStream);

    Collection<BoxFileVersion> versions = uploadedFile.getVersions();
    BoxFileVersion previousVersion = versions.iterator().next();
    previousVersion.promote();

    ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
    uploadedFile.download(downloadStream);
    String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
    assertThat(downloadedContent, equalTo(version1Content));

    uploadedFile.delete();
  }
示例#3
0
  @Test
  @Category(IntegrationTest.class)
  public void copyFileSucceeds() throws UnsupportedEncodingException {
    BoxAPIConnection api = new BoxAPIConnection(TestConfig.getAccessToken());
    BoxFolder rootFolder = BoxFolder.getRootFolder(api);
    String originalFileName = "[copyFileSucceeds] Original File.txt";
    String newFileName = "[copyFileSucceeds] New File.txt";
    String fileContent = "Test file";
    byte[] fileBytes = fileContent.getBytes(StandardCharsets.UTF_8);

    InputStream uploadStream = new ByteArrayInputStream(fileBytes);
    BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, originalFileName).getResource();

    BoxFile.Info copiedFileInfo = uploadedFile.copy(rootFolder, newFileName);
    BoxFile copiedFile = copiedFileInfo.getResource();

    ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
    copiedFile.download(downloadStream);
    String downloadedContent = downloadStream.toString(StandardCharsets.UTF_8.name());
    assertThat(downloadedContent, equalTo(fileContent));

    uploadedFile.delete();
    copiedFile.delete();
  }