KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > TestRawDataBlock


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.storage;
20
21 import java.io.*;
22
23 import junit.framework.*;
24
25 /**
26  * Class to test RawDataBlock functionality
27  *
28  * @author Marc Johnson
29  */

30
31 public class TestRawDataBlock
32     extends TestCase
33 {
34
35     /**
36      * Constructor TestRawDataBlock
37      *
38      * @param name
39      */

40
41     public TestRawDataBlock(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     /**
47      * Test creating a normal RawDataBlock
48      *
49      * @exception IOException
50      */

51
52     public void testNormalConstructor()
53         throws IOException
54     {
55         byte[] data = new byte[ 512 ];
56
57         for (int j = 0; j < 512; j++)
58         {
59             data[ j ] = ( byte ) j;
60         }
61         RawDataBlock block = new RawDataBlock(new ByteArrayInputStream(data));
62
63         assertTrue("Should not be at EOF", !block.eof());
64         byte[] out_data = block.getData();
65
66         assertEquals("Should be same length", data.length, out_data.length);
67         for (int j = 0; j < 512; j++)
68         {
69             assertEquals("Should be same value at offset " + j, data[ j ],
70                          out_data[ j ]);
71         }
72     }
73
74     /**
75      * Test creating an empty RawDataBlock
76      *
77      * @exception IOException
78      */

79
80     public void testEmptyConstructor()
81         throws IOException
82     {
83         byte[] data = new byte[ 0 ];
84         RawDataBlock block = new RawDataBlock(new ByteArrayInputStream(data));
85
86         assertTrue("Should be at EOF", block.eof());
87         try
88         {
89             block.getData();
90         }
91         catch (IOException ignored)
92         {
93
94             // as expected
95
}
96     }
97
98     /**
99      * Test creating a short RawDataBlock
100      */

101
102     public void testShortConstructor()
103     {
104         for (int k = 1; k < 512; k++)
105         {
106             byte[] data = new byte[ k ];
107
108             for (int j = 0; j < k; j++)
109             {
110                 data[ j ] = ( byte ) j;
111             }
112             RawDataBlock block = null;
113
114             try
115             {
116                 block = new RawDataBlock(new ByteArrayInputStream(data));
117                 fail("Should have thrown IOException creating short block");
118             }
119             catch (IOException ignored)
120             {
121
122                 // as expected
123
}
124         }
125     }
126
127     /**
128      * main method to run the unit tests
129      *
130      * @param ignored_args
131      */

132
133     public static void main(String JavaDoc [] ignored_args)
134     {
135         System.out
136             .println("Testing org.apache.poi.poifs.storage.RawDataBlock");
137         junit.textui.TestRunner.run(TestRawDataBlock.class);
138     }
139 }
140
Popular Tags