hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r581981 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/ main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Thu, 04 Oct 2007 18:26:27 GMT
Author: rolandw
Date: Thu Oct  4 11:26:26 2007
New Revision: 581981

URL: http://svn.apache.org/viewvc?rev=581981&view=rev
Log:
BasicHeaderIterator and test cases

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
  (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestAllMessage.java

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java?rev=581981&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
Thu Oct  4 11:26:26 2007
@@ -0,0 +1,64 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http;
+
+
+import java.util.Iterator;
+
+
+/**
+ * A type-safe iterator for {@link Header Header} objects.
+ * 
+ * @version $Revision$
+ */
+public interface HeaderIterator extends Iterator {
+
+    /**
+     * Indicates whether there is another header in this iteration.
+     *
+     * @return  <code>true</code> if there is another header,
+     *          <code>false</code> otherwise
+     */
+    boolean hasNext()
+        ;
+
+
+    /**
+     * Obtains the next header from this iteration.
+     * This method should only be called while {@link #hasNext hasNext}
+     * is true.
+     *
+     * @return  the next header in this iteration
+     */
+    Header nextHeader()
+        ;
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HeaderIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java?rev=581981&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
Thu Oct  4 11:26:26 2007
@@ -0,0 +1,180 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.message;
+
+
+import java.util.NoSuchElementException;
+
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+
+
+/**
+ * Basic implementation of a {@link HeaderIterator}.
+ * 
+ * @version $Revision$
+ */
+public class BasicHeaderIterator implements HeaderIterator {
+
+    /**
+     * An array of headers to iterate over.
+     * Not all elements of this array are necessarily part of the iteration.
+     * This array will never be modified by the iterator.
+     * Derived implementations are expected to adhere to this restriction.
+     */
+    protected final Header[] allHeaders;
+
+
+    /**
+     * The position of the next header in {@link #allHeaders allHeaders}.
+     * Negative if the iteration is over.
+     */
+    protected int currentIndex;
+
+
+    /**
+     * The header name to filter by.
+     * <code>null</code> to iterate over all headers in the array.
+     */
+    protected String headerName;
+
+
+
+    /**
+     * Creates a new header iterator.
+     *
+     * @param headers   an array of headers over which to iterate
+     * @param name      the name of the headers over which to iterate, or
+     *                  <code>null</code> for any
+     */
+    public BasicHeaderIterator(Header[] headers, String name) {
+        if (headers == null) {
+            throw new IllegalArgumentException
+                ("Header array must not be null.");
+        }
+
+        this.allHeaders = headers;
+        this.headerName = name;
+        this.currentIndex = findNext(-1);
+    }
+
+
+    /**
+     * Determines the index of the next header.
+     *
+     * @param from      one less than the index to consider first,
+     *                  -1 to search for the first header
+     *
+     * @return  the index of the next header that matches the filter name,
+     *          or negative if there are no more headers
+     */
+    protected int findNext(int from) {
+        if (from < -1)
+            return -1;
+
+        final int to = this.allHeaders.length-1;
+        boolean found = false;
+        while (!found && (from < to)) {
+            from++;
+            found = filterHeader(from);
+        }
+        return found ? from : -1;
+    }
+
+
+    /**
+     * Checks whether a header is part of the iteration.
+     *
+     * @param index     the index of the header to check
+     *
+     * @return  <code>true</code> if the header should be part of the
+     *          iteration, <code>false</code> to skip
+     */
+    protected boolean filterHeader(int index) {
+        return (this.headerName == null) ||
+            this.headerName.equalsIgnoreCase(this.allHeaders[index].getName());
+    }
+
+
+    // non-javadoc, see interface HeaderIterator
+    public boolean hasNext() {
+        return (this.currentIndex >= 0);
+    }
+
+
+    /**
+     * Obtains the next header from this iteration.
+     *
+     * @return  the next header in this iteration
+     *
+     * @throws NoSuchElementException   if there are no more headers
+     */
+    public Header nextHeader()
+        throws NoSuchElementException {
+
+        final int current = this.currentIndex;
+        if (current < 0) {
+            throw new NoSuchElementException("Iteration already finished.");
+        }
+
+        this.currentIndex = findNext(current);
+
+        return this.allHeaders[current];
+    }
+
+
+    /**
+     * Returns the next header.
+     * Same as {@link #nextHeader nextHeader}, but not type-safe.
+     *
+     * @return  the next header in this iteration
+     *
+     * @throws NoSuchElementException   if there are no more headers
+     */
+    public final Object next()
+        throws NoSuchElementException {
+        return nextHeader();
+    }
+
+
+    /**
+     * Removing headers is not supported.
+     *
+     * @throws UnsupportedOperationException    always
+     */
+    public void remove()
+        throws UnsupportedOperationException {
+
+        throw new UnsupportedOperationException
+            ("Removing headers is not supported.");
+    }
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=581981&r1=581980&r2=581981&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
Thu Oct  4 11:26:26 2007
@@ -45,6 +45,7 @@
         suite.addTest(TestNameValuePair.suite());
         suite.addTest(TestHeader.suite());
         suite.addTest(TestHeaderElement.suite());
+        suite.addTest(TestBasicHeaderIterator.suite());
         suite.addTest(TestBasicHeaderValueParser.suite());
         suite.addTest(TestBasicHeaderValueFormatter.suite());
         suite.addTest(TestStatusLine.suite());

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java?rev=581981&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
Thu Oct  4 11:26:26 2007
@@ -0,0 +1,297 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.message;
+
+import java.util.NoSuchElementException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.Header;
+import org.apache.http.HeaderIterator;
+
+
+/**
+ * Tests for {@link BasicHeaderIterator}.
+ *
+ * @version $Revision$
+ */
+public class TestBasicHeaderIterator extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestBasicHeaderIterator(String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestBasicHeaderIterator.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestBasicHeaderIterator.class);
+    }
+
+
+
+    public void testAllSame() {
+        Header[] headers = new Header[]{
+            new BasicHeader("Name", "value0"),
+            new BasicHeader("nAme", "value1, value1.1"),
+            new BasicHeader("naMe", "value2=whatever"),
+            new BasicHeader("namE", "value3;tag=nil"),
+        };
+
+        // without filter
+        HeaderIterator hit = new BasicHeaderIterator(headers, null);
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // with filter
+        hit = new BasicHeaderIterator(headers, "name");
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertFalse(hit.hasNext());
+    }
+
+
+    public void testFirstLastOneNone() {
+        Header[] headers = new Header[]{
+            new BasicHeader("match"   , "value0"),
+            new BasicHeader("mismatch", "value1, value1.1"),
+            new BasicHeader("single"  , "value2=whatever"),
+            new BasicHeader("match"   , "value3;tag=nil"),
+        };
+
+        // without filter
+        HeaderIterator hit = new BasicHeaderIterator(headers, null);
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // with filter, first & last
+        hit = new BasicHeaderIterator(headers, "match");
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // with filter, one match
+        hit = new BasicHeaderIterator(headers, "single");
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // with filter, no match
+        hit = new BasicHeaderIterator(headers, "way-off");
+        assertFalse(hit.hasNext());
+    }
+
+
+    public void testInterspersed() {
+        Header[] headers = new Header[]{
+            new BasicHeader("yellow", "00"),
+            new BasicHeader("maroon", "01"),
+            new BasicHeader("orange", "02"),
+            new BasicHeader("orange", "03"),
+            new BasicHeader("orange", "04"),
+            new BasicHeader("yellow", "05"),
+            new BasicHeader("maroon", "06"),
+            new BasicHeader("maroon", "07"),
+            new BasicHeader("maroon", "08"),
+            new BasicHeader("yellow", "09"),
+            new BasicHeader("maroon", "0a"),
+            new BasicHeader("yellow", "0b"),
+            new BasicHeader("orange", "0c"),
+            new BasicHeader("yellow", "0d"),
+            new BasicHeader("orange", "0e"),
+        };
+
+        // without filter
+        HeaderIterator hit = new BasicHeaderIterator(headers, null);
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("4", headers[4], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("5", headers[5], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("6", headers[6], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("7", headers[7], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("8", headers[8], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("9", headers[9], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("a", headers[10], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("b", headers[11], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("c", headers[12], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("d", headers[13], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("e", headers[14], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // yellow 0, 5, 9, 11, 13
+        hit = new BasicHeaderIterator(headers, "Yellow");
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("5", headers[5], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("9", headers[9], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("b", headers[11], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("d", headers[13], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // maroon 1, 6, 7, 8, 10
+        hit = new BasicHeaderIterator(headers, "marOOn");
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("6", headers[6], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("7", headers[7], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("8", headers[8], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("a", headers[10], hit.nextHeader());
+        assertFalse(hit.hasNext());
+
+        // orange 2, 3, 4, 12, 14
+        hit = new BasicHeaderIterator(headers, "OranGe");
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("4", headers[4], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("b", headers[12], hit.nextHeader());
+        assertTrue(hit.hasNext());
+        assertEquals("e", headers[14], hit.nextHeader());
+        assertFalse(hit.hasNext());
+    }
+
+
+    public void testInvalid() {
+
+        HeaderIterator hit = null;
+        try {
+            hit = new BasicHeaderIterator(null, "whatever");
+            fail("null headers not detected");
+        } catch (IllegalArgumentException iax) {
+            // expected
+        }
+
+        // this is not invalid
+        hit = new BasicHeaderIterator(new Header[0], "whatever");
+        assertFalse(hit.hasNext());
+
+        // but this is
+        try {
+            hit.nextHeader();
+            fail("next beyond end not detected");
+        } catch (NoSuchElementException nsx) {
+            // expected
+        }
+    }
+
+
+    public void testRemaining() {
+        // to satisfy Clover and take coverage to 100%
+
+        Header[] headers = new Header[]{
+            new BasicHeader("Name", "value0"),
+            new BasicHeader("nAme", "value1, value1.1"),
+            new BasicHeader("naMe", "value2=whatever"),
+            new BasicHeader("namE", "value3;tag=nil"),
+        };
+
+        // without filter, using plain next()
+        HeaderIterator hit = new BasicHeaderIterator(headers, null);
+        assertTrue(hit.hasNext());
+        assertEquals("0", headers[0], hit.next());
+        assertTrue(hit.hasNext());
+        assertEquals("1", headers[1], hit.next());
+        assertTrue(hit.hasNext());
+        assertEquals("2", headers[2], hit.next());
+        assertTrue(hit.hasNext());
+        assertEquals("3", headers[3], hit.next());
+        assertFalse(hit.hasNext());
+
+        hit = new BasicHeaderIterator(headers, null);
+        assertTrue(hit.hasNext());
+        try {
+            hit.remove();
+            fail("remove not detected");
+        } catch (UnsupportedOperationException uox) {
+            // expected
+        }
+
+        assertTrue("no next", ((BasicHeaderIterator)hit).findNext(-3) < 0);
+    }
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderIterator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message