15
15
*/
16
16
package com .github .shyiko .mysql .binlog .event .deserialization ;
17
17
18
- import com .github .shyiko .mysql .binlog .event .EventType ;
19
- import com .github .shyiko .mysql .binlog .event .TransactionPayloadEventData ;
20
- import com .github .shyiko .mysql .binlog .event .XAPrepareEventData ;
18
+ import com .github .shyiko .mysql .binlog .event .*;
21
19
import com .github .shyiko .mysql .binlog .io .ByteArrayInputStream ;
22
20
import org .testng .annotations .Test ;
23
21
24
22
import java .io .IOException ;
23
+ import java .io .Serializable ;
25
24
26
25
import static org .testng .Assert .assertEquals ;
26
+ import static org .testng .Assert .assertTrue ;
27
27
28
28
/**
29
29
* @author <a href="mailto:[email protected] ">Somesh Malviya</a>
@@ -82,6 +82,8 @@ public class TransactionPayloadEventDataDeserializerTest {
82
82
.append ("]}" )
83
83
.toString ();
84
84
85
+ private static final byte [] UNCOMPRESSED_UPDATE_EVENT_BEFORE_ROW_0_BYTE_ARRAY = new byte [] {1 , 0 , 0 , 0 };
86
+
85
87
@ Test
86
88
public void deserialize () throws IOException {
87
89
TransactionPayloadEventDataDeserializer deserializer = new TransactionPayloadEventDataDeserializer ();
@@ -97,4 +99,62 @@ public void deserialize() throws IOException {
97
99
assertEquals (EventType .XID , transactionPayloadEventData .getUncompressedEvents ().get (3 ).getHeader ().getEventType ());
98
100
assertEquals (UNCOMPRESSED_UPDATE_EVENT , transactionPayloadEventData .getUncompressedEvents ().get (2 ).getData ().toString ());
99
101
}
102
+
103
+ @ Test
104
+ public void deserializeUsingEventDeserializer () throws IOException {
105
+
106
+ ByteArrayInputStream dataStream = new ByteArrayInputStream (DATA );
107
+
108
+ // Mock create target TransactionPayloadEventData DATA event header
109
+ final EventHeaderV4 eventHeader = new EventHeaderV4 ();
110
+ eventHeader .setEventType (EventType .TRANSACTION_PAYLOAD );
111
+ eventHeader .setEventLength (DATA .length + 19L );
112
+ eventHeader .setTimestamp (1646406641000L );
113
+ eventHeader .setServerId (223344 );
114
+
115
+
116
+ EventHeaderDeserializer eventHeaderDeserializer = new EventHeaderDeserializer () {
117
+
118
+ private long count = 0L ;
119
+
120
+ private EventHeaderDeserializer defaultEventHeaderDeserializer = new EventHeaderV4Deserializer ();
121
+
122
+ @ Override
123
+ public EventHeader deserialize (ByteArrayInputStream inputStream ) throws IOException {
124
+ if (count > 0 ) {
125
+ // uncompressed event header deserialize
126
+ return defaultEventHeaderDeserializer .deserialize (inputStream );
127
+ }
128
+ count ++;
129
+ // we need to return target TransactionPayloadEventData DATA event header we had mocked
130
+ return eventHeader ;
131
+ }
132
+ };
133
+
134
+ EventDeserializer eventDeserializer = new EventDeserializer (eventHeaderDeserializer , new NullEventDataDeserializer ());
135
+ eventDeserializer .setCompatibilityMode (EventDeserializer .CompatibilityMode .INTEGER_AS_BYTE_ARRAY );
136
+
137
+ Event event = eventDeserializer .nextEvent (dataStream );
138
+
139
+ assertTrue (event .getHeader ().getEventType () == EventType .TRANSACTION_PAYLOAD );
140
+ assertTrue (event .getData () instanceof TransactionPayloadEventData );
141
+
142
+ TransactionPayloadEventData transactionPayloadEventData = event .getData ();
143
+ assertEquals (COMPRESSION_TYPE , transactionPayloadEventData .getCompressionType ());
144
+ assertEquals (PAYLOAD_SIZE , transactionPayloadEventData .getPayloadSize ());
145
+ assertEquals (UNCOMPRESSED_SIZE , transactionPayloadEventData .getUncompressedSize ());
146
+ assertEquals (NUMBER_OF_UNCOMPRESSED_EVENTS , transactionPayloadEventData .getUncompressedEvents ().size ());
147
+ assertEquals (EventType .QUERY , transactionPayloadEventData .getUncompressedEvents ().get (0 ).getHeader ().getEventType ());
148
+ assertEquals (EventType .TABLE_MAP , transactionPayloadEventData .getUncompressedEvents ().get (1 ).getHeader ().getEventType ());
149
+ assertEquals (EventType .EXT_UPDATE_ROWS , transactionPayloadEventData .getUncompressedEvents ().get (2 ).getHeader ().getEventType ());
150
+ assertEquals (EventType .XID , transactionPayloadEventData .getUncompressedEvents ().get (3 ).getHeader ().getEventType ());
151
+ // assertEquals(UNCOMPRESSED_UPDATE_EVENT, transactionPayloadEventData.getUncompressedEvents().get(2).getData().toString());
152
+ assertTrue (transactionPayloadEventData .getUncompressedEvents ().get (2 ).getData () instanceof UpdateRowsEventData );
153
+
154
+ UpdateRowsEventData updateRowsEventData = transactionPayloadEventData .getUncompressedEvents ().get (2 ).getData ();
155
+ assertEquals (1 , updateRowsEventData .getRows ().size ());
156
+ Serializable [] updateBefore = updateRowsEventData .getRows ().get (0 ).getKey ();
157
+ assertEquals (UNCOMPRESSED_UPDATE_EVENT_BEFORE_ROW_0_BYTE_ARRAY , updateBefore [0 ]);
158
+ }
159
+
100
160
}
0 commit comments