8
8
import com .fasterxml .jackson .annotation .JsonValue ;
9
9
10
10
import com .fasterxml .jackson .databind .ObjectMapper ;
11
+ import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
11
12
import com .fasterxml .jackson .dataformat .avro .*;
12
13
13
14
import org .apache .avro .Schema ;
@@ -57,20 +58,21 @@ public ByteBuffer getBytes() {
57
58
/* Tests
58
59
/**********************************************************
59
60
*/
60
-
61
+
62
+ private final AvroMapper MAPPER = newMapper ();
63
+
61
64
public void testBasic () throws Exception
62
65
{
63
- AvroMapper mapper = getMapper ();
64
66
AvroSchemaGenerator gen = new AvroSchemaGenerator ();
65
- mapper .acceptJsonFormatVisitor (RootType .class , gen );
67
+ MAPPER .acceptJsonFormatVisitor (RootType .class , gen );
66
68
AvroSchema schema = gen .getGeneratedSchema ();
67
69
assertNotNull (schema );
68
70
69
71
String json = schema .getAvroSchema ().toString (true );
70
72
assertNotNull (json );
71
73
72
74
// And read it back too just for fun
73
- AvroSchema s2 = mapper .schemaFrom (json );
75
+ AvroSchema s2 = MAPPER .schemaFrom (json );
74
76
assertNotNull (s2 );
75
77
76
78
// System.out.println("Basic schema:\n"+json);
@@ -88,15 +90,14 @@ public void testBasic() throws Exception
88
90
89
91
public void testEmployee () throws Exception
90
92
{
91
- AvroMapper mapper = getMapper ();
92
93
AvroSchemaGenerator gen = new AvroSchemaGenerator ();
93
- mapper .acceptJsonFormatVisitor (Employee .class , gen );
94
+ MAPPER .acceptJsonFormatVisitor (Employee .class , gen );
94
95
AvroSchema schema = gen .getGeneratedSchema ();
95
96
assertNotNull (schema );
96
97
97
98
String json = schema .getAvroSchema ().toString (true );
98
99
assertNotNull (json );
99
- AvroSchema s2 = mapper .schemaFrom (json );
100
+ AvroSchema s2 = MAPPER .schemaFrom (json );
100
101
assertNotNull (s2 );
101
102
102
103
Employee empl = new Employee ();
@@ -106,7 +107,7 @@ public void testEmployee() throws Exception
106
107
empl .boss = null ;
107
108
108
109
// So far so good: try producing actual Avro data...
109
- byte [] bytes = mapper .writer (schema ).writeValueAsBytes (empl );
110
+ byte [] bytes = MAPPER .writer (schema ).writeValueAsBytes (empl );
110
111
assertNotNull (bytes );
111
112
112
113
// and bring it back, too
@@ -120,15 +121,14 @@ public void testEmployee() throws Exception
120
121
121
122
public void testMap () throws Exception
122
123
{
123
- AvroMapper mapper = getMapper ();
124
124
AvroSchemaGenerator gen = new AvroSchemaGenerator ();
125
- mapper .acceptJsonFormatVisitor (StringMap .class , gen );
125
+ MAPPER .acceptJsonFormatVisitor (StringMap .class , gen );
126
126
AvroSchema schema = gen .getGeneratedSchema ();
127
127
assertNotNull (schema );
128
128
129
129
String json = schema .getAvroSchema ().toString (true );
130
130
assertNotNull (json );
131
- AvroSchema s2 = mapper .schemaFrom (json );
131
+ AvroSchema s2 = MAPPER .schemaFrom (json );
132
132
assertNotNull (s2 );
133
133
134
134
// should probably verify, maybe... ?
@@ -139,18 +139,16 @@ public void testMap() throws Exception
139
139
// [Issue#8]
140
140
public void testWithDate () throws Exception
141
141
{
142
- ObjectMapper mapper = new ObjectMapper (new AvroFactory ());
143
142
AvroSchemaGenerator gen = new AvroSchemaGenerator ();
144
- mapper .acceptJsonFormatVisitor (WithDate .class , gen );
143
+ MAPPER .acceptJsonFormatVisitor (WithDate .class , gen );
145
144
AvroSchema schema = gen .getGeneratedSchema ();
146
145
assertNotNull (schema );
147
146
}
148
147
149
148
public void testFixed () throws Exception
150
149
{
151
150
AvroSchemaGenerator gen = new AvroSchemaGenerator ();
152
- ObjectMapper mapper = new ObjectMapper (new AvroFactory ());
153
- mapper .acceptJsonFormatVisitor (WithFixedField .class , gen );
151
+ MAPPER .acceptJsonFormatVisitor (WithFixedField .class , gen );
154
152
Schema generated = gen .getAvroSchema ();
155
153
Schema fixedFieldSchema = generated .getField ("fixedField" ).schema ();
156
154
assertEquals (Schema .Type .FIXED , fixedFieldSchema .getType ());
@@ -160,4 +158,18 @@ public void testFixed() throws Exception
160
158
assertEquals (Schema .Type .FIXED , wrappedFieldSchema .getType ());
161
159
assertEquals (8 , wrappedFieldSchema .getFixedSize ());
162
160
}
161
+
162
+ // as per [dataformats-binary#98], no can do (unless we start supporting polymorphic
163
+ // handling or something...)
164
+ public void testSchemaForUntypedMap () throws Exception
165
+ {
166
+ try {
167
+ MAPPER .schemaFor (Map .class );
168
+ fail ("Not expected to work yet" );
169
+ } catch (InvalidDefinitionException e ) {
170
+ verifyException (e , "\" Any\" type" );
171
+ verifyException (e , "not supported" );
172
+ verifyException (e , "`java.lang.Object`" );
173
+ }
174
+ }
163
175
}
0 commit comments