Source Code Cross Referenced for IOUtilsWriteTestCase.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.File;
020:        import java.io.FileInputStream;
021:        import java.io.FileOutputStream;
022:        import java.io.InputStream;
023:        import java.io.InputStreamReader;
024:        import java.io.OutputStream;
025:        import java.io.OutputStreamWriter;
026:        import java.io.Reader;
027:        import java.io.StringWriter;
028:        import java.io.Writer;
029:        import java.util.Arrays;
030:        import java.util.List;
031:
032:        import junit.framework.Test;
033:        import junit.framework.TestSuite;
034:        import junit.textui.TestRunner;
035:
036:        import org.apache.commons.io.output.ByteArrayOutputStream;
037:        import org.apache.commons.io.testtools.FileBasedTestCase;
038:        import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
039:
040:        /**
041:         * JUnit tests for IOUtils write methods.
042:         * 
043:         * @author Jeff Turner
044:         * @author Matthew Hawthorne
045:         * @author Jeremias Maerki
046:         * @author Stephen Colebourne
047:         * @version $Id: IOUtilsWriteTestCase.java 437567 2006-08-28 06:39:07Z bayard $
048:         * @see IOUtils
049:         */
050:        public class IOUtilsWriteTestCase extends FileBasedTestCase {
051:
052:            private static final int FILE_SIZE = 1024 * 4 + 1;
053:
054:            private byte[] inData = generateTestData(FILE_SIZE);
055:
056:            public static void main(String[] args) {
057:                TestRunner.run(suite());
058:            }
059:
060:            public static Test suite() {
061:                return new TestSuite(IOUtilsWriteTestCase.class);
062:            }
063:
064:            public IOUtilsWriteTestCase(String testName) {
065:                super (testName);
066:            }
067:
068:            // ----------------------------------------------------------------
069:            // Setup
070:            // ----------------------------------------------------------------
071:
072:            public void setUp() throws Exception {
073:            }
074:
075:            public void tearDown() throws Exception {
076:            }
077:
078:            // ----------------------------------------------------------------
079:            // Tests
080:            // ----------------------------------------------------------------
081:
082:            //-----------------------------------------------------------------------
083:            public void testWrite_byteArrayToOutputStream() throws Exception {
084:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
085:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
086:                        baout, true, true);
087:
088:                IOUtils.write(inData, out);
089:                out.off();
090:                out.flush();
091:
092:                assertEquals("Sizes differ", inData.length, baout.size());
093:                assertTrue("Content differs", Arrays.equals(inData, baout
094:                        .toByteArray()));
095:            }
096:
097:            public void testWrite_byteArrayToOutputStream_nullData()
098:                    throws Exception {
099:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
100:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
101:                        baout, true, true);
102:
103:                IOUtils.write((byte[]) null, out);
104:                out.off();
105:                out.flush();
106:
107:                assertEquals("Sizes differ", 0, baout.size());
108:            }
109:
110:            public void testWrite_byteArrayToOutputStream_nullStream()
111:                    throws Exception {
112:                try {
113:                    IOUtils.write(inData, (OutputStream) null);
114:                    fail();
115:                } catch (NullPointerException ex) {
116:                }
117:            }
118:
119:            //-----------------------------------------------------------------------
120:            public void testWrite_byteArrayToWriter() throws Exception {
121:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
122:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
123:                        baout, true, true);
124:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
125:
126:                IOUtils.write(inData, writer);
127:                out.off();
128:                writer.flush();
129:
130:                assertEquals("Sizes differ", inData.length, baout.size());
131:                assertTrue("Content differs", Arrays.equals(inData, baout
132:                        .toByteArray()));
133:            }
134:
135:            public void testWrite_byteArrayToWriter_nullData() throws Exception {
136:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
137:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
138:                        baout, true, true);
139:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
140:
141:                IOUtils.write((byte[]) null, writer);
142:                out.off();
143:                writer.flush();
144:
145:                assertEquals("Sizes differ", 0, baout.size());
146:            }
147:
148:            public void testWrite_byteArrayToWriter_nullWriter()
149:                    throws Exception {
150:                try {
151:                    IOUtils.write(inData, (Writer) null);
152:                    fail();
153:                } catch (NullPointerException ex) {
154:                }
155:            }
156:
157:            //-----------------------------------------------------------------------
158:            public void testWrite_byteArrayToWriter_Encoding() throws Exception {
159:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
160:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
161:                        baout, true, true);
162:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
163:
164:                IOUtils.write(inData, writer, "UTF8");
165:                out.off();
166:                writer.flush();
167:
168:                byte[] bytes = baout.toByteArray();
169:                bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
170:                assertTrue("Content differs", Arrays.equals(inData, bytes));
171:            }
172:
173:            public void testWrite_byteArrayToWriter_Encoding_nullData()
174:                    throws Exception {
175:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
176:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
177:                        baout, true, true);
178:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
179:
180:                IOUtils.write((byte[]) null, writer, "UTF8");
181:                out.off();
182:                writer.flush();
183:
184:                assertEquals("Sizes differ", 0, baout.size());
185:            }
186:
187:            public void testWrite_byteArrayToWriter_Encoding_nullWriter()
188:                    throws Exception {
189:                try {
190:                    IOUtils.write(inData, (Writer) null, "UTF8");
191:                    fail();
192:                } catch (NullPointerException ex) {
193:                }
194:            }
195:
196:            public void testWrite_byteArrayToWriter_Encoding_nullEncoding()
197:                    throws Exception {
198:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
199:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
200:                        baout, true, true);
201:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
202:
203:                IOUtils.write(inData, writer, null);
204:                out.off();
205:                writer.flush();
206:
207:                assertEquals("Sizes differ", inData.length, baout.size());
208:                assertTrue("Content differs", Arrays.equals(inData, baout
209:                        .toByteArray()));
210:            }
211:
212:            //-----------------------------------------------------------------------
213:            public void testWrite_stringToOutputStream() throws Exception {
214:                String str = new String(inData, "US-ASCII");
215:
216:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
217:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
218:                        baout, true, true);
219:
220:                IOUtils.write(str, out);
221:                out.off();
222:                out.flush();
223:
224:                assertEquals("Sizes differ", inData.length, baout.size());
225:                assertTrue("Content differs", Arrays.equals(inData, baout
226:                        .toByteArray()));
227:            }
228:
229:            public void testWrite_stringToOutputStream_nullData()
230:                    throws Exception {
231:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
232:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
233:                        baout, true, true);
234:
235:                IOUtils.write((String) null, out);
236:                out.off();
237:                out.flush();
238:
239:                assertEquals("Sizes differ", 0, baout.size());
240:            }
241:
242:            public void testWrite_stringToOutputStream_nullStream()
243:                    throws Exception {
244:                String str = new String(inData, "US-ASCII");
245:                try {
246:                    IOUtils.write(str, (OutputStream) null);
247:                    fail();
248:                } catch (NullPointerException ex) {
249:                }
250:            }
251:
252:            //-----------------------------------------------------------------------
253:            public void testWrite_stringToOutputStream_Encoding()
254:                    throws Exception {
255:                String str = new String(inData, "US-ASCII");
256:
257:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
258:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
259:                        baout, true, true);
260:
261:                IOUtils.write(str, out, "UTF16");
262:                out.off();
263:                out.flush();
264:
265:                byte[] bytes = baout.toByteArray();
266:                bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
267:                assertTrue("Content differs", Arrays.equals(inData, bytes));
268:            }
269:
270:            public void testWrite_stringToOutputStream_Encoding_nullData()
271:                    throws Exception {
272:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
273:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
274:                        baout, true, true);
275:
276:                IOUtils.write((String) null, out);
277:                out.off();
278:                out.flush();
279:
280:                assertEquals("Sizes differ", 0, baout.size());
281:            }
282:
283:            public void testWrite_stringToOutputStream_Encoding_nullStream()
284:                    throws Exception {
285:                String str = new String(inData, "US-ASCII");
286:                try {
287:                    IOUtils.write(str, (OutputStream) null);
288:                    fail();
289:                } catch (NullPointerException ex) {
290:                }
291:            }
292:
293:            public void testWrite_stringToOutputStream_nullEncoding()
294:                    throws Exception {
295:                String str = new String(inData, "US-ASCII");
296:
297:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
298:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
299:                        baout, true, true);
300:
301:                IOUtils.write(str, out, null);
302:                out.off();
303:                out.flush();
304:
305:                assertEquals("Sizes differ", inData.length, baout.size());
306:                assertTrue("Content differs", Arrays.equals(inData, baout
307:                        .toByteArray()));
308:            }
309:
310:            //-----------------------------------------------------------------------
311:            public void testWrite_stringToWriter() throws Exception {
312:                String str = new String(inData, "US-ASCII");
313:
314:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
315:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
316:                        baout, true, true);
317:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
318:
319:                IOUtils.write(str, writer);
320:                out.off();
321:                writer.flush();
322:
323:                assertEquals("Sizes differ", inData.length, baout.size());
324:                assertTrue("Content differs", Arrays.equals(inData, baout
325:                        .toByteArray()));
326:            }
327:
328:            public void testWrite_stringToWriter_Encoding_nullData()
329:                    throws Exception {
330:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
331:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
332:                        baout, true, true);
333:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
334:
335:                IOUtils.write((String) null, writer);
336:                out.off();
337:                writer.flush();
338:
339:                assertEquals("Sizes differ", 0, baout.size());
340:            }
341:
342:            public void testWrite_stringToWriter_Encoding_nullStream()
343:                    throws Exception {
344:                String str = new String(inData, "US-ASCII");
345:                try {
346:                    IOUtils.write(str, (Writer) null);
347:                    fail();
348:                } catch (NullPointerException ex) {
349:                }
350:            }
351:
352:            //-----------------------------------------------------------------------
353:            public void testWrite_charArrayToOutputStream() throws Exception {
354:                String str = new String(inData, "US-ASCII");
355:
356:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
357:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
358:                        baout, true, true);
359:
360:                IOUtils.write(str.toCharArray(), out);
361:                out.off();
362:                out.flush();
363:
364:                assertEquals("Sizes differ", inData.length, baout.size());
365:                assertTrue("Content differs", Arrays.equals(inData, baout
366:                        .toByteArray()));
367:            }
368:
369:            public void testWrite_charArrayToOutputStream_nullData()
370:                    throws Exception {
371:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
372:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
373:                        baout, true, true);
374:
375:                IOUtils.write((char[]) null, out);
376:                out.off();
377:                out.flush();
378:
379:                assertEquals("Sizes differ", 0, baout.size());
380:            }
381:
382:            public void testWrite_charArrayToOutputStream_nullStream()
383:                    throws Exception {
384:                String str = new String(inData, "US-ASCII");
385:                try {
386:                    IOUtils.write(str.toCharArray(), (OutputStream) null);
387:                    fail();
388:                } catch (NullPointerException ex) {
389:                }
390:            }
391:
392:            //-----------------------------------------------------------------------
393:            public void testWrite_charArrayToOutputStream_Encoding()
394:                    throws Exception {
395:                String str = new String(inData, "US-ASCII");
396:
397:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
398:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
399:                        baout, true, true);
400:
401:                IOUtils.write(str.toCharArray(), out, "UTF16");
402:                out.off();
403:                out.flush();
404:
405:                byte[] bytes = baout.toByteArray();
406:                bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
407:                assertTrue("Content differs", Arrays.equals(inData, bytes));
408:            }
409:
410:            public void testWrite_charArrayToOutputStream_Encoding_nullData()
411:                    throws Exception {
412:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
413:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
414:                        baout, true, true);
415:
416:                IOUtils.write((char[]) null, out);
417:                out.off();
418:                out.flush();
419:
420:                assertEquals("Sizes differ", 0, baout.size());
421:            }
422:
423:            public void testWrite_charArrayToOutputStream_Encoding_nullStream()
424:                    throws Exception {
425:                String str = new String(inData, "US-ASCII");
426:                try {
427:                    IOUtils.write(str.toCharArray(), (OutputStream) null);
428:                    fail();
429:                } catch (NullPointerException ex) {
430:                }
431:            }
432:
433:            public void testWrite_charArrayToOutputStream_nullEncoding()
434:                    throws Exception {
435:                String str = new String(inData, "US-ASCII");
436:
437:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
438:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
439:                        baout, true, true);
440:
441:                IOUtils.write(str.toCharArray(), out, null);
442:                out.off();
443:                out.flush();
444:
445:                assertEquals("Sizes differ", inData.length, baout.size());
446:                assertTrue("Content differs", Arrays.equals(inData, baout
447:                        .toByteArray()));
448:            }
449:
450:            //-----------------------------------------------------------------------
451:            public void testWrite_charArrayToWriter() throws Exception {
452:                String str = new String(inData, "US-ASCII");
453:
454:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
455:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
456:                        baout, true, true);
457:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
458:
459:                IOUtils.write(str.toCharArray(), writer);
460:                out.off();
461:                writer.flush();
462:
463:                assertEquals("Sizes differ", inData.length, baout.size());
464:                assertTrue("Content differs", Arrays.equals(inData, baout
465:                        .toByteArray()));
466:            }
467:
468:            public void testWrite_charArrayToWriter_Encoding_nullData()
469:                    throws Exception {
470:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
471:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
472:                        baout, true, true);
473:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
474:
475:                IOUtils.write((char[]) null, writer);
476:                out.off();
477:                writer.flush();
478:
479:                assertEquals("Sizes differ", 0, baout.size());
480:            }
481:
482:            public void testWrite_charArrayToWriter_Encoding_nullStream()
483:                    throws Exception {
484:                String str = new String(inData, "US-ASCII");
485:                try {
486:                    IOUtils.write(str.toCharArray(), (Writer) null);
487:                    fail();
488:                } catch (NullPointerException ex) {
489:                }
490:            }
491:
492:            //-----------------------------------------------------------------------
493:            public void testWriteLines_OutputStream() throws Exception {
494:                Object[] data = new Object[] { "hello",
495:                        new StringBuffer("world"), "", "this is", null,
496:                        "some text" };
497:                List list = Arrays.asList(data);
498:
499:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
500:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
501:                        baout, false, true);
502:
503:                IOUtils.writeLines(list, "*", out);
504:
505:                out.off();
506:                out.flush();
507:
508:                String expected = "hello*world**this is**some text*";
509:                String actual = baout.toString();
510:                assertEquals(expected, actual);
511:            }
512:
513:            public void testWriteLines_OutputStream_nullData() throws Exception {
514:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
515:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
516:                        baout, false, true);
517:
518:                IOUtils.writeLines((List) null, "*", out);
519:                out.off();
520:                out.flush();
521:
522:                assertEquals("Sizes differ", 0, baout.size());
523:            }
524:
525:            public void testWriteLines_OutputStream_nullSeparator()
526:                    throws Exception {
527:                Object[] data = new Object[] { "hello", "world" };
528:                List list = Arrays.asList(data);
529:
530:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
531:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
532:                        baout, false, true);
533:
534:                IOUtils.writeLines(list, (String) null, out);
535:                out.off();
536:                out.flush();
537:
538:                String expected = "hello" + IOUtils.LINE_SEPARATOR + "world"
539:                        + IOUtils.LINE_SEPARATOR;
540:                String actual = baout.toString();
541:                assertEquals(expected, actual);
542:            }
543:
544:            public void testWriteLines_OutputStream_nullStream()
545:                    throws Exception {
546:                Object[] data = new Object[] { "hello", "world" };
547:                List list = Arrays.asList(data);
548:                try {
549:                    IOUtils.writeLines(list, "*", (OutputStream) null);
550:                    fail();
551:                } catch (NullPointerException ex) {
552:                }
553:            }
554:
555:            //-----------------------------------------------------------------------
556:            public void testWriteLines_OutputStream_Encoding() throws Exception {
557:                Object[] data = new Object[] { "hello\u8364",
558:                        new StringBuffer("world"), "", "this is", null,
559:                        "some text" };
560:                List list = Arrays.asList(data);
561:
562:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
563:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
564:                        baout, false, true);
565:
566:                IOUtils.writeLines(list, "*", out, "UTF-8");
567:
568:                out.off();
569:                out.flush();
570:
571:                String expected = "hello\u8364*world**this is**some text*";
572:                String actual = baout.toString("UTF-8");
573:                assertEquals(expected, actual);
574:            }
575:
576:            public void testWriteLines_OutputStream_Encoding_nullData()
577:                    throws Exception {
578:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
579:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
580:                        baout, false, true);
581:
582:                IOUtils.writeLines((List) null, "*", out, "US-ASCII");
583:                out.off();
584:                out.flush();
585:
586:                assertEquals("Sizes differ", 0, baout.size());
587:            }
588:
589:            public void testWriteLines_OutputStream_Encoding_nullSeparator()
590:                    throws Exception {
591:                Object[] data = new Object[] { "hello", "world" };
592:                List list = Arrays.asList(data);
593:
594:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
595:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
596:                        baout, false, true);
597:
598:                IOUtils.writeLines(list, (String) null, out, "US-ASCII");
599:                out.off();
600:                out.flush();
601:
602:                String expected = "hello" + IOUtils.LINE_SEPARATOR + "world"
603:                        + IOUtils.LINE_SEPARATOR;
604:                String actual = baout.toString();
605:                assertEquals(expected, actual);
606:            }
607:
608:            public void testWriteLines_OutputStream_Encoding_nullStream()
609:                    throws Exception {
610:                Object[] data = new Object[] { "hello", "world" };
611:                List list = Arrays.asList(data);
612:                try {
613:                    IOUtils.writeLines(list, "*", (OutputStream) null,
614:                            "US-ASCII");
615:                    fail();
616:                } catch (NullPointerException ex) {
617:                }
618:            }
619:
620:            public void testWriteLines_OutputStream_Encoding_nullEncoding()
621:                    throws Exception {
622:                Object[] data = new Object[] { "hello",
623:                        new StringBuffer("world"), "", "this is", null,
624:                        "some text" };
625:                List list = Arrays.asList(data);
626:
627:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
628:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
629:                        baout, false, true);
630:
631:                IOUtils.writeLines(list, "*", out, null);
632:
633:                out.off();
634:                out.flush();
635:
636:                String expected = "hello*world**this is**some text*";
637:                String actual = baout.toString();
638:                assertEquals(expected, actual);
639:            }
640:
641:            //-----------------------------------------------------------------------
642:            public void testWriteLines_Writer() throws Exception {
643:                Object[] data = new Object[] { "hello",
644:                        new StringBuffer("world"), "", "this is", null,
645:                        "some text" };
646:                List list = Arrays.asList(data);
647:
648:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
649:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
650:                        baout, true, true);
651:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
652:
653:                IOUtils.writeLines(list, "*", writer);
654:
655:                out.off();
656:                writer.flush();
657:
658:                String expected = "hello*world**this is**some text*";
659:                String actual = baout.toString();
660:                assertEquals(expected, actual);
661:            }
662:
663:            public void testWriteLines_Writer_nullData() throws Exception {
664:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
665:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
666:                        baout, true, true);
667:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
668:
669:                IOUtils.writeLines((List) null, "*", writer);
670:                out.off();
671:                writer.flush();
672:
673:                assertEquals("Sizes differ", 0, baout.size());
674:            }
675:
676:            public void testWriteLines_Writer_nullSeparator() throws Exception {
677:                Object[] data = new Object[] { "hello", "world" };
678:                List list = Arrays.asList(data);
679:
680:                ByteArrayOutputStream baout = new ByteArrayOutputStream();
681:                YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
682:                        baout, true, true);
683:                Writer writer = new OutputStreamWriter(baout, "US-ASCII");
684:
685:                IOUtils.writeLines(list, (String) null, writer);
686:                out.off();
687:                writer.flush();
688:
689:                String expected = "hello" + IOUtils.LINE_SEPARATOR + "world"
690:                        + IOUtils.LINE_SEPARATOR;
691:                String actual = baout.toString();
692:                assertEquals(expected, actual);
693:            }
694:
695:            public void testWriteLines_Writer_nullStream() throws Exception {
696:                Object[] data = new Object[] { "hello", "world" };
697:                List list = Arrays.asList(data);
698:                try {
699:                    IOUtils.writeLines(list, "*", (Writer) null);
700:                    fail();
701:                } catch (NullPointerException ex) {
702:                }
703:            }
704:
705:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.