@@ -457,39 +457,55 @@ public void downloadFileRangeSucceeds() throws IOException {
457457 @ Test
458458 @ Category (IntegrationTest .class )
459459 public void uploadAndDownloadMultipleVersionsSucceeds () throws UnsupportedEncodingException {
460- BoxAPIConnection api = new BoxAPIConnection (TestConfig .getAccessToken ());
461- BoxFolder rootFolder = BoxFolder .getRootFolder (api );
460+ BoxFile uploadedFile = null ;
462461 String fileName = "[uploadAndDownloadMultipleVersionsSucceeds] Multi-version File.txt" ;
463462 String version1Content = "Version 1" ;
464463 String version1Sha = "db3cbc01da600701b9fe4a497fe328e71fa7022f" ;
465- byte [] version1Bytes = version1Content .getBytes (StandardCharsets .UTF_8 );
466- long version1Size = version1Bytes .length ;
467464 String version2Content = "Version 2" ;
465+ ProgressListener mockUploadListener = mock (ProgressListener .class );
466+ try {
467+ uploadedFile = BoxFileTest .createAndUpdateFileHelper (fileName , version1Content ,
468+ version2Content , mockUploadListener );
469+ Collection <BoxFileVersion > versions = uploadedFile .getVersions ();
470+ BoxFileVersion previousVersion = versions .iterator ().next ();
471+
472+ ByteArrayOutputStream downloadStream = new ByteArrayOutputStream ();
473+ ProgressListener mockDownloadListener = mock (ProgressListener .class );
474+ previousVersion .download (downloadStream , mockDownloadListener );
475+ String downloadedContent = downloadStream .toString (StandardCharsets .UTF_8 .name ());
476+
477+ assertThat (versions , hasSize (1 ));
478+ assertThat (previousVersion .getSha1 (), is (equalTo (version1Sha )));
479+ assertThat (downloadedContent , equalTo (version1Content ));
480+ verify (mockDownloadListener , atLeastOnce ()).onProgressChanged (anyLong (), anyLong ());
481+ long version1Size = version1Content .getBytes (StandardCharsets .UTF_8 ).length ;
482+ verify (mockUploadListener , atLeastOnce ()).onProgressChanged (anyLong (),
483+ longThat (is (equalTo (version1Size ))));
484+
485+ } finally {
486+ if (uploadedFile != null ) {
487+ uploadedFile .delete ();
488+ }
489+ }
490+ }
491+
492+ protected static BoxFile createAndUpdateFileHelper (String fileName , String version1Content ,
493+ String version2Content , ProgressListener mockUploadListener ) {
494+ BoxAPIConnection api = new BoxAPIConnection (TestConfig .getAccessToken ());
495+ BoxFolder rootFolder = BoxFolder .getRootFolder (api );
496+
497+ byte [] version1Bytes = version1Content .getBytes (StandardCharsets .UTF_8 );
498+
499+
468500 byte [] version2Bytes = version2Content .getBytes (StandardCharsets .UTF_8 );
469501 long version2Size = version1Bytes .length ;
470502
471503 InputStream uploadStream = new ByteArrayInputStream (version1Bytes );
472504 BoxFile uploadedFile = rootFolder .uploadFile (uploadStream , fileName ).getResource ();
473505
474506 uploadStream = new ByteArrayInputStream (version2Bytes );
475- ProgressListener mockUploadListener = mock (ProgressListener .class );
476507 uploadedFile .uploadVersion (uploadStream , null , version2Size , mockUploadListener );
477-
478- Collection <BoxFileVersion > versions = uploadedFile .getVersions ();
479- BoxFileVersion previousVersion = versions .iterator ().next ();
480-
481- ByteArrayOutputStream downloadStream = new ByteArrayOutputStream ();
482- ProgressListener mockDownloadListener = mock (ProgressListener .class );
483- previousVersion .download (downloadStream , mockDownloadListener );
484- String downloadedContent = downloadStream .toString (StandardCharsets .UTF_8 .name ());
485-
486- assertThat (versions , hasSize (1 ));
487- assertThat (previousVersion .getSha1 (), is (equalTo (version1Sha )));
488- assertThat (downloadedContent , equalTo (version1Content ));
489- verify (mockDownloadListener , atLeastOnce ()).onProgressChanged (anyLong (), anyLong ());
490- verify (mockUploadListener , atLeastOnce ()).onProgressChanged (anyLong (), longThat (is (equalTo (version1Size ))));
491-
492- uploadedFile .delete ();
508+ return uploadedFile ;
493509 }
494510
495511 @ Test
@@ -1221,7 +1237,7 @@ private void abortUploadSession(BoxFileUploadSession session) {
12211237 }
12221238 }
12231239
1224- private static byte [] readAllBytes (String fileName ) throws IOException {
1240+ protected static byte [] readAllBytes (String fileName ) throws IOException {
12251241 RandomAccessFile f = new RandomAccessFile (fileName , "r" );
12261242 byte [] b = new byte [(int ) f .length ()];
12271243 f .read (b );
@@ -1254,7 +1270,7 @@ private BoxFile.Info parallelMuliputUpload(File file, BoxFolder folder, String f
12541270 return folder .uploadLargeFile (newStream , BoxFileTest .generateString (), file .length ());
12551271 }
12561272
1257- private static String generateString () {
1273+ protected static String generateString () {
12581274 Random rng = new Random ();
12591275 String characters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
12601276 int length = 10 ;
0 commit comments