Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: upgrade Mockito to version 4.x #1498

Merged
merged 2 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions google-cloud-spanner/pom.xml
Expand Up @@ -282,15 +282,10 @@
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
<dependency><groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<exclusions>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
</exclusions>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Expand Up @@ -21,7 +21,7 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Expand Up @@ -26,7 +26,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AsyncTransactionManagerImplTest {
Expand Down
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner;

/** Util class for getting the Java version the tests are executed on. */
public class JavaVersionUtil {

/** Returns the major Java version (e.g. 8, 11, 17) */
public static int getJavaMajorVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
}
Expand Up @@ -19,8 +19,8 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down
Expand Up @@ -23,6 +23,7 @@
import static org.mockito.Mockito.verify;

import com.google.api.client.util.BackOff;
import com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
Expand All @@ -37,18 +38,19 @@
import io.opencensus.trace.EndSpanOptions;
import io.opencensus.trace.Span;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;

/** Unit tests for {@link AbstractResultSet.ResumableStreamIterator}. */
@RunWith(JUnit4.class)
Expand Down Expand Up @@ -156,8 +158,12 @@ public void simple() {

@Test
public void closedSpan() {
Assume.assumeTrue(
"This test is only supported on JDK11 and lower",
JavaVersionUtil.getJavaMajorVersion() < 12);

Span span = mock(Span.class);
Whitebox.setInternalState(this.resumableStreamIterator, "span", span);
setInternalState(ResumableStreamIterator.class, this.resumableStreamIterator, "span", span);

ResultSetStream s1 = Mockito.mock(ResultSetStream.class);
Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1));
Expand Down Expand Up @@ -236,9 +242,14 @@ public void restartWithHoldBackMidStream() {

@Test
public void retryableErrorWithoutRetryInfo() throws IOException {
Assume.assumeTrue(
"This test is only supported on JDK11 and lower",
JavaVersionUtil.getJavaMajorVersion() < 12);

BackOff backOff = mock(BackOff.class);
Mockito.when(backOff.nextBackOffMillis()).thenReturn(1L);
Whitebox.setInternalState(this.resumableStreamIterator, "backOff", backOff);
setInternalState(
ResumableStreamIterator.class, this.resumableStreamIterator, "backOff", backOff);

ResultSetStream s1 = Mockito.mock(ResultSetStream.class);
Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1));
Expand Down Expand Up @@ -446,4 +457,18 @@ static List<String> consumeAtMost(int n, Iterator<PartialResultSet> iterator) {
}
return r;
}

/**
* Sets a private static final field to a specific value. This is only supported on Java11 and
* lower.
*/
private static void setInternalState(Class<?> c, Object target, String field, Object value) {
try {
Field f = c.getDeclaredField(field);
f.setAccessible(true);
f.set(target, value);
} catch (Exception e) {
throw new RuntimeException("Unable to set internal state on a private field.", e);
}
}
}
Expand Up @@ -149,10 +149,10 @@ public void batchCreateAndCloseSessions() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.eq(labels), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
usedChannels.add(channelHint);
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
Expand Down Expand Up @@ -209,10 +209,10 @@ public void batchCreateSessionsDistributesMultipleRequestsOverChannels() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.eq(labels), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
usedChannelHints.add(channelHint);
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
Expand Down Expand Up @@ -290,13 +290,13 @@ public void batchCreateSessionsWithExceptions() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.anyMap(), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
if (errorOnChannels.contains(channelHint)) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.RESOURCE_EXHAUSTED, "could not create any more sessions");
} else {
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
Expand Down Expand Up @@ -354,7 +354,7 @@ public void batchCreateSessionsServerReturnsLessSessionsPerBatch() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.anyMap(), Mockito.anyMap()))
.then(
invocation -> {
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= Math.min(MAX_SESSIONS_PER_BATCH, sessionCount); i++) {
res.add(
Expand Down
Expand Up @@ -92,11 +92,7 @@ public void setUp() {
DatabaseId db = DatabaseId.of(dbName);

Session sessionProto = Session.newBuilder().setName(sessionName).build();
Mockito.when(
rpc.createSession(
Mockito.eq(dbName),
Mockito.anyMapOf(String.class, String.class),
optionsCaptor.capture()))
Mockito.when(rpc.createSession(Mockito.eq(dbName), Mockito.anyMap(), optionsCaptor.capture()))
.thenReturn(sessionProto);
Transaction txn = Transaction.newBuilder().setId(ByteString.copyFromUtf8("TEST")).build();
Mockito.when(
Expand Down
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -77,9 +77,9 @@ private void setupMockSessionCreation() {
invocation -> {
executor.submit(
() -> {
int sessionCount = invocation.getArgumentAt(0, Integer.class);
int sessionCount = invocation.getArgument(0, Integer.class);
SessionConsumerImpl consumer =
invocation.getArgumentAt(2, SessionConsumerImpl.class);
invocation.getArgument(2, SessionConsumerImpl.class);
for (int i = 0; i < sessionCount; i++) {
consumer.onSessionReady(setupMockSession(mockSession()));
}
Expand Down
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -96,7 +96,7 @@ private void setupSpanner(DatabaseId db) {
invocation -> {
createExecutor.submit(
() -> {
int sessionCount = invocation.getArgumentAt(0, Integer.class);
int sessionCount = invocation.getArgument(0, Integer.class);
for (int s = 0; s < sessionCount; s++) {
SessionImpl session;
synchronized (lock) {
Expand All @@ -108,7 +108,7 @@ private void setupSpanner(DatabaseId db) {
}
}
SessionConsumerImpl consumer =
invocation.getArgumentAt(2, SessionConsumerImpl.class);
invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(session);
}
});
Expand Down