|
16 | 16 |
|
17 | 17 | package com.google.cloud.spanner.it;
|
18 | 18 |
|
19 |
| -import static com.google.cloud.spanner.SpannerMatchers.isSpannerException; |
20 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static org.junit.Assert.fail; |
21 | 21 |
|
22 | 22 | import com.google.cloud.Timestamp;
|
23 | 23 | import com.google.cloud.spanner.Database;
|
|
26 | 26 | import com.google.cloud.spanner.ErrorCode;
|
27 | 27 | import com.google.cloud.spanner.IntegrationTestEnv;
|
28 | 28 | import com.google.cloud.spanner.Key;
|
| 29 | +import com.google.cloud.spanner.KeySet; |
29 | 30 | import com.google.cloud.spanner.Mutation;
|
30 | 31 | import com.google.cloud.spanner.ParallelIntegrationTest;
|
31 |
| -import com.google.cloud.spanner.SpannerExceptionFactory; |
| 32 | +import com.google.cloud.spanner.SpannerException; |
32 | 33 | import com.google.cloud.spanner.Struct;
|
33 | 34 | import com.google.cloud.spanner.TimestampBound;
|
34 | 35 | import com.google.cloud.spanner.Value;
|
35 | 36 | import com.google.cloud.spanner.testing.RemoteSpannerHelper;
|
36 | 37 | import com.google.common.collect.ImmutableList;
|
37 | 38 | import java.util.Arrays;
|
38 | 39 | import java.util.concurrent.ExecutionException;
|
39 |
| -import org.junit.Before; |
| 40 | +import org.junit.After; |
| 41 | +import org.junit.BeforeClass; |
40 | 42 | import org.junit.ClassRule;
|
41 |
| -import org.junit.Rule; |
42 | 43 | import org.junit.Test;
|
43 | 44 | import org.junit.experimental.categories.Category;
|
44 |
| -import org.junit.rules.ExpectedException; |
45 | 45 | import org.junit.runner.RunWith;
|
46 | 46 | import org.junit.runners.JUnit4;
|
47 | 47 | import org.threeten.bp.Duration;
|
|
52 | 52 | @RunWith(JUnit4.class)
|
53 | 53 | public class ITCommitTimestampTest {
|
54 | 54 | @ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
|
55 |
| -@Rule public final ExpectedException expectedException = ExpectedException.none(); |
56 |
| -private Database db; |
57 |
| -private DatabaseClient client; |
58 |
| -private DatabaseAdminClient dbAdminClient; |
59 |
| -private RemoteSpannerHelper testHelper; |
60 |
| -private String instanceId; |
61 |
| -private String databaseId; |
| 55 | +private static Database db; |
| 56 | +private static DatabaseClient client; |
| 57 | +private static DatabaseAdminClient dbAdminClient; |
| 58 | +private static RemoteSpannerHelper testHelper; |
| 59 | +private static String instanceId; |
| 60 | +private static String databaseId; |
62 | 61 |
|
63 |
| -@Before |
64 |
| -public void setUp() throws Exception { |
| 62 | +@BeforeClass |
| 63 | +public static void setUp() throws Exception { |
65 | 64 | testHelper = env.getTestHelper();
|
66 | 65 | db =
|
67 | 66 | testHelper.createTestDatabase(
|
@@ -77,6 +76,11 @@ public void setUp() throws Exception {
|
77 | 76 | databaseId = db.getId().getDatabase();
|
78 | 77 | }
|
79 | 78 |
|
| 79 | +@After |
| 80 | +public void deleteAllTestRecords() { |
| 81 | +client.write(ImmutableList.of(Mutation.delete("T", KeySet.all()))); |
| 82 | +} |
| 83 | + |
80 | 84 | private Timestamp write(Mutation m) {
|
81 | 85 | return client.write(Arrays.asList(m));
|
82 | 86 | }
|
@@ -104,15 +108,18 @@ public void writeCommitTimestamp() {
|
104 | 108 |
|
105 | 109 | // 2. attempt to write CommitTimestamp to not enabled column should fail
|
106 | 110 | // error_catalog error CommitTimestampOptionNotEnabled
|
107 |
| -expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
108 |
| -expectedException.expectMessage("allow_commit_timestamp column option is not"); |
109 |
| -write( |
110 |
| -Mutation.newInsertOrUpdateBuilder("T") |
111 |
| -.set("K") |
112 |
| -.to("a") |
113 |
| -.set("T3") |
114 |
| -.to(Value.COMMIT_TIMESTAMP) |
115 |
| -.build()); |
| 111 | +try { |
| 112 | +write( |
| 113 | +Mutation.newInsertOrUpdateBuilder("T") |
| 114 | +.set("K") |
| 115 | +.to("a") |
| 116 | +.set("T3") |
| 117 | +.to(Value.COMMIT_TIMESTAMP) |
| 118 | +.build()); |
| 119 | +fail("missing expected exception"); |
| 120 | +} catch (SpannerException e) { |
| 121 | +assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
| 122 | +} |
116 | 123 | }
|
117 | 124 |
|
118 | 125 | @Test
|
@@ -148,66 +155,81 @@ public void schemaChangeTimestampInFuture() throws Exception {
|
148 | 155 | .build());
|
149 | 156 |
|
150 | 157 | // error_catalog error CommitTimestampNotInFuture
|
151 |
| -expectedException.expectCause(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
152 |
| -expectedException.expectMessage("has a timestamp in the future at key"); |
153 | 158 | String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (allow_commit_timestamp=true)";
|
154 | 159 | try {
|
155 | 160 | dbAdminClient
|
156 | 161 | .updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
|
157 | 162 | .get();
|
| 163 | +fail("missing expected exception"); |
158 | 164 | } catch (ExecutionException e) {
|
159 |
| -throw SpannerExceptionFactory.newSpannerException(e.getCause()); |
| 165 | +assertThat(e.getCause()).isInstanceOf(SpannerException.class); |
| 166 | +SpannerException se = (SpannerException) e.getCause(); |
| 167 | +assertThat(se.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
160 | 168 | }
|
161 | 169 | }
|
162 | 170 |
|
163 | 171 | @Test
|
164 | 172 | public void insertTimestampInFuture() {
|
165 | 173 | // error_catalog error TimestampInFuture
|
166 |
| -expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
167 |
| -expectedException.expectMessage("in the future"); |
168 |
| -write( |
169 |
| -Mutation.newInsertOrUpdateBuilder("T") |
170 |
| -.set("K") |
171 |
| -.to("a") |
172 |
| -.set("T1") |
173 |
| -.to(Timestamp.MAX_VALUE) |
174 |
| -.build()); |
| 174 | +try { |
| 175 | +write( |
| 176 | +Mutation.newInsertOrUpdateBuilder("T") |
| 177 | +.set("K") |
| 178 | +.to("a") |
| 179 | +.set("T1") |
| 180 | +.to(Timestamp.MAX_VALUE) |
| 181 | +.build()); |
| 182 | +fail("missing expected exception"); |
| 183 | +} catch (SpannerException e) { |
| 184 | +assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
| 185 | +} |
175 | 186 | }
|
176 | 187 |
|
177 | 188 | @Test
|
178 | 189 | public void invalidColumnOption() throws Exception {
|
179 | 190 | // error_catalog error DDLStatementWithError
|
180 |
| -expectedException.expectCause(isSpannerException(ErrorCode.INVALID_ARGUMENT)); |
181 |
| -expectedException.expectMessage("Option: bogus is unknown."); |
182 | 191 | String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (bogus=null)";
|
183 |
| -dbAdminClient |
184 |
| -.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null) |
185 |
| -.get(); |
| 192 | +try { |
| 193 | +dbAdminClient |
| 194 | +.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null) |
| 195 | +.get(); |
| 196 | +fail("missing expected exception"); |
| 197 | +} catch (ExecutionException e) { |
| 198 | +assertThat(e.getCause()).isInstanceOf(SpannerException.class); |
| 199 | +SpannerException se = (SpannerException) e.getCause(); |
| 200 | +assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 201 | +} |
186 | 202 | }
|
187 | 203 |
|
188 | 204 | @Test
|
189 | 205 | public void invalidColumnOptionValue() throws Exception {
|
190 | 206 | // error_catalog error DDLStatementWithErrors
|
191 |
| -expectedException.expectCause(isSpannerException(ErrorCode.INVALID_ARGUMENT)); |
192 |
| -expectedException.expectMessage("Errors parsing Spanner DDL statement"); |
193 | 207 | String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (allow_commit_timestamp=bogus)";
|
194 |
| -dbAdminClient |
195 |
| -.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null) |
196 |
| -.get(); |
| 208 | +try { |
| 209 | +dbAdminClient |
| 210 | +.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null) |
| 211 | +.get(); |
| 212 | +fail("missing expected exception"); |
| 213 | +} catch (ExecutionException e) { |
| 214 | +assertThat(e.getCause()).isInstanceOf(SpannerException.class); |
| 215 | +SpannerException se = (SpannerException) e.getCause(); |
| 216 | +assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT); |
| 217 | +} |
197 | 218 | }
|
198 | 219 |
|
199 | 220 | @Test
|
200 | 221 | public void invalidColumnType() throws Exception {
|
201 | 222 | // error_catalog error OptionErrorList
|
202 |
| -expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
203 |
| -expectedException.expectMessage("Option only allowed on TIMESTAMP columns"); |
204 | 223 | String statement = "ALTER TABLE T ADD COLUMN T4 INT64 OPTIONS (allow_commit_timestamp=true)";
|
205 | 224 | try {
|
206 | 225 | dbAdminClient
|
207 | 226 | .updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
|
208 | 227 | .get();
|
| 228 | +fail("missing expected exception"); |
209 | 229 | } catch (ExecutionException e) {
|
210 |
| -throw SpannerExceptionFactory.newSpannerException(e.getCause()); |
| 230 | +assertThat(e.getCause()).isInstanceOf(SpannerException.class); |
| 231 | +SpannerException se = (SpannerException) e.getCause(); |
| 232 | +assertThat(se.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
211 | 233 | }
|
212 | 234 | }
|
213 | 235 |
|
@@ -286,12 +308,17 @@ public void interleavedTableHierarchy1() {
|
286 | 308 | db.getId().getDatabase();
|
287 | 309 |
|
288 | 310 | // error_catalog error CommitTimestampOptionNotEnabled
|
289 |
| -expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
290 |
| -expectedException.expectMessage( |
291 |
| -"corresponding shared key columns in this table's interleaved table hierarchy"); |
292 |
| -client.write( |
293 |
| -Arrays.asList( |
294 |
| -Mutation.newInsertOrUpdateBuilder("T3").set("ts").to(Value.COMMIT_TIMESTAMP).build())); |
| 311 | +try { |
| 312 | +client.write( |
| 313 | +Arrays.asList( |
| 314 | +Mutation.newInsertOrUpdateBuilder("T3") |
| 315 | +.set("ts") |
| 316 | +.to(Value.COMMIT_TIMESTAMP) |
| 317 | +.build())); |
| 318 | +fail("missing expected exception"); |
| 319 | +} catch (SpannerException e) { |
| 320 | +assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
| 321 | +} |
295 | 322 | }
|
296 | 323 |
|
297 | 324 | // In interleaved table, use of commit timestamp in parent table is not
|
@@ -309,11 +336,16 @@ public void interleavedTableHierarchy2() {
|
309 | 336 | db.getId().getDatabase();
|
310 | 337 |
|
311 | 338 | // error_catalog error CommitTimestampOptionNotEnabled
|
312 |
| -expectedException.expect(isSpannerException(ErrorCode.FAILED_PRECONDITION)); |
313 |
| -expectedException.expectMessage( |
314 |
| -"corresponding shared key columns in this table's interleaved table hierarchy"); |
315 |
| -client.write( |
316 |
| -Arrays.asList( |
317 |
| -Mutation.newInsertOrUpdateBuilder("T1").set("ts").to(Value.COMMIT_TIMESTAMP).build())); |
| 339 | +try { |
| 340 | +client.write( |
| 341 | +Arrays.asList( |
| 342 | +Mutation.newInsertOrUpdateBuilder("T1") |
| 343 | +.set("ts") |
| 344 | +.to(Value.COMMIT_TIMESTAMP) |
| 345 | +.build())); |
| 346 | +fail("missing expected exception"); |
| 347 | +} catch (SpannerException e) { |
| 348 | +assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION); |
| 349 | +} |
318 | 350 | }
|
319 | 351 | }
|
0 commit comments