1515 */
1616package com .igormaznitsa .jbbp ;
1717
18- import com .igormaznitsa .jbbp .JBBPCustomFieldTypeProcessor ;
19- import com .igormaznitsa .jbbp .JBBPParser ;
2018import com .igormaznitsa .jbbp .compiler .JBBPNamedFieldInfo ;
2119import com .igormaznitsa .jbbp .compiler .tokenizer .JBBPFieldTypeParameterContainer ;
2220import com .igormaznitsa .jbbp .io .*;
@@ -31,89 +29,181 @@ public class JBBPCustomFieldTypeProcessorTest {
3129 @ Test
3230 public void testFieldsWithCustomNames () throws Exception {
3331 final AtomicInteger callCounter = new AtomicInteger ();
34-
32+
3533 final JBBPCustomFieldTypeProcessor testProcessor = new JBBPCustomFieldTypeProcessor () {
36- private final String [] types = new String []{"some1" ,"some2" ,"some3" };
34+ private final String [] types = new String []{"some1" , "some2" , "some3" };
35+
3736 public String [] getCustomFieldTypes () {
3837 return this .types ;
3938 }
4039
4140 public boolean isAllowed (final JBBPFieldTypeParameterContainer fieldType , final String fieldName , final int extraData , final boolean isArray ) {
4241 callCounter .incrementAndGet ();
43-
42+
4443 assertNotNull (fieldType );
4544 final String type = fieldType .getTypeName ();
4645 assertTrue (type .equals ("some1" ) || type .equals ("some2" ) || type .equals ("some3" ));
4746
48- if (fieldName .equals ("b" )) assertEquals ("some1" ,type );
49- if (fieldName .equals ("c" )) assertEquals ("some2" ,type );
50- if (fieldName .equals ("e" )) assertEquals ("some3" ,type );
51-
52- if (type .equals ("some3" )){
47+ if (fieldName .equals ("b" )) {
48+ assertEquals ("some1" , type );
49+ }
50+ if (fieldName .equals ("c" )) {
51+ assertEquals ("some2" , type );
52+ }
53+ if (fieldName .equals ("e" )) {
54+ assertEquals ("some3" , type );
55+ }
56+
57+ if (type .equals ("some3" )) {
5358 assertTrue (isArray );
54- }else {
59+ }
60+ else {
5561 assertFalse (isArray );
5662 }
57-
58- if (type .equals ("some2" )){
63+
64+ if (type .equals ("some2" )) {
5965 assertEquals (345 , extraData );
60- }else {
66+ }
67+ else {
6168 assertEquals (0 , extraData );
6269 }
63-
70+
6471 return true ;
6572 }
6673
6774 public JBBPAbstractField readCustomFieldType (final JBBPBitInputStream in , final JBBPBitOrder bitOrder , final int parserFlags , final JBBPFieldTypeParameterContainer customFieldTypeInfo , final JBBPNamedFieldInfo fieldName , final int extraData , final boolean readWholeStream , final int arrayLength ) throws IOException {
6875 final String type = customFieldTypeInfo .getTypeName ();
69-
76+
7077 assertEquals (JBBPBitOrder .LSB0 , bitOrder );
71-
78+
7279 assertEquals (JBBPParser .FLAG_SKIP_REMAINING_FIELDS_IF_EOF , parserFlags );
7380 assertEquals (type .equals ("some1" ) ? JBBPByteOrder .LITTLE_ENDIAN : JBBPByteOrder .BIG_ENDIAN , customFieldTypeInfo .getByteOrder ());
74-
75- if (type .equals ("some1" )){
81+
82+ if (type .equals ("some1" )) {
7683 assertEquals (0 , extraData );
77- assertEquals ("b" ,fieldName .getFieldName ());
84+ assertEquals ("b" , fieldName .getFieldName ());
7885 assertFalse (readWholeStream );
7986 assertEquals (-1 , arrayLength );
80- return new JBBPFieldByte (fieldName , (byte )in .readByte ());
81- }else if (type .equals ("some2" )){
87+ return new JBBPFieldByte (fieldName , (byte ) in .readByte ());
88+ }
89+ else if (type .equals ("some2" )) {
8290 assertEquals (345 , extraData );
8391 assertEquals ("c" , fieldName .getFieldName ());
8492 assertFalse (readWholeStream );
8593 assertEquals (-1 , arrayLength );
86- return new JBBPFieldShort (fieldName , (short )in .readUnsignedShort (customFieldTypeInfo .getByteOrder ()));
87- }else if (type .equals ("some3" )){
94+ return new JBBPFieldShort (fieldName , (short ) in .readUnsignedShort (customFieldTypeInfo .getByteOrder ()));
95+ }
96+ else if (type .equals ("some3" )) {
8897 assertEquals (0 , extraData );
8998 assertEquals ("e" , fieldName .getFieldName ());
9099 assertFalse (readWholeStream );
91- assertEquals (5 ,arrayLength );
100+ assertEquals (5 , arrayLength );
92101 return new JBBPFieldArrayByte (fieldName , in .readByteArray (arrayLength ));
93- }else {
94- fail ("Unexpected " +type );
102+ }
103+ else {
104+ fail ("Unexpected " + type );
95105 return null ;
96106 }
97107 }
98108 };
99-
109+
100110 final JBBPParser parser = JBBPParser .prepare ("int a; <some1 b; some2:345 c; long d; some3 [5] e;" , JBBPBitOrder .LSB0 , testProcessor , JBBPParser .FLAG_SKIP_REMAINING_FIELDS_IF_EOF );
101111 assertEquals (3 , callCounter .get ());
102112
103113 final JBBPFieldStruct parsed = parser .parse (new byte []{
104- (byte )0x12 ,(byte )0x34 ,(byte )0x56 ,(byte )0x78 ,
105- (byte )0xAB ,
106- (byte )0xCD ,(byte )0xDE ,
107- (byte )0x01 ,(byte )0x02 ,(byte )0x03 ,(byte )0x04 ,(byte )0x05 ,(byte )0x06 ,(byte )0x07 ,(byte )0x08 ,
108- (byte )0xAA ,(byte )0xBB ,(byte )0xCC ,(byte )0xDD ,(byte )0xEE ,
109- (byte )0xFF ,(byte )0xFF
114+ (byte ) 0x12 , (byte ) 0x34 , (byte ) 0x56 , (byte ) 0x78 ,
115+ (byte ) 0xAB ,
116+ (byte ) 0xCD , (byte ) 0xDE ,
117+ (byte ) 0x01 , (byte ) 0x02 , (byte ) 0x03 , (byte ) 0x04 , (byte ) 0x05 , (byte ) 0x06 , (byte ) 0x07 , (byte ) 0x08 ,
118+ (byte ) 0xAA , (byte ) 0xBB , (byte ) 0xCC , (byte ) 0xDD , (byte ) 0xEE ,
119+ (byte ) 0xFF , (byte ) 0xFF
110120 });
111-
121+
112122 assertEquals (0x12345678 , parsed .findFieldForNameAndType ("a" , JBBPFieldInt .class ).getAsInt ());
113- assertEquals ((byte )0xAB , parsed .findFieldForNameAndType ("b" , JBBPFieldByte .class ).getAsInt ());
114- assertEquals ((short )0xCDDE , parsed .findFieldForNameAndType ("c" , JBBPFieldShort .class ).getAsInt ());
123+ assertEquals ((byte ) 0xAB , parsed .findFieldForNameAndType ("b" , JBBPFieldByte .class ).getAsInt ());
124+ assertEquals ((short ) 0xCDDE , parsed .findFieldForNameAndType ("c" , JBBPFieldShort .class ).getAsInt ());
115125 assertEquals (0x0102030405060708L , parsed .findFieldForNameAndType ("d" , JBBPFieldLong .class ).getAsLong ());
116126 assertArrayEquals (new byte []{(byte ) 0xAA , (byte ) 0xBB , (byte ) 0xCC , (byte ) 0xDD , (byte ) 0xEE ,}, parsed .findFieldForNameAndType ("e" , JBBPFieldArrayByte .class ).getArray ());
117127 }
118-
128+
129+ @ Test
130+ public void testFieldsWithCustomNames_ExpressionInExtraFieldValue () throws Exception {
131+ final AtomicInteger callCounter = new AtomicInteger ();
132+
133+ final JBBPCustomFieldTypeProcessor testProcessor = new JBBPCustomFieldTypeProcessor () {
134+ private final String [] types = new String []{"some1" , "some2" , "some3" };
135+
136+ public String [] getCustomFieldTypes () {
137+ return this .types ;
138+ }
139+
140+ public boolean isAllowed (final JBBPFieldTypeParameterContainer fieldType , final String fieldName , final int extraData , final boolean isArray ) {
141+ callCounter .incrementAndGet ();
142+
143+ assertNotNull (fieldType );
144+ final String type = fieldType .getTypeName ();
145+ assertTrue (type .equals ("some1" ) || type .equals ("some2" ) || type .equals ("some3" ));
146+
147+ if (fieldName .equals ("b" )) {
148+ assertEquals ("some1" , type );
149+ }
150+ if (fieldName .equals ("c" )) {
151+ assertEquals ("some2" , type );
152+ }
153+ if (fieldName .equals ("e" )) {
154+ assertEquals ("some3" , type );
155+ }
156+
157+ if (type .equals ("some3" )) {
158+ assertTrue (isArray );
159+ }
160+ else {
161+ assertFalse (isArray );
162+ }
163+
164+ assertEquals (-1 , extraData );
165+
166+ return true ;
167+ }
168+
169+ public JBBPAbstractField readCustomFieldType (final JBBPBitInputStream in , final JBBPBitOrder bitOrder , final int parserFlags , final JBBPFieldTypeParameterContainer customFieldTypeInfo , final JBBPNamedFieldInfo fieldName , final int extraData , final boolean readWholeStream , final int arrayLength ) throws IOException {
170+ final String type = customFieldTypeInfo .getTypeName ();
171+
172+ if (type .equals ("some1" )) {
173+ assertEquals (0x12345678 * 2 , extraData );
174+ return new JBBPFieldByte (fieldName , (byte ) in .readByte ());
175+ }
176+ if (type .equals ("some2" )) {
177+ assertEquals (0x12345678 * 3 , extraData );
178+ return new JBBPFieldShort (fieldName , (short ) in .readUnsignedShort (customFieldTypeInfo .getByteOrder ()));
179+ }
180+ if (type .equals ("some3" )) {
181+ assertEquals (0x12345678 * 4 , extraData );
182+ return new JBBPFieldArrayByte (fieldName , in .readByteArray (arrayLength ));
183+ }
184+
185+ fail ("Unexpected field " +type );
186+ return null ;
187+ }
188+ };
189+
190+ final JBBPParser parser = JBBPParser .prepare ("int a; some1:(a*2) b; some2:(a*3) c; long d; some3:(a*4) [5] e;" , JBBPBitOrder .LSB0 , testProcessor , JBBPParser .FLAG_SKIP_REMAINING_FIELDS_IF_EOF );
191+ assertEquals (3 , callCounter .get ());
192+
193+ final JBBPFieldStruct parsed = parser .parse (new byte []{
194+ (byte ) 0x12 , (byte ) 0x34 , (byte ) 0x56 , (byte ) 0x78 ,
195+ (byte ) 0xAB ,
196+ (byte ) 0xCD , (byte ) 0xDE ,
197+ (byte ) 0x01 , (byte ) 0x02 , (byte ) 0x03 , (byte ) 0x04 , (byte ) 0x05 , (byte ) 0x06 , (byte ) 0x07 , (byte ) 0x08 ,
198+ (byte ) 0xAA , (byte ) 0xBB , (byte ) 0xCC , (byte ) 0xDD , (byte ) 0xEE ,
199+ (byte ) 0xFF , (byte ) 0xFF
200+ });
201+
202+ assertEquals (0x12345678 , parsed .findFieldForNameAndType ("a" , JBBPFieldInt .class ).getAsInt ());
203+ assertEquals ((byte ) 0xAB , parsed .findFieldForNameAndType ("b" , JBBPFieldByte .class ).getAsInt ());
204+ assertEquals ((short ) 0xCDDE , parsed .findFieldForNameAndType ("c" , JBBPFieldShort .class ).getAsInt ());
205+ assertEquals (0x0102030405060708L , parsed .findFieldForNameAndType ("d" , JBBPFieldLong .class ).getAsLong ());
206+ assertArrayEquals (new byte []{(byte ) 0xAA , (byte ) 0xBB , (byte ) 0xCC , (byte ) 0xDD , (byte ) 0xEE ,}, parsed .findFieldForNameAndType ("e" , JBBPFieldArrayByte .class ).getArray ());
207+ }
208+
119209}
0 commit comments