001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/protocol/TestBasicHttpProcessor.java $
003: * $Revision: 612566 $
004: * $Date: 2008-01-16 21:48:27 +0100 (Wed, 16 Jan 2008) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.protocol;
032:
033: import java.io.IOException;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.HttpException;
040: import org.apache.http.HttpRequest;
041: import org.apache.http.HttpRequestInterceptor;
042:
043: /**
044: * @author Andrea Selva
045: */
046: public class TestBasicHttpProcessor extends TestCase {
047:
048: class TestHttpRequestInterceptorPlaceHolder implements
049: HttpRequestInterceptor {
050:
051: public void process(HttpRequest request, HttpContext context)
052: throws HttpException, IOException {
053: }
054: }
055:
056: // ------------------------------------------------------------ Constructor
057: public TestBasicHttpProcessor(String testName) {
058: super (testName);
059: }
060:
061: // ------------------------------------------------------------------- Main
062: public static void main(String args[]) {
063: String[] testCaseName = { TestHttpExecutionContext.class
064: .getName() };
065: junit.textui.TestRunner.main(testCaseName);
066: }
067:
068: // ------------------------------------------------------- TestCase Methods
069:
070: public static Test suite() {
071: return new TestSuite(TestHttpExecutionContext.class);
072: }
073:
074: public void testAddFirstRequestInterceptorNull() {
075: HttpRequestInterceptor itcp = null;
076: BasicHttpProcessor instance = new BasicHttpProcessor();
077:
078: instance.addRequestInterceptor(itcp, 0);
079: int itcpCount = instance.getRequestInterceptorCount();
080: assertEquals(0, itcpCount);
081: assertEquals(null, instance.getRequestInterceptor(0));
082: }
083:
084: public void testAddFirsRequestInterceptor() {
085: HttpRequestInterceptor itcp1 = new HttpRequestInterceptor() {
086:
087: public void process(HttpRequest request, HttpContext context)
088: throws HttpException, IOException {
089: }
090:
091: };
092: HttpRequestInterceptor itcp2 = new HttpRequestInterceptor() {
093:
094: public void process(HttpRequest request, HttpContext context)
095: throws HttpException, IOException {
096: }
097:
098: };
099: BasicHttpProcessor instance = new BasicHttpProcessor();
100:
101: instance.addRequestInterceptor(itcp1);
102: instance.addRequestInterceptor(itcp2, 0);
103: int itcpCount = instance.getRequestInterceptorCount();
104: assertEquals(2, itcpCount);
105: assertEquals(itcp2, instance.getRequestInterceptor(0));
106: }
107:
108: public void testAddTailRequestInterceptorNull() {
109: HttpRequestInterceptor itcp = null;
110: BasicHttpProcessor instance = new BasicHttpProcessor();
111:
112: instance.addRequestInterceptor(itcp, 0);
113: int itcpCount = instance.getRequestInterceptorCount();
114: assertEquals(0, itcpCount);
115: assertEquals(null, instance
116: .getRequestInterceptor(itcpCount - 1));
117: }
118:
119: public void testAddTailRequestInterceptor() {
120: HttpRequestInterceptor itcp1 = new HttpRequestInterceptor() {
121:
122: public void process(HttpRequest request, HttpContext context)
123: throws HttpException, IOException {
124: }
125:
126: };
127: HttpRequestInterceptor itcp2 = new HttpRequestInterceptor() {
128:
129: public void process(HttpRequest request, HttpContext context)
130: throws HttpException, IOException {
131: }
132:
133: };
134: BasicHttpProcessor instance = new BasicHttpProcessor();
135:
136: instance.addRequestInterceptor(itcp1);
137: instance.addRequestInterceptor(itcp2, Integer.MAX_VALUE);
138: int itcpCount = instance.getRequestInterceptorCount();
139: assertEquals(2, itcpCount);
140: assertEquals(itcp1, instance.getRequestInterceptor(0));
141: assertEquals(itcp2, instance
142: .getRequestInterceptor(itcpCount - 1));
143:
144: instance = new BasicHttpProcessor();
145: instance.addRequestInterceptor(itcp1, -1);
146: assertEquals(1, itcpCount);
147: assertEquals(itcp1, instance.getRequestInterceptor(0));
148: assertEquals(itcp1, instance
149: .getRequestInterceptor(itcpCount - 1));
150: }
151:
152: public void testAddRequestInterceptorMiddleIndex() {
153: HttpRequestInterceptor itcp1 = new TestHttpRequestInterceptorPlaceHolder();
154: BasicHttpProcessor instance = new BasicHttpProcessor();
155: instance.addRequestInterceptor(itcp1, 2);
156: }
157:
158: public void testClearByClass() {
159: // remove a present class
160: HttpRequestInterceptor itcp1 = new TestHttpRequestInterceptorPlaceHolder();
161: HttpRequestInterceptor itcp2 = new TestHttpRequestInterceptorPlaceHolder();
162: HttpRequestInterceptor itcp3 = new HttpRequestInterceptor() {
163:
164: public void process(HttpRequest request, HttpContext context)
165: throws HttpException, IOException {
166: }
167:
168: };
169: BasicHttpProcessor instance = new BasicHttpProcessor();
170: instance.addRequestInterceptor(itcp1);
171: instance.addRequestInterceptor(itcp2);
172: instance.addRequestInterceptor(itcp3);
173: instance.removeRequestInterceptorByClass(itcp1.getClass());
174: assertEquals(1, instance.getRequestInterceptorCount());
175: instance.removeRequestInterceptorByClass(itcp3.getClass());
176: assertEquals(0, instance.getRequestInterceptorCount());
177:
178: // remove a not present class
179: instance.addRequestInterceptor(itcp1);
180: instance.addRequestInterceptor(itcp2);
181: instance.removeRequestInterceptorByClass(Integer.class);
182: assertEquals(2, instance.getRequestInterceptorCount());
183: }
184:
185: }
|