diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3932a70d4..2425d7234 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,14 +9,15 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - java: [8, 11] + java: [8, 11, 17] steps: - uses: actions/checkout@v2 - uses: stCarolas/setup-maven@v4 with: maven-version: 3.8.1 - - uses: actions/setup-java@v1 + - uses: actions/setup-java@v2 with: + distribution: zulu java-version: ${{matrix.java}} - run: java -version - run: .kokoro/build.sh @@ -29,8 +30,9 @@ jobs: - uses: stCarolas/setup-maven@v4 with: maven-version: 3.8.1 - - uses: actions/setup-java@v1 + - uses: actions/setup-java@v2 with: + distribution: zulu java-version: 8 - run: java -version - run: .kokoro/build.bat @@ -40,14 +42,15 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - java: [8, 11] + java: [8, 11, 17] steps: - uses: actions/checkout@v2 - uses: stCarolas/setup-maven@v4 with: maven-version: 3.8.1 - - uses: actions/setup-java@v1 + - uses: actions/setup-java@v2 with: + distribution: zulu java-version: ${{matrix.java}} - run: java -version - run: .kokoro/dependencies.sh @@ -58,8 +61,9 @@ jobs: - uses: stCarolas/setup-maven@v4 with: maven-version: 3.8.1 - - uses: actions/setup-java@v1 + - uses: actions/setup-java@v2 with: + distribution: zulu java-version: 8 - run: java -version - run: .kokoro/build.sh @@ -72,8 +76,9 @@ jobs: - uses: stCarolas/setup-maven@v4 with: maven-version: 3.8.1 - - uses: actions/setup-java@v1 + - uses: actions/setup-java@v2 with: + distribution: zulu java-version: 8 - run: java -version - run: .kokoro/build.sh diff --git a/.kokoro/dependencies.sh b/.kokoro/dependencies.sh index 9030ba8f9..9a5105d7e 100755 --- a/.kokoro/dependencies.sh +++ b/.kokoro/dependencies.sh @@ -28,7 +28,28 @@ source ${scriptDir}/common.sh java -version echo $JOB_TYPE -export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=128m" +function determineMavenOpts() { + local javaVersion=$( + # filter down to the version line, then pull out the version between quotes, + # then trim the version number down to its minimal number (removing any + # update or suffix number). + java -version 2>&1 | grep "version" \ + | sed -E 's/^.*"(.*?)".*$/\1/g' \ + | sed -E 's/^(1\.[0-9]\.0).*$/\1/g' + ) + + case $javaVersion in + "17") + # MaxPermSize is no longer supported as of jdk 17 + echo -n "-Xmx1024m" + ;; + *) + echo -n "-Xmx1024m -XX:MaxPermSize=128m" + ;; + esac +} + +export MAVEN_OPTS=$(determineMavenOpts) # this should run maven enforcer retry_with_backoff 3 10 \ diff --git a/google-http-client-apache-v2/pom.xml b/google-http-client-apache-v2/pom.xml index a2768ead2..97d307302 100644 --- a/google-http-client-apache-v2/pom.xml +++ b/google-http-client-apache-v2/pom.xml @@ -99,10 +99,5 @@ org.apache.httpcomponents httpcore - - org.mockito - mockito-all - test - diff --git a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java index be6f983c6..48a9d1c56 100644 --- a/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java +++ b/google-http-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java @@ -20,14 +20,13 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeFalse; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.Assume.assumeTrue; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpResponseException; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.LowLevelHttpResponse; +import com.google.api.client.testing.http.apache.MockHttpClient; import com.google.api.client.util.ByteArrayStreamingContent; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @@ -47,7 +46,9 @@ import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.HttpHostConnectException; @@ -65,6 +66,15 @@ */ public class ApacheHttpTransportTest { + private static class MockHttpResponse extends BasicHttpResponse implements CloseableHttpResponse { + public MockHttpResponse() { + super(HttpVersion.HTTP_1_1, 200, "OK"); + } + + @Override + public void close() throws IOException {} + } + @Test public void testApacheHttpTransport() { ApacheHttpTransport transport = new ApacheHttpTransport(); @@ -99,10 +109,14 @@ private void checkHttpClient(HttpClient client) { @Test public void testRequestsWithContent() throws IOException { - HttpClient mockClient = mock(HttpClient.class); - HttpResponse mockResponse = mock(HttpResponse.class); - when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse); - + HttpClient mockClient = + new MockHttpClient() { + @Override + public CloseableHttpResponse execute(HttpUriRequest request) + throws IOException, ClientProtocolException { + return new MockHttpResponse(); + } + }; ApacheHttpTransport transport = new ApacheHttpTransport(mockClient); // Test GET. @@ -204,6 +218,9 @@ public void process(HttpRequest request, HttpContext context) public void testConnectTimeout() { // Apache HttpClient doesn't appear to behave correctly on windows assumeFalse(isWindows()); + // TODO(chanseok): Java 17 returns an IOException (SocketException: Network is unreachable). + // Figure out a way to verify connection timeout works on Java 17+. + assumeTrue(System.getProperty("java.version").compareTo("17") < 0); HttpTransport httpTransport = new ApacheHttpTransport(); GenericUrl url = new GenericUrl("http://google.com:81"); @@ -213,7 +230,7 @@ public void testConnectTimeout() { } catch (HttpHostConnectException | ConnectTimeoutException expected) { // expected } catch (IOException e) { - fail("unexpected IOException: " + e.getClass().getName()); + fail("unexpected IOException: " + e.getClass().getName() + ": " + e.getMessage()); } } @@ -222,9 +239,9 @@ private static class FakeServer implements AutoCloseable { private final ExecutorService executorService; FakeServer(HttpHandler httpHandler) throws IOException { - this.server = HttpServer.create(new InetSocketAddress(0), 0); - this.executorService = Executors.newFixedThreadPool(1); - server.setExecutor(this.executorService); + server = HttpServer.create(new InetSocketAddress(0), 0); + executorService = Executors.newFixedThreadPool(1); + server.setExecutor(executorService); server.createContext("/", httpHandler); server.start(); } @@ -235,8 +252,8 @@ public int getPort() { @Override public void close() { - this.server.stop(0); - this.executorService.shutdownNow(); + server.stop(0); + executorService.shutdownNow(); } } diff --git a/google-http-client/pom.xml b/google-http-client/pom.xml index dd91f9594..8ef066b16 100644 --- a/google-http-client/pom.xml +++ b/google-http-client/pom.xml @@ -167,11 +167,6 @@ truth test - - org.mockito - mockito-all - test - io.opencensus opencensus-impl diff --git a/google-http-client/src/main/java/com/google/api/client/http/GZipEncoding.java b/google-http-client/src/main/java/com/google/api/client/http/GZipEncoding.java index 28574a80d..c811b002c 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/GZipEncoding.java +++ b/google-http-client/src/main/java/com/google/api/client/http/GZipEncoding.java @@ -28,10 +28,12 @@ */ public class GZipEncoding implements HttpEncoding { + @Override public String getName() { return "gzip"; } + @Override public void encode(StreamingContent content, OutputStream out) throws IOException { // must not close the underlying output stream OutputStream out2 = diff --git a/google-http-client/src/main/java/com/google/api/client/testing/json/MockJsonParser.java b/google-http-client/src/main/java/com/google/api/client/testing/json/MockJsonParser.java index 422ddd962..64b48bcee 100644 --- a/google-http-client/src/main/java/com/google/api/client/testing/json/MockJsonParser.java +++ b/google-http-client/src/main/java/com/google/api/client/testing/json/MockJsonParser.java @@ -38,7 +38,7 @@ public class MockJsonParser extends JsonParser { private final JsonFactory factory; - MockJsonParser(JsonFactory factory) { + public MockJsonParser(JsonFactory factory) { this.factory = factory; } diff --git a/google-http-client/src/test/java/com/google/api/client/http/GZipEncodingTest.java b/google-http-client/src/test/java/com/google/api/client/http/GZipEncodingTest.java index 5ecd0f8e9..4963b05bd 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/GZipEncodingTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/GZipEncodingTest.java @@ -28,19 +28,33 @@ */ public class GZipEncodingTest extends TestCase { - byte[] EXPECED_ZIPPED = + private static final byte[] EXPECED_ZIPPED = + new byte[] { + 31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -53, -49, -57, 13, 0, -30, -66, -14, 54, 28, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + }; + + // TODO: remove when no longer using Java < 16: https://bugs.openjdk.java.net/browse/JDK-8244706 + @Deprecated + private static final byte[] EXPECED_ZIPPED_BELOW_JAVA_16 = new byte[] { 31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -53, -49, -57, 13, 0, -30, -66, -14, 54, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; public void test() throws IOException { + // TODO: remove when no longer using Java < 16. + byte[] expected = + System.getProperty("java.version").compareTo("16") >= 0 + ? EXPECED_ZIPPED + : EXPECED_ZIPPED_BELOW_JAVA_16; + GZipEncoding encoding = new GZipEncoding(); ByteArrayStreamingContent content = new ByteArrayStreamingContent(StringUtils.getBytesUtf8("oooooooooooooooooooooooooooo")); TestableByteArrayOutputStream out = new TestableByteArrayOutputStream(); encoding.encode(content, out); assertFalse(out.isClosed()); - Assert.assertArrayEquals(EXPECED_ZIPPED, out.getBuffer()); + Assert.assertArrayEquals(expected, out.getBuffer()); } } diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpEncodingStreamingContentTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpEncodingStreamingContentTest.java index f353004a2..265814722 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpEncodingStreamingContentTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpEncodingStreamingContentTest.java @@ -28,13 +28,27 @@ */ public class HttpEncodingStreamingContentTest extends TestCase { - byte[] EXPECED_ZIPPED = + private static final byte[] EXPECED_ZIPPED = + new byte[] { + 31, -117, 8, 0, 0, 0, 0, 0, 0, -1, -53, -49, -57, 13, 0, -30, -66, -14, 54, 28, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + }; + + // TODO: remove when no longer using Java < 16: https://bugs.openjdk.java.net/browse/JDK-8244706 + @Deprecated + private static final byte[] EXPECED_ZIPPED_BELOW_JAVA_16 = new byte[] { 31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -53, -49, -57, 13, 0, -30, -66, -14, 54, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; public void test() throws IOException { + // TODO: remove when no longer using Java < 16. + byte[] expected = + System.getProperty("java.version").compareTo("16") >= 0 + ? EXPECED_ZIPPED + : EXPECED_ZIPPED_BELOW_JAVA_16; + GZipEncoding encoding = new GZipEncoding(); ByteArrayStreamingContent content = new ByteArrayStreamingContent(StringUtils.getBytesUtf8("oooooooooooooooooooooooooooo")); @@ -43,6 +57,6 @@ public void test() throws IOException { new HttpEncodingStreamingContent(content, encoding); encodingContent.writeTo(out); assertFalse(out.isClosed()); - Assert.assertArrayEquals(EXPECED_ZIPPED, out.getBuffer()); + Assert.assertArrayEquals(expected, out.getBuffer()); } } diff --git a/google-http-client/src/test/java/com/google/api/client/json/JsonObjectParserTest.java b/google-http-client/src/test/java/com/google/api/client/json/JsonObjectParserTest.java index 3192b62ba..d4f030a22 100644 --- a/google-http-client/src/test/java/com/google/api/client/json/JsonObjectParserTest.java +++ b/google-http-client/src/test/java/com/google/api/client/json/JsonObjectParserTest.java @@ -14,17 +14,18 @@ package com.google.api.client.json; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - +import com.google.api.client.testing.json.MockJsonFactory; +import com.google.api.client.testing.json.MockJsonParser; import com.google.common.base.Charsets; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; import java.lang.reflect.Type; import java.nio.charset.Charset; import junit.framework.TestCase; +import org.junit.Test; /** * Tests for the {@link JsonObjectParser} class. @@ -34,6 +35,7 @@ */ public class JsonObjectParserTest extends TestCase { + @Test public void testConstructor_null() { try { new JsonObjectParser((JsonFactory) null); @@ -42,36 +44,49 @@ public void testConstructor_null() { } } + @Test public void testParse_InputStream() throws Exception { - InputStream in = new ByteArrayInputStream(new byte[256]); - Charset utf8 = Charsets.UTF_8; - Type type = Integer[].class; + InputStream in = new ByteArrayInputStream(new byte[0]); Integer[] parsed = new Integer[1]; - JsonParser mockJsonParser = mock(JsonParser.class); - when(mockJsonParser.parse(type, true)).thenReturn(parsed); - - JsonFactory mockJsonFactory = mock(JsonFactory.class); - when(mockJsonFactory.createJsonParser(in, utf8)).thenReturn(mockJsonParser); - // Test the JsonObjectParser - JsonObjectParser jop = new JsonObjectParser(mockJsonFactory); - assertEquals(parsed, jop.parseAndClose(in, utf8, type)); + JsonObjectParser jop = new JsonObjectParser(setUpMockJsonFactory(Integer[].class, parsed)); + assertEquals(parsed, jop.parseAndClose(in, Charsets.UTF_8, Integer[].class)); } + @Test public void testParse_Reader() throws Exception { Reader in = new StringReader("something"); - Type type = Integer[].class; Integer[] parsed = new Integer[1]; - JsonParser mockJsonParser = mock(JsonParser.class); - when(mockJsonParser.parse(type, true)).thenReturn(parsed); + // Test the JsonObjectParser + JsonObjectParser jop = new JsonObjectParser(setUpMockJsonFactory(Integer[].class, parsed)); + assertEquals(parsed, jop.parseAndClose(in, Integer[].class)); + } - JsonFactory mockJsonFactory = mock(JsonFactory.class); - when(mockJsonFactory.createJsonParser(in)).thenReturn(mockJsonParser); + // Mockito.mock() on JsonFactory and JsonParser fails with Java 17, so set them up manually. + private static final JsonFactory setUpMockJsonFactory( + final Class clazz, final T parsedResult) { + final MockJsonParser jsonParser = + new MockJsonParser(null) { + @Override + public Object parse(Type dataType, boolean close) throws IOException { + assertEquals(clazz, dataType); + return parsedResult; + } + }; - // Test the JsonObjectParser - JsonObjectParser jop = new JsonObjectParser(mockJsonFactory); - assertEquals(parsed, jop.parseAndClose(in, type)); + return new MockJsonFactory() { + @Override + public JsonParser createJsonParser(Reader in) throws IOException { + return jsonParser; + } + + @Override + public JsonParser createJsonParser(InputStream in, Charset charset) throws IOException { + assertEquals(Charsets.UTF_8, charset); + return jsonParser; + } + }; } } diff --git a/pom.xml b/pom.xml index 7015186b4..6fc5609c3 100644 --- a/pom.xml +++ b/pom.xml @@ -227,11 +227,6 @@ google-http-client-test ${project.http-client.version} - - org.mockito - mockito-all - 1.10.19 - com.google.j2objc j2objc-annotations diff --git a/synth.metadata b/synth.metadata index 9f73dc50c..53b033004 100644 --- a/synth.metadata +++ b/synth.metadata @@ -4,14 +4,14 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/google-http-java-client.git", - "sha": "c21e7458a2485326b64ef4e5081fa6719c8e5f41" + "sha": "9f389ef89195af77eff8f1e1c1c9ee9bf9c7792c" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "0752ff727a19a467dffed335d5e59303689cf0d1" + "sha": "a4be3384ccb92364795d981f2863f6986fcee620" } } ],