From: Roger Meier Date: Fri, 4 May 2012 23:35:45 +0000 (+0000) Subject: THRIFT-1596 Delphi: Test clients should have a return codes that reflect whether... X-Git-Tag: 0.9.1~380 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=bb6de7aab09e4feb1377bd2486d420855495a11b;p=common%2Fthrift.git THRIFT-1596 Delphi: Test clients should have a return codes that reflect whether they succeeded or not Patch: Jens Geyer git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1334250 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/delphi/test/TestClient.pas b/lib/delphi/test/TestClient.pas index 0155c9a4..90b92a22 100644 --- a/lib/delphi/test/TestClient.pas +++ b/lib/delphi/test/TestClient.pas @@ -85,14 +85,14 @@ function BoolToString( b : Boolean) : string; begin if b then result := 'true' - else result := 'false'; -end; - -// not available in all versions, so make sure we have this one imported -function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent'; - -{ TTestClient } - + else result := 'false'; +end; + +// not available in all versions, so make sure we have this one imported +function IsDebuggerPresent: BOOL; stdcall; external KERNEL32 name 'IsDebuggerPresent'; + +{ TTestClient } + class procedure TTestClient.Execute(const args: array of string); var i : Integer; @@ -266,9 +266,12 @@ var i2 : IXtruct2; mapout : IThriftDictionary; mapin : IThriftDictionary; + strmapout : IThriftDictionary; + strmapin : IThriftDictionary; j : Integer; first : Boolean; key : Integer; + strkey : string; listout : IThriftList; listin : IThriftList; setout : IHashSet; @@ -308,37 +311,52 @@ var begin client := TThriftTest.TClient.Create( FProtocol); - try - if not FTransport.IsOpen then - begin - FTransport.Open; - end; - except - on E: Exception do - begin - Console.WriteLine( E.Message ); - Exit; - end; - end; + FTransport.Open; // in-depth exception test // (1) do we get an exception at all? // (2) do we get the right exception? // (3) does the exception contain the expected data? StartTestGroup( 'testException'); + // case 1: exception type declared in IDL at the function call try client.testException('Xception'); Expect( FALSE, 'testException(''Xception''): must trow an exception'); except on e:TXception do begin - Expect( e.ErrorCode = 1001, 'error code'); - Expect( e.Message_ = 'This is an Xception', 'error message'); + Expect( e.ErrorCode = 1001, 'error code'); + Expect( e.Message_ = 'Xception', 'error message'); Console.WriteLine( ' = ' + IntToStr(e.ErrorCode) + ', ' + e.Message_ ); end; on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"'); on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"'); end; + // case 2: exception type NOT declared in IDL at the function call + // this will close the connection + try + client.testException('TException'); + Expect( FALSE, 'testException(''TException''): must trow an exception'); + except + on e:TTransportException do begin + Console.WriteLine( e.ClassName+' = '+e.Message); // this is what we get + if FTransport.IsOpen then FTransport.Close; + FTransport.Open; // re-open connection, server has already closed + end; + on e:TException do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"'); + on e:Exception do Expect( FALSE, 'Unexpected exception type "'+e.ClassName+'"'); + end; + + // case 3: no exception + try + client.testException('something'); + Expect( TRUE, 'testException(''something''): must not trow an exception'); + except + on e:TTransportException do Expect( FALSE, 'Unexpected : "'+e.ToString+'"'); + on e:Exception do Expect( FALSE, 'Unexpected exception "'+e.ClassName+'"'); + end; + + // simple things StartTestGroup( 'simple Thrift calls'); client.testVoid(); @@ -433,6 +451,40 @@ begin end; + // map: A map of strictly unique keys to values. + // Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. + StartTestGroup( 'testStringMap'); + strmapout := TThriftDictionaryImpl.Create; + for j := 0 to 4 do + begin + strmapout.AddOrSetValue( IntToStr(j), IntToStr(j - 10)); + end; + Console.Write('testStringMap({'); + first := True; + for strkey in strmapout.Keys do + begin + if first + then first := False + else Console.Write( ', ' ); + Console.Write( strkey + ' => ' + strmapout[strkey]); + end; + Console.WriteLine('})'); + + strmapin := client.testStringMap( strmapout ); + Expect( strmapin.Count = strmapout.Count, 'testStringMap: strmapin.Count = strmapout.Count'); + for j := 0 to 4 do + begin + Expect( strmapout.ContainsKey(IntToStr(j)), + 'testStringMap: strmapout.ContainsKey('+IntToStr(j)+') = ' + + BoolToString(strmapout.ContainsKey(IntToStr(j)))); + end; + for strkey in strmapin.Keys do + begin + Expect( strmapin[strkey] = strmapout[strkey], 'testStringMap: '+strkey + ' => ' + strmapin[strkey]); + Expect( strmapin[strkey] = IntToStr( StrToInt(strkey) - 10), 'testStringMap: strmapin['+strkey+'] = '+strmapin[strkey]); + end; + + // set: An unordered set of unique elements. // Translates to an STL set, Java HashSet, set in Python, etc. // Note: PHP does not support sets, so it is treated similar to a List @@ -611,7 +663,7 @@ begin Expect( not looney.__isset_UserMap, 'looney.__isset_UserMap = '+BoolToString(looney.__isset_UserMap)); Expect( not looney.__isset_Xtructs, 'looney.__isset_Xtructs = '+BoolToString(looney.__isset_Xtructs)); // - for ret in [TNumberz.SIX, TNumberz.THREE] do begin + for ret in [TNumberz.TWO, TNumberz.THREE] do begin crazy := first_map[ret]; Console.WriteLine('first_map['+intToStr(Ord(ret))+']'); @@ -635,7 +687,7 @@ begin Expect( goodbye.__isset_I32_thing, 'goodbye.__isset_I32_thing = '+BoolToString(goodbye.__isset_I32_thing)); Expect( goodbye.__isset_I64_thing, 'goodbye.__isset_I64_thing = '+BoolToString(goodbye.__isset_I64_thing)); - Expect( hello.String_thing = 'hello', 'hello.String_thing = "'+hello.String_thing+'"'); + Expect( hello.String_thing = 'Hello2', 'hello.String_thing = "'+hello.String_thing+'"'); Expect( hello.Byte_thing = 2, 'hello.Byte_thing = '+IntToStr(hello.Byte_thing)); Expect( hello.I32_thing = 2, 'hello.I32_thing = '+IntToStr(hello.I32_thing)); Expect( hello.I64_thing = 2, 'hello.I64_thing = '+IntToStr(hello.I64_thing)); @@ -875,6 +927,7 @@ begin Console.WriteLine('FAILED TESTS:'); for sLine in FErrors do Console.WriteLine('- '+sLine); Console.WriteLine( StringOfChar('=',60)); + InterlockedIncrement( ExitCode); // return <> 0 on errors end; Console.WriteLine(''); end; diff --git a/lib/delphi/test/TestServer.pas b/lib/delphi/test/TestServer.pas index b8b4d725..fa48e818 100644 --- a/lib/delphi/test/TestServer.pas +++ b/lib/delphi/test/TestServer.pas @@ -110,8 +110,15 @@ begin Console.WriteLine('testException(' + arg + ')'); if ( arg = 'Xception') then begin - raise TXception.Create( 1001, 'This is an Xception'); + raise TXception.Create( 1001, arg); end; + + if (arg = 'TException') then + begin + raise TException.Create(''); + end; + + // else do not throw anything end; function TTestServer.TTestHandlerImpl.testI32(thing: Integer): Integer; @@ -140,7 +147,7 @@ begin Console.WriteLine('testInsanity()'); hello := TXtructImpl.Create; - hello.String_thing := 'hello'; + hello.String_thing := 'Hello2'; hello.Byte_thing := 2; hello.I32_thing := 2; hello.I64_thing := 2; @@ -164,7 +171,7 @@ begin first_map := TThriftDictionaryImpl.Create; second_map := TThriftDictionaryImpl.Create; - first_map.AddOrSetValue( TNumberz.SIX, crazy); + first_map.AddOrSetValue( TNumberz.TWO, crazy); first_map.AddOrSetValue( TNumberz.THREE, crazy); second_map.AddOrSetValue( TNumberz.SIX, looney); @@ -348,8 +355,25 @@ end; function TTestServer.TTestHandlerImpl.testStringMap( const thing: IThriftDictionary): IThriftDictionary; +var + first : Boolean; + key : string; begin - + Console.Write('testStringMap({'); + first := True; + for key in thing.Keys do + begin + if (first) then + begin + first := false; + end else + begin + Console.Write(', '); + end; + Console.Write(key + ' => ' + thing[key]); + end; + Console.WriteLine('})'); + Result := thing; end; function TTestServer.TTestHandlerImpl.testTypedef( const thing: Int64): Int64; diff --git a/lib/delphi/test/client.dpr b/lib/delphi/test/client.dpr index 655308d5..f12fe843 100644 --- a/lib/delphi/test/client.dpr +++ b/lib/delphi/test/client.dpr @@ -56,8 +56,10 @@ begin TTestClient.Execute( args ); Readln; except - on E: Exception do + on E: Exception do begin Writeln(E.ClassName, ': ', E.Message); + ExitCode := $FFFF; + end; end; end.