Source Code Cross Referenced for DeferredFileOutputStreamTest.java in  » Library » apache-common-IO » org » apache » commons » io » output » 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 » Library » apache common IO » org.apache.commons.io.output 
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.io.output;
018:
019:        import junit.framework.TestCase; //import org.apache.commons.fileupload.DeferredFileOutputStream;
020:
021:        import java.io.File;
022:        import java.io.FileInputStream;
023:        import java.io.FileNotFoundException;
024:        import java.io.IOException;
025:        import java.util.Arrays;
026:
027:        /**
028:         * Unit tests for the <code>DeferredFileOutputStream</code> class.
029:         *
030:         * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
031:         *
032:         * @version $Id: DeferredFileOutputStreamTest.java 437680 2006-08-28 11:57:00Z scolebourne $
033:         */
034:        public class DeferredFileOutputStreamTest extends TestCase {
035:
036:            /**
037:             * The test data as a string (which is the simplest form).
038:             */
039:            private String testString = "0123456789";
040:
041:            /**
042:             * The test data as a byte array, derived from the string.
043:             */
044:            private byte[] testBytes = testString.getBytes();
045:
046:            /**
047:             * Standard JUnit test case constructor.
048:             *
049:             * @param name The name of the test case.
050:             */
051:            public DeferredFileOutputStreamTest(String name) {
052:                super (name);
053:            }
054:
055:            /**
056:             * Tests the case where the amount of data falls below the threshold, and
057:             * is therefore confined to memory.
058:             */
059:            public void testBelowThreshold() {
060:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
061:                        testBytes.length + 42, null);
062:                try {
063:                    dfos.write(testBytes, 0, testBytes.length);
064:                    dfos.close();
065:                } catch (IOException e) {
066:                    fail("Unexpected IOException");
067:                }
068:                assertTrue(dfos.isInMemory());
069:
070:                byte[] resultBytes = dfos.getData();
071:                assertTrue(resultBytes.length == testBytes.length);
072:                assertTrue(Arrays.equals(resultBytes, testBytes));
073:            }
074:
075:            /**
076:             * Tests the case where the amount of data is exactly the same as the
077:             * threshold. The behavior should be the same as that for the amount of
078:             * data being below (i.e. not exceeding) the threshold.
079:             */
080:            public void testAtThreshold() {
081:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
082:                        testBytes.length, null);
083:                try {
084:                    dfos.write(testBytes, 0, testBytes.length);
085:                    dfos.close();
086:                } catch (IOException e) {
087:                    fail("Unexpected IOException");
088:                }
089:                assertTrue(dfos.isInMemory());
090:
091:                byte[] resultBytes = dfos.getData();
092:                assertTrue(resultBytes.length == testBytes.length);
093:                assertTrue(Arrays.equals(resultBytes, testBytes));
094:            }
095:
096:            /**
097:             * Tests the case where the amount of data exceeds the threshold, and is
098:             * therefore written to disk. The actual data written to disk is verified,
099:             * as is the file itself.
100:             */
101:            public void testAboveThreshold() {
102:                File testFile = new File("testAboveThreshold.dat");
103:
104:                // Ensure that the test starts from a clean base.
105:                testFile.delete();
106:
107:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
108:                        testBytes.length - 5, testFile);
109:                try {
110:                    dfos.write(testBytes, 0, testBytes.length);
111:                    dfos.close();
112:                } catch (IOException e) {
113:                    fail("Unexpected IOException");
114:                }
115:                assertFalse(dfos.isInMemory());
116:                assertNull(dfos.getData());
117:
118:                verifyResultFile(testFile);
119:
120:                // Ensure that the test starts from a clean base.
121:                testFile.delete();
122:            }
123:
124:            /**
125:             * Tests the case where there are multiple writes beyond the threshold, to
126:             * ensure that the <code>thresholdReached()</code> method is only called
127:             * once, as the threshold is crossed for the first time.
128:             */
129:            public void testThresholdReached() {
130:                File testFile = new File("testThresholdReached.dat");
131:
132:                // Ensure that the test starts from a clean base.
133:                testFile.delete();
134:
135:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
136:                        testBytes.length / 2, testFile);
137:                int chunkSize = testBytes.length / 3;
138:
139:                try {
140:                    dfos.write(testBytes, 0, chunkSize);
141:                    dfos.write(testBytes, chunkSize, chunkSize);
142:                    dfos.write(testBytes, chunkSize * 2, testBytes.length
143:                            - chunkSize * 2);
144:                    dfos.close();
145:                } catch (IOException e) {
146:                    fail("Unexpected IOException");
147:                }
148:                assertFalse(dfos.isInMemory());
149:                assertNull(dfos.getData());
150:
151:                verifyResultFile(testFile);
152:
153:                // Ensure that the test starts from a clean base.
154:                testFile.delete();
155:            }
156:
157:            /**
158:             * Test wether writeTo() properly writes small content.
159:             */
160:            public void testWriteToSmall() {
161:                File testFile = new File("testWriteToMem.dat");
162:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
163:                // Ensure that the test starts from a clean base.
164:                testFile.delete();
165:
166:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
167:                        testBytes.length * 2, testFile);
168:                try {
169:                    dfos.write(testBytes);
170:
171:                    assertFalse(testFile.exists());
172:                    assertTrue(dfos.isInMemory());
173:
174:                    try {
175:                        dfos.writeTo(baos);
176:                        fail("Should not have been able to write before closing");
177:                    } catch (IOException ioe) {
178:                        // ok, as expected
179:                    }
180:
181:                    dfos.close();
182:                    dfos.writeTo(baos);
183:                } catch (IOException ioe) {
184:                    fail("Unexpected IOException");
185:                }
186:                byte[] copiedBytes = baos.toByteArray();
187:                assertTrue(Arrays.equals(testBytes, copiedBytes));
188:
189:                testFile.delete();
190:            }
191:
192:            /**
193:             * Test wether writeTo() properly writes large content.
194:             */
195:            public void testWriteToLarge() {
196:                File testFile = new File("testWriteToFile.dat");
197:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
198:                // Ensure that the test starts from a clean base.
199:                testFile.delete();
200:
201:                DeferredFileOutputStream dfos = new DeferredFileOutputStream(
202:                        testBytes.length / 2, testFile);
203:                try {
204:                    dfos.write(testBytes);
205:
206:                    assertTrue(testFile.exists());
207:                    assertFalse(dfos.isInMemory());
208:
209:                    try {
210:                        dfos.writeTo(baos);
211:                        fail("Should not have been able to write before closeing");
212:                    } catch (IOException ioe) {
213:                        // ok, as expected
214:                    }
215:
216:                    dfos.close();
217:                    dfos.writeTo(baos);
218:                } catch (IOException ioe) {
219:                    fail("Unexpected IOException");
220:                }
221:                byte[] copiedBytes = baos.toByteArray();
222:                assertTrue(Arrays.equals(testBytes, copiedBytes));
223:                verifyResultFile(testFile);
224:                testFile.delete();
225:            }
226:
227:            /**
228:             * Verifies that the specified file contains the same data as the original
229:             * test data.
230:             *
231:             * @param testFile The file containing the test output.
232:             */
233:            private void verifyResultFile(File testFile) {
234:                try {
235:                    FileInputStream fis = new FileInputStream(testFile);
236:                    assertTrue(fis.available() == testBytes.length);
237:
238:                    byte[] resultBytes = new byte[testBytes.length];
239:                    assertTrue(fis.read(resultBytes) == testBytes.length);
240:
241:                    assertTrue(Arrays.equals(resultBytes, testBytes));
242:                    assertTrue(fis.read(resultBytes) == -1);
243:
244:                    try {
245:                        fis.close();
246:                    } catch (IOException e) {
247:                        // Ignore an exception on close
248:                    }
249:                } catch (FileNotFoundException e) {
250:                    fail("Unexpected FileNotFoundException");
251:                } catch (IOException e) {
252:                    fail("Unexpected IOException");
253:                }
254:            }
255:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.