2
2
3
3
import java .io .IOException ;
4
4
5
- import org .junit .Assert ;
6
- import org .junit .Test ;
5
+ import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
6
+
7
+ import com .fasterxml .jackson .core .JsonParser ;
8
+ import com .fasterxml .jackson .core .JsonToken ;
7
9
8
10
import com .fasterxml .jackson .dataformat .protobuf .ProtobufMapper ;
9
11
import com .fasterxml .jackson .dataformat .protobuf .schema .ProtobufSchema ;
@@ -95,11 +97,25 @@ public void setA(String a) {
95
97
}
96
98
}
97
99
98
- @ Test
100
+ // [dataformats-binary#135]: endless END_OBJECT at end of doc
101
+ @ JsonPropertyOrder ({ "name" , "age" , "emails" , "boss" })
102
+ static class Employee135 {
103
+ public int age ;
104
+
105
+ public String [] emails ;
106
+ }
107
+
108
+ /*
109
+ /**********************************************************
110
+ /* Test methods
111
+ /**********************************************************
112
+ */
113
+
114
+ private final ProtobufMapper MAPPER = new ProtobufMapper ();
115
+
99
116
public void testNestedRoundtrip () throws IOException
100
117
{
101
118
TestObject testClass = new TestObject ();
102
- ProtobufMapper om = new ProtobufMapper ();
103
119
ProtobufSchema s = ProtobufSchemaLoader .std .parse (PROTO );
104
120
testClass .a = VALUE_A ;
105
121
testClass .b = new TestSub ();
@@ -109,14 +125,53 @@ public void testNestedRoundtrip() throws IOException
109
125
testClass .b .d = new TestSubSub ();
110
126
testClass .b .d .a = VALUE_SUB_A ;
111
127
112
- byte [] proto = om .writer (s )
128
+ byte [] proto = MAPPER .writer (s )
113
129
.writeValueAsBytes (testClass );
114
- TestObject res = om .readerFor (TestObject .class ).with (s )
130
+ TestObject res = MAPPER .readerFor (TestObject .class ).with (s )
115
131
.readValue (proto );
116
132
117
- Assert .assertEquals (VALUE_A , res .a );
118
- Assert .assertEquals (VALUE_C , res .b .c );
119
- Assert .assertEquals (VALUE_B , res .b .b );
120
- Assert .assertEquals (VALUE_SUB_A , res .b .d .a );
133
+ assertEquals (VALUE_A , res .a );
134
+ assertEquals (VALUE_C , res .b .c );
135
+ assertEquals (VALUE_B , res .b .b );
136
+ assertEquals (VALUE_SUB_A , res .b .d .a );
137
+ }
138
+
139
+ // [dataformats-binary#135]: endless END_OBJECT at end of doc
140
+ public void testIssue135 () throws Exception
141
+ {
142
+ String protobuf_str = "message Employee {\n "
143
+ + " required int32 age = 1;\n "
144
+ + " repeated string emails = 3;\n "
145
+ + "}\n " ;
146
+ final ProtobufSchema schema = MAPPER .schemaLoader ().parse (protobuf_str );
147
+
148
+ Employee135 empl = new Employee135 ();
149
+ empl .age = 30 ;
150
+ empl .
emails =
new String []{
"[email protected] " };
151
+
152
+ byte [] protobufData = MAPPER .writer (schema )
153
+ .writeValueAsBytes (empl );
154
+
155
+ JsonParser p = new ProtobufFactory ().createParser (protobufData );
156
+ p .setSchema (schema );
157
+
158
+ assertToken (JsonToken .START_OBJECT , p .nextToken ());
159
+
160
+ assertToken (JsonToken .FIELD_NAME , p .nextToken ());
161
+ assertEquals ("age" , p .currentName ());
162
+ assertToken (JsonToken .VALUE_NUMBER_INT , p .nextToken ());
163
+ assertEquals (30 , p .getIntValue ());
164
+
165
+ assertToken (JsonToken .FIELD_NAME , p .nextToken ());
166
+ assertEquals ("emails" , p .currentName ());
167
+ assertToken (JsonToken .START_ARRAY , p .nextToken ());
168
+ assertToken (JsonToken .VALUE_STRING , p .nextToken ());
169
+ assertEquals (
"[email protected] " ,
p .
getText ());
170
+ assertToken (JsonToken .END_ARRAY , p .nextToken ());
171
+
172
+ assertToken (JsonToken .END_OBJECT , p .nextToken ());
173
+
174
+ assertNull (p .nextToken ());
175
+ p .close ();
121
176
}
122
177
}
0 commit comments