File tree Expand file tree Collapse file tree
java-spanner/samples/snippets/src/test/java/com/example/spanner Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class CreateFullBackupScheduleSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase ();
36+ }
3137
3238 @ Test
3339 public void testCreateFullBackupScheduleSample () throws Exception {
Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class CreateIncrementalBackupScheduleSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase (multiRegionalInstanceId );
36+ }
3137
3238 @ Test
3339 public void testCreateIncrementalBackupScheduleSample () throws Exception {
Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class DeleteBackupScheduleSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase ();
36+ }
3137
3238 @ Test
3339 public void testDeleteBackupScheduleSample () throws Exception {
Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class GetBackupScheduleSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase ();
36+ }
3137
3238 @ Test
3339 public void testGetBackupScheduleSample () throws Exception {
Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class ListBackupSchedulesSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase ();
36+ }
3137
3238 @ Test
3339 public void testListBackupSchedulesSample () throws Exception {
Original file line number Diff line number Diff line change 2323import com .google .cloud .spanner .admin .database .v1 .DatabaseAdminSettings ;
2424import com .google .cloud .spanner .admin .instance .v1 .InstanceAdminClient ;
2525import com .google .cloud .spanner .admin .instance .v1 .InstanceAdminSettings ;
26+ import com .google .spanner .admin .database .v1 .CreateDatabaseRequest ;
2627import com .google .spanner .admin .database .v1 .DatabaseDialect ;
28+ import com .google .spanner .admin .database .v1 .InstanceName ;
2729import java .io .IOException ;
2830import java .util .concurrent .TimeUnit ;
2931import org .junit .AfterClass ;
@@ -180,4 +182,20 @@ static String getCreateDatabaseStatement(
180182 return "CREATE DATABASE \" " + databaseName + "\" " ;
181183 }
182184 }
185+
186+ protected static String createEphemeralDatabase () throws Exception {
187+ return createEphemeralDatabase (instanceId );
188+ }
189+
190+ protected static String createEphemeralDatabase (String targetInstanceId ) throws Exception {
191+ String databaseId = idGenerator .generateDatabaseId ();
192+ CreateDatabaseRequest request =
193+ CreateDatabaseRequest .newBuilder ()
194+ .setParent (InstanceName .of (projectId , targetInstanceId ).toString ())
195+ .setCreateStatement (
196+ getCreateDatabaseStatement (databaseId , DatabaseDialect .GOOGLE_STANDARD_SQL ))
197+ .build ();
198+ databaseAdminClient .createDatabaseAsync (request ).get (5 , TimeUnit .MINUTES );
199+ return databaseId ;
200+ }
183201}
Original file line number Diff line number Diff line change 2020
2121import com .google .spanner .admin .database .v1 .BackupScheduleName ;
2222import java .util .UUID ;
23+ import org .junit .BeforeClass ;
2324import org .junit .Test ;
2425import org .junit .runner .RunWith ;
2526import org .junit .runners .JUnit4 ;
2627
2728@ RunWith (JUnit4 .class )
2829public class UpdateBackupScheduleSampleIT extends SampleTestBaseV2 {
29- // Default instance and given db should exist for tests to pass.
30- private static String databaseId = System .getProperty ("spanner.sample.database" , "mysample" );
30+
31+ private static String databaseId ;
32+
33+ @ BeforeClass
34+ public static void setUp () throws Exception {
35+ databaseId = createEphemeralDatabase ();
36+ }
3137
3238 @ Test
3339 public void testUpdateBackupScheduleSample () throws Exception {
You can’t perform that action at this time.
0 commit comments