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
This commit is contained in:
Oleg Kalnichevski 2010-04-02 10:03:44 +00:00
parent 46a08fbedb
commit 680d449797
2 changed files with 129 additions and 5 deletions

View File

@ -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.NotThreadSafe;
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);
}
}

View File

@ -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));
}
}