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 0155c9a..90b92a2 100644
--- a/lib/delphi/test/TestClient.pas
+++ b/lib/delphi/test/TestClient.pas
@@ -85,14 +85,14 @@
 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 @@
   i2 : IXtruct2;
   mapout : IThriftDictionary<Integer,Integer>;
   mapin : IThriftDictionary<Integer,Integer>;
+  strmapout : IThriftDictionary<string,string>;
+  strmapin : IThriftDictionary<string,string>;
   j : Integer;
   first : Boolean;
   key : Integer;
+  strkey : string;
   listout : IThriftList<Integer>;
   listin : IThriftList<Integer>;
   setout : IHashSet<Integer>;
@@ -308,37 +311,52 @@
 
 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 @@
   end;
 
 
+  // map<type1,type2>: 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<string,string>.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<type>: 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 @@
   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 @@
     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 @@
     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 b8b4d72..fa48e81 100644
--- a/lib/delphi/test/TestServer.pas
+++ b/lib/delphi/test/TestServer.pas
@@ -110,8 +110,15 @@
   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 @@
 
   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 @@
   first_map := TThriftDictionaryImpl<TNumberz, IInsanity>.Create;
   second_map := TThriftDictionaryImpl<TNumberz, IInsanity>.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 @@
 
 function TTestServer.TTestHandlerImpl.testStringMap(
   const thing: IThriftDictionary<string, string>): IThriftDictionary<string, string>;
+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 655308d..f12fe84 100644
--- a/lib/delphi/test/client.dpr
+++ b/lib/delphi/test/client.dpr
@@ -56,8 +56,10 @@
     TTestClient.Execute( args );
     Readln;
   except
-    on E: Exception do
+    on E: Exception do begin
       Writeln(E.ClassName, ': ', E.Message);
+      ExitCode := $FFFF;
+    end;
   end;
 end.