Source Code Cross Referenced for StreamReaderTest.java in  » Testing » StoryTestIQ » fitnesse » 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 » Testing » StoryTestIQ » fitnesse.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002:        // Released under the terms of the GNU General Public License version 2 or later.
003:        package fitnesse.util;
004:
005:        import java.io.ByteArrayOutputStream;
006:        import java.io.PipedInputStream;
007:        import java.io.PipedOutputStream;
008:
009:        import fitnesse.testutil.AbstractRegex;
010:
011:        public class StreamReaderTest extends AbstractRegex {
012:            private PipedOutputStream output;
013:
014:            StreamReader reader;
015:
016:            String readResult;
017:
018:            byte[] byteResult;
019:
020:            private Thread thread;
021:
022:            public void setUp() throws Exception {
023:                output = new PipedOutputStream();
024:                reader = new StreamReader(new PipedInputStream(output));
025:            }
026:
027:            public void tearDown() throws Exception {
028:                output.close();
029:                reader.close();
030:            }
031:
032:            private void writeToPipe(String value) throws Exception {
033:                byte[] bytes = value.getBytes();
034:                output.write(bytes);
035:            }
036:
037:            public void testReadLine() throws Exception {
038:                startReading(new ReadLine());
039:                writeToPipe("a line\r\n");
040:                finishReading();
041:                assertEquals("a line", readResult);
042:            }
043:
044:            public void testReadLineBytes() throws Exception {
045:                startReading(new ReadLineBytes());
046:                writeToPipe("a line\r\n");
047:                finishReading();
048:                assertEquals("a line", new String(byteResult));
049:            }
050:
051:            public void testBufferCanGrow() throws Exception {
052:                startReading(new ReadLine());
053:                for (int i = 0; i < 1001; i++)
054:                    writeToPipe(i + ",");
055:                writeToPipe("\r\n");
056:
057:                finishReading();
058:                assertHasRegexp("1000", readResult);
059:            }
060:
061:            public void testReadNumberOfBytesAsString() throws Exception {
062:                startReading(new ReadCount(100));
063:                StringBuffer buffer = new StringBuffer();
064:                for (int i = 0; i < 100; i++) {
065:                    buffer.append("*");
066:                    writeToPipe("*");
067:                }
068:                finishReading();
069:
070:                assertEquals(buffer.toString(), readResult);
071:            }
072:
073:            public void testReadNumberOfBytes() throws Exception {
074:                startReading(new ReadCountBytes(100));
075:                StringBuffer buffer = new StringBuffer();
076:                for (int i = 0; i < 100; i++) {
077:                    buffer.append("*");
078:                    writeToPipe("*");
079:                }
080:                finishReading();
081:
082:                assertEquals(buffer.toString(), new String(byteResult));
083:            }
084:
085:            public void testReadNumberOfBytesWithClosedInput() throws Exception {
086:                startReading(new ReadCountBytes(100));
087:
088:                for (int i = 0; i < 50; i++)
089:                    writeToPipe("*");
090:                output.close();
091:                finishReading();
092:
093:                assertEquals("bytes consumed", 50, reader
094:                        .numberOfBytesConsumed());
095:                assertEquals("bytes returned", 50, byteResult.length);
096:            }
097:
098:            public void testReadingZeroBytes() throws Exception {
099:                startReading(new ReadCount(0));
100:                finishReading();
101:                assertEquals("", readResult);
102:            }
103:
104:            public void testReadUpTo() throws Exception {
105:                checkReadUoTo("--boundary", "some bytes--boundary",
106:                        "some bytes");
107:            }
108:
109:            public void testReadUpToNonEnd() throws Exception {
110:                checkReadUoTo("--bound", "some bytes--boundary", "some bytes");
111:            }
112:
113:            public void testReadBytesUpTo() throws Exception {
114:                startReading(new ReadUpToBytes("--boundary"));
115:                writeToPipe("some bytes--boundary");
116:                finishReading();
117:
118:                assertEquals("some bytes", new String(byteResult));
119:            }
120:
121:            public void testReadUpTo2() throws Exception {
122:                checkReadUoTo("--bob", "----bob\r\n", "--");
123:            }
124:
125:            public void testReadUpTo3() throws Exception {
126:                checkReadUoTo("12345", "112123123412345", "1121231234");
127:            }
128:
129:            private void checkReadUoTo(String boundary, String input,
130:                    String expected) throws Exception {
131:                startReading(new ReadUpTo(boundary));
132:                writeToPipe(input);
133:                finishReading();
134:
135:                assertEquals(expected, readResult);
136:            }
137:
138:            public void testCopyBytesUpTo() throws Exception {
139:                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
140:                writeToPipe("some bytes--boundary");
141:                reader.copyBytesUpTo("--boundary", outputStream);
142:                assertEquals("some bytes", outputStream.toString());
143:            }
144:
145:            public void testEofReadCount() throws Exception {
146:                writeToPipe("abcdefghijklmnopqrstuvwxyz");
147:                output.close();
148:                assertFalse(reader.isEof());
149:                reader.read(10);
150:                assertFalse(reader.isEof());
151:                reader.read(16);
152:                assertFalse(reader.isEof());
153:                reader.read(1);
154:                assertTrue(reader.isEof());
155:            }
156:
157:            public void testEofReadLine() throws Exception {
158:                writeToPipe("one line\ntwo lines\nthree lines");
159:                output.close();
160:                assertFalse(reader.isEof());
161:                reader.readLine();
162:                assertFalse(reader.isEof());
163:                reader.readLine();
164:                assertFalse(reader.isEof());
165:                reader.readLine();
166:                assertTrue(reader.isEof());
167:            }
168:
169:            public void testEofReadUpTo() throws Exception {
170:                writeToPipe("mark one, mark two, the end");
171:                output.close();
172:                assertFalse(reader.isEof());
173:                reader.readUpTo("one");
174:                assertFalse(reader.isEof());
175:                reader.readUpTo("two");
176:                assertFalse(reader.isEof());
177:                reader.readUpTo("three");
178:                assertTrue(reader.isEof());
179:            }
180:
181:            public void testBytesConsumed() throws Exception {
182:                writeToPipe("One line\r\n12345abc-boundary");
183:                assertEquals(0, reader.numberOfBytesConsumed());
184:
185:                reader.readLine();
186:                assertEquals(10, reader.numberOfBytesConsumed());
187:
188:                reader.read(5);
189:                assertEquals(15, reader.numberOfBytesConsumed());
190:
191:                reader.readUpTo("-boundary");
192:                assertEquals(27, reader.numberOfBytesConsumed());
193:            }
194:
195:            public void testEarlyClosingStream() throws Exception {
196:                startReading(new ReadCount(10));
197:                output.close();
198:                finishReading();
199:                assertEquals("", readResult);
200:            }
201:
202:            private void startReading(ReadThread thread) {
203:                this .thread = thread;
204:                this .thread.start();
205:            }
206:
207:            private void finishReading() throws Exception {
208:                thread.join();
209:            }
210:
211:            abstract class ReadThread extends Thread {
212:                public void run() {
213:                    try {
214:                        doRead();
215:                    } catch (Exception e) {
216:                    }
217:                }
218:
219:                public abstract void doRead() throws Exception;
220:            }
221:
222:            class ReadLine extends ReadThread {
223:                public void doRead() throws Exception {
224:                    readResult = reader.readLine();
225:                }
226:            }
227:
228:            class ReadCount extends ReadThread {
229:                private int amount;
230:
231:                public ReadCount(int amount) {
232:                    this .amount = amount;
233:                }
234:
235:                public void doRead() throws Exception {
236:                    readResult = reader.read(amount);
237:                }
238:            }
239:
240:            class ReadUpTo extends ReadThread {
241:                private String boundary;
242:
243:                public ReadUpTo(String b) {
244:                    boundary = b;
245:                }
246:
247:                public void doRead() throws Exception {
248:                    readResult = reader.readUpTo(boundary);
249:                }
250:            }
251:
252:            class ReadLineBytes extends ReadThread {
253:                public void doRead() throws Exception {
254:                    byteResult = reader.readLineBytes();
255:                }
256:            }
257:
258:            class ReadCountBytes extends ReadThread {
259:                private int amount;
260:
261:                public ReadCountBytes(int amount) {
262:                    this .amount = amount;
263:                }
264:
265:                public void doRead() throws Exception {
266:                    byteResult = reader.readBytes(amount);
267:                }
268:            }
269:
270:            class ReadUpToBytes extends ReadThread {
271:                private String boundary;
272:
273:                public ReadUpToBytes(String b) {
274:                    boundary = b;
275:                }
276:
277:                public void doRead() throws Exception {
278:                    byteResult = reader.readBytesUpTo(boundary);
279:                }
280:            }
281:
282:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.