|
| 1 | +package io.jsonwebtoken.jsonb.io |
| 2 | + |
| 3 | +import io.jsonwebtoken.io.SerializationException |
| 4 | +import io.jsonwebtoken.io.Serializer |
| 5 | +import io.jsonwebtoken.lang.Strings |
| 6 | +import org.junit.Test |
| 7 | + |
| 8 | +import javax.json.bind.JsonbBuilder |
| 9 | +import javax.json.bind.JsonbException |
| 10 | + |
| 11 | +import static org.easymock.EasyMock.* |
| 12 | +import static org.hamcrest.CoreMatchers.instanceOf |
| 13 | +import static org.hamcrest.MatcherAssert.assertThat |
| 14 | +import static org.junit.Assert.* |
| 15 | + |
| 16 | +class JsonbSerializerTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void loadService() { |
| 20 | + def serializer = ServiceLoader.load(Serializer).iterator().next() |
| 21 | + assertThat(serializer, instanceOf(JsonbSerializer)) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void testDefaultConstructor() { |
| 26 | + def serializer = new JsonbSerializer() |
| 27 | + assertNotNull serializer.jsonb |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testObjectMapperConstructor() { |
| 32 | + def customJsonb = JsonbBuilder.create() |
| 33 | + def serializer = new JsonbSerializer<>(customJsonb) |
| 34 | + assertSame customJsonb, serializer.jsonb |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = NullPointerException) |
| 38 | + void testObjectMapperConstructorWithNullArgument() { |
| 39 | + new JsonbSerializer<>(null) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testByte() { |
| 44 | + byte[] expected = "120".getBytes(Strings.UTF_8) //ascii("x") = 120 |
| 45 | + byte[] bytes = "x".getBytes(Strings.UTF_8) |
| 46 | + byte[] result = new JsonbSerializer().serialize(bytes[0]) //single byte |
| 47 | + assertTrue Arrays.equals(expected, result) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testByteArray() { //expect Base64 string by default: |
| 52 | + byte[] bytes = "hi".getBytes(Strings.UTF_8) |
| 53 | + String expected = '"aGk="' as String //base64(hi) --> aGk= |
| 54 | + byte[] result = new JsonbSerializer().serialize(bytes) |
| 55 | + assertEquals expected, new String(result, Strings.UTF_8) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testEmptyByteArray() { //expect Base64 string by default: |
| 60 | + byte[] bytes = new byte[0] |
| 61 | + byte[] result = new JsonbSerializer().serialize(bytes) |
| 62 | + assertEquals '""', new String(result, Strings.UTF_8) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testChar() { //expect Base64 string by default: |
| 67 | + byte[] result = new JsonbSerializer().serialize('h' as char) |
| 68 | + assertEquals "\"h\"", new String(result, Strings.UTF_8) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testCharArray() { //expect Base64 string by default: |
| 73 | + byte[] result = new JsonbSerializer().serialize("hi".toCharArray()) |
| 74 | + assertEquals "\"hi\"", new String(result, Strings.UTF_8) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testSerialize() { |
| 79 | + byte[] expected = '{"hello":"世界"}'.getBytes(Strings.UTF_8) |
| 80 | + byte[] result = new JsonbSerializer().serialize([hello: '世界']) |
| 81 | + assertTrue Arrays.equals(expected, result) |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Test |
| 86 | + void testSerializeFailsWithJsonProcessingException() { |
| 87 | + |
| 88 | + def ex = createMock(JsonbException) |
| 89 | + |
| 90 | + expect(ex.getMessage()).andReturn('foo') |
| 91 | + |
| 92 | + def serializer = new JsonbSerializer() { |
| 93 | + @Override |
| 94 | + protected byte[] writeValueAsBytes(Object o) throws JsonbException { |
| 95 | + throw ex |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + replay ex |
| 100 | + |
| 101 | + try { |
| 102 | + serializer.serialize([hello: 'world']) |
| 103 | + fail() |
| 104 | + } catch (SerializationException se) { |
| 105 | + assertEquals 'Unable to serialize object: foo', se.getMessage() |
| 106 | + assertSame ex, se.getCause() |
| 107 | + } |
| 108 | + |
| 109 | + verify ex |
| 110 | + } |
| 111 | +} |
0 commit comments