• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind;
2 
3 import java.io.*;
4 import java.math.BigDecimal;
5 import java.math.BigInteger;
6 
7 import com.fasterxml.jackson.core.*;
8 import com.fasterxml.jackson.core.io.IOContext;
9 import com.fasterxml.jackson.core.base.ParserBase;
10 import com.fasterxml.jackson.core.base.GeneratorBase;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 
13 /**
14  * Basic tests to ensure that {@link FormatSchema} instances are properly
15  * passed to {@link JsonGenerator} and {@link JsonParser} instances if
16  * mapper, reader or writer is configured with one.
17  */
18 public class TestFormatSchema extends BaseMapTest
19 {
20     /*
21     /**********************************************************************
22     /* Helper classes
23     /**********************************************************************
24      */
25 
26     static class MySchema implements FormatSchema {
27         @Override
getSchemaType()28         public String getSchemaType() { return "test"; }
29     }
30 
31     static class FactoryWithSchema extends JsonFactory
32     {
33         @Override
getFormatName()34         public String getFormatName() { return "test"; }
35 
36         @Override
canUseSchema(FormatSchema schema)37         public boolean canUseSchema(FormatSchema schema) {
38             return (schema instanceof MySchema);
39         }
40 
41         private static final long serialVersionUID = 1L;
42         @Override
_createParser(Reader r, IOContext ctxt)43         protected JsonParser _createParser(Reader r, IOContext ctxt)
44             throws IOException, JsonParseException
45         {
46             return new ParserWithSchema(ctxt, _parserFeatures);
47         }
48 
49         @Override
_createGenerator(Writer out, IOContext ctxt)50         protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException
51         {
52             return new GeneratorWithSchema(_generatorFeatures, _objectCodec);
53         }
54     }
55 
56     // Ugly, but easiest way to get schema back is to throw exception...
57     @SuppressWarnings("serial")
58     static class SchemaException extends RuntimeException
59     {
60         public final FormatSchema _schema;
61 
SchemaException(FormatSchema s)62         public SchemaException(FormatSchema s) {
63             _schema = s;
64         }
65     }
66 
67     static class ParserWithSchema extends ParserBase
68     {
ParserWithSchema(IOContext ioCtxt, int features)69         public ParserWithSchema(IOContext ioCtxt, int features)
70         {
71             super(ioCtxt, features);
72         }
73 
74         @Override
setSchema(FormatSchema schema)75         public void setSchema(FormatSchema schema) {
76             throw new SchemaException(schema);
77         }
78 
79         @Override
_finishString()80         protected void _finishString() throws IOException, JsonParseException { }
81 
82         @Override
getBinaryValue(Base64Variant b64variant)83         public byte[] getBinaryValue(Base64Variant b64variant) {
84             return null;
85         }
86 
87         @Override
getEmbeddedObject()88         public byte[] getEmbeddedObject() {
89             return null;
90         }
91 
92         @Override
getText()93         public String getText() throws IOException, JsonParseException {
94             return null;
95         }
96 
97         @Override
getTextCharacters()98         public char[] getTextCharacters() throws IOException {
99             return null;
100         }
101 
102         @Override
getTextLength()103         public int getTextLength() throws IOException, JsonParseException {
104             return 0;
105         }
106 
107         @Override
getTextOffset()108         public int getTextOffset() throws IOException, JsonParseException {
109             return 0;
110         }
111 
112         @Override
nextToken()113         public JsonToken nextToken() throws IOException, JsonParseException {
114             return null;
115         }
116 
117         @Override
getCodec()118         public ObjectCodec getCodec() {
119             return null;
120         }
121 
122         @Override
setCodec(ObjectCodec c)123         public void setCodec(ObjectCodec c) { }
124 
125         @Override
_closeInput()126         protected void _closeInput() throws IOException {
127         }
128 
129         @Override
readBinaryValue(Base64Variant b64variant, OutputStream out)130         public int readBinaryValue(Base64Variant b64variant, OutputStream out) {
131             return 0;
132         }
133     }
134 
135     static class GeneratorWithSchema extends GeneratorBase
136     {
GeneratorWithSchema(int features, ObjectCodec codec)137         public GeneratorWithSchema(int features, ObjectCodec codec)
138         {
139             super(features, codec);
140         }
141 
142         @Override
setSchema(FormatSchema schema)143         public void setSchema(FormatSchema schema) {
144             throw new SchemaException(schema);
145         }
146 
147         @Override
_releaseBuffers()148         protected void _releaseBuffers() { }
149 
150         @Override
_verifyValueWrite(String typeMsg)151         protected void _verifyValueWrite(String typeMsg) throws IOException { }
152 
153         @Override
flush()154         public void flush() throws IOException { }
155 
156         @Override
writeBinary(Base64Variant b64variant, byte[] data, int offset, int len)157         public void writeBinary(Base64Variant b64variant, byte[] data,
158                 int offset, int len) throws IOException { }
159 
160         @Override
writeBoolean(boolean state)161         public void writeBoolean(boolean state) throws IOException { }
162 
163         @Override
writeFieldName(String name)164         public void writeFieldName(String name) throws IOException { }
165 
166         @Override
writeNull()167         public void writeNull() throws IOException, JsonGenerationException { }
168 
169         @Override
writeNumber(short v)170         public void writeNumber(short v) throws IOException { }
171 
172         @Override
writeNumber(int v)173         public void writeNumber(int v) throws IOException { }
174 
175         @Override
writeNumber(long v)176         public void writeNumber(long v) throws IOException { }
177 
178         @Override
writeNumber(BigInteger v)179         public void writeNumber(BigInteger v) throws IOException { }
180 
181         @Override
writeNumber(double d)182         public void writeNumber(double d) throws IOException { }
183 
184         @Override
writeNumber(float f)185         public void writeNumber(float f) throws IOException { }
186 
187         @Override
writeNumber(BigDecimal dec)188         public void writeNumber(BigDecimal dec) throws IOException { }
189 
190         @Override
writeNumber(String encodedValue)191         public void writeNumber(String encodedValue) throws IOException { }
192 
193         @Override
writeRaw(String text)194         public void writeRaw(String text) throws IOException { }
195 
196         @Override
writeRaw(String text, int offset, int len)197         public void writeRaw(String text, int offset, int len) { }
198 
199         @Override
writeRaw(char[] text, int offset, int len)200         public void writeRaw(char[] text, int offset, int len) { }
201 
202         @Override
writeRaw(char c)203         public void writeRaw(char c) throws IOException { }
204 
205         @Override
writeRawUTF8String(byte[] text, int offset, int length)206         public void writeRawUTF8String(byte[] text, int offset, int length) { }
207 
208         @Override
writeString(String text)209         public void writeString(String text) throws IOException { }
210 
211         @Override
writeString(char[] text, int offset, int len)212         public void writeString(char[] text, int offset, int len) { }
213 
214         @Override
writeUTF8String(byte[] text, int offset, int length)215         public void writeUTF8String(byte[] text, int offset, int length) { }
216 
217         @Override
writeStartArray()218         public void writeStartArray() { }
219 
220         @Override
writeEndArray()221         public void writeEndArray() throws IOException, JsonGenerationException { }
222 
223         @Override
writeStartObject()224         public void writeStartObject() { }
225 
226         @Override
writeEndObject()227         public void writeEndObject() { }
228 
229         @Override
writeBinary(Base64Variant b64variant, InputStream data, int dataLength)230         public int writeBinary(Base64Variant b64variant, InputStream data, int dataLength) {
231             return -1;
232         }
233     }
234 
235     /*
236     /**********************************************************************
237     /* Unit tests
238     /**********************************************************************
239      */
240 
testFormatForParsers()241     public void testFormatForParsers() throws Exception
242     {
243         ObjectMapper mapper = new ObjectMapper(new FactoryWithSchema());
244         MySchema s = new MySchema();
245         StringReader r = new StringReader("{}");
246         //  bit ugly, but can't think of cleaner simple way to check this...
247         try {
248             mapper.reader(s).forType(Object.class).readValue(r);
249             fail("Excpected exception");
250         } catch (SchemaException e) {
251             assertSame(s, e._schema);
252         }
253     }
254 
testFormatForGenerators()255     public void testFormatForGenerators() throws Exception
256     {
257         ObjectMapper mapper = new ObjectMapper(new FactoryWithSchema());
258         MySchema s = new MySchema();
259         StringWriter sw = new StringWriter();
260         //  bit ugly, but can't think of cleaner simple way to check this...
261         try {
262             mapper.writer(s).writeValue(sw, "Foobar");
263             fail("Excpected exception");
264         } catch (SchemaException e) {
265             assertSame(s, e._schema);
266         }
267     }
268 
269 }
270