THRIFT-4949: Improve HTTP/1 server test case

Client: java

This closes #1886.
diff --git a/lib/java/gradle.properties b/lib/java/gradle.properties
index 8559464..1cdf095 100644
--- a/lib/java/gradle.properties
+++ b/lib/java/gradle.properties
@@ -28,7 +28,9 @@
 httpclient.version=4.5.10
 httpcore.version=4.4.12
 slf4j.version=1.7.28
-servlet.version=2.5
+#servlet.version=2.5
+#It contains servlet3
+tomcat.embed.version=8.5.46
 junit.version=4.12
 mockito.version=1.10.19
 javax.annotation.version=1.3.2
diff --git a/lib/java/gradle/environment.gradle b/lib/java/gradle/environment.gradle
index 45fa63a..965a908 100644
--- a/lib/java/gradle/environment.gradle
+++ b/lib/java/gradle/environment.gradle
@@ -44,7 +44,8 @@
 // Versions used in this project
 ext.httpclientVersion = property('httpclient.version')
 ext.httpcoreVersion = property('httpcore.version')
-ext.servletVersion = property('servlet.version')
+//ext.servletVersion = property('servlet.version')
+ext.tomcatEmbedVersion = property('tomcat.embed.version')
 ext.slf4jVersion = property('slf4j.version')
 ext.junitVersion = property('junit.version')
 ext.mockitoVersion = property('mockito.version')
@@ -66,7 +67,8 @@
     compile "org.slf4j:slf4j-api:${slf4jVersion}"
     compile "org.apache.httpcomponents:httpclient:${httpclientVersion}"
     compile "org.apache.httpcomponents:httpcore:${httpcoreVersion}"
-    compile "javax.servlet:servlet-api:${servletVersion}"
+    //compile "javax.servlet:servlet-api:${servletVersion}"
+    compile "org.apache.tomcat.embed:tomcat-embed-core:${tomcatEmbedVersion}"
     compile "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
 
     testCompile "junit:junit:${junitVersion}"
diff --git a/lib/java/gradle/functionalTests.gradle b/lib/java/gradle/functionalTests.gradle
index c420d12..6a388a6 100644
--- a/lib/java/gradle/functionalTests.gradle
+++ b/lib/java/gradle/functionalTests.gradle
@@ -34,6 +34,7 @@
             include '**/test/TestClient.java'
             include '**/test/TestServer.java'
             include '**/test/TestNonblockingServer.java'
+            include '**/test/TestTServletServer.java'
         }
     }
 }
@@ -55,7 +56,7 @@
 shadowJar {
     description = 'Assemble a test JAR file for cross-check execution'
     // make sure the runners are created when this runs
-    dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer'
+    dependsOn 'generateRunnerScriptForClient', 'generateRunnerScriptForServer', 'generateRunnerScriptForNonblockingServer', 'generateRunnerScriptForTServletServer'
 
     baseName = 'functionalTest'
     destinationDir = file("$buildDir/functionalTestJar")
@@ -153,3 +154,24 @@
         serverFile.setExecutable(true, false)
     }
 }
+
+task generateRunnerScriptForTServletServer(group: 'Build') {
+    description = 'Generate a runner script for cross-check tests with TestTServletServer'
+
+    def serverFile = file("$buildDir/runservletserver${scriptExt}")
+
+    def runServerText = """\
+${scriptHead}
+
+"${javaExe}" -cp "$jarPath" "-Djavax.net.ssl.keyStore=$keyStore" -Djavax.net.ssl.keyStorePassword=thrift org.apache.thrift.test.TestTServletServer $args
+"""
+
+    inputs.property 'runServerText', runServerText
+    outputs.file serverFile
+
+    doLast {
+        serverFile.parentFile.mkdirs()
+        serverFile.text = runServerText
+        serverFile.setExecutable(true, false)
+    }
+}
diff --git a/lib/java/gradle/sourceConfiguration.gradle b/lib/java/gradle/sourceConfiguration.gradle
index 8dd0331..3bd432a 100644
--- a/lib/java/gradle/sourceConfiguration.gradle
+++ b/lib/java/gradle/sourceConfiguration.gradle
@@ -34,6 +34,7 @@
             exclude '**/test/TestClient.java'
             exclude '**/test/TestServer.java'
             exclude '**/test/TestNonblockingServer.java'
+            exclude '**/test/TestTServletServer.java'
         }
         resources {
             srcDir 'test'
diff --git a/lib/java/test/org/apache/thrift/test/TestClient.java b/lib/java/test/org/apache/thrift/test/TestClient.java
index 84410ce..dbada08 100644
--- a/lib/java/test/org/apache/thrift/test/TestClient.java
+++ b/lib/java/test/org/apache/thrift/test/TestClient.java
@@ -27,6 +27,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.http.impl.client.HttpClients;
 import org.apache.thrift.TApplicationException;
 import org.apache.thrift.TException;
 import org.apache.thrift.TSerializer;
@@ -76,6 +77,7 @@
     String protocol_type = "binary";
     String transport_type = "buffered";
     boolean ssl = false;
+    boolean http_client = false;
 
     int socketTimeout = 1000;
 
@@ -99,6 +101,8 @@
           transport_type.trim();
         } else if (args[i].equals("--ssl")) {
           ssl = true;
+        } else if (args[i].equals("--client")) {
+          http_client = true;  
         } else if (args[i].equals("--help")) {
           System.out.println("Allowed options:");
           System.out.println("  --help\t\t\tProduce help message");
@@ -145,8 +149,13 @@
 
     try {
       if (transport_type.equals("http")) {
-        String url = "http://" + host + ":" + port + "/service";
-        transport = new THttpClient(url);
+        String url = "http://" + host + ":" + port + "/test/service";
+        if (http_client == true) {
+          
+          transport = new THttpClient(url, HttpClients.createDefault());
+        } else {
+          transport = new THttpClient(url);
+        }
       } else {
         TSocket socket = null;
         if (ssl == true) {
diff --git a/lib/java/test/org/apache/thrift/test/TestServlet.java b/lib/java/test/org/apache/thrift/test/TestServlet.java
new file mode 100644
index 0000000..e63109d
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/test/TestServlet.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.test;
+
+import org.apache.thrift.TProcessor;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.protocol.TProtocolFactory;
+import org.apache.thrift.server.ServerTestBase.TestHandler;
+import org.apache.thrift.server.TExtensibleServlet;
+
+import thrift.test.ThriftTest;
+
+@SuppressWarnings("serial")
+public class TestServlet extends TExtensibleServlet {
+
+  @Override
+  protected TProtocolFactory getInProtocolFactory(){
+      TProtocolFactory tProtocolFactory = new TCompactProtocol.Factory();
+      return tProtocolFactory;
+  }
+
+  @Override
+  protected TProtocolFactory getOutProtocolFactory(){
+      TProtocolFactory tProtocolFactory = new TCompactProtocol.Factory();
+      return tProtocolFactory;
+  }
+
+  @SuppressWarnings({"rawtypes", "unchecked"})
+  @Override
+  protected TProcessor getProcessor(){
+      TestHandler testHandler = new TestHandler();
+      ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler);
+      return testProcessor;
+  }
+}
diff --git a/lib/java/test/org/apache/thrift/test/TestTServletServer.java b/lib/java/test/org/apache/thrift/test/TestTServletServer.java
new file mode 100644
index 0000000..93e7944
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/test/TestTServletServer.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift.test;
+
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.Tomcat.FixContextListener;
+
+
+/**
+ * run tomcat for test TServlet
+ */
+public class TestTServletServer {
+
+  static final int port = 9090;
+
+  public static void main(String [] args) throws Exception{
+    Tomcat tomcat = new Tomcat();
+    tomcat.setPort( port );
+    tomcat.setBaseDir(System.getProperty("user.dir")+"\\build");
+    tomcat.getHost().setAutoDeploy( false );
+
+    String contextPath = "/test";
+    StandardContext context = new StandardContext();
+    context.setPath( contextPath );
+    context.addLifecycleListener( new FixContextListener() );
+    tomcat.getHost().addChild( context );
+
+    tomcat.addServlet( contextPath, "testServlet", new TestServlet() );
+    context.addServletMappingDecoded( "/service", "testServlet");
+    tomcat.start();
+    tomcat.getServer().await();
+  }
+  
+}