001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicLineFormatter.java $
003: * $Revision: 571954 $
004: * $Date: 2007-09-02 13:05:21 +0200 (Sun, 02 Sep 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.message;
032:
033: import junit.framework.Test;
034: import junit.framework.TestCase;
035: import junit.framework.TestSuite;
036:
037: import org.apache.http.Header;
038: import org.apache.http.HttpStatus;
039: import org.apache.http.HttpVersion;
040: import org.apache.http.RequestLine;
041: import org.apache.http.StatusLine;
042: import org.apache.http.util.CharArrayBuffer;
043:
044: /**
045: * Tests for {@link BasicLineFormatter}.
046: *
047: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
048: *
049: * @version $Revision: 571954 $
050: */
051: public class TestBasicLineFormatter extends TestCase {
052:
053: // ------------------------------------------------------------ Constructor
054: public TestBasicLineFormatter(String testName) {
055: super (testName);
056: }
057:
058: // ------------------------------------------------------------------- Main
059: public static void main(String args[]) {
060: String[] testCaseName = { TestBasicLineFormatter.class
061: .getName() };
062: junit.textui.TestRunner.main(testCaseName);
063: }
064:
065: // ------------------------------------------------------- TestCase Methods
066:
067: public static Test suite() {
068: return new TestSuite(TestBasicLineFormatter.class);
069: }
070:
071: public void testHttpVersionFormatting() throws Exception {
072: String s = BasicLineFormatter.formatProtocolVersion(
073: HttpVersion.HTTP_1_1, null);
074: assertEquals("HTTP/1.1", s);
075: }
076:
077: public void testHttpVersionFormattingInvalidInput()
078: throws Exception {
079: try {
080: BasicLineFormatter.formatProtocolVersion(null,
081: BasicLineFormatter.DEFAULT);
082: fail("IllegalArgumentException should habe been thrown");
083: } catch (IllegalArgumentException ex) {
084: // expected
085: }
086: try {
087: BasicLineFormatter.DEFAULT.appendProtocolVersion(
088: new CharArrayBuffer(10), (HttpVersion) null);
089: fail("IllegalArgumentException should habe been thrown");
090: } catch (IllegalArgumentException ex) {
091: // expected
092: }
093: }
094:
095: public void testRLFormatting() throws Exception {
096: RequestLine requestline = new BasicRequestLine("GET", "/stuff",
097: HttpVersion.HTTP_1_1);
098: String s = BasicLineFormatter.formatRequestLine(requestline,
099: null);
100: assertEquals("GET /stuff HTTP/1.1", s);
101: }
102:
103: public void testRLFormattingInvalidInput() throws Exception {
104: try {
105: BasicLineFormatter.formatRequestLine(null,
106: BasicLineFormatter.DEFAULT);
107: fail("IllegalArgumentException should habe been thrown");
108: } catch (IllegalArgumentException ex) {
109: // expected
110: }
111: try {
112: BasicLineFormatter.DEFAULT.formatRequestLine(
113: new CharArrayBuffer(10), (RequestLine) null);
114: fail("IllegalArgumentException should habe been thrown");
115: } catch (IllegalArgumentException ex) {
116: // expected
117: }
118: }
119:
120: public void testSLFormatting() throws Exception {
121: StatusLine statusline = new BasicStatusLine(
122: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
123: String s = BasicLineFormatter
124: .formatStatusLine(statusline, null);
125: assertEquals("HTTP/1.1 200 OK", s);
126: statusline = new BasicStatusLine(HttpVersion.HTTP_1_1,
127: HttpStatus.SC_OK, null);
128: s = BasicLineFormatter.formatStatusLine(statusline, null);
129: assertEquals("HTTP/1.1 200 ", s);
130: // see "testSLParseSuccess" in TestBasicLineParser:
131: // trailing space is correct
132: }
133:
134: public void testSLFormattingInvalidInput() throws Exception {
135: try {
136: BasicLineFormatter.formatStatusLine(null,
137: BasicLineFormatter.DEFAULT);
138: fail("IllegalArgumentException should habe been thrown");
139: } catch (IllegalArgumentException ex) {
140: // expected
141: }
142: try {
143: BasicLineFormatter.DEFAULT.formatStatusLine(
144: new CharArrayBuffer(10), (StatusLine) null);
145: fail("IllegalArgumentException should habe been thrown");
146: } catch (IllegalArgumentException ex) {
147: // expected
148: }
149: }
150:
151: public void testHeaderFormatting() throws Exception {
152: Header header1 = new BasicHeader("name", "value");
153: String s = BasicLineFormatter.formatHeader(header1, null);
154: assertEquals("name: value", s);
155: Header header2 = new BasicHeader("name", null);
156: s = BasicLineFormatter.formatHeader(header2, null);
157: assertEquals("name: ", s);
158: }
159:
160: public void testHeaderFormattingInvalidInput() throws Exception {
161: try {
162: BasicLineFormatter.formatHeader(null,
163: BasicLineFormatter.DEFAULT);
164: fail("IllegalArgumentException should habe been thrown");
165: } catch (IllegalArgumentException ex) {
166: // expected
167: }
168: try {
169: BasicLineFormatter.DEFAULT.formatHeader(
170: new CharArrayBuffer(10), (Header) null);
171: fail("IllegalArgumentException should habe been thrown");
172: } catch (IllegalArgumentException ex) {
173: // expected
174: }
175: }
176:
177: }
|