hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r569681 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Sat, 25 Aug 2007 14:42:23 GMT
Author: rolandw
Date: Sat Aug 25 07:42:22 2007
New Revision: 569681

URL: http://svn.apache.org/viewvc?rev=569681&view=rev
Log:
removed static formatting from BasicStatusLine, adjusted test cases

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java?rev=569681&r1=569680&r2=569681&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicLineFormatter.java
Sat Aug 25 07:42:22 2007
@@ -119,6 +119,7 @@
 
         // can't use initBuffer, that would clear the argument!
         CharArrayBuffer result = buffer;
+        //@@@ guess the length in an extra method
         final int len = 8; // room for "HTTP/1.1"
         if (result == null) {
             result = new CharArrayBuffer(len);
@@ -146,12 +147,65 @@
 
 
 
+    /**
+     * Formats a status line.
+     *
+     * @param statline          the status line to format
+     * @param formatter         the formatter to use, or
+     *                          <code>null</code> for the
+     *                          {@link #DEFAULT default}
+     *
+     * @return  the formatted status line
+     */
+    public final static String formatStatusLine(final StatusLine statline,
+                                                LineFormatter formatter) {
+        if (formatter == null)
+            formatter = BasicLineFormatter.DEFAULT;
+        return formatter.formatStatusLine(null, statline).toString();
+    }
+
+
     // non-javadoc, see interface LineFormatter
     public CharArrayBuffer formatStatusLine(CharArrayBuffer buffer,
                                             StatusLine statline) {
+        if (statline == null) {
+            throw new IllegalArgumentException
+                ("Status line must not be null.");
+        }
+
         CharArrayBuffer result = initBuffer(buffer);
-        BasicStatusLine.format(result, statline); //@@@ move code here
+        doFormatStatusLine(result, statline);
+
         return result;
+    }
+
+
+    /**
+     * Actually formats a status line.
+     * Called from {@link #formatStatusLine}.
+     *
+     * @param buffer    the empty buffer into which to format,
+     *                  never <code>null</code>
+     * @param header    the status line to format, never <code>null</code>
+     */
+    protected void doFormatStatusLine(CharArrayBuffer buffer,
+                                      StatusLine statline) {
+        //@@@ let the protocol version length be guessed elsewhere
+        //@@@ guess the length in an extra method?
+        int len = 8 + 1 + 3 + 1; // room for "HTTP/1.1 200 "
+        final String reason = statline.getReasonPhrase();
+        if (reason != null) {
+            len += reason.length();
+        }
+        buffer.ensureCapacity(len);
+
+        appendProtocolVersion(buffer, statline.getHttpVersion());
+        buffer.append(' ');
+        buffer.append(Integer.toString(statline.getStatusCode()));
+        buffer.append(' '); // keep whitespace even if reason phrase is empty
+        if (reason != null) {
+            buffer.append(reason);
+        }
     }
 
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java?rev=569681&r1=569680&r2=569681&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicStatusLine.java
Sat Aug 25 07:42:22 2007
@@ -115,37 +115,8 @@
     }
 
     public String toString() {
-    	CharArrayBuffer buffer = new CharArrayBuffer(64);        
-        buffer.append(this.httpVersion);
-        buffer.append(' ');
-        buffer.append(Integer.toString(this.statusCode));
-        if (this.reasonPhrase != null && this.reasonPhrase.length() > 0) {
-            buffer.append(' ');
-            buffer.append(this.reasonPhrase);
-        }
-        return buffer.toString();
+        // no need for non-default formatting in toString()
+        return BasicLineFormatter.DEFAULT
+            .formatStatusLine(null, this).toString();
     }
-    
-    /*public@@@*/ static void format(final CharArrayBuffer buffer, final StatusLine statusline)
{
-        if (buffer == null) {
-            throw new IllegalArgumentException("String buffer may not be null");
-        }
-        if (statusline == null) {
-            throw new IllegalArgumentException("Status line may not be null");
-        }
-        BasicHttpVersionFormat.format(buffer, statusline.getHttpVersion());
-        buffer.append(' ');
-        buffer.append(Integer.toString(statusline.getStatusCode()));
-        buffer.append(' '); // keep whitespace even if reason phrase is empty
-        if (statusline.getReasonPhrase() != null) {
-            buffer.append(statusline.getReasonPhrase());
-        }
-    }
- 
-    /*public@@@*/ static String format(final StatusLine statusline) {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        format(buffer, statusline);
-        return buffer.toString();
-    }
-    
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java?rev=569681&r1=569680&r2=569681&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/LineFormatter.java
Sat Aug 25 07:42:22 2007
@@ -87,6 +87,8 @@
      *
      * @return  a buffer with the formatted protocol version appended.
      *          The caller is allowed to modify the result buffer.
+     *          If the <code>buffer</code> argument is not <code>null</code>,
+     *          the returned buffer is the argument buffer.
      */
     CharArrayBuffer appendProtocolVersion(CharArrayBuffer buffer,
                                           HttpVersion version)

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java?rev=569681&r1=569680&r2=569681&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java
Sat Aug 25 07:42:22 2007
@@ -30,9 +30,11 @@
 
 package org.apache.http.message;
 
-import org.apache.http.Header;
+import org.apache.http.HttpVersion;
+import org.apache.http.HttpStatus;
 import org.apache.http.RequestLine;
 import org.apache.http.StatusLine;
+import org.apache.http.Header;
 import org.apache.http.util.CharArrayBuffer;
 
 import junit.framework.*;
@@ -63,6 +65,34 @@
         return new TestSuite(TestBasicLineFormatter.class);
     }
 
+    
+    public void testSLFormatting() throws Exception {
+        StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
+        String s = BasicLineFormatter.formatStatusLine(statusline, null);
+        assertEquals("HTTP/1.1 200 OK", s);
+        statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
+        s = BasicLineFormatter.formatStatusLine(statusline, null);
+        assertEquals("HTTP/1.1 200 ", s);
+        // see "testSLParseSuccess" in TestBasicLineParser:
+        // trailing space is correct
+    }
+    
+    public void testSLFormattingInvalidInput() throws Exception {
+        try {
+            BasicLineFormatter.formatStatusLine
+                (null, BasicLineFormatter.DEFAULT);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            BasicLineFormatter.DEFAULT.formatStatusLine
+                (new CharArrayBuffer(10), (StatusLine) null);
+            fail("IllegalArgumentException should habe been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
 
 
     public void testHeaderFormatting() throws Exception {
@@ -84,7 +114,7 @@
         }
         try {
             BasicLineFormatter.DEFAULT.formatHeader
-                (new CharArrayBuffer(10), null);
+                (new CharArrayBuffer(10), (Header) null);
             fail("IllegalArgumentException should habe been thrown");
         } catch (IllegalArgumentException ex) {
             // expected

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java?rev=569681&r1=569680&r2=569681&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestStatusLine.java
Sat Aug 25 07:42:22 2007
@@ -92,32 +92,7 @@
         StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
         assertEquals("HTTP/1.1 200 OK", statusline.toString());
         statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
-        assertEquals("HTTP/1.1 200", statusline.toString());
+        // toString uses default formatting, hence the trailing space
+        assertEquals("HTTP/1.1 200 ", statusline.toString());
     }
-    
-    public void testFormatting() throws Exception {
-        StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK");
-        String s = BasicStatusLine.format(statusline);
-        assertEquals("HTTP/1.1 200 OK", s);
-        statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
-        s = BasicStatusLine.format(statusline);
-        assertEquals("HTTP/1.1 200 ", s);
-        // compare with "testParseSuccess" above: trailing space is correct
-    }
-    
-    public void testFormattingInvalidInput() throws Exception {
-        try {
-            BasicStatusLine.format(null, new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK,
"OK"));
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicStatusLine.format(new CharArrayBuffer(10), (StatusLine) null);
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-    }
-    
 }



Mime
View raw message