Source Code Cross Referenced for DiskFileItemSerializeTest.java in  » Net » apache-common-FileUpload » org » apache » commons » fileupload » 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 » apache common FileUpload » org.apache.commons.fileupload 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.fileupload;
018:
019:        import java.io.File;
020:        import java.io.IOException;
021:        import java.io.OutputStream;
022:        import java.io.ByteArrayInputStream;
023:        import java.io.ByteArrayOutputStream;
024:        import java.io.ObjectInputStream;
025:        import java.io.ObjectOutputStream;
026:        import java.util.Arrays;
027:
028:        import junit.framework.TestCase;
029:        import org.apache.commons.fileupload.disk.DiskFileItem;
030:        import org.apache.commons.fileupload.disk.DiskFileItemFactory;
031:
032:        /**
033:         * Serialization Unit tests for 
034:         *  {@link org.apache.commons.fileupload.disk.DiskFileItem}.
035:         */
036:        public class DiskFileItemSerializeTest extends TestCase {
037:
038:            /**
039:             * Content type for regular form items.
040:             */
041:            private static final String textContentType = "text/plain";
042:
043:            /**
044:             * Content type for file uploads.
045:             */
046:            private static final String fileContentType = "application/octet-stream";
047:
048:            /**
049:             * Very low threshold for testing memory versus disk options.
050:             */
051:            private static final int threshold = 16;
052:
053:            /**
054:             * Standard JUnit test case constructor.
055:             *
056:             * @param name The name of the test case.
057:             */
058:            public DiskFileItemSerializeTest(String name) {
059:                super (name);
060:            }
061:
062:            /**
063:             * Test creation of a field for which the amount of data falls below the
064:             * configured threshold.
065:             */
066:            public void testBelowThreshold() {
067:
068:                // Create the FileItem
069:                byte[] testFieldValueBytes = createContentBytes(threshold - 1);
070:                FileItem item = createFileItem(testFieldValueBytes);
071:
072:                // Check state is as expected
073:                assertTrue("Initial: in memory", item.isInMemory());
074:                assertEquals("Initial: size", item.getSize(),
075:                        testFieldValueBytes.length);
076:                compareBytes("Initial", item.get(), testFieldValueBytes);
077:
078:                // Serialize & Deserialize
079:                try {
080:                    FileItem newItem = (FileItem) serializeDeserialize(item);
081:
082:                    // Test deserialized content is as expected
083:                    assertTrue("Check in memory", newItem.isInMemory());
084:                    compareBytes("Check", testFieldValueBytes, newItem.get());
085:
086:                    // Compare FileItem's (except byte[])
087:                    compareFileItems(item, newItem);
088:
089:                } catch (Exception e) {
090:                    fail("Error Serializing/Deserializing: " + e);
091:                }
092:
093:            }
094:
095:            /**
096:             * Test creation of a field for which the amount of data equals the
097:             * configured threshold.
098:             */
099:            public void testThreshold() {
100:                // Create the FileItem
101:                byte[] testFieldValueBytes = createContentBytes(threshold);
102:                FileItem item = createFileItem(testFieldValueBytes);
103:
104:                // Check state is as expected
105:                assertTrue("Initial: in memory", item.isInMemory());
106:                assertEquals("Initial: size", item.getSize(),
107:                        testFieldValueBytes.length);
108:                compareBytes("Initial", item.get(), testFieldValueBytes);
109:
110:                // Serialize & Deserialize
111:                try {
112:                    FileItem newItem = (FileItem) serializeDeserialize(item);
113:
114:                    // Test deserialized content is as expected
115:                    assertTrue("Check in memory", newItem.isInMemory());
116:                    compareBytes("Check", testFieldValueBytes, newItem.get());
117:
118:                    // Compare FileItem's (except byte[])
119:                    compareFileItems(item, newItem);
120:
121:                } catch (Exception e) {
122:                    fail("Error Serializing/Deserializing: " + e);
123:                }
124:            }
125:
126:            /**
127:             * Test creation of a field for which the amount of data falls above the
128:             * configured threshold.
129:             */
130:            public void testAboveThreshold() {
131:
132:                // Create the FileItem
133:                byte[] testFieldValueBytes = createContentBytes(threshold + 1);
134:                FileItem item = createFileItem(testFieldValueBytes);
135:
136:                // Check state is as expected
137:                assertFalse("Initial: in memory", item.isInMemory());
138:                assertEquals("Initial: size", item.getSize(),
139:                        testFieldValueBytes.length);
140:                compareBytes("Initial", item.get(), testFieldValueBytes);
141:
142:                // Serialize & Deserialize
143:                try {
144:                    FileItem newItem = (FileItem) serializeDeserialize(item);
145:
146:                    // Test deserialized content is as expected
147:                    assertFalse("Check in memory", newItem.isInMemory());
148:                    compareBytes("Check", testFieldValueBytes, newItem.get());
149:
150:                    // Compare FileItem's (except byte[])
151:                    compareFileItems(item, newItem);
152:
153:                } catch (Exception e) {
154:                    fail("Error Serializing/Deserializing: " + e);
155:                }
156:            }
157:
158:            /**
159:             * Compare FileItem's (except the byte[] content)
160:             */
161:            private void compareFileItems(FileItem origItem, FileItem newItem) {
162:                assertTrue("Compare: is in Memory",
163:                        origItem.isInMemory() == newItem.isInMemory());
164:                assertTrue("Compare: is Form Field",
165:                        origItem.isFormField() == newItem.isFormField());
166:                assertEquals("Compare: Field Name", origItem.getFieldName(),
167:                        newItem.getFieldName());
168:                assertEquals("Compare: Content Type",
169:                        origItem.getContentType(), newItem.getContentType());
170:                assertEquals("Compare: File Name", origItem.getName(), newItem
171:                        .getName());
172:            }
173:
174:            /**
175:             * Compare content bytes.
176:             */
177:            private void compareBytes(String text, byte[] origBytes,
178:                    byte[] newBytes) {
179:                if (origBytes == null) {
180:                    fail(text + " origBytes are null");
181:                }
182:                if (newBytes == null) {
183:                    fail(text + " newBytes are null");
184:                }
185:                assertEquals(text + " byte[] length", origBytes.length,
186:                        newBytes.length);
187:                for (int i = 0; i < origBytes.length; i++) {
188:                    assertEquals(text + " byte[" + i + "]", origBytes[i],
189:                            newBytes[i]);
190:                }
191:            }
192:
193:            /**
194:             * Create content bytes of a specified size.
195:             */
196:            private byte[] createContentBytes(int size) {
197:                StringBuffer buffer = new StringBuffer(size);
198:                byte count = 0;
199:                for (int i = 0; i < size; i++) {
200:                    buffer.append(count + "");
201:                    count++;
202:                    if (count > 9) {
203:                        count = 0;
204:                    }
205:                }
206:                return buffer.toString().getBytes();
207:            }
208:
209:            /**
210:             * Create a FileItem with the specfied content bytes.
211:             */
212:            private FileItem createFileItem(byte[] contentBytes) {
213:                FileItemFactory factory = new DiskFileItemFactory(threshold,
214:                        null);
215:                String textFieldName = "textField";
216:
217:                FileItem item = factory.createItem(textFieldName,
218:                        textContentType, true, "My File Name");
219:                try {
220:                    OutputStream os = item.getOutputStream();
221:                    os.write(contentBytes);
222:                    os.close();
223:                } catch (IOException e) {
224:                    fail("Unexpected IOException" + e);
225:                }
226:
227:                return item;
228:
229:            }
230:
231:            /**
232:             * Do serialization and deserialization.
233:             */
234:            private Object serializeDeserialize(Object target) {
235:
236:                // Serialize the test object
237:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
238:                try {
239:                    ObjectOutputStream oos = new ObjectOutputStream(baos);
240:                    oos.writeObject(target);
241:                    oos.flush();
242:                    oos.close();
243:                } catch (Exception e) {
244:                    fail("Exception during serialization: " + e);
245:                }
246:
247:                // Deserialize the test object
248:                Object result = null;
249:                try {
250:                    ByteArrayInputStream bais = new ByteArrayInputStream(baos
251:                            .toByteArray());
252:                    ObjectInputStream ois = new ObjectInputStream(bais);
253:                    result = ois.readObject();
254:                    bais.close();
255:                } catch (Exception e) {
256:                    fail("Exception during deserialization: " + e);
257:                }
258:                return result;
259:
260:            }
261:
262:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.