You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2010/04/02 12:03:45 UTC

svn commit: r930221 - in /httpcomponents/httpclient/trunk/httpclient/src: main/java/org/apache/http/impl/client/RedirectLocations.java test/java/org/apache/http/impl/client/TestRedirectLocation.java

Author: olegk
Date: Fri Apr  2 10:03:44 2010
New Revision: 930221

URL: http://svn.apache.org/viewvc?rev=930221&view=rev
Log:
HTTPCLIENT-928: RedirectLocations to maintain a list of all redirect URIs

Added:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java
Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java?rev=930221&r1=930220&r2=930221&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java Fri Apr  2 10:03:44 2010
@@ -28,13 +28,16 @@
 package org.apache.http.impl.client;
 
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.http.annotation.NotThreadSafe;
 
 /**
- * A collection of URIs that were used as redirects.
+ * This class represents a collection of {@link URI}s used as redirect locations.
  *
  * @since 4.0
  */
@@ -42,31 +45,55 @@ import org.apache.http.annotation.NotThr
 public class RedirectLocations {
 
     private final Set<URI> uris;
+    private final List<URI> log;
     
     public RedirectLocations() {
         super();
         this.uris = new HashSet<URI>();
+        this.log = new ArrayList<URI>();
     }
     
     /**
-     * Returns true if this collection contains the given URI.
+     * Test if the URI is present in the collection.
      */
     public boolean contains(final URI uri) {
         return this.uris.contains(uri);
     }
     
     /**
-     * Adds a new URI to the list of redirects.
+     * Adds a new URI to the collection.
      */
     public void add(final URI uri) {
         this.uris.add(uri);
+        this.log.add(uri);
     }
 
     /**
-     * Removes a URI from the list of redirects.
+     * Removes a URI from the collection.
      */
     public boolean remove(final URI uri) {
-        return this.uris.remove(uri);
+        boolean removed = this.uris.remove(uri);
+        if (removed) {
+            Iterator<URI> it = this.log.iterator();
+            while (it.hasNext()) {
+                URI current = it.next();
+                if (current.equals(uri)) {
+                    it.remove();
+                }
+            }
+        }
+        return removed;
+    }
+    
+    /**
+     * Returns all redirect {@Link URI}s in the order they were added to the collection.
+     * 
+     * @return list of all URIs
+     * 
+     * @since 4.1
+     */
+    public List<URI> getAll() {
+        return new ArrayList<URI>(this.log);
     }
 
 }

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java?rev=930221&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java Fri Apr  2 10:03:44 2010
@@ -0,0 +1,97 @@
+/*
+ * ====================================================================
+ *
+ *  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.impl.client;
+
+import java.net.URI;
+import java.util.List;
+
+import junit.framework.Assert;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ *  Simple tests for {@link RedirectLocations}.
+ */
+public class TestRedirectLocation extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestRedirectLocation(final String testName) {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestRedirectLocation.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestRedirectLocation.class);
+    }
+
+    public void testBasics() throws Exception {
+        RedirectLocations locations = new RedirectLocations();
+        
+        URI uri1 = new URI("/this");
+        URI uri2 = new URI("/that");
+        URI uri3 = new URI("/this-and-that");
+        
+        locations.add(uri1);
+        locations.add(uri2);
+        locations.add(uri2);
+        locations.add(uri3);
+        locations.add(uri3);
+        
+        Assert.assertTrue(locations.contains(uri1));
+        Assert.assertTrue(locations.contains(uri2));
+        Assert.assertTrue(locations.contains(uri3));
+        Assert.assertFalse(locations.contains(new URI("/")));
+        
+        List<URI> list = locations.getAll();
+        Assert.assertNotNull(list);
+        Assert.assertEquals(5, list.size());
+        Assert.assertEquals(uri1, list.get(0));
+        Assert.assertEquals(uri2, list.get(1));
+        Assert.assertEquals(uri2, list.get(2));
+        Assert.assertEquals(uri3, list.get(3));
+        Assert.assertEquals(uri3, list.get(4));
+        
+        Assert.assertTrue(locations.remove(uri3));
+        Assert.assertTrue(locations.remove(uri1));
+        Assert.assertFalse(locations.remove(new URI("/")));
+
+        list = locations.getAll();
+        Assert.assertNotNull(list);
+        Assert.assertEquals(2, list.size());
+        Assert.assertEquals(uri2, list.get(0));
+        Assert.assertEquals(uri2, list.get(1));
+    }
+
+}