From 747238a479698e653251f18a26ae619e0d252a94 Mon Sep 17 00:00:00 2001 From: Andrea Paternesi Date: Thu, 22 Nov 2018 12:51:04 +0100 Subject: [PATCH 01/11] Added Gson serialization/deserialization exception --- extensions/gson/pom.xml | 48 +++++++++ .../io/jsonwebtoken/io/GsonDeserializer.java | 42 ++++++++ .../io/jsonwebtoken/io/GsonSerializer.java | 43 ++++++++ .../io/GsonDeserializerTest.groovy | 64 ++++++++++++ .../jsonwebtoken/io/GsonSerializerTest.groovy | 99 +++++++++++++++++++ extensions/pom.xml | 1 + 6 files changed, 297 insertions(+) create mode 100644 extensions/gson/pom.xml create mode 100644 extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java create mode 100644 extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java create mode 100644 extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy create mode 100644 extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy diff --git a/extensions/gson/pom.xml b/extensions/gson/pom.xml new file mode 100644 index 00000000..fd8ba0b3 --- /dev/null +++ b/extensions/gson/pom.xml @@ -0,0 +1,48 @@ + + + + + 4.0.0 + + + io.jsonwebtoken + jjwt-root + 0.11.0-SNAPSHOT + ../../pom.xml + + + jjwt-gson + JJWT :: Extensions :: Gson + jar + + + ${basedir}/../.. + + + + + io.jsonwebtoken + jjwt-api + + + com.google.code.gson + gson + 2.8.5 + + + + \ No newline at end of file diff --git a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java b/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java new file mode 100644 index 00000000..b7333846 --- /dev/null +++ b/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java @@ -0,0 +1,42 @@ +package io.jsonwebtoken.io; + +import com.google.gson.Gson; +import io.jsonwebtoken.lang.Assert; +import java.io.IOException; + +public class GsonDeserializer implements Deserializer { + + private final Class returnType; + private final Gson gson; + + @SuppressWarnings("unused") //used via reflection by RuntimeClasspathDeserializerLocator + public GsonDeserializer() { + this(GsonSerializer.DEFAULT_GSON); + } + + @SuppressWarnings({"unchecked", "WeakerAccess", "unused"}) // for end-users providing a custom gson + public GsonDeserializer(Gson gson) { + this(gson, (Class) Object.class); + } + + private GsonDeserializer(Gson gson, Class returnType) { + Assert.notNull(gson, "gson cannot be null."); + Assert.notNull(returnType, "Return type cannot be null."); + this.gson = gson; + this.returnType = returnType; + } + + @Override + public T deserialize(byte[] bytes) throws DeserializationException { + try { + return readValue(bytes); + } catch (IOException e) { + String msg = "Unable to deserialize bytes into a " + returnType.getName() + " instance: " + e.getMessage(); + throw new DeserializationException(msg, e); + } + } + + protected T readValue(byte[] bytes) throws IOException { + return gson.fromJson(new String(bytes), returnType); + } +} diff --git a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java b/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java new file mode 100644 index 00000000..0c8dac72 --- /dev/null +++ b/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java @@ -0,0 +1,43 @@ +package io.jsonwebtoken.io; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import io.jsonwebtoken.lang.Assert; +import io.jsonwebtoken.lang.Strings; + +public class GsonSerializer implements Serializer { + + static final Gson DEFAULT_GSON = new GsonBuilder().disableHtmlEscaping().create(); + private Gson gson; + + @SuppressWarnings("unused") //used via reflection by RuntimeClasspathDeserializerLocator + public GsonSerializer() { + this(DEFAULT_GSON); + } + + @SuppressWarnings("WeakerAccess") //intended for end-users to use when providing a custom gson + public GsonSerializer(Gson gson) { + Assert.notNull(gson, "gson cannot be null."); + this.gson = gson; + } + + @Override + public byte[] serialize(T t) throws SerializationException { + Assert.notNull(t, "Object to serialize cannot be null."); + //Gson never throws any serialization exception + return writeValueAsBytes(t); + } + + @SuppressWarnings("WeakerAccess") //for testing + protected byte[] writeValueAsBytes(T t) { + Object o; + if (t instanceof byte[]) { + o = Encoders.BASE64.encode((byte[]) t); + } else if (t instanceof char[]) { + o = new String((char[]) t); + } else { + o = t; + } + return this.gson.toJson(o).getBytes(Strings.UTF_8); + } +} diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy new file mode 100644 index 00000000..6827f3ac --- /dev/null +++ b/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy @@ -0,0 +1,64 @@ +package io.jsonwebtoken.io + +import com.google.gson.Gson +import io.jsonwebtoken.lang.Strings +import org.junit.Test + +import static org.easymock.EasyMock.* +import static org.junit.Assert.* + +class GsonDeserializerTest { + + @Test + void testDefaultConstructor() { + def deserializer = new GsonDeserializer() + assertNotNull deserializer.gson + } + + @Test + void testObjectMapperConstructor() { + def customGSON = new Gson() + def deserializer = new GsonDeserializer(customGSON) + assertSame customGSON, deserializer.gson + } + + @Test(expected = IllegalArgumentException) + void testObjectMapperConstructorWithNullArgument() { + new GsonDeserializer<>(null) + } + + @Test + void testDeserialize() { + byte[] serialized = '{"hello":"世界"}'.getBytes(Strings.UTF_8) + def expected = [hello: '世界'] + def result = new GsonDeserializer().deserialize(serialized) + assertEquals expected, result + } + + @Test + void testDeserializeFailsWithJsonProcessingException() { + + def ex = createMock(java.io.IOException) + + expect(ex.getMessage()).andReturn('foo') + + def deserializer = new GsonDeserializer() { + @Override + protected Object readValue(byte[] bytes) throws java.io.IOException { + throw ex + } + } + + replay ex + + try { + deserializer.deserialize('{"hello":"世界"}'.getBytes(Strings.UTF_8)) + fail() + } catch (DeserializationException se) { + assertEquals 'Unable to deserialize bytes into a java.lang.Object instance: foo', se.getMessage() + assertSame ex, se.getCause() + } + + verify ex + } +} diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy new file mode 100644 index 00000000..a8cf6e1a --- /dev/null +++ b/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy @@ -0,0 +1,99 @@ +package io.jsonwebtoken.io + +import io.jsonwebtoken.lang.Strings +import org.junit.Test + +import static org.easymock.EasyMock.* +import static org.junit.Assert.* +import com.google.gson.Gson + +class GsonSerializerTest { + + @Test + void testDefaultConstructor() { + def serializer = new GsonSerializer() + assertNotNull serializer.gson + } + + @Test + void testObjectMapperConstructor() { + def customGSON = new Gson() + def serializer = new GsonSerializer<>(customGSON) + assertSame customGSON, serializer.gson + } + + @Test(expected = IllegalArgumentException) + void testObjectMapperConstructorWithNullArgument() { + new GsonSerializer<>(null) + } + + @Test + void testByte() { + byte[] expected = "120".getBytes(Strings.UTF_8) //ascii("x") = 120 + byte[] bytes = "x".getBytes(Strings.UTF_8) + byte[] result = new GsonSerializer().serialize(bytes[0]) //single byte + assertTrue Arrays.equals(expected, result) + } + + @Test + void testByteArray() { //expect Base64 string by default: + byte[] bytes = "hi".getBytes(Strings.UTF_8) + String expected = '"aGk="' as String //base64(hi) --> aGk= + byte[] result = new GsonSerializer().serialize(bytes) + assertEquals expected, new String(result, Strings.UTF_8) + } + + @Test + void testEmptyByteArray() { //expect Base64 string by default: + byte[] bytes = new byte[0] + byte[] result = new GsonSerializer().serialize(bytes) + assertEquals '""', new String(result, Strings.UTF_8) + } + + @Test + void testChar() { //expect Base64 string by default: + byte[] result = new GsonSerializer().serialize('h' as char) + assertEquals "\"h\"", new String(result, Strings.UTF_8) + } + + @Test + void testCharArray() { //expect Base64 string by default: + byte[] result = new GsonSerializer().serialize("hi".toCharArray()) + assertEquals "\"hi\"", new String(result, Strings.UTF_8) + } + + @Test + void testSerialize() { + byte[] expected = '{"hello":"世界"}'.getBytes(Strings.UTF_8) + byte[] result = new GsonSerializer().serialize([hello: '世界']) + assertTrue Arrays.equals(expected, result) + } + +//THIS IS COMMENTED OUT BECAUSE GSN NEVER THROWS ANY SERIALIZATION EXCEPTION +// @Test +// void testSerializeFailsWithJsonProcessingException() { +// +// def ex = createMock(JsonProcessingException) +// +// expect(ex.getMessage()).andReturn('foo') +// +// def serializer = new GsonSerializer() { +// @Override +// protected byte[] writeValueAsBytes(Object o) throws JsonProcessingException { +// throw ex +// } +// } +// +// replay ex +// +// try { +// serializer.serialize([hello: 'world']) +// fail() +// } catch (SerializationException se) { +// assertEquals 'Unable to serialize object: foo', se.getMessage() +// assertSame ex, se.getCause() +// } +// +// verify ex +// } +} diff --git a/extensions/pom.xml b/extensions/pom.xml index e96f58f8..4d29b9eb 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -36,6 +36,7 @@ jackson orgjson + gson \ No newline at end of file From 9bf487f0b7686bec97ac368f592b7cddfa99546f Mon Sep 17 00:00:00 2001 From: patton73 Date: Fri, 21 Jun 2019 19:55:05 +0200 Subject: [PATCH 02/11] Updated RuntimeClasspath Serializer/Deserializer --- .../impl/io/RuntimeClasspathDeserializerLocator.java | 2 ++ .../jsonwebtoken/impl/io/RuntimeClasspathSerializerLocator.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java index 61d2dac7..7205aabc 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java @@ -34,6 +34,8 @@ public class RuntimeClasspathDeserializerLocator implements InstanceLocator Date: Tue, 16 Jul 2019 10:02:44 +0200 Subject: [PATCH 03/11] changed pom to match master version 0.10.8 --- extensions/gson/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/gson/pom.xml b/extensions/gson/pom.xml index fd8ba0b3..18bae59a 100644 --- a/extensions/gson/pom.xml +++ b/extensions/gson/pom.xml @@ -21,7 +21,7 @@ io.jsonwebtoken jjwt-root - 0.11.0-SNAPSHOT + 0.10.8-SNAPSHOT ../../pom.xml From 134f25b536c04df4ca416c34bf9a0ce2468fa724 Mon Sep 17 00:00:00 2001 From: Andrea Paternesi Date: Tue, 16 Jul 2019 16:19:13 +0200 Subject: [PATCH 04/11] Fix locator paths and added tests. --- .../RuntimeClasspathDeserializerLocator.java | 2 +- .../io/RuntimeClasspathSerializerLocator.java | 2 +- ...imeClasspathDeserializerLocatorTest.groovy | 20 +++++++++++++++++++ ...ntimeClasspathSerializerLocatorTest.groovy | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java index 9dc41412..18532ddb 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java @@ -34,7 +34,7 @@ public class RuntimeClasspathDeserializerLocator implements InstanceLocator Date: Tue, 16 Jul 2019 16:19:23 +0200 Subject: [PATCH 05/11] Fix locator paths and added tests. --- extensions/gson/pom.xml | 1 - pom.xml | 13 ++++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/extensions/gson/pom.xml b/extensions/gson/pom.xml index 18bae59a..2e0e09b3 100644 --- a/extensions/gson/pom.xml +++ b/extensions/gson/pom.xml @@ -41,7 +41,6 @@ com.google.code.gson gson - 2.8.5 diff --git a/pom.xml b/pom.xml index a8a18fa6..e72bcb6e 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ false - + bintray-jwtk-coveralls-maven-plugin bintray-plugins https://dl.bintray.com/jwtk/coveralls-maven-plugin @@ -90,6 +90,7 @@ 2.9.9.1 20180130 + 2.8.5 1.60 @@ -131,6 +132,11 @@ jjwt-orgjson ${project.version} + + io.jsonwebtoken + jjwt-gson + ${project.version} + com.fasterxml.jackson.core jackson-databind @@ -141,6 +147,11 @@ json ${orgjson.version} + + com.google.code.gson + gson + ${gson.version} + From 222032a20845dc8c98f3f8c83906e19a45d324a6 Mon Sep 17 00:00:00 2001 From: Andrea Paternesi Date: Tue, 16 Jul 2019 16:36:12 +0200 Subject: [PATCH 06/11] Final fix. Typo fixes. And test coverage. --- impl/pom.xml | 5 +++++ .../impl/io/RuntimeClasspathDeserializerLocator.java | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/impl/pom.xml b/impl/pom.xml index 951b1f07..7309ff46 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -54,6 +54,11 @@ jjwt-orgjson test + + io.jsonwebtoken + jjwt-gson + ${project.version} + \ No newline at end of file diff --git a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java index 18532ddb..9bd462b9 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java @@ -34,8 +34,8 @@ public class RuntimeClasspathDeserializerLocator implements InstanceLocator Date: Tue, 16 Jul 2019 16:41:58 +0200 Subject: [PATCH 07/11] Fix to pom file. --- impl/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/impl/pom.xml b/impl/pom.xml index 7309ff46..6b19e702 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -57,7 +57,7 @@ io.jsonwebtoken jjwt-gson - ${project.version} + test From bb1fb76ce96aed088392d1c250ceda1417235715 Mon Sep 17 00:00:00 2001 From: Andrea Paternesi Date: Tue, 16 Jul 2019 17:00:20 +0200 Subject: [PATCH 08/11] last fix to the pom? --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e72bcb6e..42c44419 100644 --- a/pom.xml +++ b/pom.xml @@ -69,7 +69,7 @@ false - + bintray-jwtk-coveralls-maven-plugin bintray-plugins https://dl.bintray.com/jwtk/coveralls-maven-plugin From 2bb8e4d02e54fc3c16deb9792c4c012e8cd18df3 Mon Sep 17 00:00:00 2001 From: patton73 Date: Wed, 17 Jul 2019 22:46:18 +0200 Subject: [PATCH 09/11] Fixes for reviews. Hope i did not forget something. --- .../{ => gson}/io/GsonDeserializer.java | 4 +++- .../jsonwebtoken/{ => gson}/io/GsonSerializer.java | 13 ++++++++++--- .../{ => gson}/io/GsonDeserializerTest.groovy | 0 .../{ => gson}/io/GsonSerializerTest.groovy | 0 extensions/pom.xml | 2 +- impl/pom.xml | 6 +++--- .../io/RuntimeClasspathDeserializerLocator.java | 4 ++-- .../impl/io/RuntimeClasspathSerializerLocator.java | 4 ++-- pom.xml | 6 +++--- 9 files changed, 24 insertions(+), 15 deletions(-) rename extensions/gson/src/main/java/io/jsonwebtoken/{ => gson}/io/GsonDeserializer.java (91%) rename extensions/gson/src/main/java/io/jsonwebtoken/{ => gson}/io/GsonSerializer.java (76%) rename extensions/gson/src/test/groovy/io/jsonwebtoken/{ => gson}/io/GsonDeserializerTest.groovy (100%) rename extensions/gson/src/test/groovy/io/jsonwebtoken/{ => gson}/io/GsonSerializerTest.groovy (100%) diff --git a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java b/extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonDeserializer.java similarity index 91% rename from extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java rename to extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonDeserializer.java index b7333846..b3dfc843 100644 --- a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonDeserializer.java +++ b/extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonDeserializer.java @@ -1,6 +1,8 @@ -package io.jsonwebtoken.io; +package io.jsonwebtoken.gson.io; import com.google.gson.Gson; +import io.jsonwebtoken.io.DeserializationException; +import io.jsonwebtoken.io.Deserializer; import io.jsonwebtoken.lang.Assert; import java.io.IOException; diff --git a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java b/extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonSerializer.java similarity index 76% rename from extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java rename to extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonSerializer.java index 0c8dac72..f3050690 100644 --- a/extensions/gson/src/main/java/io/jsonwebtoken/io/GsonSerializer.java +++ b/extensions/gson/src/main/java/io/jsonwebtoken/gson/io/GsonSerializer.java @@ -1,7 +1,10 @@ -package io.jsonwebtoken.io; +package io.jsonwebtoken.gson.io; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import io.jsonwebtoken.io.Encoders; +import io.jsonwebtoken.io.SerializationException; +import io.jsonwebtoken.io.Serializer; import io.jsonwebtoken.lang.Assert; import io.jsonwebtoken.lang.Strings; @@ -24,8 +27,12 @@ public class GsonSerializer implements Serializer { @Override public byte[] serialize(T t) throws SerializationException { Assert.notNull(t, "Object to serialize cannot be null."); - //Gson never throws any serialization exception - return writeValueAsBytes(t); + try { + return writeValueAsBytes(t); + } catch (Exception e) { + String msg = "Unable to serialize object: " + e.getMessage(); + throw new SerializationException(msg, e); + } } @SuppressWarnings("WeakerAccess") //for testing diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy similarity index 100% rename from extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonDeserializerTest.groovy rename to extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy similarity index 100% rename from extensions/gson/src/test/groovy/io/jsonwebtoken/io/GsonSerializerTest.groovy rename to extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy diff --git a/extensions/pom.xml b/extensions/pom.xml index e043adf6..0cf57558 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -36,7 +36,7 @@ jackson orgjson - gson + gson \ No newline at end of file diff --git a/impl/pom.xml b/impl/pom.xml index 6b19e702..2fb4bbe4 100644 --- a/impl/pom.xml +++ b/impl/pom.xml @@ -54,11 +54,11 @@ jjwt-orgjson test - + io.jsonwebtoken jjwt-gson - test - + test + \ No newline at end of file diff --git a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java index 9bd462b9..022bb289 100644 --- a/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java +++ b/impl/src/main/java/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocator.java @@ -34,8 +34,8 @@ public class RuntimeClasspathDeserializerLocator implements InstanceLocator2.9.9.1 20180130 - 2.8.5 + 2.8.5 1.60 @@ -132,7 +132,7 @@ jjwt-orgjson ${project.version} - + io.jsonwebtoken jjwt-gson ${project.version} @@ -147,7 +147,7 @@ json ${orgjson.version} - + com.google.code.gson gson ${gson.version} From 73f7f9915baccaa3f3143e549647f818606d49ce Mon Sep 17 00:00:00 2001 From: patton73 Date: Wed, 17 Jul 2019 22:51:18 +0200 Subject: [PATCH 10/11] Fixes for reviews. Hope i did not forget something. --- .../groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy | 3 ++- .../groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy | 2 +- .../impl/io/RuntimeClasspathDeserializerLocatorTest.groovy | 2 +- .../impl/io/RuntimeClasspathSerializerLocatorTest.groovy | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy index 6827f3ac..40c5caf8 100644 --- a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy +++ b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonDeserializerTest.groovy @@ -1,6 +1,7 @@ -package io.jsonwebtoken.io +package io.jsonwebtoken.gson.io import com.google.gson.Gson +import io.jsonwebtoken.io.DeserializationException import io.jsonwebtoken.lang.Strings import org.junit.Test diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy index a8cf6e1a..e73ba977 100644 --- a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy +++ b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy @@ -1,4 +1,4 @@ -package io.jsonwebtoken.io +package io.jsonwebtoken.gson.io import io.jsonwebtoken.lang.Strings import org.junit.Test diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocatorTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocatorTest.groovy index 109f4c19..986a5edb 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocatorTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathDeserializerLocatorTest.groovy @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import io.jsonwebtoken.io.Deserializer import io.jsonwebtoken.io.JacksonDeserializer import io.jsonwebtoken.io.OrgJsonDeserializer -import io.jsonwebtoken.io.GsonDeserializer +import io.jsonwebtoken.gson.io.GsonDeserializer import org.junit.After import org.junit.Before import org.junit.Test diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathSerializerLocatorTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathSerializerLocatorTest.groovy index 6005342a..b2faa4b4 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathSerializerLocatorTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/io/RuntimeClasspathSerializerLocatorTest.groovy @@ -3,7 +3,7 @@ package io.jsonwebtoken.impl.io import io.jsonwebtoken.io.Serializer import io.jsonwebtoken.io.JacksonSerializer import io.jsonwebtoken.io.OrgJsonSerializer -import io.jsonwebtoken.io.GsonSerializer +import io.jsonwebtoken.gson.io.GsonSerializer import org.junit.After import org.junit.Before import org.junit.Test From 9cc7ecbeef55e6cd8310fdee58598c7f02067e5d Mon Sep 17 00:00:00 2001 From: patton73 Date: Wed, 17 Jul 2019 23:36:21 +0200 Subject: [PATCH 11/11] Fix for missing test. --- .../gson/io/GsonSerializerTest.groovy | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy index e73ba977..b65d68a9 100644 --- a/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy +++ b/extensions/gson/src/test/groovy/io/jsonwebtoken/gson/io/GsonSerializerTest.groovy @@ -6,6 +6,7 @@ import org.junit.Test import static org.easymock.EasyMock.* import static org.junit.Assert.* import com.google.gson.Gson +import io.jsonwebtoken.io.SerializationException class GsonSerializerTest { @@ -69,31 +70,31 @@ class GsonSerializerTest { assertTrue Arrays.equals(expected, result) } -//THIS IS COMMENTED OUT BECAUSE GSN NEVER THROWS ANY SERIALIZATION EXCEPTION -// @Test -// void testSerializeFailsWithJsonProcessingException() { -// -// def ex = createMock(JsonProcessingException) -// -// expect(ex.getMessage()).andReturn('foo') -// -// def serializer = new GsonSerializer() { -// @Override -// protected byte[] writeValueAsBytes(Object o) throws JsonProcessingException { -// throw ex -// } -// } -// -// replay ex -// -// try { -// serializer.serialize([hello: 'world']) -// fail() -// } catch (SerializationException se) { -// assertEquals 'Unable to serialize object: foo', se.getMessage() -// assertSame ex, se.getCause() -// } -// -// verify ex -// } + + @Test + void testSerializeFailsWithJsonProcessingException() { + + def ex = createMock(SerializationException) + + expect(ex.getMessage()).andReturn('foo') + + def serializer = new GsonSerializer() { + @Override + protected byte[] writeValueAsBytes(Object o) throws SerializationException { + throw ex + } + } + + replay ex + + try { + serializer.serialize([hello: 'world']) + fail() + } catch (SerializationException se) { + assertEquals 'Unable to serialize object: foo', se.getMessage() + assertSame ex, se.getCause() + } + + verify ex + } }