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