You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/08/24 16:22:04 UTC

[shardingsphere-elasticjob] branch restful-api updated: Improve Serializer Factory (#1404)

This is an automated email from the ASF dual-hosted git repository.

technoboy pushed a commit to branch restful-api
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/restful-api by this push:
     new d23a9f6  Improve Serializer Factory (#1404)
d23a9f6 is described below

commit d23a9f62092e8c2c88f1065a6dd83c2ccbca2d41
Author: 吴伟杰 <ro...@me.com>
AuthorDate: Tue Aug 25 00:21:48 2020 +0800

    Improve Serializer Factory (#1404)
---
 .../RequestBodyDeserializerFactory.java            | 11 +++++++----
 .../RequestBodyDeserializerNotFoundException.java} | 23 +++++++---------------
 .../serializer/ResponseBodySerializerFactory.java  | 11 +++++++----
 .../ResponseBodySerializerNotFoundException.java}  | 23 +++++++---------------
 .../RequestBodyDeserializerFactoryTest.java        |  6 ++----
 .../ResponseBodySerializerFactoryTest.java         |  6 ++----
 6 files changed, 32 insertions(+), 48 deletions(-)

diff --git a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java
index 5971712..63b3b56 100644
--- a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java
+++ b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactory.java
@@ -75,16 +75,19 @@ public final class RequestBodyDeserializerFactory {
      * @return Deserializer
      */
     public static RequestBodyDeserializer getRequestBodyDeserializer(final String contentType) {
-        RequestBodyDeserializer deserializer = REQUEST_BODY_DESERIALIZERS.get(contentType);
-        if (null == deserializer) {
+        RequestBodyDeserializer result = REQUEST_BODY_DESERIALIZERS.get(contentType);
+        if (null == result) {
             synchronized (RequestBodyDeserializerFactory.class) {
                 if (null == REQUEST_BODY_DESERIALIZERS.get(contentType)) {
                     instantiateRequestBodyDeserializerFromFactories(contentType);
                 }
-                deserializer = REQUEST_BODY_DESERIALIZERS.get(contentType);
+                result = REQUEST_BODY_DESERIALIZERS.get(contentType);
             }
         }
-        return deserializer != MISSING_DESERIALIZER ? deserializer : null;
+        if (MISSING_DESERIALIZER == result) {
+            throw new RequestBodyDeserializerNotFoundException(contentType);
+        }
+        return result;
     }
     
     private static void instantiateRequestBodyDeserializerFromFactories(final String contentType) {
diff --git a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerNotFoundException.java
similarity index 56%
copy from elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java
copy to elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerNotFoundException.java
index 6181e10..ce7db53 100644
--- a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java
+++ b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerNotFoundException.java
@@ -17,23 +17,14 @@
 
 package org.apache.shardingsphere.elasticjob.restful.deserializer;
 
-import io.netty.handler.codec.http.HttpHeaderValues;
-import org.junit.Test;
+import java.text.MessageFormat;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-public class RequestBodyDeserializerFactoryTest {
-    
-    @Test
-    public void assertGetJsonDefaultDeserializer() {
-        RequestBodyDeserializer deserializer = RequestBodyDeserializerFactory.getRequestBodyDeserializer(HttpHeaderValues.APPLICATION_JSON.toString());
-        assertNotNull(deserializer);
-    }
+/**
+ * {@link RequestBodyDeserializer} not found for specific MIME type.
+ */
+public final class RequestBodyDeserializerNotFoundException extends RuntimeException {
     
-    @Test
-    public void assertDeserializerNotFound() {
-        RequestBodyDeserializer deserializer = RequestBodyDeserializerFactory.getRequestBodyDeserializer("Unknown");
-        assertNull(deserializer);
+    public RequestBodyDeserializerNotFoundException(final String mimeType) {
+        super(MessageFormat.format("RequestBodySerializer not found for [{0}]", mimeType));
     }
 }
diff --git a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java
index 1377bab..63a2bb4 100644
--- a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java
+++ b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactory.java
@@ -75,16 +75,19 @@ public final class ResponseBodySerializerFactory {
      * @return Serializer
      */
     public static ResponseBodySerializer getResponseBodySerializer(final String contentType) {
-        ResponseBodySerializer serializer = RESPONSE_BODY_SERIALIZERS.get(contentType);
-        if (null == serializer) {
+        ResponseBodySerializer result = RESPONSE_BODY_SERIALIZERS.get(contentType);
+        if (null == result) {
             synchronized (ResponseBodySerializerFactory.class) {
                 if (null == RESPONSE_BODY_SERIALIZERS.get(contentType)) {
                     instantiateResponseBodySerializerFromFactories(contentType);
                 }
-                serializer = RESPONSE_BODY_SERIALIZERS.get(contentType);
+                result = RESPONSE_BODY_SERIALIZERS.get(contentType);
             }
         }
-        return serializer != MISSING_SERIALIZER ? serializer : null;
+        if (MISSING_SERIALIZER == result) {
+            throw new ResponseBodySerializerNotFoundException(contentType);
+        }
+        return result;
     }
     
     private static void instantiateResponseBodySerializerFromFactories(final String contentType) {
diff --git a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerNotFoundException.java
similarity index 56%
copy from elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java
copy to elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerNotFoundException.java
index 552a3d8..1345087 100644
--- a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java
+++ b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerNotFoundException.java
@@ -17,23 +17,14 @@
 
 package org.apache.shardingsphere.elasticjob.restful.serializer;
 
-import io.netty.handler.codec.http.HttpHeaderValues;
-import org.junit.Test;
+import java.text.MessageFormat;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-public class ResponseBodySerializerFactoryTest {
-    
-    @Test
-    public void assertGetJsonDefaultSerializer() {
-        ResponseBodySerializer serializer = ResponseBodySerializerFactory.getResponseBodySerializer(HttpHeaderValues.APPLICATION_JSON.toString());
-        assertNotNull(serializer);
-    }
+/**
+ * {@link ResponseBodySerializer} not found for specific MIME type.
+ */
+public final class ResponseBodySerializerNotFoundException extends RuntimeException {
     
-    @Test
-    public void assertSerializerNotFound() {
-        ResponseBodySerializer serializer = ResponseBodySerializerFactory.getResponseBodySerializer("Unknown");
-        assertNull(serializer);
+    public ResponseBodySerializerNotFoundException(final String mimeType) {
+        super(MessageFormat.format("ResponseBodySerializer not found for [{0}]", mimeType));
     }
 }
diff --git a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java
index 6181e10..0584089 100644
--- a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java
+++ b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/deserializer/RequestBodyDeserializerFactoryTest.java
@@ -21,7 +21,6 @@ import io.netty.handler.codec.http.HttpHeaderValues;
 import org.junit.Test;
 
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 
 public class RequestBodyDeserializerFactoryTest {
     
@@ -31,9 +30,8 @@ public class RequestBodyDeserializerFactoryTest {
         assertNotNull(deserializer);
     }
     
-    @Test
+    @Test(expected = RequestBodyDeserializerNotFoundException.class)
     public void assertDeserializerNotFound() {
-        RequestBodyDeserializer deserializer = RequestBodyDeserializerFactory.getRequestBodyDeserializer("Unknown");
-        assertNull(deserializer);
+        RequestBodyDeserializerFactory.getRequestBodyDeserializer("Unknown");
     }
 }
diff --git a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java
index 552a3d8..7a45850 100644
--- a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java
+++ b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/serializer/ResponseBodySerializerFactoryTest.java
@@ -21,7 +21,6 @@ import io.netty.handler.codec.http.HttpHeaderValues;
 import org.junit.Test;
 
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 
 public class ResponseBodySerializerFactoryTest {
     
@@ -31,9 +30,8 @@ public class ResponseBodySerializerFactoryTest {
         assertNotNull(serializer);
     }
     
-    @Test
+    @Test(expected = ResponseBodySerializerNotFoundException.class)
     public void assertSerializerNotFound() {
-        ResponseBodySerializer serializer = ResponseBodySerializerFactory.getResponseBodySerializer("Unknown");
-        assertNull(serializer);
+        ResponseBodySerializerFactory.getResponseBodySerializer("Unknown");
     }
 }