1 /* 2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/test/org/apache/http/entity/TestByteArrayEntity.java $ 3 * $Revision: 385310 $ 4 * $Date: 2006-03-12 17:28:43 +0100 (Sun, 12 Mar 2006) $ 5 * 6 * ==================================================================== 7 * 8 * Copyright 1999-2006 The Apache Software Foundation 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 * ==================================================================== 22 * 23 * This software consists of voluntary contributions made by many 24 * individuals on behalf of the Apache Software Foundation. For more 25 * information on the Apache Software Foundation, please see 26 * <http://www.apache.org/>. 27 * 28 */ 29 30 package org.apache.http.entity; 31 32 import java.io.ByteArrayOutputStream; 33 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 import org.apache.http.protocol.HTTP; 39 40 /** 41 * Unit tests for {@link ByteArrayEntity}. 42 * 43 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 44 */ 45 public class TestByteArrayEntity extends TestCase { 46 47 public TestByteArrayEntity(String testName) { 48 super(testName); 49 } 50 51 public static void main(String args[]) { 52 String[] testCaseName = { TestByteArrayEntity.class.getName() }; 53 junit.textui.TestRunner.main(testCaseName); 54 } 55 56 public static Test suite() { 57 return new TestSuite(TestByteArrayEntity.class); 58 } 59 60 public void testBasics() throws Exception { 61 byte[] bytes = "Message content".getBytes(HTTP.US_ASCII); 62 ByteArrayEntity httpentity = new ByteArrayEntity(bytes); 63 64 assertEquals(bytes.length, httpentity.getContentLength()); 65 assertNotNull(httpentity.getContent()); 66 assertTrue(httpentity.isRepeatable()); 67 assertFalse(httpentity.isStreaming()); 68 } 69 70 public void testIllegalConstructor() throws Exception { 71 try { 72 new ByteArrayEntity(null); 73 fail("IllegalArgumentException should have been thrown"); 74 } catch (IllegalArgumentException ex) { 75 // expected 76 } 77 } 78 79 public void testWriteTo() throws Exception { 80 byte[] bytes = "Message content".getBytes(HTTP.US_ASCII); 81 ByteArrayEntity httpentity = new ByteArrayEntity(bytes); 82 83 ByteArrayOutputStream out = new ByteArrayOutputStream(); 84 httpentity.writeTo(out); 85 byte[] bytes2 = out.toByteArray(); 86 assertNotNull(bytes2); 87 assertEquals(bytes.length, bytes2.length); 88 for (int i = 0; i < bytes.length; i++) { 89 assertEquals(bytes[i], bytes2[i]); 90 } 91 92 out = new ByteArrayOutputStream(); 93 httpentity.writeTo(out); 94 bytes2 = out.toByteArray(); 95 assertNotNull(bytes2); 96 assertEquals(bytes.length, bytes2.length); 97 for (int i = 0; i < bytes.length; i++) { 98 assertEquals(bytes[i], bytes2[i]); 99 } 100 101 try { 102 httpentity.writeTo(null); 103 fail("IllegalArgumentException should have been thrown"); 104 } catch (IllegalArgumentException ex) { 105 // expected 106 } 107 } 108 109 }