2222import static org .junit .Assert .assertNotNull ;
2323import static org .junit .Assert .assertThrows ;
2424import static org .junit .Assert .assertTrue ;
25- import static org .junit .Assume .assumeFalse ;
2625
2726import com .google .cloud .spanner .AsyncResultSet ;
2827import com .google .cloud .spanner .AsyncResultSet .CallbackResponse ;
28+ import com .google .cloud .spanner .DatabaseClient ;
2929import com .google .cloud .spanner .Dialect ;
3030import com .google .cloud .spanner .ErrorCode ;
31+ import com .google .cloud .spanner .KeySet ;
3132import com .google .cloud .spanner .Mutation ;
3233import com .google .cloud .spanner .ParallelIntegrationTest ;
3334import com .google .cloud .spanner .ResultSet ;
3940import com .google .cloud .spanner .connection .StatementResult ;
4041import com .google .cloud .spanner .connection .StatementResult .ResultType ;
4142import com .google .cloud .spanner .connection .TransactionMode ;
42- import com .google .cloud . spanner . testing . EmulatorSpannerHelper ;
43+ import com .google .common . base . Preconditions ;
4344import com .google .common .collect .ImmutableList ;
4445import com .google .common .collect .ImmutableMap ;
4546import java .util .ArrayList ;
4647import java .util .Arrays ;
4748import java .util .Collections ;
48- import java .util .HashMap ;
49+ import java .util .HashSet ;
4950import java .util .List ;
50- import java .util .Map ;
51+ import java .util .Set ;
5152import java .util .concurrent .ExecutionException ;
5253import java .util .concurrent .Executors ;
5354import org .junit .Before ;
@@ -81,12 +82,7 @@ public class ITDmlReturningTest extends ITAbstractSpannerTest {
8182 + " SingerId BIGINT PRIMARY KEY,"
8283 + " FirstName character varying(1024),"
8384 + " LastName character varying(1024))" );
84- private final Map <Dialect , Boolean > IS_INITIALIZED = new HashMap <>();
85-
86- public ITDmlReturningTest () {
87- IS_INITIALIZED .put (Dialect .GOOGLE_STANDARD_SQL , false );
88- IS_INITIALIZED .put (Dialect .POSTGRESQL , false );
89- }
85+ private static final Set <Dialect > IS_INITIALIZED = new HashSet <>();
9086
9187 @ Parameter public Dialect dialect ;
9288
@@ -96,41 +92,34 @@ public static Object[] data() {
9692 }
9793
9894 private boolean checkAndSetInitialized () {
99- if ((dialect == Dialect .GOOGLE_STANDARD_SQL ) && !IS_INITIALIZED .get (dialect )) {
100- IS_INITIALIZED .put (dialect , true );
101- return true ;
102- }
103- if ((dialect == Dialect .POSTGRESQL ) && !IS_INITIALIZED .get (dialect )) {
104- IS_INITIALIZED .put (dialect , true );
105- return true ;
106- }
107- return false ;
95+ return !IS_INITIALIZED .add (dialect );
10896 }
10997
11098 @ Before
11199 public void setupTable () {
112- assumeFalse (
113- "DML Returning is not supported in the emulator" , EmulatorSpannerHelper .isUsingEmulator ());
114- if (checkAndSetInitialized ()) {
100+ if (!checkAndSetInitialized ()) {
115101 database =
116102 env .getTestHelper ()
117103 .createTestDatabase (dialect , Collections .singleton (DDL_MAP .get (dialect )));
118- List <String > firstNames = Arrays .asList ("ABC" , "ABC" , "DEF" , "PQR" , "ABC" );
119- List <String > lastNames = Arrays .asList ("XYZ" , "DEF" , "XYZ" , "ABC" , "GHI" );
120- List <Mutation > mutations = new ArrayList <>();
121- for (int id = 1 ; id <= 5 ; id ++) {
122- mutations .add (
123- Mutation .newInsertBuilder ("SINGERS" )
124- .set ("SINGERID" )
125- .to (id )
126- .set ("FIRSTNAME" )
127- .to (firstNames .get (id - 1 ))
128- .set ("LASTNAME" )
129- .to (lastNames .get (id - 1 ))
130- .build ());
131- }
132- env .getTestHelper ().getDatabaseClient (database ).write (mutations );
133104 }
105+ DatabaseClient client = env .getTestHelper ().getDatabaseClient (database );
106+ client .write (ImmutableList .of (Mutation .delete ("SINGERS" , KeySet .all ())));
107+
108+ List <String > firstNames = Arrays .asList ("ABC" , "ABC" , "DEF" , "PQR" , "ABC" );
109+ List <String > lastNames = Arrays .asList ("XYZ" , "DEF" , "XYZ" , "ABC" , "GHI" );
110+ List <Mutation > mutations = new ArrayList <>();
111+ for (int id = 1 ; id <= 5 ; id ++) {
112+ mutations .add (
113+ Mutation .newInsertBuilder ("SINGERS" )
114+ .set ("SINGERID" )
115+ .to (id )
116+ .set ("FIRSTNAME" )
117+ .to (firstNames .get (id - 1 ))
118+ .set ("LASTNAME" )
119+ .to (lastNames .get (id - 1 ))
120+ .build ());
121+ }
122+ env .getTestHelper ().getDatabaseClient (database ).write (mutations );
134123 }
135124
136125 @ Test
@@ -211,9 +200,9 @@ public void testDmlReturningExecuteUpdateAsync() {
211200 public void testDmlReturningExecuteBatchUpdate () {
212201 try (Connection connection = createConnection ()) {
213202 connection .setAutocommit (false );
214- final Statement UPDATE_STMT = UPDATE_RETURNING_MAP .get (dialect );
203+ final Statement updateStmt = Preconditions . checkNotNull ( UPDATE_RETURNING_MAP .get (dialect ) );
215204 long [] counts =
216- connection .executeBatchUpdate (ImmutableList .of (UPDATE_STMT , UPDATE_STMT , UPDATE_STMT ));
205+ connection .executeBatchUpdate (ImmutableList .of (updateStmt , updateStmt , updateStmt ));
217206 assertArrayEquals (counts , new long [] {3 , 3 , 3 });
218207 }
219208 }
@@ -222,10 +211,10 @@ public void testDmlReturningExecuteBatchUpdate() {
222211 public void testDmlReturningExecuteBatchUpdateAsync () {
223212 try (Connection connection = createConnection ()) {
224213 connection .setAutocommit (false );
225- final Statement UPDATE_STMT = UPDATE_RETURNING_MAP .get (dialect );
214+ final Statement updateStmt = Preconditions . checkNotNull ( UPDATE_RETURNING_MAP .get (dialect ) );
226215 long [] counts =
227216 connection
228- .executeBatchUpdateAsync (ImmutableList .of (UPDATE_STMT , UPDATE_STMT , UPDATE_STMT ))
217+ .executeBatchUpdateAsync (ImmutableList .of (updateStmt , updateStmt , updateStmt ))
229218 .get ();
230219 assertArrayEquals (counts , new long [] {3 , 3 , 3 });
231220 } catch (ExecutionException | InterruptedException e ) {
0 commit comments