Skip to content

Commit a175980

Browse files
nithinsujirpongad
authored andcommitted
spanner: expand test coverage for getDatabaseClient() (#3686)
This change also adds the jacoco coverage plugin in the spanner pom.xml and sets jacoco.skip to true to disable it by default. It can be enabled by passing -Djacoco.skip=false to the mvn command.
1 parent ae614b3 commit a175980

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

google-cloud-clients/google-cloud-spanner/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@
1818
</parent>
1919
<properties>
2020
<site.installationModule>google-cloud-spanner</site.installationModule>
21+
<jacoco.skip>true</jacoco.skip>
2122
</properties>
2223
<build>
2324
<plugins>
25+
<plugin>
26+
<groupId>org.jacoco</groupId>
27+
<artifactId>jacoco-maven-plugin</artifactId>
28+
<version>0.8.2</version>
29+
<executions>
30+
<execution>
31+
<goals>
32+
<goal>prepare-agent</goal>
33+
</goals>
34+
</execution>
35+
<execution>
36+
<id>report</id>
37+
<phase>prepare-package</phase>
38+
<goals>
39+
<goal>report</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
</plugin>
2444
<plugin>
2545
<groupId>org.apache.maven.plugins</groupId>
2646
<artifactId>maven-surefire-plugin</artifactId>

google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package com.google.cloud.spanner;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.fail;
2021

22+
import com.google.cloud.grpc.GrpcTransportOptions;
2123
import com.google.cloud.spanner.spi.v1.SpannerRpc;
22-
2324
import java.util.HashMap;
2425
import java.util.Map;
2526
import org.junit.Before;
@@ -70,4 +71,49 @@ public void createAndCloseSession() {
7071
// The same channelHint is passed for deleteSession (contained in "options").
7172
Mockito.verify(rpc).deleteSession(sessionName, options.getValue());
7273
}
74+
75+
@Test
76+
public void getDbclientAgainGivesSame() {
77+
Map<String, String> labels = new HashMap<>();
78+
labels.put("env", "dev");
79+
Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels);
80+
String dbName = "projects/p1/instances/i1/databases/d1";
81+
DatabaseId db = DatabaseId.of(dbName);
82+
83+
Mockito.when(spannerOptions.getTransportOptions())
84+
.thenReturn(GrpcTransportOptions.newBuilder().build());
85+
Mockito.when(spannerOptions.getSessionPoolOptions())
86+
.thenReturn(SessionPoolOptions.newBuilder().build());
87+
88+
DatabaseClient databaseClient = impl.getDatabaseClient(db);
89+
90+
// Get db client again
91+
DatabaseClient databaseClient1 = impl.getDatabaseClient(db);
92+
93+
assertThat(databaseClient1).isSameAs(databaseClient);
94+
}
95+
96+
@Test
97+
public void getDbclientAfterCloseThrows() {
98+
SpannerImpl imp = new SpannerImpl(rpc, 1, spannerOptions);
99+
Map<String, String> labels = new HashMap<>();
100+
labels.put("env", "dev");
101+
Mockito.when(spannerOptions.getSessionLabels()).thenReturn(labels);
102+
String dbName = "projects/p1/instances/i1/databases/d1";
103+
DatabaseId db = DatabaseId.of(dbName);
104+
105+
Mockito.when(spannerOptions.getTransportOptions())
106+
.thenReturn(GrpcTransportOptions.newBuilder().build());
107+
Mockito.when(spannerOptions.getSessionPoolOptions())
108+
.thenReturn(SessionPoolOptions.newBuilder().build());
109+
110+
imp.close();
111+
112+
try {
113+
imp.getDatabaseClient(db);
114+
fail("Expected exception");
115+
} catch (IllegalStateException e) {
116+
assertThat(e.getMessage()).contains("Cloud Spanner client has been closed");
117+
}
118+
}
73119
}

0 commit comments

Comments
 (0)