001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/entity/TestByteArrayEntity.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 ByteArrayEntity}.
044: *
045: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
046: */
047: public class TestByteArrayEntity extends TestCase {
048:
049: public TestByteArrayEntity(String testName) {
050: super (testName);
051: }
052:
053: public static void main(String args[]) {
054: String[] testCaseName = { TestByteArrayEntity.class.getName() };
055: junit.textui.TestRunner.main(testCaseName);
056: }
057:
058: public static Test suite() {
059: return new TestSuite(TestByteArrayEntity.class);
060: }
061:
062: public void testBasics() throws Exception {
063: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
064: ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
065:
066: assertEquals(bytes.length, httpentity.getContentLength());
067: assertNotNull(httpentity.getContent());
068: assertTrue(httpentity.isRepeatable());
069: assertFalse(httpentity.isStreaming());
070: }
071:
072: public void testIllegalConstructor() throws Exception {
073: try {
074: new ByteArrayEntity(null);
075: fail("IllegalArgumentException should have been thrown");
076: } catch (IllegalArgumentException ex) {
077: // expected
078: }
079: }
080:
081: public void testWriteTo() throws Exception {
082: byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
083: ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
084:
085: ByteArrayOutputStream out = new ByteArrayOutputStream();
086: httpentity.writeTo(out);
087: byte[] bytes2 = out.toByteArray();
088: assertNotNull(bytes2);
089: assertEquals(bytes.length, bytes2.length);
090: for (int i = 0; i < bytes.length; i++) {
091: assertEquals(bytes[i], bytes2[i]);
092: }
093:
094: out = new ByteArrayOutputStream();
095: httpentity.writeTo(out);
096: bytes2 = out.toByteArray();
097: assertNotNull(bytes2);
098: assertEquals(bytes.length, bytes2.length);
099: for (int i = 0; i < bytes.length; i++) {
100: assertEquals(bytes[i], bytes2[i]);
101: }
102:
103: try {
104: httpentity.writeTo(null);
105: fail("IllegalArgumentException should have been thrown");
106: } catch (IllegalArgumentException ex) {
107: // expected
108: }
109: }
110:
111: }
|