You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2012/07/03 19:55:10 UTC

svn commit: r1356852 - in /tomcat/trunk/test/org/apache/catalina/startup: BytesStreamer.java TomcatBaseTest.java

Author: fhanik
Date: Tue Jul  3 17:55:09 2012
New Revision: 1356852

URL: http://svn.apache.org/viewvc?rev=1356852&view=rev
Log:
With async and non block unit tests we need a way to stream data up to the client, byte streamer offers an easy way to do that

Added:
    tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java   (with props)
Modified:
    tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java

Added: tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java?rev=1356852&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java (added)
+++ tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java Tue Jul  3 17:55:09 2012
@@ -0,0 +1,45 @@
+/*
+ * 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.catalina.startup;
+
+/**
+ *
+ * Used by {@link TomcatBaseTest}
+ *
+ *
+ */
+public interface BytesStreamer {
+    /**
+     * Returns the length of the content about to be streamed.
+     * Return -1 if length is unknown and chunked encoding should be used
+     * @return the length if known - otherwise -1
+     */
+    int getLength();
+
+    /**
+     * return the number of bytes available in next chunk
+     * @return
+     */
+    int available();
+
+    /**
+     * returns the next byte to write.
+     * if {@link #available()} method returns >0
+     * @return
+     */
+    byte[] next();
+}

Propchange: tomcat/trunk/test/org/apache/catalina/startup/BytesStreamer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1356852&r1=1356851&r2=1356852&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Tue Jul  3 17:55:09 2012
@@ -285,9 +285,36 @@ public abstract class TomcatBaseTest ext
         return postUrl(body, path, out, null, resHead);
     }
 
-    public static int postUrl(byte[] body, String path, ByteChunk out,
+    public static int postUrl(final byte[] body, String path, ByteChunk out,
             Map<String, List<String>> reqHead,
             Map<String, List<String>> resHead) throws IOException {
+        BytesStreamer s = new BytesStreamer() {
+            boolean done = false;
+            @Override
+            public byte[] next() {
+                done = true;
+                return body;
+
+            }
+
+            @Override
+            public int getLength() {
+                return body.length;
+            }
+
+            @Override
+            public int available() {
+                if (done) return 0;
+                else return body.length;
+            }
+        };
+        return postUrl(false,s,path,out,reqHead,resHead);
+    }
+
+
+    public static int postUrl(boolean stream, BytesStreamer streamer, String path, ByteChunk out,
+                Map<String, List<String>> reqHead,
+                Map<String, List<String>> resHead) throws IOException {
 
         URL url = new URL(path);
         HttpURLConnection connection =
@@ -307,15 +334,26 @@ public abstract class TomcatBaseTest ext
                         valueList.toString());
             }
         }
+        if (streamer != null && stream) {
+            if (streamer.getLength()>0) {
+                connection.setFixedLengthStreamingMode(streamer.getLength());
+            } else {
+                connection.setChunkedStreamingMode(1024);
+            }
+        }
+
         connection.connect();
 
         // Write the request body
         OutputStream os = null;
         try {
             os = connection.getOutputStream();
-            if (body != null) {
-                os.write(body, 0, body.length);
+            while (streamer!=null && streamer.available()>0) {
+                byte[] next = streamer.next();
+                os.write(next);
+                os.flush();
             }
+
         } finally {
             if (os != null) {
                 try {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org