|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
20 | 20 |
|
| 21 | +import com.google.cloud.spanner.DatabaseAdminClient; |
| 22 | +import com.google.cloud.spanner.DatabaseNotFoundException; |
| 23 | +import com.google.cloud.spanner.Spanner; |
| 24 | +import com.google.cloud.spanner.SpannerOptions; |
21 | 25 | import java.io.ByteArrayOutputStream;
|
22 | 26 | import java.io.PrintStream;
|
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.UUID; |
23 | 30 | import org.junit.After;
|
| 31 | +import org.junit.AfterClass; |
24 | 32 | import org.junit.Before;
|
| 33 | +import org.junit.BeforeClass; |
25 | 34 | import org.junit.Test;
|
26 | 35 | import org.junit.runner.RunWith;
|
27 | 36 | import org.junit.runners.JUnit4;
|
|
32 | 41 | @RunWith(JUnit4.class)
|
33 | 42 | @SuppressWarnings("checkstyle:abbreviationaswordinname")
|
34 | 43 | public class QuickstartSampleIT {
|
35 |
| -private String instanceId = System.getProperty("spanner.test.instance"); |
36 |
| -// This database needs to exist for test to pass. |
37 |
| -private String dbId = System.getProperty("spanner.quickstart.database"); |
| 44 | +private static String instanceId = System.getProperty("spanner.test.instance"); |
| 45 | +private static String dbId = formatForTest(System.getProperty("spanner.quickstart.database")); |
| 46 | +private static DatabaseAdminClient dbClient; |
| 47 | + |
38 | 48 | private ByteArrayOutputStream bout;
|
39 | 49 | private PrintStream stdOut = System.out;
|
40 | 50 | private PrintStream out;
|
41 | 51 |
|
| 52 | +@BeforeClass |
| 53 | +public static void createDatabase() { |
| 54 | +final SpannerOptions options = SpannerOptions.newBuilder().build(); |
| 55 | +final Spanner spanner = options.getService(); |
| 56 | +dbClient = spanner.getDatabaseAdminClient(); |
| 57 | +dbClient.createDatabase(instanceId, dbId, Collections.emptyList()); |
| 58 | +} |
| 59 | + |
| 60 | +@AfterClass |
| 61 | +public static void dropDatabase() { |
| 62 | +dbClient.dropDatabase(instanceId, dbId); |
| 63 | +} |
| 64 | + |
42 | 65 | @Before
|
43 | 66 | public void setUp() {
|
44 | 67 | bout = new ByteArrayOutputStream();
|
@@ -59,4 +82,8 @@ public void testQuickstart() throws Exception {
|
59 | 82 | String got = bout.toString();
|
60 | 83 | assertThat(got).contains("1");
|
61 | 84 | }
|
| 85 | + |
| 86 | +private static String formatForTest(String name) { |
| 87 | +return name + "-" + UUID.randomUUID().toString().substring(0, 20); |
| 88 | +} |
62 | 89 | }
|
0 commit comments