THRIFT-3542 Add length limit support to Java test server

This closes #788
diff --git a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
index 4e93606..d7f8b83 100644
--- a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -71,6 +71,10 @@
       this(strictRead, strictWrite, NO_LENGTH_LIMIT, NO_LENGTH_LIMIT);
     }
 
+    public Factory(long stringLengthLimit, long containerLengthLimit) {
+      this(false, true, stringLengthLimit, containerLengthLimit);
+    }
+
     public Factory(boolean strictRead, boolean strictWrite, long stringLengthLimit, long containerLengthLimit) {
       stringLengthLimit_ = stringLengthLimit;
       containerLengthLimit_ = containerLengthLimit;
@@ -94,6 +98,10 @@
     this(trans, NO_LENGTH_LIMIT, NO_LENGTH_LIMIT, strictRead, strictWrite);
   }
 
+  public TBinaryProtocol(TTransport trans, long stringLengthLimit, long containerLengthLimit) {
+    this(trans, stringLengthLimit, containerLengthLimit, false, true);
+  }
+
   public TBinaryProtocol(TTransport trans, long stringLengthLimit, long containerLengthLimit, boolean strictRead, boolean strictWrite) {
     super(trans);
     stringLengthLimit_ = stringLengthLimit;
@@ -350,10 +358,6 @@
     int size = readI32();
 
     checkStringReadLength(size);
-    if (stringLengthLimit_ > 0 && size > stringLengthLimit_) {
-      throw new TProtocolException(TProtocolException.SIZE_LIMIT,
-                                   "String field exceeded string size limit");
-    }
 
     if (trans_.getBytesRemainingInBuffer() >= size) {
       try {
@@ -381,10 +385,7 @@
   public ByteBuffer readBinary() throws TException {
     int size = readI32();
 
-    if (stringLengthLimit_ > 0 && size > stringLengthLimit_) {
-      throw new TProtocolException(TProtocolException.SIZE_LIMIT,
-                                   "Binary field exceeded string size limit");
-    }
+    checkStringReadLength(size);
 
     if (trans_.getBytesRemainingInBuffer() >= size) {
       ByteBuffer bb = ByteBuffer.wrap(trans_.getBuffer(), trans_.getBufferPosition(), size);
diff --git a/lib/java/test/org/apache/thrift/test/TestServer.java b/lib/java/test/org/apache/thrift/test/TestServer.java
index ee0866e..14cd2ab 100644
--- a/lib/java/test/org/apache/thrift/test/TestServer.java
+++ b/lib/java/test/org/apache/thrift/test/TestServer.java
@@ -111,6 +111,8 @@
       String protocol_type = "binary";
       String server_type = "thread-pool";
       String domain_socket = "";
+      int string_limit = -1;
+      int container_limit = -1;
       try {
         for (int i = 0; i < args.length; i++) {
           if (args[i].startsWith("--port")) {
@@ -128,6 +130,10 @@
             transport_type.trim();
           } else if (args[i].equals("--ssl")) {
             ssl = true;
+          } else if (args[i].startsWith("--string-limit")) {
+            string_limit = Integer.valueOf(args[i].split("=")[1]);
+          } else if (args[i].startsWith("--container-limit")) {
+            container_limit = Integer.valueOf(args[i].split("=")[1]);
           } else if (args[i].equals("--help")) {
             System.out.println("Allowed options:");
             System.out.println("  --help\t\t\tProduce help message");
@@ -136,6 +142,8 @@
             System.out.println("  --protocol=arg (=" + protocol_type + ")\tProtocol: binary, json, compact");
             System.out.println("  --ssl\t\t\tEncrypted Transport using SSL");
             System.out.println("  --server-type=arg (=" + server_type +")\n\t\t\t\tType of server: simple, thread-pool, nonblocking, threaded-selector");
+            System.out.println("  --string-limit=arg (=" + string_limit + ")\tString read length limit");
+            System.out.println("  --container-limit=arg (=" + container_limit + ")\tContainer read length limit");
             System.exit(0);
           }
         }
@@ -186,9 +194,9 @@
       if (protocol_type.equals("json")) {
         tProtocolFactory = new TJSONProtocol.Factory();
       } else if (protocol_type.equals("compact")) {
-        tProtocolFactory = new TCompactProtocol.Factory();
+        tProtocolFactory = new TCompactProtocol.Factory(string_limit, container_limit);
       } else {
-        tProtocolFactory = new TBinaryProtocol.Factory();
+        tProtocolFactory = new TBinaryProtocol.Factory(string_limit, container_limit);
       }
 
       TTransportFactory tTransportFactory = null;
diff --git a/test/features/known_failures_Linux.json b/test/features/known_failures_Linux.json
index 7a51083..9bf600d 100644
--- a/test/features/known_failures_Linux.json
+++ b/test/features/known_failures_Linux.json
@@ -17,10 +17,6 @@
   "hs-limit_container_length_compact_buffered-ip",
   "hs-limit_string_length_binary_buffered-ip",
   "hs-limit_string_length_compact_buffered-ip",
-  "java-limit_container_length_binary_buffered-ip",
-  "java-limit_container_length_compact_buffered-ip",
-  "java-limit_string_length_binary_buffered-ip",
-  "java-limit_string_length_compact_buffered-ip",
   "nodejs-limit_container_length_binary_buffered-ip",
   "nodejs-limit_container_length_compact_buffered-ip",
   "nodejs-limit_string_length_binary_buffered-ip",