001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/protocol/TestHttpExecutionContext.java $
003: * $Revision: 558111 $
004: * $Date: 2007-07-20 22:01:50 +0200 (Fri, 20 Jul 2007) $
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 junit.framework.*;
034:
035: // the name of this test is historic, the implementation classes of HttpContext
036: // have been renamed to BasicHttpContext and SyncBasicHttpContext
037: public class TestHttpExecutionContext extends TestCase {
038:
039: // ------------------------------------------------------------ Constructor
040: public TestHttpExecutionContext(String testName) {
041: super (testName);
042: }
043:
044: // ------------------------------------------------------------------- Main
045: public static void main(String args[]) {
046: String[] testCaseName = { TestHttpExecutionContext.class
047: .getName() };
048: junit.textui.TestRunner.main(testCaseName);
049: }
050:
051: // ------------------------------------------------------- TestCase Methods
052:
053: public static Test suite() {
054: return new TestSuite(TestHttpExecutionContext.class);
055: }
056:
057: public void testContextOperations() {
058: HttpContext parentContext = new SyncBasicHttpContext(null);
059: HttpContext currentContext = new SyncBasicHttpContext(
060: parentContext);
061:
062: parentContext.setAttribute("param1", "1");
063: parentContext.setAttribute("param2", "2");
064: currentContext.setAttribute("param3", "3");
065: currentContext.setAttribute("param2", "4");
066:
067: assertEquals("1", parentContext.getAttribute("param1"));
068: assertEquals("2", parentContext.getAttribute("param2"));
069: assertEquals(null, parentContext.getAttribute("param3"));
070:
071: assertEquals("1", currentContext.getAttribute("param1"));
072: assertEquals("4", currentContext.getAttribute("param2"));
073: assertEquals("3", currentContext.getAttribute("param3"));
074: assertEquals(null, currentContext.getAttribute("param4"));
075:
076: currentContext.removeAttribute("param1");
077: currentContext.removeAttribute("param2");
078: currentContext.removeAttribute("param3");
079: currentContext.removeAttribute("param4");
080:
081: assertEquals("1", currentContext.getAttribute("param1"));
082: assertEquals("2", currentContext.getAttribute("param2"));
083: assertEquals(null, currentContext.getAttribute("param3"));
084: assertEquals(null, currentContext.getAttribute("param4"));
085: }
086:
087: public void testEmptyContextOperations() {
088: HttpContext currentContext = new SyncBasicHttpContext(null);
089: assertEquals(null, currentContext.getAttribute("param1"));
090: currentContext.removeAttribute("param1");
091: assertEquals(null, currentContext.getAttribute("param1"));
092: }
093:
094: public void testContextInvalidInput() throws Exception {
095: HttpContext currentContext = new SyncBasicHttpContext(null);
096: try {
097: currentContext.setAttribute(null, null);
098: fail("IllegalArgumentException should have been thrown");
099: } catch (IllegalArgumentException ex) {
100: // expected
101: }
102: try {
103: currentContext.getAttribute(null);
104: fail("IllegalArgumentException should have been thrown");
105: } catch (IllegalArgumentException ex) {
106: // expected
107: }
108: try {
109: currentContext.removeAttribute(null);
110: fail("IllegalArgumentException should have been thrown");
111: } catch (IllegalArgumentException ex) {
112: // expected
113: }
114: }
115:
116: }
|