From 680d4497979a68d1ea643581c409bdab5dcee292 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 2 Apr 2010 10:03:44 +0000 Subject: [PATCH] HTTPCLIENT-928: RedirectLocations to maintain a list of all redirect URIs git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@930221 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/client/RedirectLocations.java | 37 ++++++- .../impl/client/TestRedirectLocation.java | 97 +++++++++++++++++++ 2 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java diff --git a/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java b/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java index dfaed4193..f842a9cf9 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/RedirectLocations.java @@ -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 @@ public class RedirectLocations { private final Set uris; + private final List log; public RedirectLocations() { super(); this.uris = new HashSet(); + this.log = new ArrayList(); } /** - * 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 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 getAll() { + return new ArrayList(this.log); } } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java b/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java new file mode 100644 index 000000000..5a728b8fd --- /dev/null +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestRedirectLocation.java @@ -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 + * . + * + */ + +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 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)); + } + +}