Source Code Cross Referenced for CountingInputStreamTest.java in  » Library » apache-common-IO » org » apache » commons » io » input » 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.input 
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.input;
018:
019:        import java.io.ByteArrayInputStream;
020:        import java.io.OutputStream;
021:        import java.io.IOException;
022:
023:        import junit.framework.TestCase;
024:        import org.apache.commons.io.IOUtils;
025:        import org.apache.commons.io.output.NullOutputStream;
026:
027:        /**
028:         * Tests the CountingInputStream.
029:         *
030:         * @author Marcelo Liberato
031:         * @author Stephen Colebourne
032:         * @version $Id: CountingInputStreamTest.java 471628 2006-11-06 04:06:45Z bayard $
033:         */
034:        public class CountingInputStreamTest extends TestCase {
035:
036:            public CountingInputStreamTest(String name) {
037:                super (name);
038:            }
039:
040:            public void testCounting() throws Exception {
041:                String text = "A piece of text";
042:                byte[] bytes = text.getBytes();
043:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
044:                CountingInputStream cis = new CountingInputStream(bais);
045:
046:                // have to declare this larger as we're going to read 
047:                // off the end of the stream and input stream seems 
048:                // to do bounds checking
049:                byte[] result = new byte[21];
050:
051:                byte[] ba = new byte[5];
052:                int found = cis.read(ba);
053:                System.arraycopy(ba, 0, result, 0, 5);
054:                assertEquals(found, cis.getCount());
055:
056:                int value = cis.read();
057:                found++;
058:                result[5] = (byte) value;
059:                assertEquals(found, cis.getCount());
060:
061:                found += cis.read(result, 6, 5);
062:                assertEquals(found, cis.getCount());
063:
064:                found += cis.read(result, 11, 10); // off the end
065:                assertEquals(found, cis.getCount());
066:
067:                // trim to get rid of the 6 empty values
068:                String textResult = new String(result).trim();
069:                assertEquals(textResult, text);
070:            }
071:
072:            /**
073:             * Test for files > 2GB in size - see issue IO-84
074:             */
075:            public void testLargeFiles_IO84() throws Exception {
076:                long size = (long) Integer.MAX_VALUE + (long) 1;
077:                NullInputStream mock = new NullInputStream(size);
078:                CountingInputStream cis = new CountingInputStream(mock);
079:                OutputStream out = new NullOutputStream();
080:
081:                // Test integer methods
082:                IOUtils.copyLarge(cis, out);
083:                try {
084:                    cis.getCount();
085:                    fail("Expected getCount() to throw an ArithmeticException");
086:                } catch (ArithmeticException ae) {
087:                    // expected result
088:                }
089:                try {
090:                    cis.resetCount();
091:                    fail("Expected resetCount() to throw an ArithmeticException");
092:                } catch (ArithmeticException ae) {
093:                    // expected result
094:                }
095:
096:                mock.close();
097:
098:                // Test long methods
099:                IOUtils.copyLarge(cis, out);
100:                assertEquals("getByteCount()", size, cis.getByteCount());
101:                assertEquals("resetByteCount()", size, cis.resetByteCount());
102:            }
103:
104:            public void testResetting() throws Exception {
105:                String text = "A piece of text";
106:                byte[] bytes = text.getBytes();
107:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
108:                CountingInputStream cis = new CountingInputStream(bais);
109:
110:                byte[] result = new byte[bytes.length];
111:
112:                int found = cis.read(result, 0, 5);
113:                assertEquals(found, cis.getCount());
114:
115:                int count = cis.resetCount();
116:                found = cis.read(result, 6, 5);
117:                assertEquals(found, count);
118:            }
119:
120:            public void testZeroLength1() throws Exception {
121:                ByteArrayInputStream bais = new ByteArrayInputStream(
122:                        new byte[0]);
123:                CountingInputStream cis = new CountingInputStream(bais);
124:
125:                int found = cis.read();
126:                assertEquals(-1, found);
127:                assertEquals(0, cis.getCount());
128:            }
129:
130:            public void testZeroLength2() throws Exception {
131:                ByteArrayInputStream bais = new ByteArrayInputStream(
132:                        new byte[0]);
133:                CountingInputStream cis = new CountingInputStream(bais);
134:
135:                byte[] result = new byte[10];
136:
137:                int found = cis.read(result);
138:                assertEquals(-1, found);
139:                assertEquals(0, cis.getCount());
140:            }
141:
142:            public void testZeroLength3() throws Exception {
143:                ByteArrayInputStream bais = new ByteArrayInputStream(
144:                        new byte[0]);
145:                CountingInputStream cis = new CountingInputStream(bais);
146:
147:                byte[] result = new byte[10];
148:
149:                int found = cis.read(result, 0, 5);
150:                assertEquals(-1, found);
151:                assertEquals(0, cis.getCount());
152:            }
153:
154:            public void testEOF1() throws Exception {
155:                ByteArrayInputStream bais = new ByteArrayInputStream(
156:                        new byte[2]);
157:                CountingInputStream cis = new CountingInputStream(bais);
158:
159:                int found = cis.read();
160:                assertEquals(0, found);
161:                assertEquals(1, cis.getCount());
162:                found = cis.read();
163:                assertEquals(0, found);
164:                assertEquals(2, cis.getCount());
165:                found = cis.read();
166:                assertEquals(-1, found);
167:                assertEquals(2, cis.getCount());
168:            }
169:
170:            public void testEOF2() throws Exception {
171:                ByteArrayInputStream bais = new ByteArrayInputStream(
172:                        new byte[2]);
173:                CountingInputStream cis = new CountingInputStream(bais);
174:
175:                byte[] result = new byte[10];
176:
177:                int found = cis.read(result);
178:                assertEquals(2, found);
179:                assertEquals(2, cis.getCount());
180:            }
181:
182:            public void testEOF3() throws Exception {
183:                ByteArrayInputStream bais = new ByteArrayInputStream(
184:                        new byte[2]);
185:                CountingInputStream cis = new CountingInputStream(bais);
186:
187:                byte[] result = new byte[10];
188:
189:                int found = cis.read(result, 0, 5);
190:                assertEquals(2, found);
191:                assertEquals(2, cis.getCount());
192:            }
193:
194:            public void testSkipping() throws IOException {
195:                String text = "Hello World!";
196:                byte[] bytes = text.getBytes();
197:                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
198:                CountingInputStream cis = new CountingInputStream(bais);
199:
200:                assertEquals(6, cis.skip(6));
201:                assertEquals(6, cis.getCount());
202:                final byte[] result = new byte[6];
203:                cis.read(result);
204:
205:                assertEquals("World!", new String(result));
206:                assertEquals(12, cis.getCount());
207:            }
208:
209:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.