KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > TestDocumentOutputStream


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 import org.apache.poi.poifs.property.DirectoryProperty;
28 import org.apache.poi.poifs.property.DocumentProperty;
29 import org.apache.poi.poifs.storage.RawDataBlock;
30
31 /**
32  * Class to test DocumentOutputStream functionality
33  *
34  * @author Marc Johnson
35  */

36
37 public class TestDocumentOutputStream
38     extends TestCase
39 {
40
41     /**
42      * Constructor TestDocumentOutputStream
43      *
44      * @param name
45      *
46      * @exception IOException
47      */

48
49     public TestDocumentOutputStream(String JavaDoc name)
50         throws IOException
51     {
52         super(name);
53     }
54
55     /**
56      * test write(int) behavior
57      *
58      * @exception IOException
59      */

60
61     public void testWrite1()
62         throws IOException
63     {
64         ByteArrayOutputStream stream = new ByteArrayOutputStream();
65         DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
66
67         for (int j = 0; j < 25; j++)
68         {
69             dstream.write(j);
70         }
71         try
72         {
73             dstream.write(0);
74             fail("Should have caught IOException");
75         }
76         catch (IOException ignored)
77         {
78         }
79         byte[] output = stream.toByteArray();
80
81         assertEquals(25, output.length);
82         for (int j = 0; j < 25; j++)
83         {
84             assertEquals(( byte ) j, output[ j ]);
85         }
86         stream.close();
87     }
88
89     /**
90      * test write(byte[]) behavior
91      *
92      * @exception IOException
93      */

94
95     public void testWrite2()
96         throws IOException
97     {
98         ByteArrayOutputStream stream = new ByteArrayOutputStream();
99         DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
100
101         for (int j = 0; j < 6; j++)
102         {
103             byte[] array = new byte[ 4 ];
104
105             Arrays.fill(array, ( byte ) j);
106             dstream.write(array);
107         }
108         try
109         {
110             byte[] array = new byte[ 4 ];
111
112             Arrays.fill(array, ( byte ) 7);
113             dstream.write(array);
114             fail("Should have caught IOException");
115         }
116         catch (IOException ignored)
117         {
118         }
119         byte[] output = stream.toByteArray();
120
121         assertEquals(24, output.length);
122         for (int j = 0; j < 6; j++)
123         {
124             for (int k = 0; k < 4; k++)
125             {
126                 assertEquals(String.valueOf((j * 4) + k), ( byte ) j,
127                              output[ (j * 4) + k ]);
128             }
129         }
130         stream.close();
131     }
132
133     /**
134      * test write(byte[], int, int) behavior
135      *
136      * @exception IOException
137      */

138
139     public void testWrite3()
140         throws IOException
141     {
142         ByteArrayOutputStream stream = new ByteArrayOutputStream();
143         DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
144         byte[] array = new byte[ 50 ];
145
146         for (int j = 0; j < 50; j++)
147         {
148             array[ j ] = ( byte ) j;
149         }
150         dstream.write(array, 1, 25);
151         try
152         {
153             dstream.write(array, 0, 1);
154             fail("Should have caught IOException");
155         }
156         catch (IOException ignored)
157         {
158         }
159         byte[] output = stream.toByteArray();
160
161         assertEquals(25, output.length);
162         for (int j = 0; j < 25; j++)
163         {
164             assertEquals(( byte ) (j + 1), output[ j ]);
165         }
166         stream.close();
167     }
168
169     /**
170      * test writeFiller()
171      *
172      * @exception IOException
173      */

174
175     public void testWriteFiller()
176         throws IOException
177     {
178         ByteArrayOutputStream stream = new ByteArrayOutputStream();
179         DocumentOutputStream dstream = new DocumentOutputStream(stream, 25);
180
181         for (int j = 0; j < 25; j++)
182         {
183             dstream.write(j);
184         }
185         try
186         {
187             dstream.write(0);
188             fail("Should have caught IOException");
189         }
190         catch (IOException ignored)
191         {
192         }
193         dstream.writeFiller(100, ( byte ) 0xff);
194         byte[] output = stream.toByteArray();
195
196         assertEquals(100, output.length);
197         for (int j = 0; j < 25; j++)
198         {
199             assertEquals(( byte ) j, output[ j ]);
200         }
201         for (int j = 25; j < 100; j++)
202         {
203             assertEquals(String.valueOf(j), ( byte ) 0xff, output[ j ]);
204         }
205         stream.close();
206     }
207
208     /**
209      * main method to run the unit tests
210      *
211      * @param ignored_args
212      */

213
214     public static void main(String JavaDoc [] ignored_args)
215     {
216         System.out.println(
217             "Testing org.apache.poi.poifs.filesystem.DocumentOutputStream");
218         junit.textui.TestRunner.run(TestDocumentOutputStream.class);
219     }
220 }
221
Popular Tags