1818import static org .junit .Assert .assertEquals ;
1919import static org .junit .Assert .assertTrue ;
2020
21- import com .google .api .core .*;
2221import com .google .api .core .ApiFuture ;
2322import com .google .api .gax .core .ExecutorProvider ;
2423import com .google .api .gax .core .InstantiatingExecutorProvider ;
3231import com .google .cloud .bigquery .storage .test .Test .UpdatedFooType2 ;
3332import com .google .cloud .bigquery .storage .v1alpha2 .Storage .AppendRowsResponse ;
3433import com .google .protobuf .ByteString ;
34+ import com .google .protobuf .Descriptors .DescriptorValidationException ;
3535import com .google .protobuf .Timestamp ;
36+ import java .io .IOException ;
3637import java .util .Arrays ;
3738import java .util .HashSet ;
3839import java .util .UUID ;
40+ import java .util .concurrent .ExecutionException ;
3941import java .util .logging .Logger ;
4042import org .json .JSONArray ;
4143import org .json .JSONObject ;
4244import org .junit .After ;
45+ import org .junit .Assert ;
4346import org .junit .Before ;
4447import org .junit .Test ;
4548import org .junit .runner .RunWith ;
@@ -168,7 +171,6 @@ public void setUp() throws Exception {
168171
169172 @ After
170173 public void tearDown () throws Exception {
171- LOG .info ("tearDown called" );
172174 serviceHelper .stop ();
173175 }
174176
@@ -181,19 +183,28 @@ private JsonStreamWriter.Builder getTestJsonStreamWriterBuilder(
181183 }
182184
183185 @ Test
184- public void testTwoParamNewBuilder () throws Exception {
186+ public void testTwoParamNewBuilder_nullSchema () {
185187 try {
186188 getTestJsonStreamWriterBuilder (null , TABLE_SCHEMA );
189+ Assert .fail ("expected NullPointerException" );
187190 } catch (NullPointerException e ) {
188191 assertEquals (e .getMessage (), "StreamName is null." );
189192 }
193+ }
190194
195+ @ Test
196+ public void testTwoParamNewBuilder_nullStream () {
191197 try {
192198 getTestJsonStreamWriterBuilder (TEST_STREAM , null );
199+ Assert .fail ("expected NullPointerException" );
193200 } catch (NullPointerException e ) {
194201 assertEquals (e .getMessage (), "TableSchema is null." );
195202 }
203+ }
196204
205+ @ Test
206+ public void testTwoParamNewBuilder ()
207+ throws DescriptorValidationException , IOException , InterruptedException {
197208 JsonStreamWriter writer = getTestJsonStreamWriterBuilder (TEST_STREAM , TABLE_SCHEMA ).build ();
198209 assertEquals (TEST_STREAM , writer .getStreamName ());
199210 }
@@ -522,7 +533,9 @@ public void testAppendMultipleSchemaUpdate() throws Exception {
522533 }
523534
524535 @ Test
525- public void testAppendAlreadyExistsException () throws Exception {
536+ // This might be a bug but it is the current behavior. Investigate.
537+ public void testAppendAlreadyExists_doesNotThrowxception ()
538+ throws DescriptorValidationException , IOException , InterruptedException , ExecutionException {
526539 try (JsonStreamWriter writer =
527540 getTestJsonStreamWriterBuilder (TEST_STREAM , TABLE_SCHEMA ).build ()) {
528541 testBigQueryWrite .addResponse (
@@ -535,11 +548,7 @@ public void testAppendAlreadyExistsException() throws Exception {
535548 jsonArr .put (foo );
536549 ApiFuture <AppendRowsResponse > appendFuture =
537550 writer .append (jsonArr , -1 , /* allowUnknownFields */ false );
538- try {
539- appendFuture .get ();
540- } catch (Throwable t ) {
541- assertEquals (t .getCause ().getMessage (), "ALREADY_EXISTS: " );
542- }
551+ appendFuture .get ();
543552 }
544553 }
545554
@@ -559,8 +568,9 @@ public void testAppendOutOfRangeException() throws Exception {
559568 writer .append (jsonArr , -1 , /* allowUnknownFields */ false );
560569 try {
561570 appendFuture .get ();
562- } catch (Throwable t ) {
563- assertEquals (t .getCause ().getMessage (), "OUT_OF_RANGE: " );
571+ Assert .fail ("expected ExecutionException" );
572+ } catch (ExecutionException ex ) {
573+ assertEquals (ex .getCause ().getMessage (), "OUT_OF_RANGE: " );
564574 }
565575 }
566576 }
@@ -584,8 +594,9 @@ public void testAppendOutOfRangeAndUpdateSchema() throws Exception {
584594 writer .append (jsonArr , -1 , /* allowUnknownFields */ false );
585595 try {
586596 appendFuture .get ();
587- } catch (Throwable t ) {
588- assertEquals (t .getCause ().getMessage (), "OUT_OF_RANGE: " );
597+ Assert .fail ("expected ExecutionException" );
598+ } catch (ExecutionException ex ) {
599+ assertEquals (ex .getCause ().getMessage (), "OUT_OF_RANGE: " );
589600 int millis = 0 ;
590601 while (millis <= 10000 ) {
591602 if (writer .getDescriptor ().getFields ().size () == 2 ) {
@@ -781,15 +792,13 @@ public void run() {
781792 ApiFuture <AppendRowsResponse > appendFuture =
782793 writer .append (jsonArr , -1 , /* allowUnknownFields */ false );
783794 AppendRowsResponse response = appendFuture .get ();
784- LOG .info ("Processing complete, offset is " + response .getOffset ());
785795 offset_sets .remove (response .getOffset ());
786796 } catch (Exception e ) {
787797 LOG .severe ("Thread execution failed: " + e .getMessage ());
788798 }
789799 }
790800 });
791801 thread_arr [i ] = t ;
792- LOG .info ("Starting thread " + i + "." );
793802 t .start ();
794803 }
795804
@@ -833,10 +842,10 @@ public void testMultiThreadAppendWithSchemaUpdate() throws Exception {
833842 final JSONArray jsonArr = new JSONArray ();
834843 jsonArr .put (foo );
835844
836- final HashSet <Long > offset_sets = new HashSet <Long >();
837- int thread_nums = 5 ;
838- Thread [] thread_arr = new Thread [thread_nums ];
839- for (int i = 0 ; i < thread_nums ; i ++) {
845+ final HashSet <Long > offsetSets = new HashSet <Long >();
846+ int numberThreads = 5 ;
847+ Thread [] thread_arr = new Thread [numberThreads ];
848+ for (int i = 0 ; i < numberThreads ; i ++) {
840849 if (i == 2 ) {
841850 testBigQueryWrite .addResponse (
842851 Storage .AppendRowsResponse .newBuilder ()
@@ -848,7 +857,7 @@ public void testMultiThreadAppendWithSchemaUpdate() throws Exception {
848857 Storage .AppendRowsResponse .newBuilder ().setOffset ((long ) i ).build ());
849858 }
850859
851- offset_sets .add ((long ) i );
860+ offsetSets .add ((long ) i );
852861 Thread t =
853862 new Thread (
854863 new Runnable () {
@@ -857,23 +866,21 @@ public void run() {
857866 ApiFuture <AppendRowsResponse > appendFuture =
858867 writer .append (jsonArr , -1 , /* allowUnknownFields */ false );
859868 AppendRowsResponse response = appendFuture .get ();
860- LOG .info ("Processing complete, offset is " + response .getOffset ());
861- offset_sets .remove (response .getOffset ());
869+ offsetSets .remove (response .getOffset ());
862870 } catch (Exception e ) {
863871 LOG .severe ("Thread execution failed: " + e .getMessage ());
864872 }
865873 }
866874 });
867875 thread_arr [i ] = t ;
868- LOG .info ("Starting thread " + i + "." );
869876 t .start ();
870877 }
871878
872- for (int i = 0 ; i < thread_nums ; i ++) {
879+ for (int i = 0 ; i < numberThreads ; i ++) {
873880 thread_arr [i ].join ();
874881 }
875- assertTrue (offset_sets .size () == 0 );
876- for (int i = 0 ; i < thread_nums ; i ++) {
882+ assertTrue (offsetSets .size () == 0 );
883+ for (int i = 0 ; i < numberThreads ; i ++) {
877884 assertEquals (
878885 1 ,
879886 testBigQueryWrite
@@ -900,16 +907,16 @@ public void run() {
900907 Thread .sleep (100 );
901908 millis += 100 ;
902909 }
903- assertTrue ( writer .getDescriptor ().getFields ().size () == 2 );
910+ assertEquals ( 2 , writer .getDescriptor ().getFields ().size ());
904911
905912 foo .put ("bar" , "allen2" );
906913 final JSONArray jsonArr2 = new JSONArray ();
907914 jsonArr2 .put (foo );
908915
909- for (int i = thread_nums ; i < thread_nums + 5 ; i ++) {
916+ for (int i = numberThreads ; i < numberThreads + 5 ; i ++) {
910917 testBigQueryWrite .addResponse (
911918 Storage .AppendRowsResponse .newBuilder ().setOffset ((long ) i ).build ());
912- offset_sets .add ((long ) i );
919+ offsetSets .add ((long ) i );
913920 Thread t =
914921 new Thread (
915922 new Runnable () {
@@ -918,23 +925,21 @@ public void run() {
918925 ApiFuture <AppendRowsResponse > appendFuture =
919926 writer .append (jsonArr2 , -1 , /* allowUnknownFields */ false );
920927 AppendRowsResponse response = appendFuture .get ();
921- LOG .info ("Processing complete, offset is " + response .getOffset ());
922- offset_sets .remove (response .getOffset ());
928+ offsetSets .remove (response .getOffset ());
923929 } catch (Exception e ) {
924930 LOG .severe ("Thread execution failed: " + e .getMessage ());
925931 }
926932 }
927933 });
928934 thread_arr [i - 5 ] = t ;
929- LOG .info ("Starting thread " + i + " with updated json data." );
930935 t .start ();
931936 }
932937
933- for (int i = 0 ; i < thread_nums ; i ++) {
938+ for (int i = 0 ; i < numberThreads ; i ++) {
934939 thread_arr [i ].join ();
935940 }
936- assertTrue (offset_sets .size () == 0 );
937- for (int i = 0 ; i < thread_nums ; i ++) {
941+ assertTrue (offsetSets .size () == 0 );
942+ for (int i = 0 ; i < numberThreads ; i ++) {
938943 assertEquals (
939944 1 ,
940945 testBigQueryWrite
0 commit comments