1313import static com .box .sdk .UniqueTestFolder .randomizeName ;
1414import static com .box .sdk .UniqueTestFolder .removeUniqueFolder ;
1515import static com .box .sdk .UniqueTestFolder .setupUniqeFolder ;
16+ import static com .box .sdk .UniqueTestFolder .uploadFileToUniqueFolder ;
1617import static com .box .sdk .UniqueTestFolder .uploadFileToUniqueFolderWithSomeContent ;
1718import static com .box .sdk .UniqueTestFolder .uploadFileWithSomeContent ;
19+ import static java .nio .charset .StandardCharsets .UTF_8 ;
1820import static org .hamcrest .MatcherAssert .assertThat ;
1921import static org .hamcrest .Matchers .contains ;
2022import static org .hamcrest .Matchers .emptyOrNullString ;
3234import com .box .sdk .BoxCollaboration .Role ;
3335import com .box .sdk .sharedlink .BoxSharedLinkRequest ;
3436import java .io .ByteArrayInputStream ;
37+ import java .io .ByteArrayOutputStream ;
3538import java .io .File ;
3639import java .io .FileInputStream ;
40+ import java .io .IOException ;
3741import java .io .InputStream ;
42+ import java .io .PipedInputStream ;
43+ import java .io .PipedOutputStream ;
3844import java .net .MalformedURLException ;
3945import java .net .URL ;
4046import java .text .SimpleDateFormat ;
4854import java .util .Map ;
4955import java .util .TimeZone ;
5056import java .util .UUID ;
57+ import java .util .concurrent .Semaphore ;
58+ import java .util .concurrent .atomic .AtomicLong ;
5159import java .util .concurrent .atomic .AtomicReference ;
60+ import java .util .stream .IntStream ;
5261import org .hamcrest .Matchers ;
5362import org .junit .AfterClass ;
5463import org .junit .Before ;
@@ -173,7 +182,7 @@ public void uploadFileSucceeds() {
173182 BoxFile uploadedFile = null ;
174183
175184 try {
176- uploadedFile = uploadFileToUniqueFolderWithSomeContent (api , "Test File.txt" );
185+ uploadedFile = uploadFileToUniqueFolderWithSomeContent (api , randomizeName ( "Test File" ) );
177186
178187 assertThat (rootFolder , hasItem (Matchers .<BoxItem .Info >hasProperty ("ID" , equalTo (uploadedFile .getID ()))));
179188 } finally {
@@ -192,7 +201,7 @@ public void uploadFileUploadFileCallbackSucceeds() {
192201
193202 try {
194203
195- final String fileContent = "Test file" ;
204+ final String fileContent = randomizeName ( "Test file" ) ;
196205 uploadedFile = rootFolder .uploadFile (outputStream -> {
197206 outputStream .write (fileContent .getBytes ());
198207 callbackWasCalled .set (true );
@@ -757,6 +766,142 @@ public void iterateWithMarker() {
757766 }
758767 }
759768
769+ @ Test
770+ public void uploadFileVersionInSeparateThreadsSucceeds () throws IOException , InterruptedException {
771+ BoxAPIConnection api = jwtApiForServiceAccount ();
772+ Semaphore semaphore = new Semaphore (0 );
773+
774+ PipedOutputStream outputStream = new PipedOutputStream ();
775+ PipedInputStream inputStream = new PipedInputStream ();
776+ outputStream .connect (inputStream );
777+
778+ String fileContent = "This is only a test" ;
779+ final BoxFile uploadedFile = uploadFileToUniqueFolder (api , randomizeName ("Test File" ), fileContent );
780+
781+ new Thread (
782+ () -> {
783+ try {
784+ new BoxFile (api , uploadedFile .getID ()).download (outputStream );
785+ } finally {
786+ try {
787+ outputStream .close ();
788+ } catch (IOException e ) {
789+ throw new RuntimeException (e );
790+ }
791+ }
792+ }).start ();
793+
794+ new Thread (
795+ () -> {
796+ new BoxFile (api , uploadedFile .getID ()).uploadNewVersion (inputStream );
797+ try {
798+ inputStream .close ();
799+ semaphore .release ();
800+ } catch (IOException e ) {
801+ throw new RuntimeException (e );
802+ }
803+ }).start ();
804+
805+
806+ semaphore .acquire ();
807+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
808+ new BoxFile (api , uploadedFile .getID ()).download (output );
809+ assertThat (output .toString (), is (fileContent ));
810+ }
811+
812+ @ Test
813+ public void uploadFileVersionWithProgressInSeparateThreadsSucceeds () throws IOException , InterruptedException {
814+ BoxAPIConnection api = jwtApiForServiceAccount ();
815+ Semaphore semaphore = new Semaphore (0 );
816+
817+ PipedOutputStream outputStream = new PipedOutputStream ();
818+ PipedInputStream inputStream = new PipedInputStream ();
819+ outputStream .connect (inputStream );
820+ AtomicLong bytesUploaded = new AtomicLong (0 );
821+ ProgressListener progressListener = (numBytes , totalBytes ) -> bytesUploaded .set (numBytes );
822+
823+ String fileContent = "This is only a test" ;
824+ long fileSize = fileContent .getBytes (UTF_8 ).length ;
825+
826+ final BoxFile uploadedFile = uploadFileToUniqueFolder (api , randomizeName ("Test File" ), fileContent );
827+
828+ new Thread (
829+ () -> {
830+ try {
831+ new BoxFile (api , uploadedFile .getID ()).download (outputStream );
832+ } finally {
833+ semaphore .release ();
834+ }
835+ }).start ();
836+
837+ new Thread (
838+ () -> {
839+ new BoxFile (api , uploadedFile .getID ())
840+ .uploadNewVersion (inputStream , new Date (), fileSize , progressListener );
841+ semaphore .release ();
842+ }).start ();
843+
844+
845+ semaphore .acquire (2 );
846+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
847+ new BoxFile (api , uploadedFile .getID ()).download (output );
848+ assertThat (output .toString (), is (fileContent ));
849+ assertThat (bytesUploaded .get (), is (fileSize ));
850+ }
851+
852+ @ Test
853+ public void uploadFileInSeparateThreadSucceeds () throws IOException , InterruptedException {
854+ BoxAPIConnection api = jwtApiForServiceAccount ();
855+ Semaphore semaphore = new Semaphore (0 );
856+
857+ PipedOutputStream outputStream = new PipedOutputStream ();
858+ PipedInputStream inputStream = new PipedInputStream ();
859+ outputStream .connect (inputStream );
860+
861+ String fileContent = "Test" ;
862+ byte [] bytes = fileContent .getBytes (UTF_8 );
863+
864+ AtomicReference <String > uploadedFileId = new AtomicReference <>();
865+
866+ new Thread (
867+ () -> {
868+ IntStream .range (0 , bytes .length )
869+ .forEach (i -> {
870+ try {
871+ outputStream .write (bytes [i ]);
872+ Thread .sleep (100 );
873+ } catch (InterruptedException | IOException e ) {
874+ throw new RuntimeException (e );
875+ }
876+ });
877+ try {
878+ outputStream .close ();
879+ semaphore .release ();
880+ } catch (IOException e ) {
881+ throw new RuntimeException (e );
882+ }
883+ }).start ();
884+
885+ new Thread (
886+ () -> {
887+ BoxFile .Info uploadedFile = getUniqueFolder (api )
888+ .uploadFile (inputStream , randomizeName ("dynamic_upload" ));
889+ uploadedFileId .set (uploadedFile .getID ());
890+ try {
891+ inputStream .close ();
892+ semaphore .release ();
893+ } catch (IOException e ) {
894+ throw new RuntimeException (e );
895+ }
896+ }).start ();
897+
898+
899+ semaphore .acquire (2 );
900+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
901+ new BoxFile (api , uploadedFileId .get ()).download (output );
902+ assertThat (output .toString (), is (fileContent ));
903+ }
904+
760905 private Collection <String > getNames (Iterable <BoxItem .Info > page ) {
761906 Collection <String > result = new ArrayList <>();
762907 for (BoxItem .Info info : page ) {
0 commit comments