001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestStringEntity.java $
003: * $Revision: 496069 $
004: * $Date: 2007-01-14 13:03:05 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.entity;
033:
034: import java.io.ByteArrayOutputStream;
035:
036: import junit.framework.Test;
037: import junit.framework.TestCase;
038: import junit.framework.TestSuite;
039:
040: import org.apache.http.protocol.HTTP;
041:
042: /**
043: * Unit tests for {@link StringEntity}.
044: *
045: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
046: */
047: public class TestStringEntity extends TestCase {
048:
049: public TestStringEntity(String testName) {
050: super (testName);
051: }
052:
053: public static void main(String args[]) {
054: String[] testCaseName = { TestStringEntity.class.getName() };
055: junit.textui.TestRunner.main(testCaseName);
056: }
057:
058: public static Test suite() {
059: return new TestSuite(TestStringEntity.class);
060: }
061:
062: public void testBasics() throws Exception {
063: String s = "Message content";
064: StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
065:
066: byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
067: assertEquals(bytes.length, httpentity.getContentLength());
068: assertNotNull(httpentity.getContent());
069: assertTrue(httpentity.isRepeatable());
070: assertFalse(httpentity.isStreaming());
071: }
072:
073: public void testIllegalConstructor() throws Exception {
074: try {
075: new StringEntity(null);
076: fail("IllegalArgumentException should have been thrown");
077: } catch (IllegalArgumentException ex) {
078: // expected
079: }
080: }
081:
082: public void testDefaultContent() throws Exception {
083: String s = "Message content";
084: StringEntity httpentity = new StringEntity(s, HTTP.US_ASCII);
085: assertEquals("text/plain; charset=US-ASCII", httpentity
086: .getContentType().getValue());
087: httpentity = new StringEntity(s);
088: assertEquals("text/plain; charset=ISO-8859-1", httpentity
089: .getContentType().getValue());
090: }
091:
092: public void testWriteTo() throws Exception {
093: String s = "Message content";
094: byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
095: StringEntity httpentity = new StringEntity(s);
096:
097: ByteArrayOutputStream out = new ByteArrayOutputStream();
098: httpentity.writeTo(out);
099: byte[] bytes2 = out.toByteArray();
100: assertNotNull(bytes2);
101: assertEquals(bytes.length, bytes2.length);
102: for (int i = 0; i < bytes.length; i++) {
103: assertEquals(bytes[i], bytes2[i]);
104: }
105:
106: out = new ByteArrayOutputStream();
107: httpentity.writeTo(out);
108: bytes2 = out.toByteArray();
109: assertNotNull(bytes2);
110: assertEquals(bytes.length, bytes2.length);
111: for (int i = 0; i < bytes.length; i++) {
112: assertEquals(bytes[i], bytes2[i]);
113: }
114:
115: try {
116: httpentity.writeTo(null);
117: fail("IllegalArgumentException should have been thrown");
118: } catch (IllegalArgumentException ex) {
119: // expected
120: }
121: }
122:
123: }
|