Source Code Cross Referenced for CopyUtilsTest.java in  » Library » apache-common-IO » org » apache » commons » io » 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 
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;
018:
019:        import java.io.ByteArrayInputStream;
020:        import java.io.InputStream;
021:        import java.io.OutputStream;
022:        import java.io.Reader;
023:        import java.io.Writer;
024:        import java.util.Arrays;
025:
026:        import org.apache.commons.io.output.ByteArrayOutputStream;
027:        import org.apache.commons.io.testtools.YellOnCloseInputStream;
028:        import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
029:        import org.apache.commons.io.testtools.FileBasedTestCase;
030:
031:        import junit.framework.Test;
032:        import junit.framework.TestSuite;
033:        import junit.textui.TestRunner;
034:
035:        /**
036:         * JUnit tests for CopyUtils.
037:         * 
038:         * @author Jeff Turner
039:         * @author Matthew Hawthorne
040:         * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
041:         * @version $Id: CopyUtilsTest.java 437567 2006-08-28 06:39:07Z bayard $
042:         * @see CopyUtils
043:         */
044:        public class CopyUtilsTest extends FileBasedTestCase {
045:
046:            /*
047:             * NOTE this is not particularly beautiful code. A better way to check for
048:             * flush and close status would be to implement "trojan horse" wrapper
049:             * implementations of the various stream classes, which set a flag when
050:             * relevant methods are called. (JT)
051:             */
052:
053:            private static final int FILE_SIZE = 1024 * 4 + 1;
054:
055:            private byte[] inData = generateTestData(FILE_SIZE);
056:
057:            public static void main(String[] args) {
058:                TestRunner.run(suite());
059:            }
060:
061:            public static Test suite() {
062:                return new TestSuite(CopyUtilsTest.class);
063:            }
064:
065:            public CopyUtilsTest(String testName) {
066:                super (testName);
067:            }
068:
069:            // ----------------------------------------------------------------
070:            // Setup
071:            // ----------------------------------------------------------------
072:
073:            public void setUp() throws Exception {
074:            }
075:
076:            public void tearDown() throws Exception {
077:            }
078:
079:            // ----------------------------------------------------------------
080:            // Tests
081:            // ----------------------------------------------------------------
082:
083:            public void testCopy_byteArrayToOutputStream() throws Exception {
084:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
085:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
086:                        false, true);
087:
088:                CopyUtils.copy(inData, out);
089:
090:                assertEquals("Sizes differ", inData.length, baout.size());
091:                assertTrue("Content differs", Arrays.equals(inData, baout
092:                        .toByteArray()));
093:            }
094:
095:            public void testCopy_byteArrayToWriter() throws Exception {
096:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
097:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
098:                        false, true);
099:                Writer writer = new java.io.OutputStreamWriter(baout,
100:                        "US-ASCII");
101:
102:                CopyUtils.copy(inData, writer);
103:                writer.flush();
104:
105:                assertEquals("Sizes differ", inData.length, baout.size());
106:                assertTrue("Content differs", Arrays.equals(inData, baout
107:                        .toByteArray()));
108:            }
109:
110:            public void testCopy_inputStreamToOutputStream() throws Exception {
111:                InputStream in = new ByteArrayInputStream(inData);
112:                in = new YellOnCloseInputStream(in);
113:
114:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
115:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
116:                        false, true);
117:
118:                int count = CopyUtils.copy(in, out);
119:
120:                assertTrue("Not all bytes were read", in.available() == 0);
121:                assertEquals("Sizes differ", inData.length, baout.size());
122:                assertTrue("Content differs", Arrays.equals(inData, baout
123:                        .toByteArray()));
124:            }
125:
126:            public void testCopy_inputStreamToWriter() throws Exception {
127:                InputStream in = new ByteArrayInputStream(inData);
128:                in = new YellOnCloseInputStream(in);
129:
130:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
131:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
132:                        false, true);
133:                Writer writer = new java.io.OutputStreamWriter(baout,
134:                        "US-ASCII");
135:
136:                CopyUtils.copy(in, writer);
137:                writer.flush();
138:
139:                assertTrue("Not all bytes were read", in.available() == 0);
140:                assertEquals("Sizes differ", inData.length, baout.size());
141:                assertTrue("Content differs", Arrays.equals(inData, baout
142:                        .toByteArray()));
143:            }
144:
145:            public void testCopy_readerToOutputStream() throws Exception {
146:                InputStream in = new ByteArrayInputStream(inData);
147:                in = new YellOnCloseInputStream(in);
148:                Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
149:
150:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
151:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
152:                        false, true);
153:
154:                CopyUtils.copy(reader, out);
155:                //Note: this method *does* flush. It is equivalent to:
156:                //  OutputStreamWriter _out = new OutputStreamWriter(fout);
157:                //  IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
158:                //  _out.flush();
159:                //  out = fout;
160:
161:                // Note: rely on the method to flush
162:                assertEquals("Sizes differ", inData.length, baout.size());
163:                assertTrue("Content differs", Arrays.equals(inData, baout
164:                        .toByteArray()));
165:            }
166:
167:            public void testCopy_readerToWriter() throws Exception {
168:                InputStream in = new ByteArrayInputStream(inData);
169:                in = new YellOnCloseInputStream(in);
170:                Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
171:
172:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
173:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
174:                        false, true);
175:                Writer writer = new java.io.OutputStreamWriter(baout,
176:                        "US-ASCII");
177:
178:                int count = CopyUtils.copy(reader, writer);
179:                writer.flush();
180:                assertEquals(
181:                        "The number of characters returned by copy is wrong",
182:                        inData.length, count);
183:                assertEquals("Sizes differ", inData.length, baout.size());
184:                assertTrue("Content differs", Arrays.equals(inData, baout
185:                        .toByteArray()));
186:            }
187:
188:            public void testCopy_stringToOutputStream() throws Exception {
189:                String str = new String(inData, "US-ASCII");
190:
191:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
192:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
193:                        false, true);
194:
195:                CopyUtils.copy(str, out);
196:                //Note: this method *does* flush. It is equivalent to:
197:                //  OutputStreamWriter _out = new OutputStreamWriter(fout);
198:                //  IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
199:                //  _out.flush();
200:                //  out = fout;
201:                // note: we don't flush here; this IOUtils method does it for us
202:
203:                assertEquals("Sizes differ", inData.length, baout.size());
204:                assertTrue("Content differs", Arrays.equals(inData, baout
205:                        .toByteArray()));
206:            }
207:
208:            public void testCopy_stringToWriter() throws Exception {
209:                String str = new String(inData, "US-ASCII");
210:
211:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
212:                OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
213:                        false, true);
214:                Writer writer = new java.io.OutputStreamWriter(baout,
215:                        "US-ASCII");
216:
217:                CopyUtils.copy(str, writer);
218:                writer.flush();
219:
220:                assertEquals("Sizes differ", inData.length, baout.size());
221:                assertTrue("Content differs", Arrays.equals(inData, baout
222:                        .toByteArray()));
223:            }
224:
225:        } // CopyUtilsTest
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.