From ed71f06a9f8cdf86beb9700d3f3c5caae79de473 Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Sat, 20 Jan 2024 23:28:51 +0900 Subject: [PATCH 1/2] Add CI --- .github/workflows/ci.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..9cbdb11 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + java_version: [11, 17] + steps: + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.java_version }} + uses: actions/setup-java@v3 + with: + distribution: temurin + java-version: ${{ matrix.java_version }} + - name: Setup and execute Gradle 'check' task + uses: gradle/gradle-build-action@v2 + with: + arguments: check + - name: Upload Gradle test reports + uses: actions/upload-artifact@v3 + with: + name: gradle_test_reports + path: build/reports/tests/test + From 61a7c8b6f8642d5063593d301640ad3d50d34cca Mon Sep 17 00:00:00 2001 From: Mitsunori Komatsu Date: Sat, 20 Jan 2024 23:31:33 +0900 Subject: [PATCH 2/2] Apply :spotlessApply --- .../msgpack/MessagePackConverterFactory.java | 80 ++++---- .../MessagePackRequestBodyConverter.java | 31 ++- .../MessagePackResponseBodyConverter.java | 29 ++- .../MessagePackConverterFactoryTest.java | 186 ++++++++---------- 4 files changed, 148 insertions(+), 178 deletions(-) diff --git a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactory.java b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactory.java index fc734a3..926e1f4 100644 --- a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactory.java +++ b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactory.java @@ -12,50 +12,46 @@ import retrofit2.Converter; import retrofit2.Retrofit; -public class MessagePackConverterFactory - extends Converter.Factory -{ - public static MessagePackConverterFactory create() - { - return create(new ObjectMapper(new MessagePackFactory())); +public class MessagePackConverterFactory extends Converter.Factory { + public static MessagePackConverterFactory create() { + return create(new ObjectMapper(new MessagePackFactory())); + } + + @SuppressWarnings("ConstantConditions") // Guarding public API nullability. + public static MessagePackConverterFactory create(ObjectMapper mapper) { + if (mapper == null) { + throw new IllegalArgumentException("'mapper' is null"); } - @SuppressWarnings("ConstantConditions") // Guarding public API nullability. - public static MessagePackConverterFactory create(ObjectMapper mapper) - { - if (mapper == null) { - throw new IllegalArgumentException("'mapper' is null"); - } - - if (!(mapper.getFactory() instanceof MessagePackFactory)) { - throw new IllegalArgumentException("'mapper' doesn't have MessagePackFactory"); - } - - return new MessagePackConverterFactory(mapper); - } - - private final ObjectMapper mapper; - - private MessagePackConverterFactory(ObjectMapper mapper) - { - this.mapper = mapper; - } - - @Override - public Converter requestBodyConverter(Type type, - Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) - { - JavaType javaType = mapper.getTypeFactory().constructType(type); - ObjectWriter writer = mapper.writerFor(javaType); - return new MessagePackRequestBodyConverter<>(writer); + if (!(mapper.getFactory() instanceof MessagePackFactory)) { + throw new IllegalArgumentException("'mapper' doesn't have MessagePackFactory"); } - @Override - public Converter responseBodyConverter(Type type, - Annotation[] annotations, Retrofit retrofit) - { - JavaType javaType = mapper.getTypeFactory().constructType(type); - ObjectReader reader = mapper.readerFor(javaType); - return new MessagePackResponseBodyConverter<>(reader); - } + return new MessagePackConverterFactory(mapper); + } + + private final ObjectMapper mapper; + + private MessagePackConverterFactory(ObjectMapper mapper) { + this.mapper = mapper; + } + + @Override + public Converter requestBodyConverter( + Type type, + Annotation[] parameterAnnotations, + Annotation[] methodAnnotations, + Retrofit retrofit) { + JavaType javaType = mapper.getTypeFactory().constructType(type); + ObjectWriter writer = mapper.writerFor(javaType); + return new MessagePackRequestBodyConverter<>(writer); + } + + @Override + public Converter responseBodyConverter( + Type type, Annotation[] annotations, Retrofit retrofit) { + JavaType javaType = mapper.getTypeFactory().constructType(type); + ObjectReader reader = mapper.readerFor(javaType); + return new MessagePackResponseBodyConverter<>(reader); + } } diff --git a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackRequestBodyConverter.java b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackRequestBodyConverter.java index 00a42b0..d7bbbd8 100644 --- a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackRequestBodyConverter.java +++ b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackRequestBodyConverter.java @@ -1,29 +1,24 @@ package org.komamitsu.retrofit.converter.msgpack; import com.fasterxml.jackson.databind.ObjectWriter; +import java.io.IOException; import okhttp3.MediaType; import okhttp3.RequestBody; import retrofit2.Converter; -import java.io.IOException; - -class MessagePackRequestBodyConverter - implements Converter -{ - private static final MediaType MEDIA_TYPE = MediaType.parse("application/x-msgpack; charset=UTF-8"); +class MessagePackRequestBodyConverter implements Converter { + private static final MediaType MEDIA_TYPE = + MediaType.parse("application/x-msgpack; charset=UTF-8"); - private final ObjectWriter adapter; + private final ObjectWriter adapter; - MessagePackRequestBodyConverter(ObjectWriter adapter) - { - this.adapter = adapter; - } + MessagePackRequestBodyConverter(ObjectWriter adapter) { + this.adapter = adapter; + } - @Override - public RequestBody convert(T value) - throws IOException - { - byte[] bytes = adapter.writeValueAsBytes(value); - return RequestBody.create(MEDIA_TYPE, bytes); - } + @Override + public RequestBody convert(T value) throws IOException { + byte[] bytes = adapter.writeValueAsBytes(value); + return RequestBody.create(MEDIA_TYPE, bytes); + } } diff --git a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackResponseBodyConverter.java b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackResponseBodyConverter.java index ea05367..9747632 100644 --- a/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackResponseBodyConverter.java +++ b/src/main/java/org/komamitsu/retrofit/converter/msgpack/MessagePackResponseBodyConverter.java @@ -1,27 +1,22 @@ package org.komamitsu.retrofit.converter.msgpack; import com.fasterxml.jackson.databind.ObjectReader; -import okhttp3.ResponseBody; -import retrofit2.Converter; - import java.io.IOException; import java.io.InputStream; +import okhttp3.ResponseBody; +import retrofit2.Converter; -class MessagePackResponseBodyConverter - implements Converter -{ - private final ObjectReader adapter; +class MessagePackResponseBodyConverter implements Converter { + private final ObjectReader adapter; - MessagePackResponseBodyConverter(ObjectReader adapter) - { - this.adapter = adapter; - } + MessagePackResponseBodyConverter(ObjectReader adapter) { + this.adapter = adapter; + } - @Override - public T convert(ResponseBody value) throws IOException - { - try (InputStream in = value.byteStream()) { - return adapter.readValue(in); - } + @Override + public T convert(ResponseBody value) throws IOException { + try (InputStream in = value.byteStream()) { + return adapter.readValue(in); } + } } diff --git a/src/test/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactoryTest.java b/src/test/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactoryTest.java index 7932e26..1d52c68 100644 --- a/src/test/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactoryTest.java +++ b/src/test/java/org/komamitsu/retrofit/converter/msgpack/MessagePackConverterFactoryTest.java @@ -1,9 +1,11 @@ package org.komamitsu.retrofit.converter.msgpack; -import com.fasterxml.jackson.annotation.JsonCreator; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; + import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; @@ -18,114 +20,96 @@ import retrofit2.http.Body; import retrofit2.http.POST; -import java.io.IOException; +public class MessagePackConverterFactoryTest { + @Rule public final MockWebServer server = new MockWebServer(); + private Service service; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; + public static class Pojo { + private final int i; + private final float f; + private final String s; -public class MessagePackConverterFactoryTest -{ - @Rule - public final MockWebServer server = new MockWebServer(); - private Service service; - - public static class Pojo - { - private final int i; - private final float f; - private final String s; - - public Pojo( - @JsonProperty("i") int i, - @JsonProperty("f") float f, - @JsonProperty("s") String s) - { - this.i = i; - this.f = f; - this.s = s; - } - - @JsonProperty("i") - public int getI() - { - return i; - } - - @JsonProperty("f") - public float getF() - { - return f; - } - - @JsonProperty("s") - public String getS() - { - return s; - } - - @Override - public boolean equals(Object o) - { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Pojo pojo = (Pojo) o; - - if (i != pojo.i) { - return false; - } - if (Float.compare(pojo.f, f) != 0) { - return false; - } - return s != null ? s.equals(pojo.s) : pojo.s == null; - } - - @Override - public int hashCode() - { - int result = i; - result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0); - result = 31 * result + (s != null ? s.hashCode() : 0); - return result; - } + public Pojo(@JsonProperty("i") int i, @JsonProperty("f") float f, @JsonProperty("s") String s) { + this.i = i; + this.f = f; + this.s = s; } - interface Service - { - @POST("/") - Call postPojo(@Body Pojo pojo); + @JsonProperty("i") + public int getI() { + return i; } - @Before - public void setUp() - { - Retrofit retrofit = new Retrofit.Builder() - .baseUrl(server.url("/")) - .addConverterFactory(MessagePackConverterFactory.create()) - .build(); - service = retrofit.create(Service.class); + @JsonProperty("f") + public float getF() { + return f; } - @Test - public void requestBodyConverter() - throws IOException, InterruptedException - { - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); - - Pojo requestPojo = new Pojo(42, (float) Math.PI, "Hello"); - Pojo responsePojo = new Pojo(99, 1.23f, "World"); - server.enqueue(new MockResponse().setBody( - new Buffer().write(objectMapper.writeValueAsBytes(responsePojo)))); + @JsonProperty("s") + public String getS() { + return s; + } - Response response = service.postPojo(requestPojo).execute(); - assertThat(response.body(), is(responsePojo)); + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Pojo pojo = (Pojo) o; + + if (i != pojo.i) { + return false; + } + if (Float.compare(pojo.f, f) != 0) { + return false; + } + return s != null ? s.equals(pojo.s) : pojo.s == null; + } - RecordedRequest recordedRequest = server.takeRequest(); - Pojo recordedPojo = objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class); - assertThat(recordedPojo, is(requestPojo)); + @Override + public int hashCode() { + int result = i; + result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0); + result = 31 * result + (s != null ? s.hashCode() : 0); + return result; } + } + + interface Service { + @POST("/") + Call postPojo(@Body Pojo pojo); + } + + @Before + public void setUp() { + Retrofit retrofit = + new Retrofit.Builder() + .baseUrl(server.url("/")) + .addConverterFactory(MessagePackConverterFactory.create()) + .build(); + service = retrofit.create(Service.class); + } + + @Test + public void requestBodyConverter() throws IOException, InterruptedException { + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); + + Pojo requestPojo = new Pojo(42, (float) Math.PI, "Hello"); + Pojo responsePojo = new Pojo(99, 1.23f, "World"); + server.enqueue( + new MockResponse() + .setBody(new Buffer().write(objectMapper.writeValueAsBytes(responsePojo)))); + + Response response = service.postPojo(requestPojo).execute(); + assertThat(response.body(), is(responsePojo)); + + RecordedRequest recordedRequest = server.takeRequest(); + Pojo recordedPojo = + objectMapper.readValue(recordedRequest.getBody().readByteArray(), Pojo.class); + assertThat(recordedPojo, is(requestPojo)); + } }