Source Code Cross Referenced for TestEntityUtils.java in  » Net » httpcomponents-core-4.0-beta1 » org » apache » http » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » httpcomponents core 4.0 beta1 » org.apache.http.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/util/TestEntityUtils.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.util;
033:
034:        import java.io.InputStream;
035:        import java.io.ByteArrayInputStream;
036:
037:        import org.apache.http.Header;
038:        import org.apache.http.entity.BasicHttpEntity;
039:        import org.apache.http.message.BasicHeader;
040:
041:        import junit.framework.Test;
042:        import junit.framework.TestCase;
043:        import junit.framework.TestSuite;
044:
045:        /**
046:         * Unit tests for {@link EntityUtils}.
047:         *
048:         * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
049:         */
050:        public class TestEntityUtils extends TestCase {
051:
052:            public TestEntityUtils(String testName) {
053:                super (testName);
054:            }
055:
056:            public static void main(String args[]) {
057:                String[] testCaseName = { TestEntityUtils.class.getName() };
058:                junit.textui.TestRunner.main(testCaseName);
059:            }
060:
061:            public static Test suite() {
062:                return new TestSuite(TestEntityUtils.class);
063:            }
064:
065:            public void testNullEntityToByteArray() throws Exception {
066:                try {
067:                    EntityUtils.toByteArray(null);
068:                    fail("IllegalArgumentException should have been thrown");
069:                } catch (IllegalArgumentException ex) {
070:                    // expected
071:                }
072:            }
073:
074:            public void testEmptyContentToByteArray() throws Exception {
075:                NullHttpEntity httpentity = new NullHttpEntity();
076:                byte[] bytes = EntityUtils.toByteArray(httpentity);
077:                assertNotNull(bytes);
078:                assertEquals(0, bytes.length);
079:            }
080:
081:            public void testMaxIntContentToByteArray() throws Exception {
082:                byte[] content = "Message content".getBytes("ISO-8859-1");
083:                BasicHttpEntity httpentity = new BasicHttpEntity();
084:                httpentity.setContent(new ByteArrayInputStream(content));
085:                httpentity.setContentLength(Integer.MAX_VALUE + 100L);
086:                try {
087:                    EntityUtils.toByteArray(httpentity);
088:                    fail("IllegalArgumentException should have been thrown");
089:                } catch (IllegalArgumentException ex) {
090:                    // expected
091:                }
092:            }
093:
094:            public void testUnknownLengthContentToByteArray() throws Exception {
095:                byte[] bytes = "Message content".getBytes("ISO-8859-1");
096:                BasicHttpEntity httpentity = new BasicHttpEntity();
097:                httpentity.setContent(new ByteArrayInputStream(bytes));
098:                httpentity.setContentLength(-1L);
099:                byte[] bytes2 = EntityUtils.toByteArray(httpentity);
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:
107:            public void testKnownLengthContentToByteArray() throws Exception {
108:                byte[] bytes = "Message content".getBytes("ISO-8859-1");
109:                BasicHttpEntity httpentity = new BasicHttpEntity();
110:                httpentity.setContent(new ByteArrayInputStream(bytes));
111:                httpentity.setContentLength(bytes.length);
112:                byte[] bytes2 = EntityUtils.toByteArray(httpentity);
113:                assertNotNull(bytes2);
114:                assertEquals(bytes.length, bytes2.length);
115:                for (int i = 0; i < bytes.length; i++) {
116:                    assertEquals(bytes[i], bytes2[i]);
117:                }
118:            }
119:
120:            public void testNullEntityGetContentCharset() throws Exception {
121:                try {
122:                    EntityUtils.getContentCharSet(null);
123:                    fail("IllegalArgumentException should have been thrown");
124:                } catch (IllegalArgumentException ex) {
125:                    // expected
126:                }
127:            }
128:
129:            public void testNullContentTypeGetContentCharset() throws Exception {
130:                BasicHttpEntity httpentity = new BasicHttpEntity();
131:                httpentity.setContentType((Header) null);
132:                assertNull(EntityUtils.getContentCharSet(httpentity));
133:            }
134:
135:            public void testNoCharsetGetContentCharset() throws Exception {
136:                BasicHttpEntity httpentity = new BasicHttpEntity();
137:                httpentity.setContentType(new BasicHeader("Content-Type",
138:                        "text/plain; param=yadayada"));
139:                assertNull(EntityUtils.getContentCharSet(httpentity));
140:            }
141:
142:            public void testGetContentCharset() throws Exception {
143:                BasicHttpEntity httpentity = new BasicHttpEntity();
144:                httpentity.setContentType(new BasicHeader("Content-Type",
145:                        "text/plain; charset = UTF-8"));
146:                assertEquals("UTF-8", EntityUtils.getContentCharSet(httpentity));
147:            }
148:
149:            public void testNullEntityToString() throws Exception {
150:                try {
151:                    EntityUtils.toString(null);
152:                    fail("IllegalArgumentException should have been thrown");
153:                } catch (IllegalArgumentException ex) {
154:                    // expected
155:                }
156:            }
157:
158:            public void testEmptyContentToString() throws Exception {
159:                NullHttpEntity httpentity = new NullHttpEntity();
160:                String s = EntityUtils.toString(httpentity);
161:                assertNotNull(s);
162:                assertEquals("", s);
163:            }
164:
165:            public void testMaxIntContentToString() throws Exception {
166:                byte[] content = "Message content".getBytes("ISO-8859-1");
167:                BasicHttpEntity httpentity = new BasicHttpEntity();
168:                httpentity.setContent(new ByteArrayInputStream(content));
169:                httpentity.setContentLength(Integer.MAX_VALUE + 100L);
170:                try {
171:                    EntityUtils.toString(httpentity);
172:                    fail("IllegalArgumentException should have been thrown");
173:                } catch (IllegalArgumentException ex) {
174:                    // expected
175:                }
176:            }
177:
178:            public void testUnknownLengthContentToString() throws Exception {
179:                byte[] bytes = "Message content".getBytes("ISO-8859-1");
180:                BasicHttpEntity httpentity = new BasicHttpEntity();
181:                httpentity.setContent(new ByteArrayInputStream(bytes));
182:                httpentity.setContentLength(-1L);
183:                String s = EntityUtils.toString(httpentity, "ISO-8859-1");
184:                assertEquals("Message content", s);
185:            }
186:
187:            public void testKnownLengthContentToString() throws Exception {
188:                byte[] bytes = "Message content".getBytes("ISO-8859-1");
189:                BasicHttpEntity httpentity = new BasicHttpEntity();
190:                httpentity.setContent(new ByteArrayInputStream(bytes));
191:                httpentity.setContentLength(bytes.length);
192:                String s = EntityUtils.toString(httpentity, "ISO-8859-1");
193:                assertEquals("Message content", s);
194:            }
195:
196:            static final int SWISS_GERMAN_HELLO[] = { 0x47, 0x72, 0xFC, 0x65,
197:                    0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
198:
199:            static final int RUSSIAN_HELLO[] = { 0x412, 0x441, 0x435, 0x43C,
200:                    0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
201:
202:            private static String constructString(int[] unicodeChars) {
203:                StringBuffer buffer = new StringBuffer();
204:                if (unicodeChars != null) {
205:                    for (int i = 0; i < unicodeChars.length; i++) {
206:                        buffer.append((char) unicodeChars[i]);
207:                    }
208:                }
209:                return buffer.toString();
210:            }
211:
212:            public void testNoCharsetContentToString() throws Exception {
213:                String content = constructString(SWISS_GERMAN_HELLO);
214:                byte[] bytes = content.getBytes("ISO-8859-1");
215:                BasicHttpEntity httpentity = new BasicHttpEntity();
216:                httpentity.setContent(new ByteArrayInputStream(bytes));
217:                httpentity.setContentType(new BasicHeader("Content-Type",
218:                        "text/plain"));
219:                String s = EntityUtils.toString(httpentity);
220:                assertEquals(content, s);
221:            }
222:
223:            public void testDefaultCharsetContentToString() throws Exception {
224:                String content = constructString(RUSSIAN_HELLO);
225:                byte[] bytes = content.getBytes("KOI8-R");
226:                BasicHttpEntity httpentity = new BasicHttpEntity();
227:                httpentity.setContent(new ByteArrayInputStream(bytes));
228:                httpentity.setContentType(new BasicHeader("Content-Type",
229:                        "text/plain"));
230:                String s = EntityUtils.toString(httpentity, "KOI8-R");
231:                assertEquals(content, s);
232:            }
233:
234:            public void testContentWithContentTypeToString() throws Exception {
235:                String content = constructString(RUSSIAN_HELLO);
236:                byte[] bytes = content.getBytes("UTF-8");
237:                BasicHttpEntity httpentity = new BasicHttpEntity();
238:                httpentity.setContent(new ByteArrayInputStream(bytes));
239:                httpentity.setContentType(new BasicHeader("Content-Type",
240:                        "text/plain; charset=UTF-8"));
241:                String s = EntityUtils.toString(httpentity, "ISO-8859-1");
242:                assertEquals(content, s);
243:            }
244:
245:            /**
246:             * Helper class that returns <code>null</code> as the content.
247:             */
248:            public static class NullHttpEntity extends BasicHttpEntity {
249:
250:                // default constructor
251:                /**
252:                 * Obtains no content.
253:                 * This method disables the state checks in the base class.
254:                 *
255:                 * @return <code>null</code>
256:                 */
257:                public InputStream getContent() {
258:                    return null;
259:                }
260:            } // class NullEntity
261:
262:        } // class TestEntityUtils
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.