1616
1717package com .google .cloud .spanner .it ;
1818
19- import static com .google .cloud .spanner .SpannerMatchers .isSpannerException ;
2019import static com .google .common .truth .Truth .assertThat ;
20+ import static org .junit .Assert .fail ;
2121
2222import com .google .cloud .Timestamp ;
2323import com .google .cloud .spanner .Database ;
2626import com .google .cloud .spanner .ErrorCode ;
2727import com .google .cloud .spanner .IntegrationTestEnv ;
2828import com .google .cloud .spanner .Key ;
29+ import com .google .cloud .spanner .KeySet ;
2930import com .google .cloud .spanner .Mutation ;
3031import com .google .cloud .spanner .ParallelIntegrationTest ;
31- import com .google .cloud .spanner .SpannerExceptionFactory ;
32+ import com .google .cloud .spanner .SpannerException ;
3233import com .google .cloud .spanner .Struct ;
3334import com .google .cloud .spanner .TimestampBound ;
3435import com .google .cloud .spanner .Value ;
3536import com .google .cloud .spanner .testing .RemoteSpannerHelper ;
3637import com .google .common .collect .ImmutableList ;
3738import java .util .Arrays ;
3839import java .util .concurrent .ExecutionException ;
39- import org .junit .Before ;
40+ import org .junit .After ;
41+ import org .junit .BeforeClass ;
4042import org .junit .ClassRule ;
41- import org .junit .Rule ;
4243import org .junit .Test ;
4344import org .junit .experimental .categories .Category ;
44- import org .junit .rules .ExpectedException ;
4545import org .junit .runner .RunWith ;
4646import org .junit .runners .JUnit4 ;
4747import org .threeten .bp .Duration ;
5252@ RunWith (JUnit4 .class )
5353public class ITCommitTimestampTest {
5454 @ 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 ;
6261
63- @ Before
64- public void setUp () throws Exception {
62+ @ BeforeClass
63+ public static void setUp () throws Exception {
6564 testHelper = env .getTestHelper ();
6665 db =
6766 testHelper .createTestDatabase (
@@ -77,6 +76,11 @@ public void setUp() throws Exception {
7776 databaseId = db .getId ().getDatabase ();
7877 }
7978
79+ @ After
80+ public void deleteAllTestRecords () {
81+ client .write (ImmutableList .of (Mutation .delete ("T" , KeySet .all ())));
82+ }
83+
8084 private Timestamp write (Mutation m ) {
8185 return client .write (Arrays .asList (m ));
8286 }
@@ -104,15 +108,18 @@ public void writeCommitTimestamp() {
104108
105109 // 2. attempt to write CommitTimestamp to not enabled column should fail
106110 // 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+ }
116123 }
117124
118125 @ Test
@@ -148,66 +155,81 @@ public void schemaChangeTimestampInFuture() throws Exception {
148155 .build ());
149156
150157 // error_catalog error CommitTimestampNotInFuture
151- expectedException .expectCause (isSpannerException (ErrorCode .FAILED_PRECONDITION ));
152- expectedException .expectMessage ("has a timestamp in the future at key" );
153158 String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (allow_commit_timestamp=true)" ;
154159 try {
155160 dbAdminClient
156161 .updateDatabaseDdl (instanceId , databaseId , ImmutableList .of (statement ), null )
157162 .get ();
163+ fail ("missing expected exception" );
158164 } 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 );
160168 }
161169 }
162170
163171 @ Test
164172 public void insertTimestampInFuture () {
165173 // 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+ }
175186 }
176187
177188 @ Test
178189 public void invalidColumnOption () throws Exception {
179190 // error_catalog error DDLStatementWithError
180- expectedException .expectCause (isSpannerException (ErrorCode .INVALID_ARGUMENT ));
181- expectedException .expectMessage ("Option: bogus is unknown." );
182191 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+ }
186202 }
187203
188204 @ Test
189205 public void invalidColumnOptionValue () throws Exception {
190206 // error_catalog error DDLStatementWithErrors
191- expectedException .expectCause (isSpannerException (ErrorCode .INVALID_ARGUMENT ));
192- expectedException .expectMessage ("Errors parsing Spanner DDL statement" );
193207 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+ }
197218 }
198219
199220 @ Test
200221 public void invalidColumnType () throws Exception {
201222 // error_catalog error OptionErrorList
202- expectedException .expect (isSpannerException (ErrorCode .FAILED_PRECONDITION ));
203- expectedException .expectMessage ("Option only allowed on TIMESTAMP columns" );
204223 String statement = "ALTER TABLE T ADD COLUMN T4 INT64 OPTIONS (allow_commit_timestamp=true)" ;
205224 try {
206225 dbAdminClient
207226 .updateDatabaseDdl (instanceId , databaseId , ImmutableList .of (statement ), null )
208227 .get ();
228+ fail ("missing expected exception" );
209229 } 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 );
211233 }
212234 }
213235
@@ -286,12 +308,17 @@ public void interleavedTableHierarchy1() {
286308 db .getId ().getDatabase ();
287309
288310 // 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+ }
295322 }
296323
297324 // In interleaved table, use of commit timestamp in parent table is not
@@ -309,11 +336,16 @@ public void interleavedTableHierarchy2() {
309336 db .getId ().getDatabase ();
310337
311338 // 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+ }
318350 }
319351}
0 commit comments