Source Code Cross Referenced for PipedReaderTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.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:
018:        package org.apache.harmony.luni.tests.java.io;
019:
020:        import java.io.IOException;
021:        import java.io.PipedReader;
022:        import java.io.PipedWriter;
023:
024:        import junit.framework.TestCase;
025:
026:        public class PipedReaderTest extends TestCase {
027:
028:            static class PWriter implements  Runnable {
029:                public PipedWriter pw;
030:
031:                public PWriter(PipedReader reader) {
032:                    try {
033:                        pw = new PipedWriter(reader);
034:                    } catch (Exception e) {
035:                        System.out.println("Couldn't create writer");
036:                    }
037:                }
038:
039:                public PWriter() {
040:                    pw = new PipedWriter();
041:                }
042:
043:                public void run() {
044:                    try {
045:                        char[] c = new char[11];
046:                        "Hello World".getChars(0, 11, c, 0);
047:                        pw.write(c);
048:                        Thread.sleep(10000);
049:                    } catch (InterruptedException e) {
050:                    } catch (Exception e) {
051:                        System.out.println("Exception occurred: "
052:                                + e.toString());
053:                    }
054:                }
055:            }
056:
057:            PipedReader preader;
058:
059:            PWriter pwriter;
060:
061:            Thread t;
062:
063:            /**
064:             * @tests java.io.PipedReader#PipedReader()
065:             */
066:            public void test_Constructor() {
067:                // Used in test
068:            }
069:
070:            /**
071:             * @tests java.io.PipedReader#PipedReader(java.io.PipedWriter)
072:             */
073:            public void test_ConstructorLjava_io_PipedWriter()
074:                    throws IOException {
075:                preader = new PipedReader(new PipedWriter());
076:            }
077:
078:            /**
079:             * @tests java.io.PipedReader#close()
080:             */
081:            public void test_close() throws Exception {
082:                char[] c = null;
083:                preader = new PipedReader();
084:                t = new Thread(new PWriter(preader), "");
085:                t.start();
086:                Thread.sleep(500); // Allow writer to start
087:                c = new char[11];
088:                preader.read(c, 0, 11);
089:                preader.close();
090:                assertEquals("Read incorrect chars", "Hello World", new String(
091:                        c));
092:            }
093:
094:            /**
095:             * @tests java.io.PipedReader#connect(java.io.PipedWriter)
096:             */
097:            public void test_connectLjava_io_PipedWriter() throws Exception {
098:                char[] c = null;
099:
100:                preader = new PipedReader();
101:                t = new Thread(pwriter = new PWriter(), "");
102:                preader.connect(pwriter.pw);
103:                t.start();
104:                Thread.sleep(500); // Allow writer to start
105:                c = new char[11];
106:                preader.read(c, 0, 11);
107:
108:                assertEquals("Read incorrect chars", "Hello World", new String(
109:                        c));
110:                try {
111:                    preader.connect(pwriter.pw);
112:                    fail("Failed to throw exception connecting to pre-connected reader");
113:                } catch (IOException e) {
114:                    // Expected
115:                }
116:            }
117:
118:            /**
119:             * @tests java.io.PipedReader#read()
120:             */
121:            public void test_read() throws Exception {
122:                char[] c = null;
123:                preader = new PipedReader();
124:                t = new Thread(new PWriter(preader), "");
125:                t.start();
126:                Thread.sleep(500); // Allow writer to start
127:                c = new char[11];
128:                for (int i = 0; i < c.length; i++) {
129:                    c[i] = (char) preader.read();
130:                }
131:                assertEquals("Read incorrect chars", "Hello World", new String(
132:                        c));
133:            }
134:
135:            /**
136:             * @tests java.io.PipedReader#read(char[], int, int)
137:             */
138:            public void test_read$CII() throws Exception {
139:                char[] c = null;
140:                preader = new PipedReader();
141:                t = new Thread(new PWriter(preader), "");
142:                t.start();
143:                Thread.sleep(500); // Allow writer to start
144:                c = new char[11];
145:                int n = 0;
146:                int x = n;
147:                while (x < 11) {
148:                    n = preader.read(c, x, 11 - x);
149:                    x = x + n;
150:                }
151:                assertEquals("Read incorrect chars", "Hello World", new String(
152:                        c));
153:                try {
154:                    preader.close();
155:                    preader.read(c, 8, 7);
156:                    fail("Failed to throw exception reading from closed reader");
157:                } catch (IOException e) {
158:                    // Expected
159:                }
160:            }
161:
162:            /**
163:             * @tests java.io.PipedReader#read(char[], int, int)
164:             */
165:            public void test_read$CII_2() throws IOException {
166:                // Regression for HARMONY-387
167:                PipedWriter pw = new PipedWriter();
168:                PipedReader obj = null;
169:                try {
170:                    obj = new PipedReader(pw);
171:                    obj.read(new char[0], (int) 0, (int) -1);
172:                    fail("IndexOutOfBoundsException expected");
173:                } catch (IndexOutOfBoundsException t) {
174:                    assertEquals(
175:                            "IndexOutOfBoundsException rather than a subclass expected",
176:                            IndexOutOfBoundsException.class, t.getClass());
177:                }
178:            }
179:
180:            /**
181:             * @tests java.io.PipedReader#read(char[], int, int)
182:             */
183:            public void test_read$CII_3() throws IOException {
184:                PipedWriter pw = new PipedWriter();
185:                PipedReader obj = null;
186:                try {
187:                    obj = new PipedReader(pw);
188:                    obj.read(new char[0], (int) -1, (int) 0);
189:                    fail("IndexOutOfBoundsException expected");
190:                } catch (ArrayIndexOutOfBoundsException t) {
191:                    fail("IndexOutOfBoundsException expected");
192:                } catch (IndexOutOfBoundsException t) {
193:                    // Expected
194:                }
195:            }
196:
197:            /**
198:             * @tests java.io.PipedReader#read(char[], int, int)
199:             */
200:            public void test_read$CII_4() throws IOException {
201:                PipedWriter pw = new PipedWriter();
202:                PipedReader obj = null;
203:                try {
204:                    obj = new PipedReader(pw);
205:                    obj.read(new char[0], (int) -1, (int) -1);
206:                    fail("IndexOutOfBoundsException expected");
207:                } catch (ArrayIndexOutOfBoundsException t) {
208:                    fail("IndexOutOfBoundsException expected");
209:                } catch (IndexOutOfBoundsException t) {
210:                    // Expected
211:                }
212:            }
213:
214:            /**
215:             * @tests java.io.PipedReader#read(char[], int, int)
216:             */
217:            public void test_read_$CII_IOException() throws IOException {
218:                PipedWriter pw = new PipedWriter();
219:                PipedReader pr = new PipedReader(pw);
220:                char[] buf = null;
221:                pr.close();
222:                try {
223:                    pr.read(buf, 0, 10);
224:                    fail("Should throws IOException"); //$NON-NLS-1$
225:                } catch (IOException e) {
226:                    // expected
227:                } finally {
228:                    pw = null;
229:                    pr = null;
230:                }
231:
232:                pr = new PipedReader();
233:                buf = null;
234:                pr.close();
235:                try {
236:                    pr.read(buf, 0, 10);
237:                    fail("Should throws IOException"); //$NON-NLS-1$
238:                } catch (IOException e) {
239:                    // expected
240:                } finally {
241:                    pr = null;
242:                }
243:
244:                pw = new PipedWriter();
245:                pr = new PipedReader(pw);
246:                buf = new char[10];
247:                pr.close();
248:                try {
249:                    pr.read(buf, -1, 0);
250:                    fail("Should throws IOException"); //$NON-NLS-1$
251:                } catch (IOException e) {
252:                    // expected
253:                } finally {
254:                    pw = null;
255:                    pr = null;
256:                }
257:
258:                pw = new PipedWriter();
259:                pr = new PipedReader(pw);
260:                buf = new char[10];
261:                pr.close();
262:                try {
263:                    pr.read(buf, 0, -1);
264:                    fail("Should throws IOException"); //$NON-NLS-1$
265:                } catch (IOException e) {
266:                    // expected
267:                } finally {
268:                    pw = null;
269:                    pr = null;
270:                }
271:
272:                pw = new PipedWriter();
273:                pr = new PipedReader(pw);
274:                buf = new char[10];
275:                pr.close();
276:                try {
277:                    pr.read(buf, 1, 10);
278:                    fail("Should throws IOException"); //$NON-NLS-1$
279:                } catch (IOException e) {
280:                    // expected
281:                } finally {
282:                    pw = null;
283:                    pr = null;
284:                }
285:
286:                pw = new PipedWriter();
287:                pr = new PipedReader(pw);
288:                pr.close();
289:                try {
290:                    pr.read(new char[0], -1, -1);
291:                    fail("should throw IOException"); //$NON-NLS-1$
292:                } catch (IOException e) {
293:                    // expected
294:                } finally {
295:                    pw = null;
296:                    pr = null;
297:                }
298:
299:                pw = new PipedWriter();
300:                pr = new PipedReader(pw);
301:                pr.close();
302:                try {
303:                    pr.read(null, 0, 1);
304:                    fail("should throw IOException"); //$NON-NLS-1$
305:                } catch (IOException e) {
306:                    // expected
307:                } finally {
308:                    pw = null;
309:                    pr = null;
310:                }
311:
312:                pw = new PipedWriter();
313:                pr = new PipedReader(pw);
314:                try {
315:                    pr.read(null, -1, 1);
316:                    fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
317:                } catch (IndexOutOfBoundsException e) {
318:                    // expected
319:                } finally {
320:                    pw = null;
321:                    pr = null;
322:                }
323:
324:                pw = new PipedWriter();
325:                pr = new PipedReader(pw);
326:                try {
327:                    pr.read(null, 0, -1);
328:                    fail("should throw NullPointerException"); //$NON-NLS-1$
329:                } catch (NullPointerException e) {
330:                    // expected
331:                } finally {
332:                    pw = null;
333:                    pr = null;
334:                }
335:
336:                pw = new PipedWriter();
337:                pr = new PipedReader(pw);
338:                try {
339:                    pr.read(new char[10], 11, 0);
340:                    fail("should throw IndexOutOfBoundsException"); //$NON-NLS-1$
341:                } catch (IndexOutOfBoundsException e) {
342:                    // expected
343:                } finally {
344:                    pw = null;
345:                    pr = null;
346:                }
347:
348:                pw = new PipedWriter();
349:                pr = new PipedReader(pw);
350:                try {
351:                    pr.read(null, 1, 0);
352:                    fail("should throw NullPointerException"); //$NON-NLS-1$
353:                } catch (NullPointerException e) {
354:                    // expected
355:                } finally {
356:                    pw = null;
357:                    pr = null;
358:                }
359:            }
360:
361:            /**
362:             * @tests java.io.PipedReader#ready()
363:             */
364:            public void test_ready() throws Exception {
365:                char[] c = null;
366:                preader = new PipedReader();
367:                t = new Thread(new PWriter(preader), "");
368:                t.start();
369:                Thread.sleep(500); // Allow writer to start
370:                assertTrue("Reader should be ready", preader.ready());
371:                c = new char[11];
372:                for (int i = 0; i < c.length; i++)
373:                    c[i] = (char) preader.read();
374:                assertFalse(
375:                        "Reader should not be ready after reading all chars",
376:                        preader.ready());
377:            }
378:
379:            /**
380:             * Tears down the fixture, for example, close a network connection. This
381:             * method is called after a test is executed.
382:             */
383:            protected void tearDown() throws Exception {
384:                if (t != null) {
385:                    t.interrupt();
386:                }
387:                super.tearDown();
388:            }
389:        }
ww__w._j___a__v__a___2___s__.co__m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.