KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > StreamUtilTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.*;
6
7 import junit.framework.TestCase;
8
9 public class StreamUtilTest extends TestCase {
10
11     protected String JavaDoc dataRoot;
12
13     protected void setUp() throws Exception JavaDoc {
14         super.setUp();
15         if (dataRoot != null) {
16             return;
17         }
18         dataRoot = System.getProperty("mod.dir");
19         if (dataRoot == null) {
20             dataRoot = "";
21         }
22         dataRoot += "/test/data";
23         System.out.println(dataRoot);
24     }
25
26     public void testCopy() {
27         AsciiStringInputStream in = new AsciiStringInputStream("input");
28         StringOutputStream out = new StringOutputStream();
29         try {
30             StreamUtil.copy(in, out);
31         } catch (IOException ioex) {
32             fail("StreamUtil.copy " + ioex.toString());
33         }
34         assertEquals("input", out.toString());
35         StreamUtil.close(out);
36         StreamUtil.close(in);
37     }
38
39     public void testCompare() {
40         try {
41             FileInputStream in1 = new FileInputStream(new File(dataRoot, "file/a.txt"));
42             AsciiStringInputStream in2 = new AsciiStringInputStream("test file\r\n");
43             assertTrue(StreamUtil.compare(in1, in2));
44             StreamUtil.close(in2);
45             StreamUtil.close(in1);
46         } catch (FileNotFoundException e) {
47             fail("StreamUtil.testCloneCompare " + e.toString());
48         } catch (IOException e) {
49             fail("StreamUtil.testCloneCompare " + e.toString());
50         }
51     }
52
53     public void testGetBytes() {
54         try {
55             FileInputStream in = new FileInputStream(new File(dataRoot, "file/a.txt"));
56             byte[] data = StreamUtil.readBytes(in);
57             StreamUtil.close(in);
58
59             String JavaDoc s = new String JavaDoc(data);
60             assertEquals("test file\r\n", s);
61
62             in = new FileInputStream(new File(dataRoot, "file/a.txt"));
63             String JavaDoc str = new String JavaDoc(StreamUtil.readChars(in));
64             StreamUtil.close(in);
65             assertEquals("test file\r\n", str);
66         } catch (FileNotFoundException e) {
67             fail("StreamUtil.testGetBytes " + e.toString());
68         } catch (IOException e) {
69             fail("StreamUtil.testGetBytes " + e.toString());
70         }
71     }
72 }
73
Popular Tags