@@ -49,7 +49,7 @@ interface
4949
5050type
5151 TFactoryPair = record
52- prot : IProtocolFactory;
52+ proto : IProtocolFactory;
5353 trans : ITransportFactory;
5454 end ;
5555
@@ -72,8 +72,9 @@ TTestSerializer = class //extends TestCase {
7272 class procedure Deserialize ( const input : TBytes; const target : IBase; const factory : TFactoryPair); overload;
7373 class procedure Deserialize ( const input : TStream; const target : IBase; const factory : TFactoryPair); overload;
7474
75- class procedure ValidateReadToEnd ( const input : TBytes; const serial : TDeserializer); overload;
76- class procedure ValidateReadToEnd ( const input : TStream; const serial : TDeserializer); overload;
75+ class procedure Deserialize ( const input : TBytes; out target : TGuid; const factory : TFactoryPair); overload;
76+
77+ class procedure ValidateReadToEnd ( const serial : TDeserializer); overload;
7778
7879 class function LengthOf ( const bytes : TBytes) : Integer; overload; inline;
7980 class function LengthOf ( const bytes : IThriftBytes) : Integer; overload; inline;
@@ -90,6 +91,9 @@ TTestSerializer = class //extends TestCase {
9091 procedure Test_ExceptionStruct ( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
9192 procedure Test_SimpleException ( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
9293
94+ procedure Test_ProtocolConformity ( const factory : TFactoryPair; const stream : TFileStream);
95+ procedure Test_UuidDeserialization ( const factory : TFactoryPair; const stream : TFileStream);
96+
9397 public
9498 constructor Create;
9599 destructor Destroy; override;
@@ -143,7 +147,7 @@ destructor TTestSerializer.Destroy;
143147procedure TTestSerializer.AddFactoryCombination ( const aProto : IProtocolFactory; const aTrans : ITransportFactory);
144148var rec : TFactoryPair;
145149begin
146- rec.prot := aProto;
150+ rec.proto := aProto;
147151 rec.trans := aTrans;
148152 FProtocols.Add( rec);
149153end ;
@@ -177,6 +181,53 @@ class function TTestSerializer.DataPtrOf( const bytes : IThriftBytes) : Pointer;
177181end ;
178182
179183
184+ procedure TTestSerializer.Test_ProtocolConformity ( const factory : TFactoryPair; const stream : TFileStream);
185+ begin
186+ Test_UuidDeserialization( factory, stream);
187+ // add more tests here
188+ end ;
189+
190+
191+ procedure TTestSerializer.Test_UuidDeserialization ( const factory : TFactoryPair; const stream : TFileStream);
192+
193+ function CreateGuidBytes : TBytes;
194+ var obj : TObject;
195+ i : Integer;
196+ begin
197+ obj := factory.proto as TObject;
198+
199+ if obj is TJSONProtocolImpl.TFactory then begin
200+ result := TEncoding.UTF8.GetBytes(' "00112233-4455-6677-8899-aabbccddeeff"' );
201+ Exit;
202+ end ;
203+
204+ if (obj is TBinaryProtocolImpl.TFactory)
205+ or (obj is TCompactProtocolImpl.TFactory)
206+ then begin
207+ SetLength(result,16 );
208+ for i := 0 to Length(result)-1 do result[i] := (i * $10 ) + i;
209+ Exit;
210+ end ;
211+
212+ raise Exception.Create(' Unhandled case' );
213+ end ;
214+
215+
216+ var tested, correct : TGuid;
217+ bytes : TBytes;
218+ begin
219+ // write
220+ bytes := CreateGuidBytes();
221+
222+ // init + read
223+ Deserialize( bytes, tested, factory);
224+
225+ // check
226+ correct := TGuid.Create(' {00112233-4455-6677-8899-aabbccddeeff}' );
227+ ASSERT( tested = correct);
228+ end ;
229+
230+
180231procedure TTestSerializer.Test_OneOfEach ( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
181232var tested, correct : IOneOfEach;
182233 bytes : TBytes;
@@ -385,6 +436,11 @@ procedure TTestSerializer.Test_Serializer_Deserializer;
385436 for factory in FProtocols do begin
386437 Writeln(' - ' +UserFriendlyName(factory));
387438
439+ // protocol conformity tests
440+ if (method = TMethod.mt_Bytes) and (factory.trans = nil )
441+ then Test_ProtocolConformity( factory, stream);
442+
443+ // normal objects
388444 Test_OneOfEach( method, factory, stream);
389445 Test_CompactStruct( method, factory, stream);
390446 Test_ExceptionStruct( method, factory, stream);
@@ -402,7 +458,7 @@ procedure TTestSerializer.Test_Serializer_Deserializer;
402458
403459class function TTestSerializer.UserFriendlyName ( const factory : TFactoryPair) : string;
404460begin
405- result := Copy( (factory.prot as TObject).ClassName, 2 , MAXINT);
461+ result := Copy( (factory.proto as TObject).ClassName, 2 , MAXINT);
406462
407463 if factory.trans <> nil
408464 then result := Copy( (factory.trans as TObject).ClassName, 2 , MAXINT) +' ' + result;
@@ -472,7 +528,7 @@ class function TTestSerializer.Serialize(const input : IBase; const factory : TF
472528 config := TThriftConfigurationImpl.Create;
473529 // config.MaxMessageSize := 0; // we don't read anything here
474530
475- serial := TSerializer.Create( factory.prot , factory.trans, config);
531+ serial := TSerializer.Create( factory.proto , factory.trans, config);
476532 try
477533 result := serial.Serialize( input);
478534 finally
@@ -488,7 +544,7 @@ class procedure TTestSerializer.Serialize(const input : IBase; const factory : T
488544 config := TThriftConfigurationImpl.Create;
489545 // config.MaxMessageSize := 0; // we don't read anything here
490546
491- serial := TSerializer.Create( factory.prot , factory.trans, config);
547+ serial := TSerializer.Create( factory.proto , factory.trans, config);
492548 try
493549 serial.Serialize( input, aStream);
494550 finally
@@ -504,10 +560,10 @@ class procedure TTestSerializer.Deserialize( const input : TBytes; const target
504560 config := TThriftConfigurationImpl.Create;
505561 config.MaxMessageSize := Length(input);
506562
507- serial := TDeserializer.Create( factory.prot , factory.trans, config);
563+ serial := TDeserializer.Create( factory.proto , factory.trans, config);
508564 try
509565 serial.Deserialize( input, target);
510- ValidateReadToEnd( input, serial);
566+ ValidateReadToEnd( serial);
511567 finally
512568 serial.Free;
513569 end ;
@@ -521,44 +577,49 @@ class procedure TTestSerializer.Deserialize( const input : TStream; const target
521577 config := TThriftConfigurationImpl.Create;
522578 config.MaxMessageSize := input.Size;
523579
524- serial := TDeserializer.Create( factory.prot , factory.trans, config);
580+ serial := TDeserializer.Create( factory.proto , factory.trans, config);
525581 try
526582 serial.Deserialize( input, target);
527- ValidateReadToEnd( input, serial);
583+ ValidateReadToEnd( serial);
528584 finally
529585 serial.Free;
530586 end ;
531587end ;
532588
533589
534- class procedure TTestSerializer.ValidateReadToEnd ( const input : TBytes; const serial : TDeserializer );
535- // we should not have any more byte to read
536- var dummy : IBase ;
590+ class procedure TTestSerializer.Deserialize ( const input : TBytes; out target : TGuid; const factory : TFactoryPair );
591+ var serial : TDeserializer;
592+ config : IThriftConfiguration ;
537593begin
594+ config := TThriftConfigurationImpl.Create;
595+ config.MaxMessageSize := Length(input);
596+
597+ serial := TDeserializer.Create( factory.proto, factory.trans, config);
538598 try
539- dummy := TOneOfEachImpl.Create;
540- serial.Deserialize( input, dummy);
541- raise EInOutError.Create(' Expected exception not thrown?' );
542- except
543- on e:TTransportExceptionEndOfFile do { expected} ;
544- on e:Exception do raise; // unexpected
599+ serial.Stream.Write(input[0 ], Length(input));
600+ serial.Stream.Position := 0 ;
601+ serial.Transport.ResetMessageSizeAndConsumedBytes(); // size has changed
602+
603+ target := serial.Protocol.ReadUuid;
604+ finally
605+ serial.Free;
545606 end ;
546607end ;
547608
548609
549- class procedure TTestSerializer.ValidateReadToEnd ( const input : TStream; const serial : TDeserializer);
610+ class procedure TTestSerializer.ValidateReadToEnd ( const serial : TDeserializer);
550611// we should not have any more byte to read
551612var dummy : IBase;
552613begin
553614 try
554- input.Position := 0 ;
555615 dummy := TOneOfEachImpl.Create;
556- serial.Deserialize( input , dummy);
616+ serial.Deserialize( nil , dummy);
557617 raise EInOutError.Create(' Expected exception not thrown?' );
558618 except
559- on e:TTransportExceptionEndOfFile do { expected} ;
619+ on e:TTransportException do { expected} ;
560620 on e:Exception do raise; // unexpected
561621 end ;
562622end ;
563623
624+
564625end .
0 commit comments