2222import com .google .cloud .asset .v1 .ContentType ;
2323import com .google .cloud .bigquery .BigQuery ;
2424import com .google .cloud .bigquery .BigQuery .DatasetDeleteOption ;
25+ import com .google .cloud .bigquery .BigQueryException ;
2526import com .google .cloud .bigquery .BigQueryOptions ;
26- import com .google .cloud .bigquery .Dataset ;
2727import com .google .cloud .bigquery .DatasetId ;
2828import com .google .cloud .bigquery .DatasetInfo ;
2929import com .google .cloud .bigquery .testing .RemoteBigQueryHelper ;
3030import com .google .cloud .storage .BlobInfo ;
3131import com .google .cloud .storage .Storage ;
3232import com .google .cloud .storage .Storage .BlobListOption ;
3333import com .google .cloud .storage .StorageOptions ;
34+ import com .google .cloud .testing .junit4 .MultipleAttemptsRule ;
3435import java .io .ByteArrayOutputStream ;
3536import java .io .PrintStream ;
3637import java .util .UUID ;
38+ import java .util .concurrent .TimeUnit ;
3739import org .junit .After ;
3840import org .junit .Before ;
41+ import org .junit .Rule ;
3942import org .junit .Test ;
43+ import org .junit .rules .Timeout ;
4044import org .junit .runner .RunWith ;
4145import org .junit .runners .JUnit4 ;
4246
4347/** Tests for quickstart sample. */
4448@ RunWith (JUnit4 .class )
4549@ SuppressWarnings ("checkstyle:abbreviationaswordinname" )
4650public class QuickStartIT {
51+ @ Rule public final Timeout testTimeout = new Timeout (10 , TimeUnit .MINUTES );
52+ @ Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule (3 );
53+
4754 private static final String bucketName = "java-docs-samples-testing" ;
4855 private static final String path = UUID .randomUUID ().toString ();
4956 private static final String datasetName = RemoteBigQueryHelper .generateDatasetName ();
@@ -69,9 +76,6 @@ private static final void deleteObjects() {
6976 @ Before
7077 public void setUp () {
7178 bigquery = BigQueryOptions .getDefaultInstance ().getService ();
72- if (bigquery .getDataset (datasetName ) == null ) {
73- Dataset dataset = bigquery .create (DatasetInfo .newBuilder (datasetName ).build ());
74- }
7579 bout = new ByteArrayOutputStream ();
7680 out = new PrintStream (bout );
7781 originalPrintStream = System .out ;
@@ -98,8 +102,7 @@ public void testExportAssetExample() throws Exception {
98102
99103 @ Test
100104 public void testExportAssetBigqueryPerTypeExample () throws Exception {
101- String dataset =
102- String .format ("projects/%s/datasets/%s" , ServiceOptions .getDefaultProjectId (), datasetName );
105+ String dataset = getDataset ();
103106 String table = "java_test_per_type" ;
104107 ExportAssetsBigqueryExample .exportBigQuery (
105108 dataset , table , ContentType .RESOURCE , /*perType*/ true );
@@ -109,8 +112,7 @@ public void testExportAssetBigqueryPerTypeExample() throws Exception {
109112
110113 @ Test
111114 public void testExportAssetBigqueryExample () throws Exception {
112- String dataset =
113- String .format ("projects/%s/datasets/%s" , ServiceOptions .getDefaultProjectId (), datasetName );
115+ String dataset = getDataset ();
114116 String table = "java_test" ;
115117 ExportAssetsBigqueryExample .exportBigQuery (
116118 dataset , table , ContentType .RESOURCE , /*perType*/ false );
@@ -127,4 +129,13 @@ public void testBatchGetAssetsHistory() throws Exception {
127129 assertThat (got ).contains (bucketAssetName );
128130 }
129131 }
132+
133+ protected String getDataset () throws BigQueryException {
134+ if (bigquery .getDataset (datasetName ) == null ) {
135+ bigquery .create (DatasetInfo .newBuilder (datasetName ).build ());
136+ }
137+ return String .format (
138+ "projects/%s/datasets/%s" , ServiceOptions .getDefaultProjectId (), datasetName );
139+
140+ }
130141}
0 commit comments