|
| 1 | +package com.fasterxml.jackson.databind.ext; |
| 2 | + |
| 3 | +import java.sql.Blob; |
| 4 | + |
| 5 | +import javax.sql.rowset.serial.SerialBlob; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.Base64Variants; |
| 8 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | + |
| 11 | +// Tests for `java.sql.Date`, `java.sql.Time` and `java.sql.Timestamp` |
| 12 | +public class SqlBlobDeserializationTest extends BaseMapTest |
| 13 | +{ |
| 14 | + static class BlobObject { |
| 15 | + Blob sqlBlob1; |
| 16 | + |
| 17 | + public Blob getSqlBlob1() { |
| 18 | + return sqlBlob1; |
| 19 | + } |
| 20 | + |
| 21 | + public void setSqlBlob1(Blob sqlBlob1) { |
| 22 | + this.sqlBlob1 = sqlBlob1; |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + /* |
| 29 | + /********************************************************** |
| 30 | + /* Test methods |
| 31 | + /********************************************************** |
| 32 | + */ |
| 33 | + |
| 34 | + private final ObjectMapper m = new ObjectMapper(); |
| 35 | + public void testSqlBlobDeserializer() throws Exception { |
| 36 | + |
| 37 | + String testWord="TestObject1"; |
| 38 | + String base64Blob=Base64Variants.getDefaultVariant().encode(testWord.getBytes()); |
| 39 | + |
| 40 | + String json=m.writeValueAsString(base64Blob); |
| 41 | + Blob obj2=m.readValue(json, Blob.class); |
| 42 | + String result=new String( |
| 43 | + obj2.getBytes(1L, (int)obj2.length())); |
| 44 | + assertEquals(result, testWord); |
| 45 | + |
| 46 | + |
| 47 | + } |
| 48 | + public void testSqlBlobDeserializer2() throws Exception { |
| 49 | + |
| 50 | + String testWord="TestObject1"; |
| 51 | + SerialBlob blob1=new SerialBlob(testWord.getBytes()); |
| 52 | + |
| 53 | + BlobObject obj1=new BlobObject(); |
| 54 | + obj1.sqlBlob1=blob1; |
| 55 | + |
| 56 | + String json=m.writeValueAsString(obj1); |
| 57 | + BlobObject obj2=m.readValue(json, BlobObject.class); |
| 58 | + String result=new String( |
| 59 | + obj2.getSqlBlob1().getBytes(1L, (int)obj2.getSqlBlob1().length())); |
| 60 | + assertEquals(result, testWord); |
| 61 | + |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +} |
0 commit comments