hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r568036 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Tue, 21 Aug 2007 09:27:21 GMT
Author: rolandw
Date: Tue Aug 21 02:27:19 2007
New Revision: 568036

URL: http://svn.apache.org/viewvc?rev=568036&view=rev
Log:
removed static parsing code from BasicHeaderElement, adjusted test cases

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
      - copied, changed from r568005, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeader.java
Tue Aug 21 02:27:19 2007
@@ -132,7 +132,8 @@
      */
     public HeaderElement[] getElements() {
         if (this.value != null) {
-            return BasicHeaderElement.parseAll(this.value);
+            // result intentionally not cached, it's probably not used again
+            return BasicHeaderValueParser.parseElements(this.value, null);
         } else {
             return new HeaderElement[] {}; 
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
Tue Aug 21 02:27:19 2007
@@ -176,104 +176,6 @@
 
     // --------------------------------------------------------- Public Methods
 
-    /**
-     * This parses the value part of a header. The result is an array of
-     * HeaderElement objects.
-     *
-     * @param buffer    the buffer from which to parse
-     * @param indexFrom where to start parsing in the buffer
-     * @param indexTo   where to stop parsing in the buffer
-     *
-     * @return array of {@link HeaderElement}s.
-     */
-    public static final HeaderElement[] parseAll(
-            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
-        if (buffer == null) {
-            throw new IllegalArgumentException("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
-        }
-        List elements = new ArrayList(); 
-        int cur = indexFrom;
-        int from = indexFrom;
-        boolean qouted = false;
-        boolean escaped = false;
-        while (cur < indexTo) {
-            char ch = buffer.charAt(cur);
-            if (ch == '"' && !escaped) {
-                qouted = !qouted;
-            }
-            HeaderElement element = null;
-            if ((!qouted) && (ch == ',')) {
-                element = parse(buffer, from, cur);
-                from = cur + 1;
-            } else if (cur == indexTo - 1) {
-                element = parse(buffer, from, indexTo);
-            }
-            if (element != null && !(element.getName().length() == 0 && element.getValue()
== null)) {
-                elements.add(element);
-            }
-            if (escaped) {
-                escaped = false;
-            } else {
-                escaped = qouted && ch == '\\';
-            }
-            cur++;
-        }
-        return (HeaderElement[])
-            elements.toArray(new HeaderElement[elements.size()]);
-    }
-
-    /**
-     * This parses the value part of a header. The result is an array of
-     * HeaderElement objects.
-     *
-     * @param s  the string representation of the header value
-     *                     (as received from the web server).
-     * @return array of {@link HeaderElement}s.
-     */
-    public static final HeaderElement[] parseAll(final String s) {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
-        }
-        CharArrayBuffer buffer = new CharArrayBuffer(s.length()); 
-        buffer.append(s);
-        return parseAll(buffer, 0, buffer.length());
-    }
-
-    public static HeaderElement parse(
-            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
-        if (buffer == null) {
-            throw new IllegalArgumentException("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
-        }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
-        }
-        NameValuePair[] nvps = BasicNameValuePair.parseAll(buffer, indexFrom, indexTo);
-        return new BasicHeaderElement(nvps);
-    }
-
-    public static final HeaderElement parse(final String s) {
-        if (s == null) {
-            throw new IllegalArgumentException("String may not be null");
-        }
-        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
-        buffer.append(s);
-        return parse(buffer, 0, buffer.length());
-    }
 
     public static void format(
             final CharArrayBuffer buffer, 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
Tue Aug 21 02:27:19 2007
@@ -37,6 +37,7 @@
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
+import org.apache.http.ParseException;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -74,6 +75,33 @@
     // public default constructor
 
 
+    /**
+     * Parses elements with the given parser.
+     *
+     * @param value     the header value to parse
+     * @param parser    the parser to use, or <code>null</code> for default
+     *
+     * @return  array holding the header elements, never <code>null</code>
+     */
+    public final static
+        HeaderElement[] parseElements(String value,
+                                      HeaderValueParser parser)
+        throws ParseException {
+
+        if (value == null) {
+            throw new IllegalArgumentException
+                ("Value to parse may not be null.");
+        }
+
+        if (parser == null)
+            parser = BasicHeaderValueParser.DEFAULT;
+
+        CharArrayBuffer buffer = new CharArrayBuffer(value.length());
+        buffer.append(value);
+        return parser.parseElements(buffer, 0, buffer.length());
+    }
+
+
     // non-javadoc, see interface HeaderValueParser
     public HeaderElement[] parseElements(final CharArrayBuffer buffer,
                                          final int indexFrom,
@@ -127,13 +155,38 @@
 
 
     /**
-     * Parses a single header element.
-     * A header element consist of a semicolon-separate list
-     * of name=value definitions.
+     * Parses an element with the given parser.
+     *
+     * @param value     the header element to parse
+     * @param parser    the parser to use, or <code>null</code> for default
+     *
+     * @return  the parsed header element
      */
-    protected HeaderElement parseHeaderElement(final CharArrayBuffer buffer,
-                                               final int indexFrom,
-                                               final int indexTo) {
+    public final static
+        HeaderElement parseHeaderElement(String value,
+                                         HeaderValueParser parser)
+        throws ParseException {
+
+        if (value == null) {
+            throw new IllegalArgumentException
+                ("Value to parse may not be null.");
+        }
+
+        if (parser == null)
+            parser = BasicHeaderValueParser.DEFAULT;
+
+        CharArrayBuffer buffer = new CharArrayBuffer(value.length());
+        buffer.append(value);
+        return parser.parseHeaderElement(buffer, 0, buffer.length());
+    }
+
+
+
+
+    // non-javadoc, see interface HeaderValueParser
+    public HeaderElement parseHeaderElement(final CharArrayBuffer buffer,
+                                            final int indexFrom,
+                                            final int indexTo) {
 
         if (buffer == null) {
             throw new IllegalArgumentException
@@ -163,6 +216,33 @@
      */
     protected HeaderElement createHeaderElement(NameValuePair[] nvps) {
         return new BasicHeaderElement(nvps);
+    }
+
+
+    /**
+     * Parses parameters with the given parser.
+     *
+     * @param value     the parameter list to parse
+     * @param parser    the parser to use, or <code>null</code> for default
+     *
+     * @return  array holding the parameters, never <code>null</code>
+     */
+    public final static
+        NameValuePair[] parseParameters(String value,
+                                        HeaderValueParser parser)
+        throws ParseException {
+
+        if (value == null) {
+            throw new IllegalArgumentException
+                ("Value to parse may not be null.");
+        }
+
+        if (parser == null)
+            parser = BasicHeaderValueParser.DEFAULT;
+
+        CharArrayBuffer buffer = new CharArrayBuffer(value.length());
+        buffer.append(value);
+        return parser.parseParameters(buffer, 0, buffer.length());
     }
 
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
Tue Aug 21 02:27:19 2007
@@ -94,6 +94,24 @@
 
 
     /**
+     * Parses a single header element.
+     * A header element consist of a semicolon-separate list
+     * of name=value definitions.
+     *
+     * @param buffer    buffer holding the element to parse
+     *
+     * @return  the parsed element
+     *
+     * @throws ParseException        in case of a parse error
+     */
+    HeaderElement parseHeaderElement(CharArrayBuffer buffer,
+                                     int indexFrom,
+                                     int indexTo)
+        throws ParseException
+        ;
+
+
+    /**
      * Parses a list of name-value pairs.
      * These lists are used to specify parameters to a header element.
      * Parse errors are indicated as <code>RuntimeException</code>.

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java
Tue Aug 21 02:27:19 2007
@@ -43,6 +43,7 @@
         suite.addTest(TestNameValuePair.suite());
         suite.addTest(TestHeader.suite());
         suite.addTest(TestHeaderElement.suite());
+        suite.addTest(TestBasicHeaderValueParser.suite());
         suite.addTest(TestStatusLine.suite());
         suite.addTest(TestRequestLine.suite());
         suite.addTest(TestHttpVersion.suite());

Copied: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
(from r568005, jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java?p2=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java&p1=jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java&r1=568005&r2=568036&rev=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
Tue Aug 21 02:27:19 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHeaderElement.java,v
1.7 2004/02/22 18:08:49 olegk Exp $
+ * $Header$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -34,82 +34,44 @@
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
-import org.apache.http.message.BasicHeaderElement;
-import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.CharArrayBuffer;
 
 import junit.framework.*;
 
 /**
- * Simple tests for {@link HeaderElement}.
+ * Tests for header value parsing.
  *
  * @author Rodney Waldhoff
  * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
  * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
  * @author <a href="mailto:oleg at ural.ru">oleg Kalnichevski</a>
+ * @author and others
  * @version $Id$
  */
-public class TestHeaderElement extends TestCase {
+public class TestBasicHeaderValueParser extends TestCase {
 
     // ------------------------------------------------------------ Constructor
-    public TestHeaderElement(String testName) {
+    public TestBasicHeaderValueParser(String testName) {
         super(testName);
     }
 
     // ------------------------------------------------------------------- Main
     public static void main(String args[]) {
-        String[] testCaseName = { TestHeaderElement.class.getName() };
+        String[] testCaseName = { TestBasicHeaderValueParser.class.getName() };
         junit.textui.TestRunner.main(testCaseName);
     }
 
     // ------------------------------------------------------- TestCase Methods
 
     public static Test suite() {
-        return new TestSuite(TestHeaderElement.class);
-    }
-
-    public void testConstructor() throws Exception {
-        HeaderElement element = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param1", "value1"),
-                    new BasicNameValuePair("param2", "value2")
-                } );
-        assertEquals("name", element.getName());
-        assertEquals("value", element.getValue());
-        assertEquals(2, element.getParameters().length);
-        assertEquals("value1", element.getParameterByName("param1").getValue());
-        assertEquals("value2", element.getParameterByName("param2").getValue());
-    }
-
-    public void testConstructor2() throws Exception {
-        HeaderElement element = new BasicHeaderElement("name", "value");
-        assertEquals("name", element.getName());
-        assertEquals("value", element.getValue());
-        assertEquals(0, element.getParameters().length);
-    }
-
-    public void testCharArrayConstructor() throws Exception {
-        String s = "name = value; param1 = value1";
-        HeaderElement element = BasicHeaderElement.parse(s); 
-        assertEquals("name", element.getName());
-        assertEquals("value", element.getValue());
-        assertEquals(1, element.getParameters().length);
-        assertEquals("value1", element.getParameterByName("param1").getValue());
-    }
-    
-    public void testInvalidName() {
-        try {
-            new BasicHeaderElement(null, null, null); 
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
+        return new TestSuite(TestBasicHeaderValueParser.class);
     }
     
+
     public void testParseHeaderElements() throws Exception {
         String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
             "name5=value5, name6= ; name7 = value7; name8 = \" value8\"";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
+        HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         // there are 3 elements
         assertEquals(3,elements.length);
         // 1st element
@@ -142,7 +104,7 @@
     public void testParseEscaped() {
         String s = 
           "test1 =  \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\"";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(s);
+        HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
         assertEquals(3, elements.length);
         assertEquals("test1", elements[0].getName());
         assertEquals("\\\"stuff\\\"", elements[0].getValue());
@@ -154,19 +116,19 @@
 
     public void testFringeCase1() throws Exception {
         String headerValue = "name1 = value1,";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
+        HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 1, elements.length);
     }
 
     public void testFringeCase2() throws Exception {
         String headerValue = "name1 = value1, ";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
+        HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 1, elements.length);
     }
 
     public void testFringeCase3() throws Exception {
         String headerValue = ",, ,, ,";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
+        HeaderElement[] elements = BasicHeaderValueParser.parseElements(headerValue, null);
         assertEquals("Number of elements", 0, elements.length);
     }
 
@@ -174,198 +136,62 @@
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("name = value");
         try {
-            BasicHeaderElement.parseAll(null, 0, 0);
+            BasicHeaderValueParser.parseElements(null, null);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
         }
+
         try {
-            BasicHeaderElement.parseAll(null);
+            BasicHeaderValueParser.DEFAULT.parseElements(null, 0, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parseAll(buffer, -1, 0);
+            BasicHeaderValueParser.DEFAULT.parseElements(buffer, -1, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parseAll(buffer, 0, 1000);
+            BasicHeaderValueParser.DEFAULT.parseElements(buffer, 0, 1000);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parseAll(buffer, 2, 1);
+            BasicHeaderValueParser.DEFAULT.parseElements(buffer, 2, 1);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
+
+
         try {
-            BasicHeaderElement.parse(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(null);
+            BasicHeaderValueParser.DEFAULT.parseHeaderElement(null, 0, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parse(buffer, -1, 0);
+            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, -1, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parse(buffer, 0, 1000);
+            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, 0, 1000);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            BasicHeaderElement.parse(buffer, 2, 1);
+            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, 2, 1);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
     }
-    
-    public void testParamByName() throws Exception {
-        String s = "name = value; param1 = value1; param2 = value2";
-        HeaderElement element = BasicHeaderElement.parse(s); 
-        assertEquals("value1", element.getParameterByName("param1").getValue());
-        assertEquals("value2", element.getParameterByName("param2").getValue());
-        assertNull(element.getParameterByName("param3"));
-        try {
-            element.getParameterByName(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            //expected
-        }
-    }
 
-    public void testHashCode() {
-        HeaderElement element1 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param1", "value1"),
-                    new BasicNameValuePair("param2", "value2")
-                } );
-        HeaderElement element2 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param2", "value2"),
-                    new BasicNameValuePair("param1", "value1")
-                } );
-        HeaderElement element3 = new BasicHeaderElement("name", "value"); 
-        HeaderElement element4 = new BasicHeaderElement("name", "value"); 
-        HeaderElement element5 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param1", "value1"),
-                    new BasicNameValuePair("param2", "value2")
-                } );
-        assertTrue(element1.hashCode() != element2.hashCode());
-        assertTrue(element1.hashCode() != element3.hashCode());
-        assertTrue(element2.hashCode() != element3.hashCode());
-        assertTrue(element3.hashCode() == element4.hashCode());
-        assertTrue(element1.hashCode() == element5.hashCode());
-    }
-    
-    public void testEquals() {
-        HeaderElement element1 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param1", "value1"),
-                    new BasicNameValuePair("param2", "value2")
-                } );
-        HeaderElement element2 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param2", "value2"),
-                    new BasicNameValuePair("param1", "value1")
-                } );
-        HeaderElement element3 = new BasicHeaderElement("name", "value"); 
-        HeaderElement element4 = new BasicHeaderElement("name", "value"); 
-        HeaderElement element5 = new BasicHeaderElement("name", "value", 
-                new NameValuePair[] {
-                    new BasicNameValuePair("param1", "value1"),
-                    new BasicNameValuePair("param2", "value2")
-                } );
-        assertTrue(element1.equals(element1));
-        assertTrue(!element1.equals(element2));
-        assertTrue(!element1.equals(element3));
-        assertTrue(!element2.equals(element3));
-        assertTrue(element3.equals(element4));
-        assertTrue(element1.equals(element5));
-        assertFalse(element1.equals(null));
-        assertFalse(element1.equals("name = value; param1 = value1; param2 = value2"));
-    }
-    
-    public void testToString() {
-        String s = "name=value; param1=value1; param2=value2";
-        HeaderElement element = BasicHeaderElement.parse(s);
-        assertEquals(s, element.toString());
-        s = "name; param1=value1; param2=value2";
-        element = BasicHeaderElement.parse(s);
-        assertEquals(s, element.toString());
-    }
-    
-    public void testElementFormatting() throws Exception {
-        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
-        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
-        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
-        NameValuePair param4 = new BasicNameValuePair("param", null);
-        NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4};
-        HeaderElement element = new BasicHeaderElement("name", "value", params); 
-        
-        assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\";
param", 
-                BasicHeaderElement.format(element));
-    }
-    
-    public void testElementArrayFormatting() throws Exception {
-        NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); 
-        NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); 
-        NameValuePair param3 = new BasicNameValuePair("param", "this,that");
-        NameValuePair param4 = new BasicNameValuePair("param", null);
-        HeaderElement element1 = new BasicHeaderElement("name1", "value1", new NameValuePair[]
{param1}); 
-        HeaderElement element2 = new BasicHeaderElement("name2", "value2", new NameValuePair[]
{param2}); 
-        HeaderElement element3 = new BasicHeaderElement("name3", "value3", new NameValuePair[]
{param3}); 
-        HeaderElement element4 = new BasicHeaderElement("name4", "value4", new NameValuePair[]
{param4}); 
-        HeaderElement element5 = new BasicHeaderElement("name5", null); 
-        HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4,
element5}; 
-        
-        assertEquals("name1=value1; param=regular_stuff, name2=value2; " +
-                "param=\"this\\\\that\", name3=value3; param=\"this,that\", " +
-                "name4=value4; param, name5", 
-                BasicHeaderElement.formatAll(elements));
-    }
-    
-    public void testFormatInvalidInput() throws Exception {
-        try {
-            BasicHeaderElement.format(null, new BasicHeaderElement("name1", "value1"));
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.format(new CharArrayBuffer(10), (HeaderElement) null);
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.formatAll(null, new HeaderElement[] {new BasicHeaderElement("name1",
"value1")});
-            fail("IllegalArgumentException should habe been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.formatAll(new CharArrayBuffer(10), (HeaderElement[]) 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/TestHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java?rev=568036&r1=568035&r2=568036&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
Tue Aug 21 02:27:19 2007
@@ -68,7 +68,7 @@
         return new TestSuite(TestHeaderElement.class);
     }
 
-    public void testConstructor() throws Exception {
+    public void testConstructor3() throws Exception {
         HeaderElement element = new BasicHeaderElement("name", "value", 
                 new NameValuePair[] {
                     new BasicNameValuePair("param1", "value1"),
@@ -88,9 +88,11 @@
         assertEquals(0, element.getParameters().length);
     }
 
-    public void testCharArrayConstructor() throws Exception {
+
+    public void testConstructor1() throws Exception {
         String s = "name = value; param1 = value1";
-        HeaderElement element = BasicHeaderElement.parse(s); 
+        NameValuePair[] nvps = BasicHeaderValueParser.parseParameters(s, null);
+        HeaderElement element = new BasicHeaderElement(nvps);
         assertEquals("name", element.getName());
         assertEquals("value", element.getValue());
         assertEquals(1, element.getParameters().length);
@@ -105,139 +107,12 @@
             //expected
         }
     }
-    
-    public void testParseHeaderElements() throws Exception {
-        String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
-            "name5=value5, name6= ; name7 = value7; name8 = \" value8\"";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
-        // there are 3 elements
-        assertEquals(3,elements.length);
-        // 1st element
-        assertEquals("name1",elements[0].getName());
-        assertEquals("value1",elements[0].getValue());
-        // 1st element has 2 getParameters()
-        assertEquals(2,elements[0].getParameters().length);
-        assertEquals("name2",elements[0].getParameters()[0].getName());
-        assertEquals(null, elements[0].getParameters()[0].getValue());
-        assertEquals("name3",elements[0].getParameters()[1].getName());
-        assertEquals("value3",elements[0].getParameters()[1].getValue());
-        // 2nd element
-        assertEquals("name4",elements[1].getName());
-        assertEquals("value4",elements[1].getValue());
-        // 2nd element has 1 parameter
-        assertEquals(1,elements[1].getParameters().length);
-        assertEquals("name5",elements[1].getParameters()[0].getName());
-        assertEquals("value5",elements[1].getParameters()[0].getValue());
-        // 3rd element
-        assertEquals("name6",elements[2].getName());
-        assertEquals("",elements[2].getValue());
-        // 3rd element has 2 getParameters()
-        assertEquals(2,elements[2].getParameters().length);
-        assertEquals("name7",elements[2].getParameters()[0].getName());
-        assertEquals("value7",elements[2].getParameters()[0].getValue());
-        assertEquals("name8",elements[2].getParameters()[1].getName());
-        assertEquals(" value8",elements[2].getParameters()[1].getValue());
-    }
-
-    public void testParseEscaped() {
-        String s = 
-          "test1 =  \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\"";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(s);
-        assertEquals(3, elements.length);
-        assertEquals("test1", elements[0].getName());
-        assertEquals("\\\"stuff\\\"", elements[0].getValue());
-        assertEquals("test2", elements[1].getName());
-        assertEquals("\\\\", elements[1].getValue());
-        assertEquals("test3", elements[2].getName());
-        assertEquals("stuff, stuff", elements[2].getValue());
-    }
-
-    public void testFringeCase1() throws Exception {
-        String headerValue = "name1 = value1,";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
-        assertEquals("Number of elements", 1, elements.length);
-    }
-
-    public void testFringeCase2() throws Exception {
-        String headerValue = "name1 = value1, ";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
-        assertEquals("Number of elements", 1, elements.length);
-    }
-
-    public void testFringeCase3() throws Exception {
-        String headerValue = ",, ,, ,";
-        HeaderElement[] elements = BasicHeaderElement.parseAll(headerValue);
-        assertEquals("Number of elements", 0, elements.length);
-    }
-
-    public void testInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("name = value");
-        try {
-            BasicHeaderElement.parseAll(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parseAll(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parseAll(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parseAll(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parseAll(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderElement.parse(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
-    
+
     public void testParamByName() throws Exception {
-        String s = "name = value; param1 = value1; param2 = value2";
-        HeaderElement element = BasicHeaderElement.parse(s); 
+        CharArrayBuffer buffer = new CharArrayBuffer(64);
+        buffer.append("name = value; param1 = value1; param2 = value2");
+        HeaderElement element = BasicHeaderValueParser.DEFAULT
+            .parseHeaderElement(buffer, 0, buffer.length()); 
         assertEquals("value1", element.getParameterByName("param1").getValue());
         assertEquals("value2", element.getParameterByName("param2").getValue());
         assertNull(element.getParameterByName("param3"));
@@ -303,11 +178,21 @@
     }
     
     public void testToString() {
+        CharArrayBuffer buffer = new CharArrayBuffer(64);
         String s = "name=value; param1=value1; param2=value2";
-        HeaderElement element = BasicHeaderElement.parse(s);
+        buffer.append(s);
+
+
+        HeaderElement element = BasicHeaderValueParser.DEFAULT
+            .parseHeaderElement(buffer, 0, buffer.length());
         assertEquals(s, element.toString());
+
         s = "name; param1=value1; param2=value2";
-        element = BasicHeaderElement.parse(s);
+        buffer.clear();
+        buffer.append(s);
+
+        element = BasicHeaderValueParser.DEFAULT
+            .parseHeaderElement(buffer, 0, buffer.length());
         assertEquals(s, element.toString());
     }
     



Mime
View raw message