THRIFT-1745 Python JSON protocol 
TJSONProtocol.py: Frederic Delbos

THRIFT-847 Test Framework harmonization across all languages 
Integration into py lib and test suite



git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1404838 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py
index 8a7fda6..f9121c8 100755
--- a/test/py/RunClientServer.py
+++ b/test/py/RunClientServer.py
@@ -55,7 +55,9 @@
 PROTOS= [
     'accel',
     'binary',
-    'compact' ]
+    'compact']
+# FIXME: add json
+# disabled because json HTTP test hangs... why?
 
 SERVERS = [
   "TSimpleServer",
diff --git a/test/py/TestClient.py b/test/py/TestClient.py
index 71001b1..471e030 100755
--- a/test/py/TestClient.py
+++ b/test/py/TestClient.py
@@ -19,8 +19,8 @@
 # under the License.
 #
 
-import sys, glob
-sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+import sys, glob, os
+sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0])
 
 import unittest
 import time
@@ -63,6 +63,7 @@
 from thrift.transport import TZlibTransport
 from thrift.protocol import TBinaryProtocol
 from thrift.protocol import TCompactProtocol
+from thrift.protocol import TJSONProtocol
 
 class AbstractTest(unittest.TestCase):
   def setUp(self):
@@ -125,7 +126,7 @@
   def testNest(self):
     inner = Xtruct(string_thing="Zero", byte_thing=1, i32_thing=-3,
       i64_thing=-5)
-    x = Xtruct2(struct_thing=inner)
+    x = Xtruct2(struct_thing=inner, byte_thing=0, i32_thing=0)
     y = self.client.testNest(x)
     self.assertEqual(y, x)
 
@@ -163,13 +164,13 @@
       pass
 
   def testMulti(self):
-    xpected = Xtruct(byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
+    xpected = Xtruct(string_thing='Hello2', byte_thing=74, i32_thing=0xff00ff, i64_thing=0xffffffffd0d0)
     y = self.client.testMulti(xpected.byte_thing,
           xpected.i32_thing,
           xpected.i64_thing,
           { 0:'abc' },
           Numberz.FIVE,
-          0xf0f0f0)
+          0xf0f0f0)  
     self.assertEqual(y, xpected)
 
   def testException(self):
@@ -208,6 +209,9 @@
 class CompactTest(AbstractTest):
   protocol_factory = TCompactProtocol.TCompactProtocolFactory()
 
+class JSONTest(AbstractTest):
+  protocol_factory = TJSONProtocol.TJSONProtocolFactory()
+
 class AcceleratedBinaryTest(AbstractTest):
   protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
 
@@ -220,6 +224,8 @@
     suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
   elif options.proto == 'compact':
     suite.addTest(loader.loadTestsFromTestCase(CompactTest))
+  elif options.proto == 'json':
+    suite.addTest(loader.loadTestsFromTestCase(JSONTest))
   else:
     raise AssertionError('Unknown protocol given with --proto: %s' % options.proto)
   return suite
diff --git a/test/py/TestServer.py b/test/py/TestServer.py
index 6f4af44..1eae097 100755
--- a/test/py/TestServer.py
+++ b/test/py/TestServer.py
@@ -19,8 +19,8 @@
 # under the License.
 #
 from __future__ import division
-import sys, glob, time
-sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
+import sys, glob, time, os
+sys.path.insert(0, glob.glob(os.path.join(os.path.dirname(__file__),'../../lib/py/build/lib.*'))[0])
 from optparse import OptionParser
 
 parser = OptionParser()
@@ -40,7 +40,7 @@
     dest="verbose", const=0,
     help="minimal output")
 parser.add_option('--proto',  dest="proto", type="string",
-    help="protocol to use, one of: accel, binary, compact")
+    help="protocol to use, one of: accel, binary, compact, json")
 parser.set_defaults(port=9090, verbose=1, proto='binary')
 options, args = parser.parse_args()
 
@@ -53,11 +53,13 @@
 from thrift.transport import TZlibTransport
 from thrift.protocol import TBinaryProtocol
 from thrift.protocol import TCompactProtocol
+from thrift.protocol import TJSONProtocol
 from thrift.server import TServer, TNonblockingServer, THttpServer
 
 PROT_FACTORIES = {'binary': TBinaryProtocol.TBinaryProtocolFactory,
     'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory,
-    'compact': TCompactProtocol.TCompactProtocolFactory}
+    'compact': TCompactProtocol.TCompactProtocolFactory,
+    'json': TJSONProtocol.TJSONProtocolFactory}
 
 class TestHandler:
 
@@ -156,7 +158,7 @@
   def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5):
     if options.verbose > 1:
       print 'testMulti(%s)' % [arg0, arg1, arg2, arg3, arg4, arg5]
-    x = Xtruct(byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
+    x = Xtruct(string_thing='Hello2', byte_thing=arg0, i32_thing=arg1, i64_thing=arg2)
     return x
 
 # set up the protocol factory form the --proto option
diff --git a/test/test.sh b/test/test.sh
index 30ca838..36e57c3 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -55,6 +55,7 @@
       echo "=================== client message ==================="
       tail log/${testname}_client.log
       echo "======================================================"
+      echo ""
       print_header
     fi
     sleep 10
@@ -93,7 +94,42 @@
   done;
 done;
 
-
+do_test "py-py" "binary" "buffered-ip" \
+        "py/TestClient.py --proto=binary --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "py/TestServer.py --proto=binary --port=9090 --genpydir=py/gen-py TSimpleServer" \
+        "10"
+do_test "py-py" "json" "buffered-ip" \
+        "py/TestClient.py --proto=json --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "py/TestServer.py --proto=json --port=9090 --genpydir=py/gen-py TSimpleServer" \
+        "10"
+do_test "py-cpp" "binary" "buffered-ip" \
+        "py/TestClient.py --proto=binary --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "cpp/TestServer" \
+        "10"
+do_test "py-cpp" "json" "buffered-ip" \
+        "py/TestClient.py --proto=json --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "cpp/TestServer --protocol=json" \
+        "10"
+do_test "cpp-py" "binary" "buffered-ip" \
+        "cpp/TestClient --protocol=binary --port=9090" \
+        "py/TestServer.py --proto=binary --port=9090 --genpydir=py/gen-py TSimpleServer" \
+        "10"
+do_test "cpp-py" "json" "buffered-ip" \
+        "cpp/TestClient --protocol=json --port=9090" \
+        "py/TestServer.py --proto=json --port=9090 --genpydir=py/gen-py TSimpleServer" \
+        "10"
+do_test "py-java"  "binary" "buffered-ip" \
+        "py/TestClient.py --proto=binary --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "ant -f  ../lib/java/build.xml testserver" \
+        "100"
+do_test "py-java"  "json"   "buffered-ip" \
+        "py/TestClient.py --proto=json --port=9090 --host=localhost --genpydir=py/gen-py" \
+        "ant -f  ../lib/java/build.xml testserver" \
+        "100"
+do_test "java-py"  "binary" "buffered-ip" \
+        "ant -f  ../lib/java/build.xml testclient" \
+        "py/TestServer.py --proto=binary --port=9090 --genpydir=py/gen-py TSimpleServer" \
+        "10"
 do_test "java-java" "binary" "buffered-ip" \
         "ant -f  ../lib/java/build.xml testclient" \
         "ant -f  ../lib/java/build.xml testserver" \